xref: /freebsd/include/stdlib.h (revision d93a896ef95946b0bf1219866fcb324b78543444)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
30  * $FreeBSD$
31  */
32 
33 #ifndef _STDLIB_H_
34 #define	_STDLIB_H_
35 
36 #include <sys/cdefs.h>
37 #include <sys/_null.h>
38 #include <sys/_types.h>
39 
40 __NULLABILITY_PRAGMA_PUSH
41 
42 #if __BSD_VISIBLE
43 #ifndef _RUNE_T_DECLARED
44 typedef	__rune_t	rune_t;
45 #define	_RUNE_T_DECLARED
46 #endif
47 #endif
48 
49 #ifndef _SIZE_T_DECLARED
50 typedef	__size_t	size_t;
51 #define	_SIZE_T_DECLARED
52 #endif
53 
54 #ifndef	__cplusplus
55 #ifndef _WCHAR_T_DECLARED
56 typedef	___wchar_t	wchar_t;
57 #define	_WCHAR_T_DECLARED
58 #endif
59 #endif
60 
61 typedef struct {
62 	int	quot;		/* quotient */
63 	int	rem;		/* remainder */
64 } div_t;
65 
66 typedef struct {
67 	long	quot;
68 	long	rem;
69 } ldiv_t;
70 
71 #define	EXIT_FAILURE	1
72 #define	EXIT_SUCCESS	0
73 
74 #define	RAND_MAX	0x7ffffffd
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);
85 int	 abs(int) __pure2;
86 int	 atexit(void (* _Nonnull)(void));
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_size(1) __alloc_size(2);
94 div_t	 div(int, int) __pure2;
95 _Noreturn void	 exit(int);
96 void	 free(void *);
97 char	*getenv(const char *);
98 long	 labs(long) __pure2;
99 ldiv_t	 ldiv(long, long) __pure2;
100 void	*malloc(size_t) __malloc_like __result_use_check __alloc_size(1);
101 int	 mblen(const char *, size_t);
102 size_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
103 int	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
104 void	 qsort(void *, size_t, size_t,
105 	    int (* _Nonnull)(const void *, const void *));
106 int	 rand(void);
107 void	*realloc(void *, size_t) __result_use_check __alloc_size(2);
108 void	 srand(unsigned);
109 double	 strtod(const char * __restrict, char ** __restrict);
110 float	 strtof(const char * __restrict, char ** __restrict);
111 long	 strtol(const char * __restrict, char ** __restrict, int);
112 long double
113 	 strtold(const char * __restrict, char ** __restrict);
114 unsigned long
115 	 strtoul(const char * __restrict, char ** __restrict, int);
116 int	 system(const char *);
117 int	 wctomb(char *, wchar_t);
118 size_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
119 
120 /*
121  * Functions added in C99 which we make conditionally available in the
122  * BSD^C89 namespace if the compiler supports `long long'.
123  * The #if test is more complicated than it ought to be because
124  * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
125  * is not supported in the compilation environment (which therefore means
126  * that it can't really be ISO C99).
127  *
128  * (The only other extension made by C99 in thie header is _Exit().)
129  */
130 #if __ISO_C_VISIBLE >= 1999 || defined(__cplusplus)
131 #ifdef __LONG_LONG_SUPPORTED
132 /* LONGLONG */
133 typedef struct {
134 	long long quot;
135 	long long rem;
136 } lldiv_t;
137 
138 /* LONGLONG */
139 long long
140 	 atoll(const char *);
141 /* LONGLONG */
142 long long
143 	 llabs(long long) __pure2;
144 /* LONGLONG */
145 lldiv_t	 lldiv(long long, long long) __pure2;
146 /* LONGLONG */
147 long long
148 	 strtoll(const char * __restrict, char ** __restrict, int);
149 /* LONGLONG */
150 unsigned long long
151 	 strtoull(const char * __restrict, char ** __restrict, int);
152 #endif /* __LONG_LONG_SUPPORTED */
153 
154 _Noreturn void	 _Exit(int);
155 #endif /* __ISO_C_VISIBLE >= 1999 */
156 
157 /*
158  * If we're in a mode greater than C99, expose C11 functions.
159  */
160 #if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L
161 void *	aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
162 	    __alloc_size(2);
163 int	at_quick_exit(void (*)(void));
164 _Noreturn void
165 	quick_exit(int);
166 #endif /* __ISO_C_VISIBLE >= 2011 */
167 /*
168  * Extensions made by POSIX relative to C.
169  */
170 #if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE
171 char	*realpath(const char * __restrict, char * __restrict);
172 #endif
173 #if __POSIX_VISIBLE >= 199506
174 int	 rand_r(unsigned *);			/* (TSF) */
175 #endif
176 #if __POSIX_VISIBLE >= 200112
177 int	 posix_memalign(void **, size_t, size_t); /* (ADV) */
178 int	 setenv(const char *, const char *, int);
179 int	 unsetenv(const char *);
180 #endif
181 
182 #if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
183 int	 getsubopt(char **, char *const *, char **);
184 #ifndef _MKDTEMP_DECLARED
185 char	*mkdtemp(char *);
186 #define	_MKDTEMP_DECLARED
187 #endif
188 #ifndef _MKSTEMP_DECLARED
189 int	 mkstemp(char *);
190 #define	_MKSTEMP_DECLARED
191 #endif
192 #endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
193 
194 /*
195  * The only changes to the XSI namespace in revision 6 were the deletion
196  * of the ttyslot() and valloc() functions, which FreeBSD never declared
197  * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
198  * FreeBSD also does not have, and mktemp(), are to be deleted.
199  */
200 #if __XSI_VISIBLE
201 /* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
202 long	 a64l(const char *);
203 double	 drand48(void);
204 /* char	*ecvt(double, int, int * __restrict, int * __restrict); */
205 double	 erand48(unsigned short[3]);
206 /* char	*fcvt(double, int, int * __restrict, int * __restrict); */
207 /* char	*gcvt(double, int, int * __restrict, int * __restrict); */
208 int	 grantpt(int);
209 char	*initstate(unsigned int, char *, size_t);
210 long	 jrand48(unsigned short[3]);
211 char	*l64a(long);
212 void	 lcong48(unsigned short[7]);
213 long	 lrand48(void);
214 #if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600)
215 char	*mktemp(char *);
216 #define	_MKTEMP_DECLARED
217 #endif
218 long	 mrand48(void);
219 long	 nrand48(unsigned short[3]);
220 int	 posix_openpt(int);
221 char	*ptsname(int);
222 int	 putenv(char *);
223 long	 random(void);
224 unsigned short
225 	*seed48(unsigned short[3]);
226 char	*setstate(/* const */ char *);
227 void	 srand48(long);
228 void	 srandom(unsigned int);
229 int	 unlockpt(int);
230 #endif /* __XSI_VISIBLE */
231 
232 #if __BSD_VISIBLE
233 extern const char *malloc_conf;
234 extern void (*malloc_message)(void *, const char *);
235 
236 /*
237  * The alloca() function can't be implemented in C, and on some
238  * platforms it can't be implemented at all as a callable function.
239  * The GNU C compiler provides a built-in alloca() which we can use;
240  * in all other cases, provide a prototype, mainly to pacify various
241  * incarnations of lint.  On platforms where alloca() is not in libc,
242  * programs which use it will fail to link when compiled with non-GNU
243  * 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 #elif defined(lint)
249 void	*alloca(size_t);
250 #endif
251 
252 void	 abort2(const char *, int, void **) __dead2;
253 __uint32_t
254 	 arc4random(void);
255 void	 arc4random_addrandom(unsigned char *, int);
256 void	 arc4random_buf(void *, size_t);
257 void	 arc4random_stir(void);
258 __uint32_t
259 	 arc4random_uniform(__uint32_t);
260 #ifdef __BLOCKS__
261 int	 atexit_b(void (^ _Nonnull)(void));
262 void	*bsearch_b(const void *, const void *, size_t,
263 	    size_t, int (^ _Nonnull)(const void *, const void *));
264 #endif
265 char	*getbsize(int *, long *);
266 					/* getcap(3) functions */
267 char	*cgetcap(char *, const char *, int);
268 int	 cgetclose(void);
269 int	 cgetent(char **, char **, const char *);
270 int	 cgetfirst(char **, char **);
271 int	 cgetmatch(const char *, const char *);
272 int	 cgetnext(char **, char **);
273 int	 cgetnum(char *, const char *, long *);
274 int	 cgetset(const char *);
275 int	 cgetstr(char *, const char *, char **);
276 int	 cgetustr(char *, const char *, char **);
277 
278 int	 daemon(int, int);
279 char	*devname(__dev_t, __mode_t);
280 char	*devname_r(__dev_t, __mode_t, char *, int);
281 char	*fdevname(int);
282 char	*fdevname_r(int, char *, int);
283 int	 getloadavg(double [], int);
284 const char *
285 	 getprogname(void);
286 
287 int	 heapsort(void *, size_t, size_t,
288 	    int (* _Nonnull)(const void *, const void *));
289 #ifdef __BLOCKS__
290 int	 heapsort_b(void *, size_t, size_t,
291 	    int (^ _Nonnull)(const void *, const void *));
292 void	 qsort_b(void *, size_t, size_t,
293 	    int (^ _Nonnull)(const void *, const void *));
294 #endif
295 int	 l64a_r(long, char *, int);
296 int	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
297 #ifdef __BLOCKS__
298 int	 mergesort_b(void *, size_t, size_t, int (^)(const void *, const void *));
299 #endif
300 int	 mkostemp(char *, int);
301 int	 mkostemps(char *, int, int);
302 void	 qsort_r(void *, size_t, size_t, void *,
303 	    int (*)(void *, const void *, const void *));
304 int	 radixsort(const unsigned char **, int, const unsigned char *,
305 	    unsigned);
306 void	*reallocarray(void *, size_t, size_t) __result_use_check __alloc_size(2)
307 	    __alloc_size(3);
308 void	*reallocf(void *, size_t) __alloc_size(2);
309 int	 rpmatch(const char *);
310 void	 setprogname(const char *);
311 int	 sradixsort(const unsigned char **, int, const unsigned char *,
312 	    unsigned);
313 void	 sranddev(void);
314 void	 srandomdev(void);
315 long long
316 	strtonum(const char *, long long, long long, const char **);
317 
318 /* Deprecated interfaces, to be removed. */
319 __int64_t
320 	 strtoq(const char *, char **, int);
321 __uint64_t
322 	 strtouq(const char *, char **, int);
323 
324 extern char *suboptarg;			/* getsubopt(3) external variable */
325 #endif /* __BSD_VISIBLE */
326 
327 #if __EXT1_VISIBLE
328 
329 #ifndef _ERRNO_T_DEFINED
330 #define _ERRNO_T_DEFINED
331 typedef int errno_t;
332 #endif
333 
334 /* K.3.6 */
335 typedef void (*constraint_handler_t)(const char * __restrict,
336     void * __restrict, errno_t);
337 /* K.3.6.1.1 */
338 constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
339 /* K.3.6.1.2 */
340 _Noreturn void abort_handler_s(const char * __restrict, void * __restrict,
341     errno_t);
342 /* K3.6.1.3 */
343 void ignore_handler_s(const char * __restrict, void * __restrict, errno_t);
344 #endif /* __EXT1_VISIBLE */
345 
346 __END_DECLS
347 __NULLABILITY_PRAGMA_POP
348 
349 #endif /* !_STDLIB_H_ */
350