stdlib.h revision 203964
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.
13203964Simp * 3. Neither the name of the University nor the names of its contributors
141539Srgrimes *    may be used to endorse or promote products derived from this software
151539Srgrimes *    without specific prior written permission.
161539Srgrimes *
171539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201539Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271539Srgrimes * SUCH DAMAGE.
281539Srgrimes *
2923657Speter *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
3055031Sbde * $FreeBSD: head/include/stdlib.h 203964 2010-02-16 19:39:50Z imp $
311539Srgrimes */
321539Srgrimes
331539Srgrimes#ifndef _STDLIB_H_
347865Sbde#define	_STDLIB_H_
351539Srgrimes
3633861Sbde#include <sys/cdefs.h>
37123257Smarcel#include <sys/_null.h>
38102227Smike#include <sys/_types.h>
3933861Sbde
40103728Swollman#if __BSD_VISIBLE
41102227Smike#ifndef _RUNE_T_DECLARED
42102227Smiketypedef	__rune_t	rune_t;
43102227Smike#define	_RUNE_T_DECLARED
4415483Sbde#endif
4515483Sbde#endif
4615483Sbde
47102227Smike#ifndef _SIZE_T_DECLARED
48102227Smiketypedef	__size_t	size_t;
49102227Smike#define	_SIZE_T_DECLARED
501539Srgrimes#endif
511539Srgrimes
5299640Sobrien#ifndef	__cplusplus
53102227Smike#ifndef _WCHAR_T_DECLARED
54102227Smiketypedef	__wchar_t	wchar_t;
55102227Smike#define	_WCHAR_T_DECLARED
561539Srgrimes#endif
5799640Sobrien#endif
581539Srgrimes
591539Srgrimestypedef struct {
60103766Sbde	int	quot;		/* quotient */
61103766Sbde	int	rem;		/* remainder */
621539Srgrimes} div_t;
631539Srgrimes
641539Srgrimestypedef struct {
65103766Sbde	long	quot;
66103766Sbde	long	rem;
671539Srgrimes} ldiv_t;
681539Srgrimes
691539Srgrimes#define	EXIT_FAILURE	1
701539Srgrimes#define	EXIT_SUCCESS	0
711539Srgrimes
721539Srgrimes#define	RAND_MAX	0x7fffffff
731539Srgrimes
741539Srgrimesextern int __mb_cur_max;
751539Srgrimes#define	MB_CUR_MAX	__mb_cur_max
761539Srgrimes
771539Srgrimes__BEGIN_DECLS
7893032Simpvoid	 abort(void) __dead2;
7993032Simpint	 abs(int) __pure2;
8093032Simpint	 atexit(void (*)(void));
8193032Simpdouble	 atof(const char *);
8293032Simpint	 atoi(const char *);
8393032Simplong	 atol(const char *);
8493032Simpvoid	*bsearch(const void *, const void *, size_t,
8593032Simp	    size_t, int (*)(const void *, const void *));
86187961Sdasvoid	*calloc(size_t, size_t) __malloc_like;
8793032Simpdiv_t	 div(int, int) __pure2;
8893032Simpvoid	 exit(int) __dead2;
8993032Simpvoid	 free(void *);
9093032Simpchar	*getenv(const char *);
9193032Simplong	 labs(long) __pure2;
9293032Simpldiv_t	 ldiv(long, long) __pure2;
93187961Sdasvoid	*malloc(size_t) __malloc_like;
94103728Swollmanint	 mblen(const char *, size_t);
95103766Sbdesize_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
96103728Swollmanint	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
9793032Simpvoid	 qsort(void *, size_t, size_t,
9893032Simp	    int (*)(const void *, const void *));
9993032Simpint	 rand(void);
10093032Simpvoid	*realloc(void *, size_t);
10193032Simpvoid	 srand(unsigned);
102103766Sbdedouble	 strtod(const char * __restrict, char ** __restrict);
103112163Sdasfloat	 strtof(const char * __restrict, char ** __restrict);
104103766Sbdelong	 strtol(const char * __restrict, char ** __restrict, int);
105112163Sdaslong double
106112163Sdas	 strtold(const char * __restrict, char ** __restrict);
1071539Srgrimesunsigned long
108103012Stjr	 strtoul(const char * __restrict, char ** __restrict, int);
10993032Simpint	 system(const char *);
11093032Simpint	 wctomb(char *, wchar_t);
111103012Stjrsize_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
1121539Srgrimes
113103728Swollman/*
114103728Swollman * Functions added in C99 which we make conditionally available in the
115103728Swollman * BSD^C89 namespace if the compiler supports `long long'.
116103728Swollman * The #if test is more complicated than it ought to be because
117103728Swollman * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
118103728Swollman * is not supported in the compilation environment (which therefore means
119103728Swollman * that it can't really be ISO C99).
120103728Swollman *
121103728Swollman * (The only other extension made by C99 in thie header is _Exit().)
122103728Swollman */
123103728Swollman#if __ISO_C_VISIBLE >= 1999
124103728Swollman#ifdef __LONG_LONG_SUPPORTED
125103728Swollman/* LONGLONG */
126103728Swollmantypedef struct {
127103728Swollman	long long quot;
128103728Swollman	long long rem;
129103728Swollman} lldiv_t;
13069201Sphk
131103728Swollman/* LONGLONG */
132103728Swollmanlong long
133103728Swollman	 atoll(const char *);
134103728Swollman/* LONGLONG */
135103728Swollmanlong long
136103728Swollman	 llabs(long long) __pure2;
137103728Swollman/* LONGLONG */
138103728Swollmanlldiv_t	 lldiv(long long, long long) __pure2;
139103728Swollman/* LONGLONG */
140103766Sbdelong long
141103766Sbde	 strtoll(const char * __restrict, char ** __restrict, int);
142103728Swollman/* LONGLONG */
143103728Swollmanunsigned long long
144103766Sbde	 strtoull(const char * __restrict, char ** __restrict, int);
145103728Swollman#endif /* __LONG_LONG_SUPPORTED */
146103728Swollman
147103766Sbdevoid	 _Exit(int) __dead2;
148103728Swollman#endif /* __ISO_C_VISIBLE >= 1999 */
149103728Swollman
150103728Swollman/*
151103728Swollman * Extensions made by POSIX relative to C.  We don't know yet which edition
152103728Swollman * of POSIX made these extensions, so assume they've always been there until
153103728Swollman * research can be done.
154103728Swollman */
155103728Swollman#if __POSIX_VISIBLE /* >= ??? */
156154250Sjasoneint	 posix_memalign(void **, size_t, size_t); /* (ADV) */
157103728Swollmanint	 rand_r(unsigned *);			/* (TSF) */
15893032Simpint	 setenv(const char *, const char *, int);
159171195Sscfint	 unsetenv(const char *);
160103728Swollman#endif
1611539Srgrimes
162189349Sdas#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
163189349Sdasint	 getsubopt(char **, char *const *, char **);
164189349Sdas#ifndef _MKDTEMP_DECLARED
165189349Sdaschar	*mkdtemp(char *);
166189349Sdas#define	_MKDTEMP_DECLARED
167189349Sdas#endif
168189349Sdas#ifndef _MKSTEMP_DECLARED
169189349Sdasint	 mkstemp(char *);
170189349Sdas#define	_MKSTEMP_DECLARED
171189349Sdas#endif
172189349Sdas#endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
173189349Sdas
174103728Swollman/*
175103728Swollman * The only changes to the XSI namespace in revision 6 were the deletion
176103728Swollman * of the ttyslot() and valloc() functions, which FreeBSD never declared
177103728Swollman * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
178103728Swollman * FreeBSD also does not have, and mktemp(), are to be deleted.
179103728Swollman */
180103728Swollman#if __XSI_VISIBLE
181103728Swollman/* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
182153707Strhodeslong	 a64l(const char *);
18393032Simpdouble	 drand48(void);
184103766Sbde/* char	*ecvt(double, int, int * __restrict, int * __restrict); */
18593032Simpdouble	 erand48(unsigned short[3]);
186103766Sbde/* char	*fcvt(double, int, int * __restrict, int * __restrict); */
187103766Sbde/* char	*gcvt(double, int, int * __restrict, int * __restrict); */
188108574Sjmallettint	 grantpt(int);
189103728Swollmanchar	*initstate(unsigned long /* XSI requires u_int */, char *, long);
19093032Simplong	 jrand48(unsigned short[3]);
191153707Strhodeschar	*l64a(long);
19293032Simpvoid	 lcong48(unsigned short[7]);
19393032Simplong	 lrand48(void);
194189782Sdas#if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600)
195103728Swollmanchar	*mktemp(char *);
196103766Sbde#define	_MKTEMP_DECLARED
197103728Swollman#endif
19893032Simplong	 mrand48(void);
19993032Simplong	 nrand48(unsigned short[3]);
200108574Sjmallettint	 posix_openpt(int);
201108574Sjmallettchar	*ptsname(int);
202171195Sscfint	 putenv(char *);
203103728Swollmanlong	 random(void);
204103728Swollmanchar	*realpath(const char *, char resolved_path[]);
2057865Sbdeunsigned short
20693032Simp	*seed48(unsigned short[3]);
207103728Swollman#ifndef _SETKEY_DECLARED
208103728Swollmanint	 setkey(const char *);
209103728Swollman#define	_SETKEY_DECLARED
210103728Swollman#endif
211103728Swollmanchar	*setstate(/* const */ char *);
21293032Simpvoid	 srand48(long);
213103728Swollmanvoid	 srandom(unsigned long);
214108574Sjmallettint	 unlockpt(int);
215103728Swollman#endif /* __XSI_VISIBLE */
2164749Sats
217103728Swollman#if __BSD_VISIBLE
218103728Swollmanextern const char *_malloc_options;
219103766Sbdeextern void (*_malloc_message)(const char *, const char *, const char *,
220103766Sbde	    const char *);
221103728Swollman
222116397Sdes/*
223116397Sdes * The alloca() function can't be implemented in C, and on some
224116397Sdes * platforms it can't be implemented at all as a callable function.
225116397Sdes * The GNU C compiler provides a built-in alloca() which we can use;
226116397Sdes * in all other cases, provide a prototype, mainly to pacify various
227116397Sdes * incarnations of lint.  On platforms where alloca() is not in libc,
228116397Sdes * programs which use it will fail to link when compiled with non-GNU
229116397Sdes * compilers.
230116397Sdes */
231116831Sobrien#if __GNUC__ >= 2 || defined(__INTEL_COMPILER)
232116831Sobrien#undef  alloca	/* some GNU bits try to get cute and define this on their own */
233116397Sdes#define alloca(sz) __builtin_alloca(sz)
234116831Sobrien#elif defined(lint)
235116831Sobrienvoid	*alloca(size_t);
236116397Sdes#endif
237116397Sdes
238189820Sdasvoid	 abort2(const char *, int, void **) __dead2;
23941927Sdt__uint32_t
24093032Simp	 arc4random(void);
241180658Sachevoid	 arc4random_addrandom(unsigned char *, int);
242180658Sachevoid	 arc4random_buf(void *, size_t);
24393032Simpvoid	 arc4random_stir(void);
244180689Sache__uint32_t
245180689Sache	 arc4random_uniform(__uint32_t);
246108445Sobrienchar	*getbsize(int *, long *);
247103728Swollman					/* getcap(3) functions */
24893032Simpchar	*cgetcap(char *, const char *, int);
24993032Simpint	 cgetclose(void);
25093032Simpint	 cgetent(char **, char **, const char *);
25193032Simpint	 cgetfirst(char **, char **);
25293032Simpint	 cgetmatch(const char *, const char *);
25393032Simpint	 cgetnext(char **, char **);
25493032Simpint	 cgetnum(char *, const char *, long *);
25593032Simpint	 cgetset(const char *);
25693032Simpint	 cgetstr(char *, const char *, char **);
25793032Simpint	 cgetustr(char *, const char *, char **);
2581539Srgrimes
25993032Simpint	 daemon(int, int);
260150052Sstefanfchar	*devname(__dev_t, __mode_t);
261150052Sstefanfchar 	*devname_r(__dev_t, __mode_t, char *, int);
262188497Sedchar	*fdevname(int);
263188497Sedchar 	*fdevname_r(int, char *, int);
26493032Simpint	 getloadavg(double [], int);
26588399Smike__const char *
26693032Simp	 getprogname(void);
2671539Srgrimes
26893032Simpint	 heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
269153707Strhodesint	 l64a_r(long, char *, int);
27093032Simpint	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
271103164Swollmanvoid	 qsort_r(void *, size_t, size_t, void *,
272103164Swollman	    int (*)(void *, const void *, const void *));
27393032Simpint	 radixsort(const unsigned char **, int, const unsigned char *,
27493032Simp	    unsigned);
27593032Simpvoid    *reallocf(void *, size_t);
276139922Stjrint	 rpmatch(const char *);
27793032Simpvoid	 setprogname(const char *);
278103164Swollmanint	 sradixsort(const unsigned char **, int, const unsigned char *,
279103164Swollman	    unsigned);
28093032Simpvoid	 sranddev(void);
28193032Simpvoid	 srandomdev(void);
282156707Sandrelong long
283156707Sandre	strtonum(const char *, long long, long long, const char **);
284103728Swollman
285103728Swollman/* Deprecated interfaces, to be removed in FreeBSD 6.0. */
286103766Sbde__int64_t
287103766Sbde	 strtoq(const char *, char **, int);
28841927Sdt__uint64_t
28993032Simp	 strtouq(const char *, char **, int);
290126136Sache
291126136Sacheextern char *suboptarg;			/* getsubopt(3) external variable */
292103728Swollman#endif /* __BSD_VISIBLE */
2931539Srgrimes__END_DECLS
2941539Srgrimes
2957865Sbde#endif /* !_STDLIB_H_ */
296