159deaec5SRodney W. Grimes /*-
22321c474SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
32321c474SPedro F. Giffuni *
459deaec5SRodney W. Grimes * Copyright (c) 1990, 1993
559deaec5SRodney W. Grimes * The Regents of the University of California. All rights reserved.
659deaec5SRodney W. Grimes *
759deaec5SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
859deaec5SRodney W. Grimes * modification, are permitted provided that the following conditions
959deaec5SRodney W. Grimes * are met:
1059deaec5SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1159deaec5SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
1259deaec5SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
1359deaec5SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
1459deaec5SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15f2556687SWarner Losh * 3. Neither the name of the University nor the names of its contributors
1659deaec5SRodney W. Grimes * may be used to endorse or promote products derived from this software
1759deaec5SRodney W. Grimes * without specific prior written permission.
1859deaec5SRodney W. Grimes *
1959deaec5SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2059deaec5SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2159deaec5SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2259deaec5SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2359deaec5SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2459deaec5SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2559deaec5SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2659deaec5SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2759deaec5SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2859deaec5SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2959deaec5SRodney W. Grimes * SUCH DAMAGE.
3059deaec5SRodney W. Grimes *
31c59376afSPeter Wemm * @(#)stdlib.h 8.5 (Berkeley) 5/19/95
3259deaec5SRodney W. Grimes */
3359deaec5SRodney W. Grimes
3459deaec5SRodney W. Grimes #ifndef _STDLIB_H_
3559deaec5SRodney W. Grimes #define _STDLIB_H_
3659deaec5SRodney W. Grimes
3785b46962SBruce Evans #include <sys/cdefs.h>
3812eb46c8SMarcel Moolenaar #include <sys/_null.h>
39abbd8902SMike Barcroft #include <sys/_types.h>
4059deaec5SRodney W. Grimes
41649702c5SPedro F. Giffuni __NULLABILITY_PRAGMA_PUSH
42649702c5SPedro F. Giffuni
433ecc48e2SGarrett Wollman #if __BSD_VISIBLE
44abbd8902SMike Barcroft #ifndef _RUNE_T_DECLARED
45abbd8902SMike Barcroft typedef __rune_t rune_t;
46abbd8902SMike Barcroft #define _RUNE_T_DECLARED
47cdd84b02SBruce Evans #endif
48cdd84b02SBruce Evans #endif
49cdd84b02SBruce Evans
50abbd8902SMike Barcroft #ifndef _SIZE_T_DECLARED
51abbd8902SMike Barcroft typedef __size_t size_t;
52abbd8902SMike Barcroft #define _SIZE_T_DECLARED
5359deaec5SRodney W. Grimes #endif
5459deaec5SRodney W. Grimes
556087d244SDavid E. O'Brien #ifndef __cplusplus
56abbd8902SMike Barcroft #ifndef _WCHAR_T_DECLARED
570a4c54d6STijl Coosemans typedef ___wchar_t wchar_t;
58abbd8902SMike Barcroft #define _WCHAR_T_DECLARED
5959deaec5SRodney W. Grimes #endif
606087d244SDavid E. O'Brien #endif
6159deaec5SRodney W. Grimes
6259deaec5SRodney W. Grimes typedef struct {
6359deaec5SRodney W. Grimes int quot; /* quotient */
6459deaec5SRodney W. Grimes int rem; /* remainder */
6559deaec5SRodney W. Grimes } div_t;
6659deaec5SRodney W. Grimes
6759deaec5SRodney W. Grimes typedef struct {
68a4c8a68cSBruce Evans long quot;
69a4c8a68cSBruce Evans long rem;
7059deaec5SRodney W. Grimes } ldiv_t;
7159deaec5SRodney W. Grimes
7259deaec5SRodney W. Grimes #define EXIT_FAILURE 1
7359deaec5SRodney W. Grimes #define EXIT_SUCCESS 0
7459deaec5SRodney W. Grimes
75672e1225SConrad Meyer /*
76672e1225SConrad Meyer * I.e., INT_MAX; rand(3) returns a signed integer but must produce output in
77672e1225SConrad Meyer * the range [0, RAND_MAX], so half of the possible output range is unused.
78672e1225SConrad Meyer */
79672e1225SConrad Meyer #define RAND_MAX 0x7fffffff
8059deaec5SRodney W. Grimes
8159deaec5SRodney W. Grimes __BEGIN_DECLS
823ac9d659SDavid Chisnall #ifdef _XLOCALE_H_
833ac9d659SDavid Chisnall #include <xlocale/_stdlib.h>
843ac9d659SDavid Chisnall #endif
853c87aa1dSDavid Chisnall extern int __mb_cur_max;
863c87aa1dSDavid Chisnall extern int ___mb_cur_max(void);
87822b22a9SEd Schouten #define MB_CUR_MAX ((size_t)___mb_cur_max())
883c87aa1dSDavid Chisnall
8957979d1bSDavid Chisnall _Noreturn void abort(void);
90bb28f3c2SWarner Losh int abs(int) __pure2;
91649702c5SPedro F. Giffuni int atexit(void (* _Nonnull)(void));
92bb28f3c2SWarner Losh double atof(const char *);
93bb28f3c2SWarner Losh int atoi(const char *);
94bb28f3c2SWarner Losh long atol(const char *);
95bb28f3c2SWarner Losh void *bsearch(const void *, const void *, size_t,
96649702c5SPedro F. Giffuni size_t, int (*)(const void * _Nonnull, const void *));
97153cbcd6SPedro F. Giffuni void *calloc(size_t, size_t) __malloc_like __result_use_check
98b8d1747eSPedro F. Giffuni __alloc_size2(1, 2);
99bb28f3c2SWarner Losh div_t div(int, int) __pure2;
10057979d1bSDavid Chisnall _Noreturn void exit(int);
101bb28f3c2SWarner Losh void free(void *);
102bb28f3c2SWarner Losh char *getenv(const char *);
103bb28f3c2SWarner Losh long labs(long) __pure2;
104bb28f3c2SWarner Losh ldiv_t ldiv(long, long) __pure2;
105153cbcd6SPedro F. Giffuni void *malloc(size_t) __malloc_like __result_use_check __alloc_size(1);
1063ecc48e2SGarrett Wollman int mblen(const char *, size_t);
1073ecc48e2SGarrett Wollman size_t mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
1083ecc48e2SGarrett Wollman int mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
109bb28f3c2SWarner Losh void qsort(void *, size_t, size_t,
110649702c5SPedro F. Giffuni int (* _Nonnull)(const void *, const void *));
111bb28f3c2SWarner Losh int rand(void);
112153cbcd6SPedro F. Giffuni void *realloc(void *, size_t) __result_use_check __alloc_size(2);
113bb28f3c2SWarner Losh void srand(unsigned);
1145618f724SRobert Drehmel double strtod(const char * __restrict, char ** __restrict);
1156a66acb5SDavid Schultz float strtof(const char * __restrict, char ** __restrict);
1165618f724SRobert Drehmel long strtol(const char * __restrict, char ** __restrict, int);
1176a66acb5SDavid Schultz long double
1186a66acb5SDavid Schultz strtold(const char * __restrict, char ** __restrict);
11959deaec5SRodney W. Grimes unsigned long
1205618f724SRobert Drehmel strtoul(const char * __restrict, char ** __restrict, int);
121bb28f3c2SWarner Losh int system(const char *);
122bb28f3c2SWarner Losh int wctomb(char *, wchar_t);
1239771f1e2STim J. Robbins size_t wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
12459deaec5SRodney W. Grimes
1253ecc48e2SGarrett Wollman /*
1263ecc48e2SGarrett Wollman * Functions added in C99 which we make conditionally available in the
1273ecc48e2SGarrett Wollman * BSD^C89 namespace if the compiler supports `long long'.
1283ecc48e2SGarrett Wollman * The #if test is more complicated than it ought to be because
1293ecc48e2SGarrett Wollman * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
1303ecc48e2SGarrett Wollman * is not supported in the compilation environment (which therefore means
1313ecc48e2SGarrett Wollman * that it can't really be ISO C99).
1323ecc48e2SGarrett Wollman *
133ce7db385SElyes Haouas * (The only other extension made by C99 in this header is _Exit().)
1343ecc48e2SGarrett Wollman */
1354ec98362SDimitry Andric #if __ISO_C_VISIBLE >= 1999 || defined(__cplusplus)
1363ecc48e2SGarrett Wollman #ifdef __LONG_LONG_SUPPORTED
1373ecc48e2SGarrett Wollman /* LONGLONG */
1383ecc48e2SGarrett Wollman typedef struct {
1393ecc48e2SGarrett Wollman long long quot;
1403ecc48e2SGarrett Wollman long long rem;
1413ecc48e2SGarrett Wollman } lldiv_t;
142798c1fd8SPoul-Henning Kamp
1433ecc48e2SGarrett Wollman /* LONGLONG */
1443ecc48e2SGarrett Wollman long long
1453ecc48e2SGarrett Wollman atoll(const char *);
1463ecc48e2SGarrett Wollman /* LONGLONG */
1473ecc48e2SGarrett Wollman long long
1483ecc48e2SGarrett Wollman llabs(long long) __pure2;
1493ecc48e2SGarrett Wollman /* LONGLONG */
1503ecc48e2SGarrett Wollman lldiv_t lldiv(long long, long long) __pure2;
1513ecc48e2SGarrett Wollman /* LONGLONG */
1523ecc48e2SGarrett Wollman long long
1533ecc48e2SGarrett Wollman strtoll(const char * __restrict, char ** __restrict, int);
1543ecc48e2SGarrett Wollman /* LONGLONG */
1553ecc48e2SGarrett Wollman unsigned long long
1563ecc48e2SGarrett Wollman strtoull(const char * __restrict, char ** __restrict, int);
1573ecc48e2SGarrett Wollman #endif /* __LONG_LONG_SUPPORTED */
1583ecc48e2SGarrett Wollman
15957979d1bSDavid Chisnall _Noreturn void _Exit(int);
1603ecc48e2SGarrett Wollman #endif /* __ISO_C_VISIBLE >= 1999 */
1613ecc48e2SGarrett Wollman
1623ecc48e2SGarrett Wollman /*
1635d8c5f69SEd Schouten * If we're in a mode greater than C99, expose C11 functions.
1640a31efe0SDavid Chisnall */
16579d09835SDavid Chisnall #if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L
166ee90489bSPedro F. Giffuni void * aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
167ee90489bSPedro F. Giffuni __alloc_size(2);
1685d8c5f69SEd Schouten int at_quick_exit(void (*)(void));
169b1214a51SEd Schouten _Noreturn void
170b1214a51SEd Schouten quick_exit(int);
171b1214a51SEd Schouten #endif /* __ISO_C_VISIBLE >= 2011 */
1720a31efe0SDavid Chisnall /*
173e45e1f27SJilles Tjoelker * Extensions made by POSIX relative to C.
1743ecc48e2SGarrett Wollman */
175e45e1f27SJilles Tjoelker #if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE
176585b8e0fSKonstantin Belousov char *realpath(const char * __restrict, char * __restrict);
177e45e1f27SJilles Tjoelker #endif
178e45e1f27SJilles Tjoelker #if __POSIX_VISIBLE >= 199506
179e45e1f27SJilles Tjoelker int rand_r(unsigned *); /* (TSF) */
180e45e1f27SJilles Tjoelker #endif
181e45e1f27SJilles Tjoelker #if __POSIX_VISIBLE >= 200112
182f1b298adSPedro F. Giffuni int posix_memalign(void **, size_t, size_t); /* (ADV) */
183bb28f3c2SWarner Losh int setenv(const char *, const char *, int);
1842966d28cSSean Farley int unsetenv(const char *);
1853ecc48e2SGarrett Wollman #endif
18659deaec5SRodney W. Grimes
18765e8b129SDavid Schultz #if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
18865e8b129SDavid Schultz int getsubopt(char **, char *const *, char **);
18965e8b129SDavid Schultz #ifndef _MKDTEMP_DECLARED
19065e8b129SDavid Schultz char *mkdtemp(char *);
19165e8b129SDavid Schultz #define _MKDTEMP_DECLARED
19265e8b129SDavid Schultz #endif
19365e8b129SDavid Schultz #ifndef _MKSTEMP_DECLARED
19465e8b129SDavid Schultz int mkstemp(char *);
19565e8b129SDavid Schultz #define _MKSTEMP_DECLARED
19665e8b129SDavid Schultz #endif
19765e8b129SDavid Schultz #endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
19865e8b129SDavid Schultz
1993ecc48e2SGarrett Wollman /*
2003ecc48e2SGarrett Wollman * The only changes to the XSI namespace in revision 6 were the deletion
2013ecc48e2SGarrett Wollman * of the ttyslot() and valloc() functions, which FreeBSD never declared
2023ecc48e2SGarrett Wollman * in this header. For revision 7, ecvt(), fcvt(), and gcvt(), which
2033ecc48e2SGarrett Wollman * FreeBSD also does not have, and mktemp(), are to be deleted.
2043ecc48e2SGarrett Wollman */
2053ecc48e2SGarrett Wollman #if __XSI_VISIBLE
2063ecc48e2SGarrett Wollman /* XXX XSI requires pollution from <sys/wait.h> here. We'd rather not. */
207257551c6STom Rhodes long a64l(const char *);
208bb28f3c2SWarner Losh double drand48(void);
2093ecc48e2SGarrett Wollman /* char *ecvt(double, int, int * __restrict, int * __restrict); */
210bb28f3c2SWarner Losh double erand48(unsigned short[3]);
2113ecc48e2SGarrett Wollman /* char *fcvt(double, int, int * __restrict, int * __restrict); */
2123ecc48e2SGarrett Wollman /* char *gcvt(double, int, int * __restrict, int * __restrict); */
2138de6c267SEd Schouten char *initstate(unsigned int, char *, size_t);
214bb28f3c2SWarner Losh long jrand48(unsigned short[3]);
215257551c6STom Rhodes char *l64a(long);
216bb28f3c2SWarner Losh void lcong48(unsigned short[7]);
217bb28f3c2SWarner Losh long lrand48(void);
218d9c43159SDavid Schultz #if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600)
2193ecc48e2SGarrett Wollman char *mktemp(char *);
2203ecc48e2SGarrett Wollman #define _MKTEMP_DECLARED
2213ecc48e2SGarrett Wollman #endif
222bb28f3c2SWarner Losh long mrand48(void);
223bb28f3c2SWarner Losh long nrand48(unsigned short[3]);
2242966d28cSSean Farley int putenv(char *);
2253ecc48e2SGarrett Wollman long random(void);
22667c54240SBruce Evans unsigned short
227bb28f3c2SWarner Losh *seed48(unsigned short[3]);
2283ecc48e2SGarrett Wollman char *setstate(/* const */ char *);
229bb28f3c2SWarner Losh void srand48(long);
2308de6c267SEd Schouten void srandom(unsigned int);
2315011fb43SXin LI #endif /* __XSI_VISIBLE */
2325011fb43SXin LI
2335011fb43SXin LI #if __XSI_VISIBLE
2345011fb43SXin LI int grantpt(int);
2355011fb43SXin LI int posix_openpt(int);
2365011fb43SXin LI char *ptsname(int);
237d3951ad1SJuli Mallett int unlockpt(int);
2383ecc48e2SGarrett Wollman #endif /* __XSI_VISIBLE */
2395011fb43SXin LI #if __BSD_VISIBLE
2405011fb43SXin LI /* ptsname_r will be included in POSIX issue 8 */
2415011fb43SXin LI int ptsname_r(int, char *, size_t);
2425011fb43SXin LI #endif
2433ecc48e2SGarrett Wollman
2443ecc48e2SGarrett Wollman #if __BSD_VISIBLE
245a4bd5210SJason Evans extern const char *malloc_conf;
246a4bd5210SJason Evans extern void (*malloc_message)(void *, const char *);
247f8f6d0dcSAndreas Schulz
24879806b4cSDag-Erling Smørgrav /*
24979806b4cSDag-Erling Smørgrav * The alloca() function can't be implemented in C, and on some
25079806b4cSDag-Erling Smørgrav * platforms it can't be implemented at all as a callable function.
251383f241dSKonstantin Belousov * The GNU C compiler provides a built-in alloca() which we can use.
252383f241dSKonstantin Belousov * On platforms where alloca() is not in libc, programs which use it
253383f241dSKonstantin Belousov * will fail to link when compiled with non-GNU compilers.
25479806b4cSDag-Erling Smørgrav */
25560b426f4SWarner Losh #if __GNUC__ >= 2
256d7875fc3SDavid E. O'Brien #undef alloca /* some GNU bits try to get cute and define this on their own */
25779806b4cSDag-Erling Smørgrav #define alloca(sz) __builtin_alloca(sz)
25879806b4cSDag-Erling Smørgrav #endif
25979806b4cSDag-Erling Smørgrav
260b38a55d4SDavid Schultz void abort2(const char *, int, void **) __dead2;
2618252a465SDmitrij Tejblum __uint32_t
262bb28f3c2SWarner Losh arc4random(void);
2633204108bSAndrey A. Chernov void arc4random_buf(void *, size_t);
264c0046493SAndrey A. Chernov __uint32_t
265c0046493SAndrey A. Chernov arc4random_uniform(__uint32_t);
266c1e80940SXin LI
267f4189cd6SPedro F. Giffuni #ifdef __BLOCKS__
268649702c5SPedro F. Giffuni int atexit_b(void (^ _Nonnull)(void));
269f4189cd6SPedro F. Giffuni void *bsearch_b(const void *, const void *, size_t,
270649702c5SPedro F. Giffuni size_t, int (^ _Nonnull)(const void *, const void *));
271f4189cd6SPedro F. Giffuni #endif
272c2e55537SDavid E. O'Brien char *getbsize(int *, long *);
2733ecc48e2SGarrett Wollman /* getcap(3) functions */
274bb28f3c2SWarner Losh char *cgetcap(char *, const char *, int);
275bb28f3c2SWarner Losh int cgetclose(void);
276bb28f3c2SWarner Losh int cgetent(char **, char **, const char *);
277bb28f3c2SWarner Losh int cgetfirst(char **, char **);
278bb28f3c2SWarner Losh int cgetmatch(const char *, const char *);
279bb28f3c2SWarner Losh int cgetnext(char **, char **);
280bb28f3c2SWarner Losh int cgetnum(char *, const char *, long *);
281bb28f3c2SWarner Losh int cgetset(const char *);
282bb28f3c2SWarner Losh int cgetstr(char *, const char *, char **);
283bb28f3c2SWarner Losh int cgetustr(char *, const char *, char **);
28459deaec5SRodney W. Grimes
285597b0267SMariusz Zaborski int clearenv(void);
286597b0267SMariusz Zaborski
287bb28f3c2SWarner Losh int daemon(int, int);
28816545cf5SMariusz Zaborski int daemonfd(int, int);
2899d301680SStefan Farfeleder char *devname(__dev_t, __mode_t);
2909d301680SStefan Farfeleder char *devname_r(__dev_t, __mode_t, char *, int);
29126d4f5e9SEd Schouten char *fdevname(int);
29226d4f5e9SEd Schouten char *fdevname_r(int, char *, int);
293bb28f3c2SWarner Losh int getloadavg(double [], int);
294f6ab8089SEd Schouten const char *
295bb28f3c2SWarner Losh getprogname(void);
29659deaec5SRodney W. Grimes
297649702c5SPedro F. Giffuni int heapsort(void *, size_t, size_t,
298649702c5SPedro F. Giffuni int (* _Nonnull)(const void *, const void *));
29946cdc140SDavid Chisnall #ifdef __BLOCKS__
300649702c5SPedro F. Giffuni int heapsort_b(void *, size_t, size_t,
301649702c5SPedro F. Giffuni int (^ _Nonnull)(const void *, const void *));
302f4189cd6SPedro F. Giffuni void qsort_b(void *, size_t, size_t,
303649702c5SPedro F. Giffuni int (^ _Nonnull)(const void *, const void *));
30446cdc140SDavid Chisnall #endif
305257551c6STom Rhodes int l64a_r(long, char *, int);
306bb28f3c2SWarner Losh int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
30746cdc140SDavid Chisnall #ifdef __BLOCKS__
30846cdc140SDavid Chisnall int mergesort_b(void *, size_t, size_t, int (^)(const void *, const void *));
30946cdc140SDavid Chisnall #endif
31065ba8dffSJilles Tjoelker int mkostemp(char *, int);
31165ba8dffSJilles Tjoelker int mkostemps(char *, int, int);
31207657474SMark Johnston int mkostempsat(int, char *, int, int);
313af3c7888SEd Schouten void qsort_r(void *, size_t, size_t,
314af3c7888SEd Schouten int (*)(const void *, const void *, void *), void *);
315bb28f3c2SWarner Losh int radixsort(const unsigned char **, int, const unsigned char *,
316bb28f3c2SWarner Losh unsigned);
317b8d1747eSPedro F. Giffuni void *reallocarray(void *, size_t, size_t) __result_use_check
318b8d1747eSPedro F. Giffuni __alloc_size2(2, 3);
319dd5edb11SPedro F. Giffuni void *reallocf(void *, size_t) __result_use_check __alloc_size(2);
32017ebe400STim J. Robbins int rpmatch(const char *);
321a06761e3SWarner Losh char *secure_getenv(const char *);
322bb28f3c2SWarner Losh void setprogname(const char *);
3230855f655SGarrett Wollman int sradixsort(const unsigned char **, int, const unsigned char *,
3240855f655SGarrett Wollman unsigned);
325bb28f3c2SWarner Losh void srandomdev(void);
326c74dfa2fSAndre Oppermann long long
327c74dfa2fSAndre Oppermann strtonum(const char *, long long, long long, const char **);
3283ecc48e2SGarrett Wollman
32910723054SPedro F. Giffuni /* Deprecated interfaces, to be removed. */
330a4c8a68cSBruce Evans __int64_t
331a4c8a68cSBruce Evans strtoq(const char *, char **, int);
3328252a465SDmitrij Tejblum __uint64_t
333bb28f3c2SWarner Losh strtouq(const char *, char **, int);
3348720578dSAndrey A. Chernov
335af3c7888SEd Schouten /*
336af3c7888SEd Schouten * In FreeBSD 14, the prototype of qsort_r() was modified to comply with
337af3c7888SEd Schouten * POSIX. The standardized qsort_r()'s order of last two parameters was
338af3c7888SEd Schouten * changed, and the comparator function is now taking thunk as its last
339af3c7888SEd Schouten * parameter, and both are different from the ones expected by the historical
340af3c7888SEd Schouten * FreeBSD qsort_r() interface.
341af3c7888SEd Schouten *
342af3c7888SEd Schouten * Apply a workaround where we explicitly link against the historical
343af3c7888SEd Schouten * interface, qsort_r@FBSD_1.0, in case when qsort_r() is called with
344af3c7888SEd Schouten * the last parameter with a function pointer that exactly matches the
345af3c7888SEd Schouten * historical FreeBSD qsort_r() comparator signature, so applications
346af3c7888SEd Schouten * written for the historical interface can continue to work without
347af3c7888SEd Schouten * modification.
348af3c7888SEd Schouten */
349af3c7888SEd Schouten #if defined(__generic) || defined(__cplusplus)
350af3c7888SEd Schouten void __qsort_r_compat(void *, size_t, size_t, void *,
351af3c7888SEd Schouten int (*)(void *, const void *, const void *));
352af3c7888SEd Schouten __sym_compat(qsort_r, __qsort_r_compat, FBSD_1.0);
353af3c7888SEd Schouten #endif
354af3c7888SEd Schouten #if defined(__generic) && !defined(__cplusplus)
355af3c7888SEd Schouten #define qsort_r(base, nel, width, arg4, arg5) \
356af3c7888SEd Schouten __generic(arg5, int (*)(void *, const void *, const void *), \
357af3c7888SEd Schouten __qsort_r_compat, qsort_r)(base, nel, width, arg4, arg5)
358af3c7888SEd Schouten #elif defined(__cplusplus)
359af3c7888SEd Schouten __END_DECLS
360af3c7888SEd Schouten extern "C++" {
qsort_r(void * base,size_t nmemb,size_t size,void * thunk,int (* compar)(void *,const void *,const void *))361af3c7888SEd Schouten static inline void qsort_r(void *base, size_t nmemb, size_t size,
362af3c7888SEd Schouten void *thunk, int (*compar)(void *, const void *, const void *)) {
363af3c7888SEd Schouten __qsort_r_compat(base, nmemb, size, thunk, compar);
364af3c7888SEd Schouten }
365af3c7888SEd Schouten }
366af3c7888SEd Schouten __BEGIN_DECLS
367af3c7888SEd Schouten #endif
368af3c7888SEd Schouten
3698720578dSAndrey A. Chernov extern char *suboptarg; /* getsubopt(3) external variable */
3703ecc48e2SGarrett Wollman #endif /* __BSD_VISIBLE */
3719851b340SKonstantin Belousov
3729851b340SKonstantin Belousov #if __EXT1_VISIBLE
3739851b340SKonstantin Belousov
3740d2fabfcSEdward Tomasz Napierala #ifndef _RSIZE_T_DEFINED
3750d2fabfcSEdward Tomasz Napierala #define _RSIZE_T_DEFINED
3760d2fabfcSEdward Tomasz Napierala typedef size_t rsize_t;
3770d2fabfcSEdward Tomasz Napierala #endif
3780d2fabfcSEdward Tomasz Napierala
3799851b340SKonstantin Belousov #ifndef _ERRNO_T_DEFINED
3809851b340SKonstantin Belousov #define _ERRNO_T_DEFINED
3819851b340SKonstantin Belousov typedef int errno_t;
3829851b340SKonstantin Belousov #endif
3839851b340SKonstantin Belousov
3849851b340SKonstantin Belousov /* K.3.6 */
3859851b340SKonstantin Belousov typedef void (*constraint_handler_t)(const char * __restrict,
3869851b340SKonstantin Belousov void * __restrict, errno_t);
3879851b340SKonstantin Belousov /* K.3.6.1.1 */
3889851b340SKonstantin Belousov constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
3899851b340SKonstantin Belousov /* K.3.6.1.2 */
3909851b340SKonstantin Belousov _Noreturn void abort_handler_s(const char * __restrict, void * __restrict,
3919851b340SKonstantin Belousov errno_t);
3929851b340SKonstantin Belousov /* K3.6.1.3 */
3939851b340SKonstantin Belousov void ignore_handler_s(const char * __restrict, void * __restrict, errno_t);
3940d2fabfcSEdward Tomasz Napierala /* K.3.6.3.2 */
3950d2fabfcSEdward Tomasz Napierala errno_t qsort_s(void *, rsize_t, rsize_t,
3960d2fabfcSEdward Tomasz Napierala int (*)(const void *, const void *, void *), void *);
397*a7469c9cSHans Petter Selasky #endif /* __EXT1_VISIBLE */
3988dcf3a82SHans Petter Selasky
39959deaec5SRodney W. Grimes __END_DECLS
400649702c5SPedro F. Giffuni __NULLABILITY_PRAGMA_POP
40159deaec5SRodney W. Grimes
40267c54240SBruce Evans #endif /* !_STDLIB_H_ */
403