gdraheim / zziplib

The ZZIPlib provides read access on ZIP-archives and unpacked data. It features an additional simplified API following the standard Posix API for file access
Other
62 stars 50 forks source link

error: implicit declaration of function ‘strnlen’ #25

Closed ryandesign closed 6 years ago

ryandesign commented 6 years ago

libzzip 0.13.68 fails to build on older systems, such as Mac OS X 10.6 Snow Leopard and earlier, that do not have the strnlen function:

In file included from ../../zzip/mmapped.c:35:
../../zzip/__string.h: In function ‘_zzip_strndup’:
../../zzip/__string.h:27: warning: return discards qualifiers from pointer target type
../../zzip/__string.h:30: error: implicit declaration of function ‘strnlen’
../../zzip/__string.h:31: error: implicit declaration of function ‘malloc’
../../zzip/__string.h:31: warning: incompatible implicit declaration of built-in function ‘malloc’
../../zzip/mmapped.c: In function ‘zzip_disk_findfirst’:
../../zzip/mmapped.c:444: warning: ISO C90 forbids mixed declarations and code
make[3]: *** [mmapped.lo] Error 1

See #2 for prior discussion of this issue.

gdraheim commented 6 years ago

So the question is: how should it look like?

gdraheim commented 6 years ago

Solaris has problems as well:

===>

Dear Guido,

We are reported that in some versions of Solaris, both of strndup and strnlen are not included. Thus we changed __string.h as follows in TeX Live:

--- __string.h.orig    Tue Feb 06 05:00:56 2018
+++ __string.h    Wed Feb 28 08:54:17 2018
@@ -18,6 +18,18 @@
#define _zzip_strndup strndup
#else

+#if defined(sun) || defined(__sun)
+static size_t  my_strnlen(const char*  str, size_t  maxlen)
+{
+    char *p = memchr(str, 0, maxlen);
+    if (p == NULL)
+       return maxlen;
+    else
+       return (p - str);
+}
+#define strnlen(x,y) my_strnlen((x),(y))
+#endif /* sun || __sun */
+
/* if your system does not have strndup: */
zzip__new__ static char *
_zzip_strndup(char const *p, size_t maxlen)
mojca commented 6 years ago

I would like to suggest using the same strategy as gnuplot does. In configure.ac they use

AC_CHECK_FUNCS(...  strndup strnlen ...)

and then

#ifndef HAVE_STRNDUP
char *strndup __PROTO((const char * str, size_t n));
#endif

#ifndef HAVE_STRNLEN
size_t strnlen __PROTO((const char *str, size_t n));
#endif

and

#ifndef HAVE_STRNLEN
size_t
strnlen(const char *str, size_t n)
{
    const char * stop = (char *)memchr(str, '\0', n);
    return stop ? stop - str : n;
}
#endif

#ifndef HAVE_STRNDUP
char * 
strndup(const char * str, size_t n)
{
    char * ret = NULL;
    size_t len = strnlen(str, n);
    ret = (char *) malloc(len + 1);
    if (ret == NULL) return NULL;
    ret[len] = '\0';
    return (char *)memcpy(ret, str, len);
}
#endif
gdraheim commented 6 years ago

The patch looks great, I have edited it slightly. Please check.

mojca commented 6 years ago

I now tested on Mac OS X 10.6, Sun OS 5.10 and 11. It seems OK on those. (@kencu: you can try to remove the SL fixes once v0.13.69 gets released.)

Compiling on MSYS2 (mentioned in #2) failed for other reasons (#6). MinGW-packages stayed at version 0.13.62, so I could not check how they might have fixed the problem.

gdraheim commented 6 years ago

done.