stdlib.h revision 116831
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 116831 2003-06-25 18:11:32Z obrien $
351539Srgrimes */
361539Srgrimes
371539Srgrimes#ifndef _STDLIB_H_
387865Sbde#define	_STDLIB_H_
391539Srgrimes
4033861Sbde#include <sys/cdefs.h>
41102227Smike#include <sys/_types.h>
4233861Sbde
43103728Swollman#if __BSD_VISIBLE
44102227Smike#ifndef _RUNE_T_DECLARED
45102227Smiketypedef	__rune_t	rune_t;
46102227Smike#define	_RUNE_T_DECLARED
4715483Sbde#endif
4815483Sbde#endif
4915483Sbde
50102227Smike#ifndef _SIZE_T_DECLARED
51102227Smiketypedef	__size_t	size_t;
52102227Smike#define	_SIZE_T_DECLARED
531539Srgrimes#endif
541539Srgrimes
5599640Sobrien#ifndef	__cplusplus
56102227Smike#ifndef _WCHAR_T_DECLARED
57102227Smiketypedef	__wchar_t	wchar_t;
58102227Smike#define	_WCHAR_T_DECLARED
591539Srgrimes#endif
6099640Sobrien#endif
611539Srgrimes
621539Srgrimestypedef struct {
63103766Sbde	int	quot;		/* quotient */
64103766Sbde	int	rem;		/* remainder */
651539Srgrimes} div_t;
661539Srgrimes
671539Srgrimestypedef struct {
68103766Sbde	long	quot;
69103766Sbde	long	rem;
701539Srgrimes} ldiv_t;
711539Srgrimes
721539Srgrimes#ifndef NULL
731539Srgrimes#define	NULL	0
741539Srgrimes#endif
751539Srgrimes
761539Srgrimes#define	EXIT_FAILURE	1
771539Srgrimes#define	EXIT_SUCCESS	0
781539Srgrimes
791539Srgrimes#define	RAND_MAX	0x7fffffff
801539Srgrimes
811539Srgrimesextern int __mb_cur_max;
821539Srgrimes#define	MB_CUR_MAX	__mb_cur_max
831539Srgrimes
841539Srgrimes__BEGIN_DECLS
8593032Simpvoid	 abort(void) __dead2;
8693032Simpint	 abs(int) __pure2;
8793032Simpint	 atexit(void (*)(void));
8893032Simpdouble	 atof(const char *);
8993032Simpint	 atoi(const char *);
9093032Simplong	 atol(const char *);
9193032Simpvoid	*bsearch(const void *, const void *, size_t,
9293032Simp	    size_t, int (*)(const void *, const void *));
9393032Simpvoid	*calloc(size_t, size_t);
9493032Simpdiv_t	 div(int, int) __pure2;
9593032Simpvoid	 exit(int) __dead2;
9693032Simpvoid	 free(void *);
9793032Simpchar	*getenv(const char *);
9893032Simplong	 labs(long) __pure2;
9993032Simpldiv_t	 ldiv(long, long) __pure2;
10093032Simpvoid	*malloc(size_t);
101103728Swollmanint	 mblen(const char *, size_t);
102103766Sbdesize_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
103103728Swollmanint	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
10493032Simpvoid	 qsort(void *, size_t, size_t,
10593032Simp	    int (*)(const void *, const void *));
10693032Simpint	 rand(void);
10793032Simpvoid	*realloc(void *, size_t);
10893032Simpvoid	 srand(unsigned);
109103766Sbdedouble	 strtod(const char * __restrict, char ** __restrict);
110112163Sdasfloat	 strtof(const char * __restrict, char ** __restrict);
111103766Sbdelong	 strtol(const char * __restrict, char ** __restrict, int);
112112163Sdaslong double
113112163Sdas	 strtold(const char * __restrict, char ** __restrict);
1141539Srgrimesunsigned long
115103012Stjr	 strtoul(const char * __restrict, char ** __restrict, int);
11693032Simpint	 system(const char *);
11793032Simpint	 wctomb(char *, wchar_t);
118103012Stjrsize_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
1191539Srgrimes
120103728Swollman/*
121103728Swollman * Functions added in C99 which we make conditionally available in the
122103728Swollman * BSD^C89 namespace if the compiler supports `long long'.
123103728Swollman * The #if test is more complicated than it ought to be because
124103728Swollman * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
125103728Swollman * is not supported in the compilation environment (which therefore means
126103728Swollman * that it can't really be ISO C99).
127103728Swollman *
128103728Swollman * (The only other extension made by C99 in thie header is _Exit().)
129103728Swollman */
130103728Swollman#if __ISO_C_VISIBLE >= 1999
131103728Swollman#ifdef __LONG_LONG_SUPPORTED
132103728Swollman/* LONGLONG */
133103728Swollmantypedef struct {
134103728Swollman	long long quot;
135103728Swollman	long long rem;
136103728Swollman} lldiv_t;
13769201Sphk
138103728Swollman/* LONGLONG */
139103728Swollmanlong long
140103728Swollman	 atoll(const char *);
141103728Swollman/* LONGLONG */
142103728Swollmanlong long
143103728Swollman	 llabs(long long) __pure2;
144103728Swollman/* LONGLONG */
145103728Swollmanlldiv_t	 lldiv(long long, long long) __pure2;
146103728Swollman/* LONGLONG */
147103766Sbdelong long
148103766Sbde	 strtoll(const char * __restrict, char ** __restrict, int);
149103728Swollman/* LONGLONG */
150103728Swollmanunsigned long long
151103766Sbde	 strtoull(const char * __restrict, char ** __restrict, int);
152103728Swollman#endif /* __LONG_LONG_SUPPORTED */
153103728Swollman
154103766Sbdevoid	 _Exit(int) __dead2;
155103728Swollman#endif /* __ISO_C_VISIBLE >= 1999 */
156103728Swollman
157103728Swollman/*
158103728Swollman * Extensions made by POSIX relative to C.  We don't know yet which edition
159103728Swollman * of POSIX made these extensions, so assume they've always been there until
160103728Swollman * research can be done.
161103728Swollman */
162103728Swollman#if __POSIX_VISIBLE /* >= ??? */
163103728Swollman/* int	 posix_memalign(void **, size_t, size_t); (ADV) */
164103728Swollmanint	 rand_r(unsigned *);			/* (TSF) */
16593032Simpint	 setenv(const char *, const char *, int);
166103728Swollmanvoid	 unsetenv(const char *);
167103728Swollman#endif
1681539Srgrimes
169103728Swollman/*
170103728Swollman * The only changes to the XSI namespace in revision 6 were the deletion
171103728Swollman * of the ttyslot() and valloc() functions, which FreeBSD never declared
172103728Swollman * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
173103728Swollman * FreeBSD also does not have, and mktemp(), are to be deleted.
174103728Swollman */
175103728Swollman#if __XSI_VISIBLE
176103728Swollman/* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
177103728Swollman/* long	 a64l(const char *); */
17893032Simpdouble	 drand48(void);
179103766Sbde/* char	*ecvt(double, int, int * __restrict, int * __restrict); */
18093032Simpdouble	 erand48(unsigned short[3]);
181103766Sbde/* char	*fcvt(double, int, int * __restrict, int * __restrict); */
182103766Sbde/* char	*gcvt(double, int, int * __restrict, int * __restrict); */
183103728Swollman#ifndef _GETSUBOPT_DECLARED
184103728Swollmanint	 getsubopt(char **, char *const *, char **);
185103728Swollman#define	_GETSUBOPT_DECLARED
186103728Swollman#endif
187108574Sjmallettint	 grantpt(int);
188103728Swollmanchar	*initstate(unsigned long /* XSI requires u_int */, char *, long);
18993032Simplong	 jrand48(unsigned short[3]);
190103728Swollman/* char	*l64a(long); */
19193032Simpvoid	 lcong48(unsigned short[7]);
19293032Simplong	 lrand48(void);
193103728Swollman#ifndef _MKSTEMP_DECLARED
194103728Swollmanint	 mkstemp(char *);
195103728Swollman#define	_MKSTEMP_DECLARED
196103728Swollman#endif
197103728Swollman#ifndef _MKTEMP_DECLARED
198103728Swollmanchar	*mktemp(char *);
199103766Sbde#define	_MKTEMP_DECLARED
200103728Swollman#endif
20193032Simplong	 mrand48(void);
20293032Simplong	 nrand48(unsigned short[3]);
203108574Sjmallettint	 posix_openpt(int);
204108574Sjmallettchar	*ptsname(int);
205103728Swollmanint	 putenv(const char *);
206103728Swollmanlong	 random(void);
207103728Swollmanchar	*realpath(const char *, char resolved_path[]);
2087865Sbdeunsigned short
20993032Simp	*seed48(unsigned short[3]);
210103728Swollman#ifndef _SETKEY_DECLARED
211103728Swollmanint	 setkey(const char *);
212103728Swollman#define	_SETKEY_DECLARED
213103728Swollman#endif
214103728Swollmanchar	*setstate(/* const */ char *);
21593032Simpvoid	 srand48(long);
216103728Swollmanvoid	 srandom(unsigned long);
217108574Sjmallettint	 unlockpt(int);
218103728Swollman#endif /* __XSI_VISIBLE */
2194749Sats
220103728Swollman#if __BSD_VISIBLE
221103728Swollmanextern const char *_malloc_options;
222103766Sbdeextern void (*_malloc_message)(const char *, const char *, const char *,
223103766Sbde	    const char *);
224103728Swollman
225116397Sdes/*
226116397Sdes * The alloca() function can't be implemented in C, and on some
227116397Sdes * platforms it can't be implemented at all as a callable function.
228116397Sdes * The GNU C compiler provides a built-in alloca() which we can use;
229116397Sdes * in all other cases, provide a prototype, mainly to pacify various
230116397Sdes * incarnations of lint.  On platforms where alloca() is not in libc,
231116397Sdes * programs which use it will fail to link when compiled with non-GNU
232116397Sdes * compilers.
233116397Sdes */
234116831Sobrien#if __GNUC__ >= 2 || defined(__INTEL_COMPILER)
235116831Sobrien#undef  alloca	/* some GNU bits try to get cute and define this on their own */
236116397Sdes#define alloca(sz) __builtin_alloca(sz)
237116831Sobrien#elif defined(lint)
238116831Sobrienvoid	*alloca(size_t);
239116831Sobrien#error Please add alloca support on all FreeBSD architectures for this compiler.
240116397Sdes#else
241116397Sdes#endif
242116397Sdes
24341927Sdt__uint32_t
24493032Simp	 arc4random(void);
24593032Simpvoid	 arc4random_addrandom(unsigned char *dat, int datlen);
24693032Simpvoid	 arc4random_stir(void);
247108445Sobrienchar	*getbsize(int *, long *);
248103728Swollman					/* getcap(3) functions */
24993032Simpchar	*cgetcap(char *, const char *, int);
25093032Simpint	 cgetclose(void);
25193032Simpint	 cgetent(char **, char **, const char *);
25293032Simpint	 cgetfirst(char **, char **);
25393032Simpint	 cgetmatch(const char *, const char *);
25493032Simpint	 cgetnext(char **, char **);
25593032Simpint	 cgetnum(char *, const char *, long *);
25693032Simpint	 cgetset(const char *);
25793032Simpint	 cgetstr(char *, const char *, char **);
25893032Simpint	 cgetustr(char *, const char *, char **);
2591539Srgrimes
26093032Simpint	 daemon(int, int);
26193032Simpchar	*devname(int, int);
262116680Sdwmalonechar 	*devname_r(int, int, char *, int);
26393032Simpint	 getloadavg(double [], int);
26488399Smike__const char *
26593032Simp	 getprogname(void);
2661539Srgrimes
26793032Simpint	 heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
26893032Simpint	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
269103164Swollmanvoid	 qsort_r(void *, size_t, size_t, void *,
270103164Swollman	    int (*)(void *, const void *, const void *));
27193032Simpint	 radixsort(const unsigned char **, int, const unsigned char *,
27293032Simp	    unsigned);
27393032Simpvoid    *reallocf(void *, size_t);
27493032Simpvoid	 setprogname(const char *);
275103164Swollmanint	 sradixsort(const unsigned char **, int, const unsigned char *,
276103164Swollman	    unsigned);
27793032Simpvoid	 sranddev(void);
27893032Simpvoid	 srandomdev(void);
279103728Swollman
280103728Swollman/* Deprecated interfaces, to be removed in FreeBSD 6.0. */
281103766Sbde__int64_t
282103766Sbde	 strtoq(const char *, char **, int);
28341927Sdt__uint64_t
28493032Simp	 strtouq(const char *, char **, int);
285103728Swollman#endif /* __BSD_VISIBLE */
2861539Srgrimes__END_DECLS
2871539Srgrimes
2887865Sbde#endif /* !_STDLIB_H_ */
289