stdlib.h revision 189349
11539Srgrimes/*-
21539Srgrimes * Copyright (c) 1990, 1993
31539Srgrimes *	The Regents of the University of California.  All rights reserved.
41539Srgrimes *
51539Srgrimes * Redistribution and use in source and binary forms, with or without
61539Srgrimes * modification, are permitted provided that the following conditions
71539Srgrimes * are met:
81539Srgrimes * 1. Redistributions of source code must retain the above copyright
91539Srgrimes *    notice, this list of conditions and the following disclaimer.
101539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111539Srgrimes *    notice, this list of conditions and the following disclaimer in the
121539Srgrimes *    documentation and/or other materials provided with the distribution.
131539Srgrimes * 3. All advertising materials mentioning features or use of this software
141539Srgrimes *    must display the following acknowledgement:
151539Srgrimes *	This product includes software developed by the University of
161539Srgrimes *	California, Berkeley and its contributors.
171539Srgrimes * 4. Neither the name of the University nor the names of its contributors
181539Srgrimes *    may be used to endorse or promote products derived from this software
191539Srgrimes *    without specific prior written permission.
201539Srgrimes *
211539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241539Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311539Srgrimes * SUCH DAMAGE.
321539Srgrimes *
3323657Speter *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
3455031Sbde * $FreeBSD: head/include/stdlib.h 189349 2009-03-04 03:31:10Z das $
351539Srgrimes */
361539Srgrimes
371539Srgrimes#ifndef _STDLIB_H_
387865Sbde#define	_STDLIB_H_
391539Srgrimes
4033861Sbde#include <sys/cdefs.h>
41123257Smarcel#include <sys/_null.h>
42102227Smike#include <sys/_types.h>
4333861Sbde
44103728Swollman#if __BSD_VISIBLE
45102227Smike#ifndef _RUNE_T_DECLARED
46102227Smiketypedef	__rune_t	rune_t;
47102227Smike#define	_RUNE_T_DECLARED
4815483Sbde#endif
4915483Sbde#endif
5015483Sbde
51102227Smike#ifndef _SIZE_T_DECLARED
52102227Smiketypedef	__size_t	size_t;
53102227Smike#define	_SIZE_T_DECLARED
541539Srgrimes#endif
551539Srgrimes
5699640Sobrien#ifndef	__cplusplus
57102227Smike#ifndef _WCHAR_T_DECLARED
58102227Smiketypedef	__wchar_t	wchar_t;
59102227Smike#define	_WCHAR_T_DECLARED
601539Srgrimes#endif
6199640Sobrien#endif
621539Srgrimes
631539Srgrimestypedef struct {
64103766Sbde	int	quot;		/* quotient */
65103766Sbde	int	rem;		/* remainder */
661539Srgrimes} div_t;
671539Srgrimes
681539Srgrimestypedef struct {
69103766Sbde	long	quot;
70103766Sbde	long	rem;
711539Srgrimes} ldiv_t;
721539Srgrimes
731539Srgrimes#define	EXIT_FAILURE	1
741539Srgrimes#define	EXIT_SUCCESS	0
751539Srgrimes
761539Srgrimes#define	RAND_MAX	0x7fffffff
771539Srgrimes
781539Srgrimesextern int __mb_cur_max;
791539Srgrimes#define	MB_CUR_MAX	__mb_cur_max
801539Srgrimes
811539Srgrimes__BEGIN_DECLS
8293032Simpvoid	 abort(void) __dead2;
83153684Sphkvoid	 abort2(const char *, int, void **) __dead2;
8493032Simpint	 abs(int) __pure2;
8593032Simpint	 atexit(void (*)(void));
8693032Simpdouble	 atof(const char *);
8793032Simpint	 atoi(const char *);
8893032Simplong	 atol(const char *);
8993032Simpvoid	*bsearch(const void *, const void *, size_t,
9093032Simp	    size_t, int (*)(const void *, const void *));
91187961Sdasvoid	*calloc(size_t, size_t) __malloc_like;
9293032Simpdiv_t	 div(int, int) __pure2;
9393032Simpvoid	 exit(int) __dead2;
9493032Simpvoid	 free(void *);
9593032Simpchar	*getenv(const char *);
9693032Simplong	 labs(long) __pure2;
9793032Simpldiv_t	 ldiv(long, long) __pure2;
98187961Sdasvoid	*malloc(size_t) __malloc_like;
99103728Swollmanint	 mblen(const char *, size_t);
100103766Sbdesize_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
101103728Swollmanint	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
10293032Simpvoid	 qsort(void *, size_t, size_t,
10393032Simp	    int (*)(const void *, const void *));
10493032Simpint	 rand(void);
10593032Simpvoid	*realloc(void *, size_t);
10693032Simpvoid	 srand(unsigned);
107103766Sbdedouble	 strtod(const char * __restrict, char ** __restrict);
108112163Sdasfloat	 strtof(const char * __restrict, char ** __restrict);
109103766Sbdelong	 strtol(const char * __restrict, char ** __restrict, int);
110112163Sdaslong double
111112163Sdas	 strtold(const char * __restrict, char ** __restrict);
1121539Srgrimesunsigned long
113103012Stjr	 strtoul(const char * __restrict, char ** __restrict, int);
11493032Simpint	 system(const char *);
11593032Simpint	 wctomb(char *, wchar_t);
116103012Stjrsize_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
1171539Srgrimes
118103728Swollman/*
119103728Swollman * Functions added in C99 which we make conditionally available in the
120103728Swollman * BSD^C89 namespace if the compiler supports `long long'.
121103728Swollman * The #if test is more complicated than it ought to be because
122103728Swollman * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
123103728Swollman * is not supported in the compilation environment (which therefore means
124103728Swollman * that it can't really be ISO C99).
125103728Swollman *
126103728Swollman * (The only other extension made by C99 in thie header is _Exit().)
127103728Swollman */
128103728Swollman#if __ISO_C_VISIBLE >= 1999
129103728Swollman#ifdef __LONG_LONG_SUPPORTED
130103728Swollman/* LONGLONG */
131103728Swollmantypedef struct {
132103728Swollman	long long quot;
133103728Swollman	long long rem;
134103728Swollman} lldiv_t;
13569201Sphk
136103728Swollman/* LONGLONG */
137103728Swollmanlong long
138103728Swollman	 atoll(const char *);
139103728Swollman/* LONGLONG */
140103728Swollmanlong long
141103728Swollman	 llabs(long long) __pure2;
142103728Swollman/* LONGLONG */
143103728Swollmanlldiv_t	 lldiv(long long, long long) __pure2;
144103728Swollman/* LONGLONG */
145103766Sbdelong long
146103766Sbde	 strtoll(const char * __restrict, char ** __restrict, int);
147103728Swollman/* LONGLONG */
148103728Swollmanunsigned long long
149103766Sbde	 strtoull(const char * __restrict, char ** __restrict, int);
150103728Swollman#endif /* __LONG_LONG_SUPPORTED */
151103728Swollman
152103766Sbdevoid	 _Exit(int) __dead2;
153103728Swollman#endif /* __ISO_C_VISIBLE >= 1999 */
154103728Swollman
155103728Swollman/*
156103728Swollman * Extensions made by POSIX relative to C.  We don't know yet which edition
157103728Swollman * of POSIX made these extensions, so assume they've always been there until
158103728Swollman * research can be done.
159103728Swollman */
160103728Swollman#if __POSIX_VISIBLE /* >= ??? */
161154250Sjasoneint	 posix_memalign(void **, size_t, size_t); /* (ADV) */
162103728Swollmanint	 rand_r(unsigned *);			/* (TSF) */
16393032Simpint	 setenv(const char *, const char *, int);
164171195Sscfint	 unsetenv(const char *);
165103728Swollman#endif
1661539Srgrimes
167189349Sdas#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
168189349Sdasint	 getsubopt(char **, char *const *, char **);
169189349Sdas#ifndef _MKDTEMP_DECLARED
170189349Sdaschar	*mkdtemp(char *);
171189349Sdas#define	_MKDTEMP_DECLARED
172189349Sdas#endif
173189349Sdas#ifndef _MKSTEMP_DECLARED
174189349Sdasint	 mkstemp(char *);
175189349Sdas#define	_MKSTEMP_DECLARED
176189349Sdas#endif
177189349Sdas#endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
178189349Sdas
179103728Swollman/*
180103728Swollman * The only changes to the XSI namespace in revision 6 were the deletion
181103728Swollman * of the ttyslot() and valloc() functions, which FreeBSD never declared
182103728Swollman * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
183103728Swollman * FreeBSD also does not have, and mktemp(), are to be deleted.
184103728Swollman */
185103728Swollman#if __XSI_VISIBLE
186103728Swollman/* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
187153707Strhodeslong	 a64l(const char *);
18893032Simpdouble	 drand48(void);
189103766Sbde/* char	*ecvt(double, int, int * __restrict, int * __restrict); */
19093032Simpdouble	 erand48(unsigned short[3]);
191103766Sbde/* char	*fcvt(double, int, int * __restrict, int * __restrict); */
192103766Sbde/* char	*gcvt(double, int, int * __restrict, int * __restrict); */
193108574Sjmallettint	 grantpt(int);
194103728Swollmanchar	*initstate(unsigned long /* XSI requires u_int */, char *, long);
19593032Simplong	 jrand48(unsigned short[3]);
196153707Strhodeschar	*l64a(long);
19793032Simpvoid	 lcong48(unsigned short[7]);
19893032Simplong	 lrand48(void);
199189349Sdas#if !defined(_MKTEMP_DECLARED) && __XSI_VISIBLE <= 600
200103728Swollmanchar	*mktemp(char *);
201103766Sbde#define	_MKTEMP_DECLARED
202103728Swollman#endif
20393032Simplong	 mrand48(void);
20493032Simplong	 nrand48(unsigned short[3]);
205108574Sjmallettint	 posix_openpt(int);
206108574Sjmallettchar	*ptsname(int);
207171195Sscfint	 putenv(char *);
208103728Swollmanlong	 random(void);
209103728Swollmanchar	*realpath(const char *, char resolved_path[]);
2107865Sbdeunsigned short
21193032Simp	*seed48(unsigned short[3]);
212103728Swollman#ifndef _SETKEY_DECLARED
213103728Swollmanint	 setkey(const char *);
214103728Swollman#define	_SETKEY_DECLARED
215103728Swollman#endif
216103728Swollmanchar	*setstate(/* const */ char *);
21793032Simpvoid	 srand48(long);
218103728Swollmanvoid	 srandom(unsigned long);
219108574Sjmallettint	 unlockpt(int);
220103728Swollman#endif /* __XSI_VISIBLE */
2214749Sats
222103728Swollman#if __BSD_VISIBLE
223103728Swollmanextern const char *_malloc_options;
224103766Sbdeextern void (*_malloc_message)(const char *, const char *, const char *,
225103766Sbde	    const char *);
226103728Swollman
227116397Sdes/*
228116397Sdes * The alloca() function can't be implemented in C, and on some
229116397Sdes * platforms it can't be implemented at all as a callable function.
230116397Sdes * The GNU C compiler provides a built-in alloca() which we can use;
231116397Sdes * in all other cases, provide a prototype, mainly to pacify various
232116397Sdes * incarnations of lint.  On platforms where alloca() is not in libc,
233116397Sdes * programs which use it will fail to link when compiled with non-GNU
234116397Sdes * compilers.
235116397Sdes */
236116831Sobrien#if __GNUC__ >= 2 || defined(__INTEL_COMPILER)
237116831Sobrien#undef  alloca	/* some GNU bits try to get cute and define this on their own */
238116397Sdes#define alloca(sz) __builtin_alloca(sz)
239116831Sobrien#elif defined(lint)
240116831Sobrienvoid	*alloca(size_t);
241116397Sdes#endif
242116397Sdes
24341927Sdt__uint32_t
24493032Simp	 arc4random(void);
245180658Sachevoid	 arc4random_addrandom(unsigned char *, int);
246180658Sachevoid	 arc4random_buf(void *, size_t);
24793032Simpvoid	 arc4random_stir(void);
248180689Sache__uint32_t
249180689Sache	 arc4random_uniform(__uint32_t);
250108445Sobrienchar	*getbsize(int *, long *);
251103728Swollman					/* getcap(3) functions */
25293032Simpchar	*cgetcap(char *, const char *, int);
25393032Simpint	 cgetclose(void);
25493032Simpint	 cgetent(char **, char **, const char *);
25593032Simpint	 cgetfirst(char **, char **);
25693032Simpint	 cgetmatch(const char *, const char *);
25793032Simpint	 cgetnext(char **, char **);
25893032Simpint	 cgetnum(char *, const char *, long *);
25993032Simpint	 cgetset(const char *);
26093032Simpint	 cgetstr(char *, const char *, char **);
26193032Simpint	 cgetustr(char *, const char *, char **);
2621539Srgrimes
26393032Simpint	 daemon(int, int);
264150052Sstefanfchar	*devname(__dev_t, __mode_t);
265150052Sstefanfchar 	*devname_r(__dev_t, __mode_t, char *, int);
266188497Sedchar	*fdevname(int);
267188497Sedchar 	*fdevname_r(int, char *, int);
26893032Simpint	 getloadavg(double [], int);
26988399Smike__const char *
27093032Simp	 getprogname(void);
2711539Srgrimes
27293032Simpint	 heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
273153707Strhodesint	 l64a_r(long, char *, int);
27493032Simpint	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
275103164Swollmanvoid	 qsort_r(void *, size_t, size_t, void *,
276103164Swollman	    int (*)(void *, const void *, const void *));
27793032Simpint	 radixsort(const unsigned char **, int, const unsigned char *,
27893032Simp	    unsigned);
27993032Simpvoid    *reallocf(void *, size_t);
280139922Stjrint	 rpmatch(const char *);
28193032Simpvoid	 setprogname(const char *);
282103164Swollmanint	 sradixsort(const unsigned char **, int, const unsigned char *,
283103164Swollman	    unsigned);
28493032Simpvoid	 sranddev(void);
28593032Simpvoid	 srandomdev(void);
286156707Sandrelong long
287156707Sandre	strtonum(const char *, long long, long long, const char **);
288103728Swollman
289103728Swollman/* Deprecated interfaces, to be removed in FreeBSD 6.0. */
290103766Sbde__int64_t
291103766Sbde	 strtoq(const char *, char **, int);
29241927Sdt__uint64_t
29393032Simp	 strtouq(const char *, char **, int);
294126136Sache
295126136Sacheextern char *suboptarg;			/* getsubopt(3) external variable */
296103728Swollman#endif /* __BSD_VISIBLE */
2971539Srgrimes__END_DECLS
2981539Srgrimes
2997865Sbde#endif /* !_STDLIB_H_ */
300