xref: /freebsd/include/stdlib.h (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
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 #define	RAND_MAX	0x7ffffffd
77 
78 __BEGIN_DECLS
79 #ifdef _XLOCALE_H_
80 #include <xlocale/_stdlib.h>
81 #endif
82 extern int __mb_cur_max;
83 extern int ___mb_cur_max(void);
84 #define	MB_CUR_MAX	((size_t)___mb_cur_max())
85 
86 _Noreturn void	 abort(void);
87 int	 abs(int) __pure2;
88 int	 atexit(void (* _Nonnull)(void));
89 double	 atof(const char *);
90 int	 atoi(const char *);
91 long	 atol(const char *);
92 void	*bsearch(const void *, const void *, size_t,
93 	    size_t, int (*)(const void * _Nonnull, const void *));
94 void	*calloc(size_t, size_t) __malloc_like __result_use_check
95 	     __alloc_size2(1, 2);
96 div_t	 div(int, int) __pure2;
97 _Noreturn void	 exit(int);
98 void	 free(void *);
99 char	*getenv(const char *);
100 long	 labs(long) __pure2;
101 ldiv_t	 ldiv(long, long) __pure2;
102 void	*malloc(size_t) __malloc_like __result_use_check __alloc_size(1);
103 int	 mblen(const char *, size_t);
104 size_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
105 int	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
106 void	 qsort(void *, size_t, size_t,
107 	    int (* _Nonnull)(const void *, const void *));
108 int	 rand(void);
109 void	*realloc(void *, size_t) __result_use_check __alloc_size(2);
110 void	 srand(unsigned);
111 double	 strtod(const char * __restrict, char ** __restrict);
112 float	 strtof(const char * __restrict, char ** __restrict);
113 long	 strtol(const char * __restrict, char ** __restrict, int);
114 long double
115 	 strtold(const char * __restrict, char ** __restrict);
116 unsigned long
117 	 strtoul(const char * __restrict, char ** __restrict, int);
118 int	 system(const char *);
119 int	 wctomb(char *, wchar_t);
120 size_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
121 
122 /*
123  * Functions added in C99 which we make conditionally available in the
124  * BSD^C89 namespace if the compiler supports `long long'.
125  * The #if test is more complicated than it ought to be because
126  * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
127  * is not supported in the compilation environment (which therefore means
128  * that it can't really be ISO C99).
129  *
130  * (The only other extension made by C99 in thie header is _Exit().)
131  */
132 #if __ISO_C_VISIBLE >= 1999 || defined(__cplusplus)
133 #ifdef __LONG_LONG_SUPPORTED
134 /* LONGLONG */
135 typedef struct {
136 	long long quot;
137 	long long rem;
138 } lldiv_t;
139 
140 /* LONGLONG */
141 long long
142 	 atoll(const char *);
143 /* LONGLONG */
144 long long
145 	 llabs(long long) __pure2;
146 /* LONGLONG */
147 lldiv_t	 lldiv(long long, long long) __pure2;
148 /* LONGLONG */
149 long long
150 	 strtoll(const char * __restrict, char ** __restrict, int);
151 /* LONGLONG */
152 unsigned long long
153 	 strtoull(const char * __restrict, char ** __restrict, int);
154 #endif /* __LONG_LONG_SUPPORTED */
155 
156 _Noreturn void	 _Exit(int);
157 #endif /* __ISO_C_VISIBLE >= 1999 */
158 
159 /*
160  * If we're in a mode greater than C99, expose C11 functions.
161  */
162 #if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L
163 void *	aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
164 	    __alloc_size(2);
165 int	at_quick_exit(void (*)(void));
166 _Noreturn void
167 	quick_exit(int);
168 #endif /* __ISO_C_VISIBLE >= 2011 */
169 /*
170  * Extensions made by POSIX relative to C.
171  */
172 #if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE
173 char	*realpath(const char * __restrict, char * __restrict);
174 #endif
175 #if __POSIX_VISIBLE >= 199506
176 int	 rand_r(unsigned *);			/* (TSF) */
177 #endif
178 #if __POSIX_VISIBLE >= 200112
179 int	 posix_memalign(void **, size_t, size_t); /* (ADV) */
180 int	 setenv(const char *, const char *, int);
181 int	 unsetenv(const char *);
182 #endif
183 
184 #if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
185 int	 getsubopt(char **, char *const *, char **);
186 #ifndef _MKDTEMP_DECLARED
187 char	*mkdtemp(char *);
188 #define	_MKDTEMP_DECLARED
189 #endif
190 #ifndef _MKSTEMP_DECLARED
191 int	 mkstemp(char *);
192 #define	_MKSTEMP_DECLARED
193 #endif
194 #endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
195 
196 /*
197  * The only changes to the XSI namespace in revision 6 were the deletion
198  * of the ttyslot() and valloc() functions, which FreeBSD never declared
199  * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
200  * FreeBSD also does not have, and mktemp(), are to be deleted.
201  */
202 #if __XSI_VISIBLE
203 /* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
204 long	 a64l(const char *);
205 double	 drand48(void);
206 /* char	*ecvt(double, int, int * __restrict, int * __restrict); */
207 double	 erand48(unsigned short[3]);
208 /* char	*fcvt(double, int, int * __restrict, int * __restrict); */
209 /* char	*gcvt(double, int, int * __restrict, int * __restrict); */
210 int	 grantpt(int);
211 char	*initstate(unsigned int, char *, size_t);
212 long	 jrand48(unsigned short[3]);
213 char	*l64a(long);
214 void	 lcong48(unsigned short[7]);
215 long	 lrand48(void);
216 #if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600)
217 char	*mktemp(char *);
218 #define	_MKTEMP_DECLARED
219 #endif
220 long	 mrand48(void);
221 long	 nrand48(unsigned short[3]);
222 int	 posix_openpt(int);
223 char	*ptsname(int);
224 int	 putenv(char *);
225 long	 random(void);
226 unsigned short
227 	*seed48(unsigned short[3]);
228 char	*setstate(/* const */ char *);
229 void	 srand48(long);
230 void	 srandom(unsigned int);
231 int	 unlockpt(int);
232 #endif /* __XSI_VISIBLE */
233 
234 #if __BSD_VISIBLE
235 extern const char *malloc_conf;
236 extern void (*malloc_message)(void *, const char *);
237 
238 /*
239  * The alloca() function can't be implemented in C, and on some
240  * platforms it can't be implemented at all as a callable function.
241  * The GNU C compiler provides a built-in alloca() which we can use.
242  * On platforms where alloca() is not in libc, programs which use it
243  * will fail to link when compiled with non-GNU compilers.
244  */
245 #if __GNUC__ >= 2 || defined(__INTEL_COMPILER)
246 #undef  alloca	/* some GNU bits try to get cute and define this on their own */
247 #define alloca(sz) __builtin_alloca(sz)
248 #endif
249 
250 void	 abort2(const char *, int, void **) __dead2;
251 __uint32_t
252 	 arc4random(void);
253 void	 arc4random_addrandom(unsigned char *, int);
254 void	 arc4random_buf(void *, size_t);
255 void	 arc4random_stir(void);
256 __uint32_t
257 	 arc4random_uniform(__uint32_t);
258 #ifdef __BLOCKS__
259 int	 atexit_b(void (^ _Nonnull)(void));
260 void	*bsearch_b(const void *, const void *, size_t,
261 	    size_t, int (^ _Nonnull)(const void *, const void *));
262 #endif
263 char	*getbsize(int *, long *);
264 					/* getcap(3) functions */
265 char	*cgetcap(char *, const char *, int);
266 int	 cgetclose(void);
267 int	 cgetent(char **, char **, const char *);
268 int	 cgetfirst(char **, char **);
269 int	 cgetmatch(const char *, const char *);
270 int	 cgetnext(char **, char **);
271 int	 cgetnum(char *, const char *, long *);
272 int	 cgetset(const char *);
273 int	 cgetstr(char *, const char *, char **);
274 int	 cgetustr(char *, const char *, char **);
275 
276 int	 daemon(int, int);
277 int	 daemonfd(int, int);
278 char	*devname(__dev_t, __mode_t);
279 char	*devname_r(__dev_t, __mode_t, char *, int);
280 char	*fdevname(int);
281 char	*fdevname_r(int, char *, int);
282 int	 getloadavg(double [], int);
283 const char *
284 	 getprogname(void);
285 
286 int	 heapsort(void *, size_t, size_t,
287 	    int (* _Nonnull)(const void *, const void *));
288 #ifdef __BLOCKS__
289 int	 heapsort_b(void *, size_t, size_t,
290 	    int (^ _Nonnull)(const void *, const void *));
291 void	 qsort_b(void *, size_t, size_t,
292 	    int (^ _Nonnull)(const void *, const void *));
293 #endif
294 int	 l64a_r(long, char *, int);
295 int	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
296 #ifdef __BLOCKS__
297 int	 mergesort_b(void *, size_t, size_t, int (^)(const void *, const void *));
298 #endif
299 int	 mkostemp(char *, int);
300 int	 mkostemps(char *, int, int);
301 void	 qsort_r(void *, size_t, size_t, void *,
302 	    int (*)(void *, const void *, const void *));
303 int	 radixsort(const unsigned char **, int, const unsigned char *,
304 	    unsigned);
305 void	*reallocarray(void *, size_t, size_t) __result_use_check
306 	    __alloc_size2(2, 3);
307 void	*reallocf(void *, size_t) __result_use_check __alloc_size(2);
308 int	 rpmatch(const char *);
309 void	 setprogname(const char *);
310 int	 sradixsort(const unsigned char **, int, const unsigned char *,
311 	    unsigned);
312 void	 sranddev(void);
313 void	 srandomdev(void);
314 long long
315 	strtonum(const char *, long long, long long, const char **);
316 
317 /* Deprecated interfaces, to be removed. */
318 __int64_t
319 	 strtoq(const char *, char **, int);
320 __uint64_t
321 	 strtouq(const char *, char **, int);
322 
323 extern char *suboptarg;			/* getsubopt(3) external variable */
324 #endif /* __BSD_VISIBLE */
325 
326 #if __EXT1_VISIBLE
327 
328 #ifndef _ERRNO_T_DEFINED
329 #define _ERRNO_T_DEFINED
330 typedef int errno_t;
331 #endif
332 
333 /* K.3.6 */
334 typedef void (*constraint_handler_t)(const char * __restrict,
335     void * __restrict, errno_t);
336 /* K.3.6.1.1 */
337 constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
338 /* K.3.6.1.2 */
339 _Noreturn void abort_handler_s(const char * __restrict, void * __restrict,
340     errno_t);
341 /* K3.6.1.3 */
342 void ignore_handler_s(const char * __restrict, void * __restrict, errno_t);
343 #endif /* __EXT1_VISIBLE */
344 
345 __END_DECLS
346 __NULLABILITY_PRAGMA_POP
347 
348 #endif /* !_STDLIB_H_ */
349