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