From: Mojca Miklavec Date: Wed, 28 Feb 2018 14:09:55 +0000 (+0100) Subject: provide a workaround for missing strnlen #25 X-Git-Tag: v0.13.69~53^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=873a305fbd3d38e758cad26aff34174a9d9e777a;p=zziplib provide a workaround for missing strnlen #25 The strnlen function is only defined in POSIX.1-2008. It is missing on Solaris 10 or Mac OS X 10.6 for example. --- diff --git a/config.h.in b/config.h.in index cb350a7..c9379b7 100644 --- a/config.h.in +++ b/config.h.in @@ -52,6 +52,9 @@ /* Define to 1 if you have the `strndup' function. */ #undef HAVE_STRNDUP +/* Define to 1 if you have the `strnlen' function. */ +#undef HAVE_STRNLEN + /* Define to 1 if you have the header file, and it defines `DIR'. */ #undef HAVE_SYS_DIR_H diff --git a/configure b/configure index 72a5b6c..d2f1f98 100755 --- a/configure +++ b/configure @@ -12907,7 +12907,7 @@ fi done -for ac_func in strcasecmp strndup +for ac_func in strcasecmp strnlen strndup 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 4708f8d..774d1cc 100644 --- a/configure.ac +++ b/configure.ac @@ -100,7 +100,7 @@ AC_CHECK_HEADERS(stdint.h unistd.h windows.h winnt.h winbase.h sys/int_types.h) AC_CHECK_HEADERS(sys/types.h sys/mman.h sys/stat.h sys/param.h) dnl posix'ish AC_CHECK_HEADERS(io.h direct.h zlib.h byteswap.h) AC_CHECK_HEADERS(fnmatch.h) -AC_CHECK_FUNCS( strcasecmp strndup ) +AC_CHECK_FUNCS( strcasecmp strnlen strndup ) AC_TYPE_OFF_T AC_TYPE_SIZE_T diff --git a/zzip/__string.h b/zzip/__string.h index 351b0c1..d65c022 100644 --- a/zzip/__string.h +++ b/zzip/__string.h @@ -1,6 +1,8 @@ #ifndef __ZZIP_INTERNAL_STRING_H #define __ZZIP_INTERNAL_STRING_H +#include + #ifdef __linux__ #define _GNU_SOURCE _glibc_developers_are_idiots_to_call_strndup_gnu_specific_ #endif @@ -13,6 +15,19 @@ #include #endif +#if defined ZZIP_HAVE_STRNLEN || defined strnlen +#define _zzip_strnlen strnlen +#else + +/* if your system does not have strnlen: */ +zzip__new__ size_t +_zzip_strnlen(const char *p, size_t maxlen) +{ + const char * stop = (char *)memchr(p, '\0', maxlen); + return stop ? stop - p : maxlen; +} +#endif + #if defined ZZIP_HAVE_STRNDUP || defined strndup #define _zzip_strndup strndup @@ -27,7 +42,7 @@ _zzip_strndup(char const *p, size_t maxlen) return p; } else { - size_t len = strnlen(p, maxlen); + size_t len = _zzip_strnlen(p, maxlen); char* r = malloc(len + 1); if (r == NULL) return NULL; /* errno = ENOMEM */