changeset 11983:5f256f637c47

strndup: fix improper m4 caching * m4/strndup.m4 (gl_FUNC_STRNDUP): Rework to avoid side effects inside AC_CACHE_CHECK. Use REPLACE_STRNDUP, not HAVE_STRNDUP. (gl_PREREQ_STRNDUP): Delete. * m4/string_h.m4 (gl_HEADER_STRING_H_DEFAULTS): Update default. * modules/string (Makefile.am): Substitute it. * lib/string.in.h (strndup): Modernize prototype. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Wed, 09 Sep 2009 21:34:32 -0600
parents 159699cb2db7
children 2a51ec9a6c14
files ChangeLog lib/string.in.h m4/string_h.m4 m4/strndup.m4 modules/string
diffstat 5 files changed, 34 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2009-09-09  Eric Blake  <ebb9@byu.net>
 
+	strndup: fix improper m4 caching
+	* m4/strndup.m4 (gl_FUNC_STRNDUP): Rework to avoid side effects
+	inside AC_CACHE_CHECK.  Use REPLACE_STRNDUP, not HAVE_STRNDUP.
+	(gl_PREREQ_STRNDUP): Delete.
+	* m4/string_h.m4 (gl_HEADER_STRING_H_DEFAULTS): Update default.
+	* modules/string (Makefile.am): Substitute it.
+	* lib/string.in.h (strndup): Modernize prototype.
+
 	getcwd: port to mingw
 	* m4/getcwd.m4 (gl_FUNC_GETCWD): Mingw directories are very
 	different from the POSIX assumptions made throughout the getcwd
--- a/lib/string.in.h
+++ b/lib/string.in.h
@@ -203,11 +203,11 @@
 
 /* Return a newly allocated copy of at most N bytes of STRING.  */
 #if @GNULIB_STRNDUP@
-# if ! @HAVE_STRNDUP@
+# if @REPLACE_STRNDUP@
 #  undef strndup
 #  define strndup rpl_strndup
 # endif
-# if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
+# if @REPLACE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
 extern char *strndup (char const *__string, size_t __n);
 # endif
 #elif defined GNULIB_POSIXCHECK
--- a/m4/string_h.m4
+++ b/m4/string_h.m4
@@ -5,7 +5,7 @@
 # gives unlimited permission to copy and/or distribute it,
 # with or without modifications, as long as this notice is preserved.
 
-# serial 8
+# serial 9
 
 # Written by Paul Eggert.
 
@@ -74,7 +74,6 @@
   HAVE_STPNCPY=1;		AC_SUBST([HAVE_STPNCPY])
   HAVE_STRCHRNUL=1;		AC_SUBST([HAVE_STRCHRNUL])
   HAVE_DECL_STRDUP=1;		AC_SUBST([HAVE_DECL_STRDUP])
-  HAVE_STRNDUP=1;		AC_SUBST([HAVE_STRNDUP])
   HAVE_DECL_STRNDUP=1;		AC_SUBST([HAVE_DECL_STRNDUP])
   HAVE_DECL_STRNLEN=1;		AC_SUBST([HAVE_DECL_STRNLEN])
   HAVE_STRPBRK=1;		AC_SUBST([HAVE_STRPBRK])
@@ -90,6 +89,7 @@
   REPLACE_STRSTR=0;		AC_SUBST([REPLACE_STRSTR])
   REPLACE_STRCASESTR=0;		AC_SUBST([REPLACE_STRCASESTR])
   REPLACE_STRERROR=0;		AC_SUBST([REPLACE_STRERROR])
+  REPLACE_STRNDUP=0;		AC_SUBST([REPLACE_STRNDUP])
   REPLACE_STRSIGNAL=0;		AC_SUBST([REPLACE_STRSIGNAL])
   REPLACE_STRTOK_R=0;		AC_SUBST([REPLACE_STRTOK_R])
   UNDEFINE_STRTOK_R=0;		AC_SUBST([UNDEFINE_STRTOK_R])
--- a/m4/strndup.m4
+++ b/m4/strndup.m4
@@ -1,4 +1,4 @@
-# strndup.m4 serial 16
+# strndup.m4 serial 17
 dnl Copyright (C) 2002-2003, 2005-2009 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -9,17 +9,20 @@
   dnl Persuade glibc <string.h> to declare strndup().
   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
 
+  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
   AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
   AC_CHECK_DECLS_ONCE([strndup])
+  AC_CHECK_FUNCS_ONCE([strndup])
   if test $ac_cv_have_decl_strndup = no; then
     HAVE_DECL_STRNDUP=0
   fi
 
-  # AIX 4.3.3, AIX 5.1 have a function that fails to add the terminating '\0'.
-  AC_CACHE_CHECK([for working strndup], [gl_cv_func_strndup],
-    [AC_RUN_IFELSE([
-       AC_LANG_PROGRAM([[#include <string.h>
-			 #include <stdlib.h>]], [[
+  if test $ac_cv_func_strndup = yes; then
+    # AIX 4.3.3, AIX 5.1 have a function that fails to add the terminating '\0'.
+    AC_CACHE_CHECK([for working strndup], [gl_cv_func_strndup_works],
+      [AC_RUN_IFELSE([
+         AC_LANG_PROGRAM([[#include <string.h>
+			   #include <stdlib.h>]], [[
 #ifndef HAVE_DECL_STRNDUP
   extern char *strndup (const char *, size_t);
 #endif
@@ -28,26 +31,19 @@
   free (s);
   s = strndup ("shorter string", 13);
   return s[13] != '\0';]])],
-       [gl_cv_func_strndup=yes],
-       [gl_cv_func_strndup=no],
-       [AC_CHECK_FUNC([strndup],
-          [AC_EGREP_CPP([too risky], [
-#ifdef _AIX
-               too risky
-#endif
-             ],
-             [gl_cv_func_strndup=no],
-             [gl_cv_func_strndup=yes])],
-          [gl_cv_func_strndup=no])])])
-  if test $gl_cv_func_strndup = yes; then
-    AC_DEFINE([HAVE_STRNDUP], [1],
-      [Define if you have the strndup() function and it works.])
+         [gl_cv_func_strndup_works=yes],
+         [gl_cv_func_strndup_works=no],
+         [case $host_os in
+            aix*) gl_cv_func_strndup_works="guessing no";;
+            *) gl_cv_func_strndup_works="guessing yes";;
+          esac])])
+    case $gl_cv_func_strndup_works in
+      *no)
+        REPLACE_STRNDUP=1
+        AC_LIBOBJ([strndup])
+        ;;
+    esac
   else
-    HAVE_STRNDUP=0
     AC_LIBOBJ([strndup])
-    gl_PREREQ_STRNDUP
   fi
 ])
-
-# Prerequisites of lib/strndup.c.
-AC_DEFUN([gl_PREREQ_STRNDUP], [:])
--- a/modules/string
+++ b/modules/string
@@ -66,7 +66,6 @@
 	      -e 's|@''HAVE_STPNCPY''@|$(HAVE_STPNCPY)|g' \
 	      -e 's|@''HAVE_STRCHRNUL''@|$(HAVE_STRCHRNUL)|g' \
 	      -e 's|@''HAVE_DECL_STRDUP''@|$(HAVE_DECL_STRDUP)|g' \
-	      -e 's|@''HAVE_STRNDUP''@|$(HAVE_STRNDUP)|g' \
 	      -e 's|@''HAVE_DECL_STRNDUP''@|$(HAVE_DECL_STRNDUP)|g' \
 	      -e 's|@''HAVE_DECL_STRNLEN''@|$(HAVE_DECL_STRNLEN)|g' \
 	      -e 's|@''HAVE_STRPBRK''@|$(HAVE_STRPBRK)|g' \
@@ -82,6 +81,7 @@
 	      -e 's|@''REPLACE_STRDUP''@|$(REPLACE_STRDUP)|g' \
 	      -e 's|@''REPLACE_STRSTR''@|$(REPLACE_STRSTR)|g' \
 	      -e 's|@''REPLACE_STRERROR''@|$(REPLACE_STRERROR)|g' \
+	      -e 's|@''REPLACE_STRNDUP''@|$(REPLACE_STRNDUP)|g' \
 	      -e 's|@''REPLACE_STRSIGNAL''@|$(REPLACE_STRSIGNAL)|g' \
 	      -e 's|@''REPLACE_STRTOK_R''@|$(REPLACE_STRTOK_R)|g' \
 	      -e 's|@''UNDEFINE_STRTOK_R''@|$(UNDEFINE_STRTOK_R)|g' \