Index: include/stdlib.h =================================================================== --- include/stdlib.h +++ include/stdlib.h @@ -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 **); + +/* Linux qsort_r compatibility shim */ +#if __GNUC_PREREQ__(3, 1) && !defined(__cplusplus) +typedef int (__linux_qsort_cmp_t)(const void *, const void *, void *); +void __linux_qsort_r(void *, size_t, size_t, __linux_qsort_cmp_t *, + void *); +#define qsort_r(base, nmemb, size, A, B) \ + __builtin_choose_expr( \ + __builtin_types_compatible_p(__typeof(A), __linux_qsort_cmp_t), \ + __linux_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 linux_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/linux_qsort_r.c =================================================================== --- /dev/null +++ lib/libc/stdlib/linux_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_LINUX_QSORT_R +#include "qsort.c" 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_LINUX_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_LINUX_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_LINUX_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) +(qsort_r)(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp) +#elif defined(I_AM_LINUX_QSORT_R) +void +__linux_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 +#if defined(I_AM_QSORT_R) qsort_r(a, r / es, es, thunk, cmp); +#elif defined(I_AM_LINUX_QSORT_R) + __linux_qsort_r(a, r / es, es, cmp, thunk); #else qsort(a, r / es, es, cmp); #endif