Index: include/stdlib.h =================================================================== --- include/stdlib.h +++ include/stdlib.h @@ -295,8 +295,8 @@ #endif int mkostemp(char *, int); int mkostemps(char *, int, int); -void qsort_r(void *, size_t, size_t, void *, - int (*)(void *, const void *, const void *)); +void qsort_r(void *, size_t, size_t, + int (*)(const void *, const void *, void *), void *); int radixsort(const unsigned char **, int, const unsigned char *, unsigned); void *reallocarray(void *, size_t, size_t) __result_use_check __alloc_size(2) @@ -309,7 +309,18 @@ void sranddev(void); void srandomdev(void); long long - strtonum(const char *, long long, long long, const char **); + strtonum(const char *, long long, long long, const char **); + +/* Legacy BSD-style qsort_r compatibility shim */ +#if __GNUC_PREREQ__(3, 1) && !defined(__cplusplus) +typedef int (__old_qsort_cmp_t)(void *, const void *, const void *); +void __old_qsort_r(void *, size_t, size_t, void *, __old_qsort_cmp_t *); +__sym_compat(qsort_r, __old_qsort_r, FBSD_1.0); +#define qsort_r(base, nmemb, size, A, B) \ + __builtin_choose_expr( \ + __builtin_types_compatible_p(__typeof(B), __old_qsort_cmp_t), \ + __old_qsort_r, qsort_r)(base, nmemb, size, A, B) +#endif /* Deprecated interfaces, to be removed in FreeBSD 6.0. */ __int64_t Index: lib/libc/stdlib/Makefile.inc =================================================================== --- lib/libc/stdlib/Makefile.inc +++ lib/libc/stdlib/Makefile.inc @@ -11,8 +11,8 @@ getsubopt.c hcreate.c hcreate_r.c hdestroy_r.c heapsort.c heapsort_b.c \ hsearch_r.c imaxabs.c imaxdiv.c \ insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c \ - merge.c mergesort_b.c ptsname.c qsort.c qsort_r.c quick_exit.c \ - radixsort.c rand.c \ + merge.c mergesort_b.c ptsname.c qsort.c qsort_r.c glibc_qsort_r.c \ + quick_exit.c radixsort.c rand.c \ random.c reallocarray.c reallocf.c realpath.c remque.c strfmon.c \ strtoimax.c \ strtol.c strtoll.c strtoq.c strtoul.c strtonum.c strtoull.c \ Index: lib/libc/stdlib/Symbol.map =================================================================== --- lib/libc/stdlib/Symbol.map +++ lib/libc/stdlib/Symbol.map @@ -49,7 +49,6 @@ lfind; mergesort; putenv; - qsort_r; qsort; radixsort; sradixsort; @@ -119,6 +118,7 @@ FBSD_1.5 { __cxa_thread_atexit; __cxa_thread_atexit_impl; + qsort_r; }; FBSDprivate_1.0 { Index: lib/libc/stdlib/glibc_qsort_r.c =================================================================== --- /dev/null +++ lib/libc/stdlib/glibc_qsort_r.c @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2017 Conrad Meyer + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#define I_AM_GLIBC_QSORT_R +#include "qsort.c" Index: lib/libc/stdlib/qsort.3 =================================================================== --- lib/libc/stdlib/qsort.3 +++ lib/libc/stdlib/qsort.3 @@ -32,7 +32,7 @@ .\" @(#)qsort.3 8.1 (Berkeley) 6/4/93 .\" $FreeBSD$ .\" -.Dd February 20, 2013 +.Dd January 13, 2017 .Dt QSORT 3 .Os .Sh NAME @@ -61,8 +61,8 @@ .Fa "void *base" .Fa "size_t nmemb" .Fa "size_t size" +.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *, void *\*[rp]" .Fa "void *thunk" -.Fa "int \*[lp]*compar\*[rp]\*[lp]void *, const void *, const void *\*[rp]" .Fc .Ft int .Fo heapsort @@ -142,7 +142,7 @@ .Fn qsort , except that it takes an additional argument, .Fa thunk , -which is passed unchanged as the first argument to function pointed to +which is passed unchanged as the final argument to the function .Fa compar . This allows the comparison function to access additional data without using global variables, and thus @@ -161,8 +161,9 @@ .Fn heapsort are .Em not -stable, that is, if two members compare as equal, their order in -the sorted array is undefined. +stable. +That is, if two members compare as equal, their order in the sorted +array is undefined. The .Fn heapsort_b function behaves identically to Index: lib/libc/stdlib/qsort.c =================================================================== --- lib/libc/stdlib/qsort.c +++ lib/libc/stdlib/qsort.c @@ -35,8 +35,10 @@ #include -#ifdef I_AM_QSORT_R +#if defined(I_AM_QSORT_R) typedef int cmp_t(void *, const void *, const void *); +#elif defined(I_AM_GLIBC_QSORT_R) +typedef int cmp_t(const void *, const void *, void *); #else typedef int cmp_t(const void *, const void *); #endif @@ -89,15 +91,17 @@ #define vecswap(a, b, n) \ if ((n) > 0) swapfunc(a, b, n, swaptype_long, swaptype_int) -#ifdef I_AM_QSORT_R +#if defined(I_AM_QSORT_R) #define CMP(t, x, y) (cmp((t), (x), (y))) +#elif defined(I_AM_GLIBC_QSORT_R) +#define CMP(t, x, y) (cmp((x), (y), (t))) #else #define CMP(t, x, y) (cmp((x), (y))) #endif static inline char * med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk -#ifndef I_AM_QSORT_R +#if !defined(I_AM_QSORT_R) && !defined(I_AM_GLIBC_QSORT_R) __unused #endif ) @@ -107,9 +111,12 @@ :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c )); } -#ifdef I_AM_QSORT_R +#if defined(I_AM_QSORT_R) void -qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp) +__freebsd11_qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp) +#elif defined(I_AM_GLIBC_QSORT_R) +void +(qsort_r)(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk) #else #define thunk NULL void @@ -187,8 +194,10 @@ r = MIN(pd - pc, pn - pd - es); vecswap(pb, pn - r, r); if ((r = pb - pa) > es) -#ifdef I_AM_QSORT_R - qsort_r(a, r / es, es, thunk, cmp); +#if defined(I_AM_QSORT_R) + __freebsd11_qsort_r(a, r / es, es, thunk, cmp); +#elif defined(I_AM_GLIBC_QSORT_R) + (qsort_r)(a, r / es, es, cmp, thunk); #else qsort(a, r / es, es, cmp); #endif @@ -200,3 +209,7 @@ } /* qsort(pn - r, r / es, es, cmp);*/ } + +#if defined(I_AM_QSORT_R) +__sym_compat(dirname, __freebsd11_qsort_r, FBSD_1.0); +#endif Index: lib/libc/stdlib/qsort_r.c =================================================================== --- lib/libc/stdlib/qsort_r.c +++ lib/libc/stdlib/qsort_r.c @@ -13,7 +13,7 @@ void qsort_b(void *base, size_t nel, size_t width, qsort_block compar) { - qsort_r(base, nel, width, compar, + __old_qsort_r(base, nel, width, compar, (int (*)(void *, const void *, const void *)) GET_BLOCK_FUNCTION(compar)); }