]> granicus.if.org Git - zziplib/commitdiff
provide a workaround for missing strnlen #25
authorMojca Miklavec <mojca@macports.org>
Wed, 28 Feb 2018 14:09:55 +0000 (15:09 +0100)
committerMojca Miklavec <mojca@macports.org>
Wed, 28 Feb 2018 14:09:55 +0000 (15:09 +0100)
The strnlen function is only defined in POSIX.1-2008.
It is missing on Solaris 10 or Mac OS X 10.6 for example.

config.h.in
configure
configure.ac
zzip/__string.h

index cb350a78ec41df13769b5abd30c90d5d51c33499..c9379b70d49ac186173a069827974e59edb9fb2f 100644 (file)
@@ -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 <sys/dir.h> header file, and it defines `DIR'.
    */
 #undef HAVE_SYS_DIR_H
index 72a5b6c76d66b62511af5cb7cc9b09cf271f4076..d2f1f98e9bdb5e25f1792670a582dc67cb1d8463 100755 (executable)
--- 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"
index 4708f8d2761dc869c1c4ab9f22317c3712b4fe10..774d1ccfd403e227c4372a5075b28e1301567294 100644 (file)
@@ -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
index 351b0c14162f013fcf688f5d5609da955bd49075..d65c022a4ae1f1bff21387ae9178b73ad613df3c 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef __ZZIP_INTERNAL_STRING_H
 #define __ZZIP_INTERNAL_STRING_H
 
+#include <stdlib.h>
+
 #ifdef __linux__
 #define _GNU_SOURCE _glibc_developers_are_idiots_to_call_strndup_gnu_specific_
 #endif
 #include <strings.h>
 #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 */