[PATCH] Also prototype strdup(),strndup() iif _XOPEN_SOURCE is defined appropriately

Jon TURNEY jon.turney@dronecode.org.uk
Wed Oct 24 09:54:00 GMT 2012


I'm not sure if this is wanted, but for completeness:  The SUSv2 function
strdup() and SUSv4 function strndup() are only declared if __STRICT_ANSI__ is
defined.

Attached is a patch to also prototype those functions if _XOPEN_SOURCE is
defined appropriately.

$ cat xopen-strdup-test.c
#include <string.h>

int main()
{
  strdup("this");
  strndup("this",1);
}

before patch:

$ gcc xopen-strdup-test.c -std=c99 -Werror=implicit-function-declaration
xopen-strdup-test.c: In function ‘main’:
xopen-strdup-test.c:8:3: error: implicit declaration of function ‘strdup’
xopen-strdup-test.c:9:3: error: implicit declaration of function ‘strndup’

after patch:

$ gcc xopen-strdup-test.c -std=c99 -Werror=implicit-function-declaration
xopen-strdup-test.c: In function ‘main’:
xopen-strdup-test.c:8:3: error: implicit declaration of function ‘strdup’
xopen-strdup-test.c:9:3: error: implicit declaration of function ‘strndup’

$ gcc xopen-strdup-test.c -std=c99 -Werror=implicit-function-declaration
-D_XOPEN_SOURCE=500
xopen-strdup-test.c: In function ‘main’:
xopen-strdup-test.c:9:3: error: implicit declaration of function ‘strndup’

$ gcc xopen-strdup-test.c -std=c99 -Werror=implicit-function-declaration
-D_XOPEN_SOURCE=700

newlib/ChangeLog:

2012-10-22  Jon TURNEY  <jon.turney@dronecode.org.uk>

	* libc/include/string.h (strdup, strndup): Declare if not
	__STRICT_ANSI__ or _XOPEN_SOURCE is defined appropriately.
-------------- next part --------------
Index: newlib/libc/include/string.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/string.h,v
retrieving revision 1.30
diff -u -p -r1.30 string.h
--- newlib/libc/include/string.h	10 May 2012 08:34:08 -0000	1.30
+++ newlib/libc/include/string.h	22 Oct 2012 14:50:06 -0000
@@ -10,6 +10,7 @@
 #include "_ansi.h"
 #include <sys/reent.h>
 #include <sys/cdefs.h>
+#include <sys/features.h>
 
 #define __need_size_t
 #include <stddef.h>
@@ -65,9 +66,17 @@ char 	*_EXFUN(stpncpy,(char *, const cha
 int	 _EXFUN(strcasecmp,(const char *, const char *));
 char	*_EXFUN(strcasestr,(const char *, const char *));
 char 	*_EXFUN(strchrnul,(const char *, int));
+#endif
+#if !defined(__STRICT_ANSI__) || (_XOPEN_SOURCE >= 500)
 char 	*_EXFUN(strdup,(const char *));
+#endif
+#ifndef __STRICT_ANSI__
 char 	*_EXFUN(_strdup_r,(struct _reent *, const char *));
+#endif
+#if !defined(__STRICT_ANSI__) || (_XOPEN_SOURCE >= 700)
 char 	*_EXFUN(strndup,(const char *, size_t));
+#endif
+#ifndef __STRICT_ANSI__
 char 	*_EXFUN(_strndup_r,(struct _reent *, const char *, size_t));
 /* There are two common strerror_r variants.  If you request
    _GNU_SOURCE, you get the GNU version; otherwise you get the POSIX


More information about the Newlib mailing list