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