From: Guy Harris Date: Mon, 13 Mar 2017 22:47:56 +0000 (-0700) Subject: Have a strlcat() macro for UN*X platforms that lack it. X-Git-Tag: libpcap-1.9-bp~863 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/e000478ecd3d7671d3fc65b50ad6fc8f39114d96?ds=sidebyside Have a strlcat() macro for UN*X platforms that lack it. --- diff --git a/config.h.in b/config.h.in index fdb32745..290aba28 100644 --- a/config.h.in +++ b/config.h.in @@ -166,6 +166,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_STRING_H +/* Define to 1 if you have the `strlcat' function. */ +#undef HAVE_STRLCAT + /* Define to 1 if you have the `strlcpy' function. */ #undef HAVE_STRLCPY diff --git a/configure b/configure index b1f36030..b04cec7d 100755 --- a/configure +++ b/configure @@ -4937,7 +4937,7 @@ $as_echo "$ac_cv_lbl_gcc_fixincludes" >&6; } fi fi -for ac_func in strerror strlcpy +for ac_func in strerror strlcpy strlcat do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/configure.ac b/configure.ac index c48c07fa..1d098799 100644 --- a/configure.ac +++ b/configure.ac @@ -160,7 +160,7 @@ esac AC_LBL_FIXINCLUDES -AC_CHECK_FUNCS(strerror strlcpy) +AC_CHECK_FUNCS(strerror strlcpy strlcat) needsnprintf=no AC_CHECK_FUNCS(vsnprintf snprintf,, diff --git a/portability.h b/portability.h index 8a6bf40e..117fe4dd 100644 --- a/portability.h +++ b/portability.h @@ -47,7 +47,7 @@ extern "C" { /* * Macro that does the same thing as strlcpy(). */ - #ifdef _MSC_VER + #if defined(_MSC_VER) || defined(__MINGW32__) /* * strncpy_s() is supported at least back to Visual * Studio 2005. @@ -63,6 +63,28 @@ extern "C" { #endif #endif +#ifndef HAVE_STRLCAT + /* + * Macro that does the same thing as strlcat(). + */ + #if defined(_MSC_VER) || defined(__MINGW32__) + /* + * strncat_s() is supported at least back to Visual + * Studio 2005. + */ + #define strlcat(x, y, z) \ + strncat_s((x), (z), (y), _TRUNCATE) + #else + /* + * ANSI C says strncat() always null-terminates its first argument, + * so 1) we don't need to explicitly null-terminate the string + * ourselves and 2) we need to leave room for the null terminator. + */ + #define strlcat(x, y, z) \ + strncat((x), (y), (z) - strlen((x)) - 1) + #endif +#endif + /* * For flagging arguments as format strings in MSVC. */ @@ -87,11 +109,6 @@ extern "C" { FILE *fopen_safe(const char *filename, const char* mode); #endif -#if defined(_MSC_VER) || defined(__MINGW32__) - #define strlcat(x, y, z) \ - strncat_s((x), (z), (y), _TRUNCATE) -#endif - #ifdef _MSC_VER /* * MSVC.