xref: /illumos-gate/usr/src/boot/include/stdlib.h (revision 59c22e7a)
1199767f8SToomas Soome /*-
2199767f8SToomas Soome  * Copyright (c) 1990, 1993
3199767f8SToomas Soome  *	The Regents of the University of California.  All rights reserved.
4199767f8SToomas Soome  *
5199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome  * modification, are permitted provided that the following conditions
7199767f8SToomas Soome  * are met:
8199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
13199767f8SToomas Soome  * 3. Neither the name of the University nor the names of its contributors
14199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
15199767f8SToomas Soome  *    without specific prior written permission.
16199767f8SToomas Soome  *
17199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27199767f8SToomas Soome  * SUCH DAMAGE.
28199767f8SToomas Soome  *
29199767f8SToomas Soome  *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
30199767f8SToomas Soome  * $FreeBSD$
31199767f8SToomas Soome  */
32199767f8SToomas Soome 
33199767f8SToomas Soome #ifndef _STDLIB_H_
34199767f8SToomas Soome #define	_STDLIB_H_
35199767f8SToomas Soome 
36199767f8SToomas Soome #include <sys/cdefs.h>
37199767f8SToomas Soome #include <sys/_null.h>
38199767f8SToomas Soome #include <sys/_types.h>
39199767f8SToomas Soome 
40199767f8SToomas Soome #if __BSD_VISIBLE
41199767f8SToomas Soome #ifndef _RUNE_T_DECLARED
42199767f8SToomas Soome typedef	__rune_t	rune_t;
43199767f8SToomas Soome #define	_RUNE_T_DECLARED
44199767f8SToomas Soome #endif
45199767f8SToomas Soome #endif
46199767f8SToomas Soome 
47199767f8SToomas Soome #ifndef _SIZE_T_DECLARED
48199767f8SToomas Soome typedef	__size_t	size_t;
49199767f8SToomas Soome #define	_SIZE_T_DECLARED
50199767f8SToomas Soome #endif
51199767f8SToomas Soome 
52199767f8SToomas Soome #ifndef	__cplusplus
53199767f8SToomas Soome #ifndef _WCHAR_T_DECLARED
54199767f8SToomas Soome typedef	___wchar_t	wchar_t;
55199767f8SToomas Soome #define	_WCHAR_T_DECLARED
56199767f8SToomas Soome #endif
57199767f8SToomas Soome #endif
58199767f8SToomas Soome 
59199767f8SToomas Soome typedef struct {
60199767f8SToomas Soome 	int	quot;		/* quotient */
61199767f8SToomas Soome 	int	rem;		/* remainder */
62199767f8SToomas Soome } div_t;
63199767f8SToomas Soome 
64199767f8SToomas Soome typedef struct {
65199767f8SToomas Soome 	long	quot;
66199767f8SToomas Soome 	long	rem;
67199767f8SToomas Soome } ldiv_t;
68199767f8SToomas Soome 
69199767f8SToomas Soome #define	EXIT_FAILURE	1
70199767f8SToomas Soome #define	EXIT_SUCCESS	0
71199767f8SToomas Soome 
72199767f8SToomas Soome #define	RAND_MAX	0x7ffffffd
73199767f8SToomas Soome 
74199767f8SToomas Soome __BEGIN_DECLS
75199767f8SToomas Soome #ifdef _XLOCALE_H_
76199767f8SToomas Soome #include <xlocale/_stdlib.h>
77199767f8SToomas Soome #endif
78199767f8SToomas Soome extern int __mb_cur_max;
79199767f8SToomas Soome extern int ___mb_cur_max(void);
80199767f8SToomas Soome #define	MB_CUR_MAX	(___mb_cur_max())
81199767f8SToomas Soome 
82199767f8SToomas Soome _Noreturn void	 abort(void);
83199767f8SToomas Soome int	 abs(int) __pure2;
84199767f8SToomas Soome int	 atexit(void (*)(void));
85199767f8SToomas Soome double	 atof(const char *);
86199767f8SToomas Soome int	 atoi(const char *);
87199767f8SToomas Soome long	 atol(const char *);
88199767f8SToomas Soome void	*bsearch(const void *, const void *, size_t,
89199767f8SToomas Soome 	    size_t, int (*)(const void *, const void *));
90199767f8SToomas Soome void	*calloc(size_t, size_t) __malloc_like __result_use_check
91*59c22e7aSToomas Soome 	     __alloc_size2(1, 2);
92199767f8SToomas Soome div_t	 div(int, int) __pure2;
93199767f8SToomas Soome _Noreturn void	 exit(int);
94199767f8SToomas Soome void	 free(void *);
95199767f8SToomas Soome char	*getenv(const char *);
96199767f8SToomas Soome long	 labs(long) __pure2;
97199767f8SToomas Soome ldiv_t	 ldiv(long, long) __pure2;
98199767f8SToomas Soome void	*malloc(size_t) __malloc_like __result_use_check __alloc_size(1);
99199767f8SToomas Soome int	 mblen(const char *, size_t);
100199767f8SToomas Soome size_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
101199767f8SToomas Soome int	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
102199767f8SToomas Soome void	 qsort(void *, size_t, size_t,
103199767f8SToomas Soome 	    int (*)(const void *, const void *));
104199767f8SToomas Soome int	 rand(void);
105199767f8SToomas Soome void	*realloc(void *, size_t) __result_use_check __alloc_size(2);
106199767f8SToomas Soome void	 srand(unsigned);
107199767f8SToomas Soome double	 strtod(const char * __restrict, char ** __restrict);
108199767f8SToomas Soome float	 strtof(const char * __restrict, char ** __restrict);
109199767f8SToomas Soome long	 strtol(const char * __restrict, char ** __restrict, int);
110199767f8SToomas Soome long double
111199767f8SToomas Soome 	 strtold(const char * __restrict, char ** __restrict);
112199767f8SToomas Soome unsigned long
113199767f8SToomas Soome 	 strtoul(const char * __restrict, char ** __restrict, int);
114199767f8SToomas Soome int	 system(const char *);
115199767f8SToomas Soome int	 wctomb(char *, wchar_t);
116199767f8SToomas Soome size_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
117199767f8SToomas Soome 
118199767f8SToomas Soome /*
119199767f8SToomas Soome  * Functions added in C99 which we make conditionally available in the
120199767f8SToomas Soome  * BSD^C89 namespace if the compiler supports `long long'.
121199767f8SToomas Soome  * The #if test is more complicated than it ought to be because
122199767f8SToomas Soome  * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
123199767f8SToomas Soome  * is not supported in the compilation environment (which therefore means
124199767f8SToomas Soome  * that it can't really be ISO C99).
125199767f8SToomas Soome  *
126199767f8SToomas Soome  * (The only other extension made by C99 in thie header is _Exit().)
127199767f8SToomas Soome  */
128199767f8SToomas Soome #if __ISO_C_VISIBLE >= 1999
129199767f8SToomas Soome #ifdef __LONG_LONG_SUPPORTED
130199767f8SToomas Soome /* LONGLONG */
131199767f8SToomas Soome typedef struct {
132199767f8SToomas Soome 	long long quot;
133199767f8SToomas Soome 	long long rem;
134199767f8SToomas Soome } lldiv_t;
135199767f8SToomas Soome 
136199767f8SToomas Soome /* LONGLONG */
137199767f8SToomas Soome long long
138199767f8SToomas Soome 	 atoll(const char *);
139199767f8SToomas Soome /* LONGLONG */
140199767f8SToomas Soome long long
141199767f8SToomas Soome 	 llabs(long long) __pure2;
142199767f8SToomas Soome /* LONGLONG */
143199767f8SToomas Soome lldiv_t	 lldiv(long long, long long) __pure2;
144199767f8SToomas Soome /* LONGLONG */
145199767f8SToomas Soome long long
146199767f8SToomas Soome 	 strtoll(const char * __restrict, char ** __restrict, int);
147199767f8SToomas Soome /* LONGLONG */
148199767f8SToomas Soome unsigned long long
149199767f8SToomas Soome 	 strtoull(const char * __restrict, char ** __restrict, int);
150199767f8SToomas Soome #endif /* __LONG_LONG_SUPPORTED */
151199767f8SToomas Soome 
152199767f8SToomas Soome _Noreturn void	 _Exit(int);
153199767f8SToomas Soome #endif /* __ISO_C_VISIBLE >= 1999 */
154199767f8SToomas Soome 
155199767f8SToomas Soome /*
156199767f8SToomas Soome  * If we're in a mode greater than C99, expose C11 functions.
157199767f8SToomas Soome  */
158199767f8SToomas Soome #if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L
159199767f8SToomas Soome void *	aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
160199767f8SToomas Soome 	    __alloc_size(2);
161199767f8SToomas Soome int	at_quick_exit(void (*)(void));
162199767f8SToomas Soome _Noreturn void
163199767f8SToomas Soome 	quick_exit(int);
164199767f8SToomas Soome #endif /* __ISO_C_VISIBLE >= 2011 */
165199767f8SToomas Soome /*
166199767f8SToomas Soome  * Extensions made by POSIX relative to C.
167199767f8SToomas Soome  */
168199767f8SToomas Soome #if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE
169199767f8SToomas Soome char	*realpath(const char * __restrict, char * __restrict);
170199767f8SToomas Soome #endif
171199767f8SToomas Soome #if __POSIX_VISIBLE >= 199506
172199767f8SToomas Soome int	 rand_r(unsigned *);			/* (TSF) */
173199767f8SToomas Soome #endif
174199767f8SToomas Soome #if __POSIX_VISIBLE >= 200112
1750639c0edSToomas Soome int	 posix_memalign(void **, size_t, size_t);  /* (ADV) */
176199767f8SToomas Soome int	 setenv(const char *, const char *, int);
177199767f8SToomas Soome int	 unsetenv(const char *);
178199767f8SToomas Soome #endif
179199767f8SToomas Soome 
180199767f8SToomas Soome #if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
181199767f8SToomas Soome int	 getsubopt(char **, char *const *, char **);
182199767f8SToomas Soome #ifndef _MKDTEMP_DECLARED
183199767f8SToomas Soome char	*mkdtemp(char *);
184199767f8SToomas Soome #define	_MKDTEMP_DECLARED
185199767f8SToomas Soome #endif
186199767f8SToomas Soome #ifndef _MKSTEMP_DECLARED
187199767f8SToomas Soome int	 mkstemp(char *);
188199767f8SToomas Soome #define	_MKSTEMP_DECLARED
189199767f8SToomas Soome #endif
190199767f8SToomas Soome #endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
191199767f8SToomas Soome 
192199767f8SToomas Soome /*
193199767f8SToomas Soome  * The only changes to the XSI namespace in revision 6 were the deletion
194199767f8SToomas Soome  * of the ttyslot() and valloc() functions, which FreeBSD never declared
195199767f8SToomas Soome  * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
196199767f8SToomas Soome  * FreeBSD also does not have, and mktemp(), are to be deleted.
197199767f8SToomas Soome  */
198199767f8SToomas Soome #if __XSI_VISIBLE
199199767f8SToomas Soome /* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
200199767f8SToomas Soome long	 a64l(const char *);
201199767f8SToomas Soome double	 drand48(void);
202199767f8SToomas Soome /* char	*ecvt(double, int, int * __restrict, int * __restrict); */
203199767f8SToomas Soome double	 erand48(unsigned short[3]);
204199767f8SToomas Soome /* char	*fcvt(double, int, int * __restrict, int * __restrict); */
205199767f8SToomas Soome /* char	*gcvt(double, int, int * __restrict, int * __restrict); */
206199767f8SToomas Soome int	 grantpt(int);
207199767f8SToomas Soome char	*initstate(unsigned long /* XSI requires u_int */, char *, long);
208199767f8SToomas Soome long	 jrand48(unsigned short[3]);
209199767f8SToomas Soome char	*l64a(long);
210199767f8SToomas Soome void	 lcong48(unsigned short[7]);
211199767f8SToomas Soome long	 lrand48(void);
212199767f8SToomas Soome #if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600)
213199767f8SToomas Soome char	*mktemp(char *);
214199767f8SToomas Soome #define	_MKTEMP_DECLARED
215199767f8SToomas Soome #endif
216199767f8SToomas Soome long	 mrand48(void);
217199767f8SToomas Soome long	 nrand48(unsigned short[3]);
218199767f8SToomas Soome int	 posix_openpt(int);
219199767f8SToomas Soome char	*ptsname(int);
220199767f8SToomas Soome int	 putenv(char *);
221199767f8SToomas Soome long	 random(void);
222199767f8SToomas Soome unsigned short
223199767f8SToomas Soome 	*seed48(unsigned short[3]);
224199767f8SToomas Soome #ifndef _SETKEY_DECLARED
225199767f8SToomas Soome int	 setkey(const char *);
226199767f8SToomas Soome #define	_SETKEY_DECLARED
227199767f8SToomas Soome #endif
228199767f8SToomas Soome char	*setstate(/* const */ char *);
229199767f8SToomas Soome void	 srand48(long);
230199767f8SToomas Soome void	 srandom(unsigned long);
231199767f8SToomas Soome int	 unlockpt(int);
232199767f8SToomas Soome #endif /* __XSI_VISIBLE */
233199767f8SToomas Soome 
234199767f8SToomas Soome #if __BSD_VISIBLE
235199767f8SToomas Soome extern const char *malloc_conf;
236199767f8SToomas Soome extern void (*malloc_message)(void *, const char *);
237199767f8SToomas Soome 
238199767f8SToomas Soome /*
239199767f8SToomas Soome  * The alloca() function can't be implemented in C, and on some
240199767f8SToomas Soome  * platforms it can't be implemented at all as a callable function.
241199767f8SToomas Soome  * The GNU C compiler provides a built-in alloca() which we can use;
242199767f8SToomas Soome  * in all other cases, provide a prototype, mainly to pacify various
243199767f8SToomas Soome  * incarnations of lint.  On platforms where alloca() is not in libc,
244199767f8SToomas Soome  * programs which use it will fail to link when compiled with non-GNU
245199767f8SToomas Soome  * compilers.
246199767f8SToomas Soome  */
247199767f8SToomas Soome #if __GNUC__ >= 2 || defined(__INTEL_COMPILER)
248199767f8SToomas Soome #undef  alloca	/* some GNU bits try to get cute and define this on their own */
249199767f8SToomas Soome #define alloca(sz) __builtin_alloca(sz)
250199767f8SToomas Soome #elif defined(lint)
251199767f8SToomas Soome void	*alloca(size_t);
252199767f8SToomas Soome #endif
253199767f8SToomas Soome 
254199767f8SToomas Soome void	 abort2(const char *, int, void **) __dead2;
255199767f8SToomas Soome __uint32_t
256199767f8SToomas Soome 	 arc4random(void);
257199767f8SToomas Soome void	 arc4random_addrandom(unsigned char *, int);
258199767f8SToomas Soome void	 arc4random_buf(void *, size_t);
259199767f8SToomas Soome void	 arc4random_stir(void);
260*59c22e7aSToomas Soome __uint32_t
261199767f8SToomas Soome 	 arc4random_uniform(__uint32_t);
262199767f8SToomas Soome #ifdef __BLOCKS__
263199767f8SToomas Soome int	 atexit_b(void (^)(void));
264199767f8SToomas Soome void	*bsearch_b(const void *, const void *, size_t,
265199767f8SToomas Soome 	    size_t, int (^)(const void *, const void *));
266199767f8SToomas Soome #endif
267199767f8SToomas Soome char	*getbsize(int *, long *);
268199767f8SToomas Soome 					/* getcap(3) functions */
269199767f8SToomas Soome char	*cgetcap(char *, const char *, int);
270199767f8SToomas Soome int	 cgetclose(void);
271199767f8SToomas Soome int	 cgetent(char **, char **, const char *);
272199767f8SToomas Soome int	 cgetfirst(char **, char **);
273199767f8SToomas Soome int	 cgetmatch(const char *, const char *);
274199767f8SToomas Soome int	 cgetnext(char **, char **);
275199767f8SToomas Soome int	 cgetnum(char *, const char *, long *);
276199767f8SToomas Soome int	 cgetset(const char *);
277199767f8SToomas Soome int	 cgetstr(char *, const char *, char **);
278199767f8SToomas Soome int	 cgetustr(char *, const char *, char **);
279199767f8SToomas Soome 
280199767f8SToomas Soome int	 daemon(int, int);
281199767f8SToomas Soome char	*devname(__dev_t, __mode_t);
282199767f8SToomas Soome char	*devname_r(__dev_t, __mode_t, char *, int);
283199767f8SToomas Soome char	*fdevname(int);
284199767f8SToomas Soome char	*fdevname_r(int, char *, int);
285199767f8SToomas Soome int	 getloadavg(double [], int);
286199767f8SToomas Soome const char *
287199767f8SToomas Soome 	 getprogname(void);
288199767f8SToomas Soome 
289199767f8SToomas Soome int	 heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
290199767f8SToomas Soome #ifdef __BLOCKS__
291199767f8SToomas Soome int	 heapsort_b(void *, size_t, size_t, int (^)(const void *, const void *));
292199767f8SToomas Soome void	 qsort_b(void *, size_t, size_t,
293199767f8SToomas Soome 	    int (^)(const void *, const void *));
294199767f8SToomas Soome #endif
295199767f8SToomas Soome int	 l64a_r(long, char *, int);
296199767f8SToomas Soome int	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
297199767f8SToomas Soome #ifdef __BLOCKS__
298199767f8SToomas Soome int	 mergesort_b(void *, size_t, size_t, int (^)(const void *, const void *));
299199767f8SToomas Soome #endif
300199767f8SToomas Soome int	 mkostemp(char *, int);
301199767f8SToomas Soome int	 mkostemps(char *, int, int);
302199767f8SToomas Soome void	 qsort_r(void *, size_t, size_t, void *,
303199767f8SToomas Soome 	    int (*)(void *, const void *, const void *));
304199767f8SToomas Soome int	 radixsort(const unsigned char **, int, const unsigned char *,
305199767f8SToomas Soome 	    unsigned);
306*59c22e7aSToomas Soome void	*reallocarray(void *, size_t, size_t) __result_use_check
307*59c22e7aSToomas Soome 	    __alloc_size2(2, 3);
308199767f8SToomas Soome void	*reallocf(void *, size_t) __alloc_size(2);
309199767f8SToomas Soome int	 rpmatch(const char *);
310199767f8SToomas Soome void	 setprogname(const char *);
311199767f8SToomas Soome int	 sradixsort(const unsigned char **, int, const unsigned char *,
312199767f8SToomas Soome 	    unsigned);
313199767f8SToomas Soome void	 sranddev(void);
314199767f8SToomas Soome void	 srandomdev(void);
315199767f8SToomas Soome long long
316199767f8SToomas Soome 	strtonum(const char *, long long, long long, const char **);
317199767f8SToomas Soome 
318199767f8SToomas Soome /* Deprecated interfaces, to be removed in FreeBSD 6.0. */
319199767f8SToomas Soome __int64_t
320199767f8SToomas Soome 	 strtoq(const char *, char **, int);
321199767f8SToomas Soome __uint64_t
322199767f8SToomas Soome 	 strtouq(const char *, char **, int);
323199767f8SToomas Soome 
324199767f8SToomas Soome extern char *suboptarg;			/* getsubopt(3) external variable */
325199767f8SToomas Soome #endif /* __BSD_VISIBLE */
326199767f8SToomas Soome __END_DECLS
327199767f8SToomas Soome 
328199767f8SToomas Soome #endif /* !_STDLIB_H_ */
329