changeset 18484:f60027cad6de

qsort_r: Fix macrology for platforms that lack the function. * m4/stdlib_h.m4 (gl_STDLIB_H): Check for qsort_r. (gl_STDLIB_H_DEFAULTS): Initialize HAVE_QSORT_R. * modules/stdlib (Makefile.am): Substitute HAVE_QSORT_R. * lib/stdlib.in.h (qsort_r): Provide declaration if the function does not exist. * m4/qsort_r.m4 (gl_FUNC_QSORT_R): Use AC_CHECK_FUNCS to test whether the function exists. * modules/qsort_r: Add comments.
author Bruno Haible <bruno@clisp.org>
date Sun, 16 Oct 2016 22:11:32 +0200
parents e6c0f8c2d85c
children 87c69777639a
files ChangeLog lib/stdlib.in.h m4/qsort_r.m4 m4/stdlib_h.m4 modules/qsort_r modules/stdlib
diffstat 6 files changed, 68 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Oct 26 18:10:37 2016 -0700
+++ b/ChangeLog	Sun Oct 16 22:11:32 2016 +0200
@@ -1,3 +1,15 @@
+2016-10-16  Bruno Haible  <bruno@clisp.org>
+
+	qsort_r: Fix macrology for platforms that lack the function.
+	* m4/stdlib_h.m4 (gl_STDLIB_H): Check for qsort_r.
+	(gl_STDLIB_H_DEFAULTS): Initialize HAVE_QSORT_R.
+	* modules/stdlib (Makefile.am): Substitute HAVE_QSORT_R.
+	* lib/stdlib.in.h (qsort_r): Provide declaration if the function does
+	not exist.
+	* m4/qsort_r.m4 (gl_FUNC_QSORT_R): Use AC_CHECK_FUNCS to test whether
+	the function exists.
+	* modules/qsort_r: Add comments.
+
 2016-10-26  Paul Eggert  <eggert@cs.ucla.edu>
 
 	sys_types: fix Texinfo typos
--- a/lib/stdlib.in.h	Wed Oct 26 18:10:37 2016 -0700
+++ b/lib/stdlib.in.h	Sun Oct 16 22:11:32 2016 +0200
@@ -521,6 +521,9 @@
 #endif
 
 #if @GNULIB_QSORT_R@
+/* Sort an array of NMEMB elements, starting at address BASE, each element
+   occupying SIZE bytes, in ascending order according to the comparison
+   function COMPARE.  */
 # if @REPLACE_QSORT_R@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
 #   undef qsort_r
@@ -535,12 +538,24 @@
                                                   void *),
                                   void *arg));
 # else
+#  if !@HAVE_QSORT_R@
+_GL_FUNCDECL_SYS (qsort_r, void, (void *base, size_t nmemb, size_t size,
+                                  int (*compare) (void const *, void const *,
+                                                  void *),
+                                  void *arg) _GL_ARG_NONNULL ((1, 4)));
+#  endif
 _GL_CXXALIAS_SYS (qsort_r, void, (void *base, size_t nmemb, size_t size,
                                   int (*compare) (void const *, void const *,
                                                   void *),
                                   void *arg));
 # endif
 _GL_CXXALIASWARN (qsort_r);
+#elif defined GNULIB_POSIXCHECK
+# undef qsort_r
+# if HAVE_RAW_DECL_QSORT_R
+_GL_WARN_ON_USE (qsort_r, "qsort_r is not portable - "
+                 "use gnulib module qsort_r for portability");
+# endif
 #endif
 
 
--- a/m4/qsort_r.m4	Wed Oct 26 18:10:37 2016 -0700
+++ b/m4/qsort_r.m4	Sun Oct 16 22:11:32 2016 +0200
@@ -14,35 +14,39 @@
 
   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
 
-  AC_CACHE_CHECK([for qsort_r signature], [gl_cv_qsort_r_signature],
-    [AC_LINK_IFELSE(
-       [AC_LANG_PROGRAM([[#include <stdlib.h>
-                          void qsort_r (void *, size_t, size_t,
-                                        int (*) (void const *, void const *,
-                                                 void *),
-                                        void *);
-                          void (*p) (void *, size_t, size_t,
-                                     int (*) (void const *, void const *,
-                                              void *),
-                                     void *) = qsort_r;
-                        ]])],
-       [gl_cv_qsort_r_signature=GNU],
-       [AC_LINK_IFELSE(
-          [AC_LANG_PROGRAM([[#include <stdlib.h>
-                             void qsort_r (void *, size_t, size_t, void *,
-                                           int (*) (void *,
-                                                    void const *,
-                                                    void const *));
-                             void (*p) (void *, size_t, size_t, void *,
-                                        int (*) (void *, void const *,
-                                                 void const *)) = qsort_r;
-                           ]])],
-          [gl_cv_qsort_r_signature=BSD],
-          [gl_cv_qsort_r_signature=no])])])
-
-  case $gl_cv_qsort_r_signature in
-    GNU) HAVE_QSORT_R=1;;
-    BSD) HAVE_QSORT_R=1 REPLACE_QSORT_R=1;;
-    *)   HAVE_QSORT_R=0 REPLACE_QSORT_R=1;;
-  esac
+  AC_CHECK_FUNCS_ONCE([qsort_r])
+  if test $ac_cv_func_qsort_r = yes; then
+    AC_CACHE_CHECK([for qsort_r signature], [gl_cv_qsort_r_signature],
+      [AC_LINK_IFELSE(
+         [AC_LANG_PROGRAM([[#include <stdlib.h>
+                            void qsort_r (void *, size_t, size_t,
+                                          int (*) (void const *, void const *,
+                                                   void *),
+                                          void *);
+                            void (*p) (void *, size_t, size_t,
+                                       int (*) (void const *, void const *,
+                                                void *),
+                                       void *) = qsort_r;
+                          ]])],
+         [gl_cv_qsort_r_signature=GNU],
+         [AC_LINK_IFELSE(
+            [AC_LANG_PROGRAM([[#include <stdlib.h>
+                               void qsort_r (void *, size_t, size_t, void *,
+                                             int (*) (void *,
+                                                      void const *,
+                                                      void const *));
+                               void (*p) (void *, size_t, size_t, void *,
+                                          int (*) (void *, void const *,
+                                                   void const *)) = qsort_r;
+                             ]])],
+            [gl_cv_qsort_r_signature=BSD],
+            [gl_cv_qsort_r_signature=unknown])])])
+    case $gl_cv_qsort_r_signature in
+      GNU)     ;;
+      BSD)     REPLACE_QSORT_R=1 ;;
+      unknown) HAVE_QSORT_R=0 REPLACE_QSORT_R=1 ;;
+    esac
+  else
+    HAVE_QSORT_R=0
+  fi
 ])
--- a/m4/stdlib_h.m4	Wed Oct 26 18:10:37 2016 -0700
+++ b/m4/stdlib_h.m4	Sun Oct 16 22:11:32 2016 +0200
@@ -1,4 +1,4 @@
-# stdlib_h.m4 serial 42
+# stdlib_h.m4 serial 43
 dnl Copyright (C) 2007-2016 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -21,7 +21,7 @@
 #endif
     ]], [_Exit atoll canonicalize_file_name getloadavg getsubopt grantpt
     initstate initstate_r mkdtemp mkostemp mkostemps mkstemp mkstemps
-    posix_openpt ptsname ptsname_r random random_r realpath rpmatch
+    posix_openpt ptsname ptsname_r qsort_r random random_r realpath rpmatch
     secure_getenv setenv setstate setstate_r srandom srandom_r
     strtod strtoll strtoull unlockpt unsetenv])
 ])
@@ -85,6 +85,7 @@
   HAVE_POSIX_OPENPT=1;       AC_SUBST([HAVE_POSIX_OPENPT])
   HAVE_PTSNAME=1;            AC_SUBST([HAVE_PTSNAME])
   HAVE_PTSNAME_R=1;          AC_SUBST([HAVE_PTSNAME_R])
+  HAVE_QSORT_R=1;            AC_SUBST([HAVE_QSORT_R])
   HAVE_RANDOM=1;             AC_SUBST([HAVE_RANDOM])
   HAVE_RANDOM_H=1;           AC_SUBST([HAVE_RANDOM_H])
   HAVE_RANDOM_R=1;           AC_SUBST([HAVE_RANDOM_R])
--- a/modules/qsort_r	Wed Oct 26 18:10:37 2016 -0700
+++ b/modules/qsort_r	Sun Oct 16 22:11:32 2016 +0200
@@ -13,8 +13,10 @@
 configure.ac:
 gl_FUNC_QSORT_R
 if test $HAVE_QSORT_R = 0; then
+  # The function is missing from the system or has an unknown signature.
   AC_LIBOBJ([qsort])
 elif test $REPLACE_QSORT_R = 1; then
+  # The function exists, but it has the BSD signature.
   AC_LIBOBJ([qsort_r])
 fi
 gl_STDLIB_MODULE_INDICATOR([qsort_r])
--- a/modules/stdlib	Wed Oct 26 18:10:37 2016 -0700
+++ b/modules/stdlib	Sun Oct 16 22:11:32 2016 +0200
@@ -79,6 +79,7 @@
 	      -e 's|@''HAVE_POSIX_OPENPT''@|$(HAVE_POSIX_OPENPT)|g' \
 	      -e 's|@''HAVE_PTSNAME''@|$(HAVE_PTSNAME)|g' \
 	      -e 's|@''HAVE_PTSNAME_R''@|$(HAVE_PTSNAME_R)|g' \
+	      -e 's|@''HAVE_QSORT_R''@|$(HAVE_QSORT_R)|g' \
 	      -e 's|@''HAVE_RANDOM''@|$(HAVE_RANDOM)|g' \
 	      -e 's|@''HAVE_RANDOM_H''@|$(HAVE_RANDOM_H)|g' \
 	      -e 's|@''HAVE_RANDOM_R''@|$(HAVE_RANDOM_R)|g' \