Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: implicit declaration of function ‘strnlen’ #25

Closed
ryandesign opened this issue Feb 11, 2018 · 6 comments
Closed

error: implicit declaration of function ‘strnlen’ #25

ryandesign opened this issue Feb 11, 2018 · 6 comments

Comments

@ryandesign
Copy link

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
Copy link
Owner

So the question is: how should it look like?

@gdraheim
Copy link
Owner

gdraheim commented Feb 28, 2018

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
Copy link
Contributor

mojca commented Feb 28, 2018

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

mojca pushed a commit to mojca/zziplib that referenced this issue Feb 28, 2018
The strnlen function is only defined in POSIX.1-2008.
It is missing on Solaris 10 or Mac OS X 10.6 for example.
mojca added a commit to mojca/zziplib that referenced this issue Feb 28, 2018
The strnlen function is only defined in POSIX.1-2008.
It is missing on Solaris 10 or Mac OS X 10.6 for example.
gdraheim added a commit that referenced this issue Mar 1, 2018
provide a workaround for missing strnlen #25
@gdraheim
Copy link
Owner

gdraheim commented Mar 1, 2018

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

@gdraheim gdraheim added this to the v0.13.69 release milestone Mar 1, 2018
@mojca
Copy link
Contributor

mojca commented Mar 1, 2018

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
Copy link
Owner

done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants