master
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)stdlib.h 8.5 (Berkeley) 5/19/95
32 */
33
34#ifndef _STDLIB_H_
35#define _STDLIB_H_
36
37#include <sys/cdefs.h>
38#include <sys/_null.h>
39#include <sys/_types.h>
40
41__NULLABILITY_PRAGMA_PUSH
42
43#if __BSD_VISIBLE
44#ifndef _RUNE_T_DECLARED
45typedef __rune_t rune_t;
46#define _RUNE_T_DECLARED
47#endif
48#endif
49
50#ifndef _SIZE_T_DECLARED
51typedef __size_t size_t;
52#define _SIZE_T_DECLARED
53#endif
54
55#ifndef __cplusplus
56#ifndef _WCHAR_T_DECLARED
57typedef ___wchar_t wchar_t;
58#define _WCHAR_T_DECLARED
59#endif
60#endif
61
62typedef struct {
63 int quot; /* quotient */
64 int rem; /* remainder */
65} div_t;
66
67typedef struct {
68 long quot;
69 long rem;
70} ldiv_t;
71
72#define EXIT_FAILURE 1
73#define EXIT_SUCCESS 0
74
75/*
76 * I.e., INT_MAX; rand(3) returns a signed integer but must produce output in
77 * the range [0, RAND_MAX], so half of the possible output range is unused.
78 */
79#define RAND_MAX 0x7fffffff
80
81__BEGIN_DECLS
82#ifdef _XLOCALE_H_
83#include <xlocale/_stdlib.h>
84#endif
85extern int __mb_cur_max;
86extern int ___mb_cur_max(void);
87#define MB_CUR_MAX ((size_t)___mb_cur_max())
88
89_Noreturn void abort(void);
90int abs(int) __pure2;
91int atexit(void (* _Nonnull)(void));
92double atof(const char *);
93int atoi(const char *);
94long atol(const char *);
95void *bsearch(const void *, const void *, size_t,
96 size_t, int (*)(const void * _Nonnull, const void *));
97void *calloc(size_t, size_t) __malloc_like __result_use_check
98 __alloc_size2(1, 2);
99div_t div(int, int) __pure2;
100_Noreturn void exit(int);
101void free(void *);
102char *getenv(const char *);
103long labs(long) __pure2;
104ldiv_t ldiv(long, long) __pure2;
105void *malloc(size_t) __malloc_like __result_use_check __alloc_size(1);
106int mblen(const char *, size_t);
107size_t mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
108int mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
109void qsort(void *, size_t, size_t,
110 int (* _Nonnull)(const void *, const void *));
111int rand(void);
112void *realloc(void *, size_t) __result_use_check __alloc_size(2);
113void srand(unsigned);
114double strtod(const char * __restrict, char ** __restrict);
115float strtof(const char * __restrict, char ** __restrict);
116long strtol(const char * __restrict, char ** __restrict, int);
117long double
118 strtold(const char * __restrict, char ** __restrict);
119unsigned long
120 strtoul(const char * __restrict, char ** __restrict, int);
121int system(const char *);
122int wctomb(char *, wchar_t);
123size_t wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
124
125/*
126 * Functions added in C99 which we make conditionally available in the
127 * BSD^C89 namespace if the compiler supports `long long'.
128 * The #if test is more complicated than it ought to be because
129 * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
130 * is not supported in the compilation environment (which therefore means
131 * that it can't really be ISO C99).
132 *
133 * (The only other extension made by C99 in this header is _Exit().)
134 */
135#if __ISO_C_VISIBLE >= 1999 || defined(__cplusplus)
136#ifdef __LONG_LONG_SUPPORTED
137/* LONGLONG */
138typedef struct {
139 long long quot;
140 long long rem;
141} lldiv_t;
142
143/* LONGLONG */
144long long
145 atoll(const char *);
146/* LONGLONG */
147long long
148 llabs(long long) __pure2;
149/* LONGLONG */
150lldiv_t lldiv(long long, long long) __pure2;
151/* LONGLONG */
152long long
153 strtoll(const char * __restrict, char ** __restrict, int);
154/* LONGLONG */
155unsigned long long
156 strtoull(const char * __restrict, char ** __restrict, int);
157#endif /* __LONG_LONG_SUPPORTED */
158
159_Noreturn void _Exit(int);
160#endif /* __ISO_C_VISIBLE >= 1999 */
161
162/*
163 * If we're in a mode greater than C99, expose C11 functions.
164 */
165#if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L
166void * aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
167 __alloc_size(2);
168int at_quick_exit(void (*)(void));
169_Noreturn void
170 quick_exit(int);
171#endif /* __ISO_C_VISIBLE >= 2011 */
172/*
173 * Extensions made by POSIX relative to C.
174 */
175#if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE
176char *realpath(const char * __restrict, char * __restrict);
177#endif
178#if __POSIX_VISIBLE >= 199506
179int rand_r(unsigned *); /* (TSF) */
180#endif
181#if __POSIX_VISIBLE >= 200112
182int posix_memalign(void **, size_t, size_t); /* (ADV) */
183int setenv(const char *, const char *, int);
184int unsetenv(const char *);
185#endif
186
187#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
188int getsubopt(char **, char *const *, char **);
189#ifndef _MKDTEMP_DECLARED
190char *mkdtemp(char *);
191#define _MKDTEMP_DECLARED
192#endif
193#ifndef _MKSTEMP_DECLARED
194int mkstemp(char *);
195#define _MKSTEMP_DECLARED
196#endif
197#endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
198
199/*
200 * The only changes to the XSI namespace in revision 6 were the deletion
201 * of the ttyslot() and valloc() functions, which FreeBSD never declared
202 * in this header. For revision 7, ecvt(), fcvt(), and gcvt(), which
203 * FreeBSD also does not have, and mktemp(), are to be deleted.
204 */
205#if __XSI_VISIBLE
206/* XXX XSI requires pollution from <sys/wait.h> here. We'd rather not. */
207long a64l(const char *);
208double drand48(void);
209/* char *ecvt(double, int, int * __restrict, int * __restrict); */
210double erand48(unsigned short[3]);
211/* char *fcvt(double, int, int * __restrict, int * __restrict); */
212/* char *gcvt(double, int, int * __restrict, int * __restrict); */
213char *initstate(unsigned int, char *, size_t);
214long jrand48(unsigned short[3]);
215char *l64a(long);
216void lcong48(unsigned short[7]);
217long lrand48(void);
218#if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600)
219char *mktemp(char *);
220#define _MKTEMP_DECLARED
221#endif
222long mrand48(void);
223long nrand48(unsigned short[3]);
224int putenv(char *);
225long random(void);
226unsigned short
227 *seed48(unsigned short[3]);
228char *setstate(/* const */ char *);
229void srand48(long);
230void srandom(unsigned int);
231#endif /* __XSI_VISIBLE */
232
233#if __XSI_VISIBLE
234int grantpt(int);
235int posix_openpt(int);
236char *ptsname(int);
237int unlockpt(int);
238#endif /* __XSI_VISIBLE */
239#if __BSD_VISIBLE
240/* ptsname_r will be included in POSIX issue 8 */
241int ptsname_r(int, char *, size_t);
242#endif
243
244#if __BSD_VISIBLE
245extern const char *malloc_conf;
246extern void (*malloc_message)(void *, const char *);
247
248/*
249 * The alloca() function can't be implemented in C, and on some
250 * platforms it can't be implemented at all as a callable function.
251 * The GNU C compiler provides a built-in alloca() which we can use.
252 * On platforms where alloca() is not in libc, programs which use it
253 * will fail to link when compiled with non-GNU compilers.
254 */
255#if __GNUC__ >= 2
256#undef alloca /* some GNU bits try to get cute and define this on their own */
257#define alloca(sz) __builtin_alloca(sz)
258#endif
259
260void abort2(const char *, int, void **) __dead2;
261__uint32_t
262 arc4random(void);
263void arc4random_buf(void *, size_t);
264__uint32_t
265 arc4random_uniform(__uint32_t);
266
267#ifdef __BLOCKS__
268int atexit_b(void (^ _Nonnull)(void));
269void *bsearch_b(const void *, const void *, size_t,
270 size_t, int (^ _Nonnull)(const void *, const void *));
271#endif
272char *getbsize(int *, long *);
273 /* getcap(3) functions */
274char *cgetcap(char *, const char *, int);
275int cgetclose(void);
276int cgetent(char **, char **, const char *);
277int cgetfirst(char **, char **);
278int cgetmatch(const char *, const char *);
279int cgetnext(char **, char **);
280int cgetnum(char *, const char *, long *);
281int cgetset(const char *);
282int cgetstr(char *, const char *, char **);
283int cgetustr(char *, const char *, char **);
284
285int clearenv(void);
286
287int daemon(int, int);
288int daemonfd(int, int);
289char *devname(__dev_t, __mode_t);
290char *devname_r(__dev_t, __mode_t, char *, int);
291char *fdevname(int);
292char *fdevname_r(int, char *, int);
293int getloadavg(double [], int);
294const char *
295 getprogname(void);
296
297int heapsort(void *, size_t, size_t,
298 int (* _Nonnull)(const void *, const void *));
299#ifdef __BLOCKS__
300int heapsort_b(void *, size_t, size_t,
301 int (^ _Nonnull)(const void *, const void *));
302void qsort_b(void *, size_t, size_t,
303 int (^ _Nonnull)(const void *, const void *));
304#endif
305int l64a_r(long, char *, int);
306int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
307#ifdef __BLOCKS__
308int mergesort_b(void *, size_t, size_t, int (^)(const void *, const void *));
309#endif
310int mkostemp(char *, int);
311int mkostemps(char *, int, int);
312int mkostempsat(int, char *, int, int);
313void qsort_r(void *, size_t, size_t,
314 int (*)(const void *, const void *, void *), void *);
315int radixsort(const unsigned char **, int, const unsigned char *,
316 unsigned);
317void *reallocarray(void *, size_t, size_t) __result_use_check
318 __alloc_size2(2, 3);
319void *reallocf(void *, size_t) __result_use_check __alloc_size(2);
320int rpmatch(const char *);
321char *secure_getenv(const char *);
322void setprogname(const char *);
323int sradixsort(const unsigned char **, int, const unsigned char *,
324 unsigned);
325void srandomdev(void);
326long long
327 strtonum(const char *, long long, long long, const char **);
328
329/* Deprecated interfaces, to be removed. */
330__int64_t
331 strtoq(const char *, char **, int);
332__uint64_t
333 strtouq(const char *, char **, int);
334
335/*
336 * In FreeBSD 14, the prototype of qsort_r() was modified to comply with
337 * POSIX. The standardized qsort_r()'s order of last two parameters was
338 * changed, and the comparator function is now taking thunk as its last
339 * parameter, and both are different from the ones expected by the historical
340 * FreeBSD qsort_r() interface.
341 *
342 * Apply a workaround where we explicitly link against the historical
343 * interface, qsort_r@FBSD_1.0, in case when qsort_r() is called with
344 * the last parameter with a function pointer that exactly matches the
345 * historical FreeBSD qsort_r() comparator signature, so applications
346 * written for the historical interface can continue to work without
347 * modification.
348 */
349#if defined(__generic) || defined(__cplusplus)
350void __qsort_r_compat(void *, size_t, size_t, void *,
351 int (*)(void *, const void *, const void *));
352__sym_compat(qsort_r, __qsort_r_compat, FBSD_1.0);
353#endif
354#if defined(__generic) && !defined(__cplusplus)
355#define qsort_r(base, nel, width, arg4, arg5) \
356 __generic(arg5, int (*)(void *, const void *, const void *), \
357 __qsort_r_compat, qsort_r)(base, nel, width, arg4, arg5)
358#elif defined(__cplusplus)
359__END_DECLS
360extern "C++" {
361static inline void qsort_r(void *base, size_t nmemb, size_t size,
362 void *thunk, int (*compar)(void *, const void *, const void *)) {
363 __qsort_r_compat(base, nmemb, size, thunk, compar);
364}
365}
366__BEGIN_DECLS
367#endif
368
369extern char *suboptarg; /* getsubopt(3) external variable */
370#endif /* __BSD_VISIBLE */
371
372#if __EXT1_VISIBLE
373
374#ifndef _RSIZE_T_DEFINED
375#define _RSIZE_T_DEFINED
376typedef size_t rsize_t;
377#endif
378
379#ifndef _ERRNO_T_DEFINED
380#define _ERRNO_T_DEFINED
381typedef int errno_t;
382#endif
383
384/* K.3.6 */
385typedef void (*constraint_handler_t)(const char * __restrict,
386 void * __restrict, errno_t);
387/* K.3.6.1.1 */
388constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
389/* K.3.6.1.2 */
390_Noreturn void abort_handler_s(const char * __restrict, void * __restrict,
391 errno_t);
392/* K3.6.1.3 */
393void ignore_handler_s(const char * __restrict, void * __restrict, errno_t);
394/* K.3.6.3.2 */
395errno_t qsort_s(void *, rsize_t, rsize_t,
396 int (*)(const void *, const void *, void *), void *);
397#endif /* __EXT1_VISIBLE */
398
399__END_DECLS
400__NULLABILITY_PRAGMA_POP
401
402#endif /* !_STDLIB_H_ */