summaryrefslogtreecommitdiff
path: root/lib/qsort_r.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2014-08-29 13:00:16 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2014-08-29 13:49:52 -0700
commit1ce0e7dc093c81afd3565e1977a1d0f00116df74 (patch)
treeb5a0ae15a82ed4a3232f4b7e47b8a489fb9c6ead /lib/qsort_r.c
parenta6c54be167bd5be41013a254b4e6ba840420a1d0 (diff)
downloadgnulib-1ce0e7dc093c81afd3565e1977a1d0f00116df74.tar.gz
qsort_r: new module, for GNU-style qsort_r
This works even on FreeBSD, which has an incompatible qsort_r API. * MODULES.html.sh: Add it. * doc/glibc-functions/qsort_r.texi: It's now supported. * lib/qsort.c: New file, taken from glibc with minor changes inside "#ifndef _LIBC" and with an unnecessary "#include <alloca.h>" removed. * lib/qsort_r.c: New file, compiled only on FreeBSD. * lib/stdlib.in.h (qsort_r): Declare in the usual way. * m4/stdlib_h.m4 (gl_STDLIB_H_DEFAULTS): * modules/qsort_r, modules/qsort_r-tests: New files. * modules/stdlib (Makefile): Set up its defaults. * tests/test-qsort_r.c: New file.
Diffstat (limited to 'lib/qsort_r.c')
-rw-r--r--lib/qsort_r.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/qsort_r.c b/lib/qsort_r.c
new file mode 100644
index 0000000000..ad68efa50c
--- /dev/null
+++ b/lib/qsort_r.c
@@ -0,0 +1,51 @@
+/* Reentrant sort function.
+
+ Copyright 2014 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Paul Eggert. */
+
+#include <config.h>
+
+#include <stdlib.h>
+
+/* This file is compiled only when the system has a qsort_r that needs
+ to be replaced because it has the BSD signature rather than the GNU
+ signature. */
+
+struct thunk
+{
+ int (*cmp) (void const *, void const *, void *);
+ void *arg;
+};
+
+static int
+thunk_cmp (void *thunk, void const *a, void const *b)
+{
+ struct thunk *th = thunk;
+ return th->cmp (a, b, th->arg);
+}
+
+void
+qsort_r (void *base, size_t nmemb, size_t size,
+ int (*cmp) (void const *, void const *, void *),
+ void *arg)
+{
+# undef qsort_r
+ struct thunk thunk;
+ thunk.cmp = cmp;
+ thunk.arg = arg;
+ qsort_r (base, nmemb, size, &thunk, thunk_cmp);
+}