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