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$
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
72252608Sache#define	RAND_MAX	0x7ffffffd
731539Srgrimes
74227753Stheraven__BEGIN_DECLS
75233600Stheraven#ifdef _XLOCALE_H_
76233600Stheraven#include <xlocale/_stdlib.h>
77233600Stheraven#endif
781539Srgrimesextern int __mb_cur_max;
79227753Stheravenextern int ___mb_cur_max(void);
80227753Stheraven#define	MB_CUR_MAX	(___mb_cur_max())
811539Srgrimes
82228330Stheraven_Noreturn void	 abort(void);
8393032Simpint	 abs(int) __pure2;
8493032Simpint	 atexit(void (*)(void));
8593032Simpdouble	 atof(const char *);
8693032Simpint	 atoi(const char *);
8793032Simplong	 atol(const char *);
8893032Simpvoid	*bsearch(const void *, const void *, size_t,
8993032Simp	    size_t, int (*)(const void *, const void *));
90187961Sdasvoid	*calloc(size_t, size_t) __malloc_like;
9193032Simpdiv_t	 div(int, int) __pure2;
92228330Stheraven_Noreturn void	 exit(int);
9393032Simpvoid	 free(void *);
9493032Simpchar	*getenv(const char *);
9593032Simplong	 labs(long) __pure2;
9693032Simpldiv_t	 ldiv(long, long) __pure2;
97187961Sdasvoid	*malloc(size_t) __malloc_like;
98103728Swollmanint	 mblen(const char *, size_t);
99103766Sbdesize_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
100103728Swollmanint	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
10193032Simpvoid	 qsort(void *, size_t, size_t,
10293032Simp	    int (*)(const void *, const void *));
10393032Simpint	 rand(void);
10493032Simpvoid	*realloc(void *, size_t);
10593032Simpvoid	 srand(unsigned);
106103766Sbdedouble	 strtod(const char * __restrict, char ** __restrict);
107112163Sdasfloat	 strtof(const char * __restrict, char ** __restrict);
108103766Sbdelong	 strtol(const char * __restrict, char ** __restrict, int);
109112163Sdaslong double
110112163Sdas	 strtold(const char * __restrict, char ** __restrict);
1111539Srgrimesunsigned long
112103012Stjr	 strtoul(const char * __restrict, char ** __restrict, int);
11393032Simpint	 system(const char *);
11493032Simpint	 wctomb(char *, wchar_t);
115103012Stjrsize_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
1161539Srgrimes
117103728Swollman/*
118103728Swollman * Functions added in C99 which we make conditionally available in the
119103728Swollman * BSD^C89 namespace if the compiler supports `long long'.
120103728Swollman * The #if test is more complicated than it ought to be because
121103728Swollman * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
122103728Swollman * is not supported in the compilation environment (which therefore means
123103728Swollman * that it can't really be ISO C99).
124103728Swollman *
125103728Swollman * (The only other extension made by C99 in thie header is _Exit().)
126103728Swollman */
127103728Swollman#if __ISO_C_VISIBLE >= 1999
128103728Swollman#ifdef __LONG_LONG_SUPPORTED
129103728Swollman/* LONGLONG */
130103728Swollmantypedef struct {
131103728Swollman	long long quot;
132103728Swollman	long long rem;
133103728Swollman} lldiv_t;
13469201Sphk
135103728Swollman/* LONGLONG */
136103728Swollmanlong long
137103728Swollman	 atoll(const char *);
138103728Swollman/* LONGLONG */
139103728Swollmanlong long
140103728Swollman	 llabs(long long) __pure2;
141103728Swollman/* LONGLONG */
142103728Swollmanlldiv_t	 lldiv(long long, long long) __pure2;
143103728Swollman/* LONGLONG */
144103766Sbdelong long
145103766Sbde	 strtoll(const char * __restrict, char ** __restrict, int);
146103728Swollman/* LONGLONG */
147103728Swollmanunsigned long long
148103766Sbde	 strtoull(const char * __restrict, char ** __restrict, int);
149103728Swollman#endif /* __LONG_LONG_SUPPORTED */
150103728Swollman
151228330Stheraven_Noreturn void	 _Exit(int);
152103728Swollman#endif /* __ISO_C_VISIBLE >= 1999 */
153103728Swollman
154103728Swollman/*
155229807Sed * If we're in a mode greater than C99, expose C11 functions.
156228322Stheraven */
157232971Stheraven#if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L
158234370Sjasonevoid *	aligned_alloc(size_t, size_t) __malloc_like;
159229807Sedint	at_quick_exit(void (*)(void));
160228901Sed_Noreturn void
161228901Sed	quick_exit(int);
162228901Sed#endif /* __ISO_C_VISIBLE >= 2011 */
163228322Stheraven/*
164252784Sjilles * Extensions made by POSIX relative to C.
165103728Swollman */
166252784Sjilles#if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE
167252784Sjilleschar	*realpath(const char * __restrict, char * __restrict);
168252784Sjilles#endif
169252784Sjilles#if __POSIX_VISIBLE >= 199506
170252784Sjillesint	 rand_r(unsigned *);			/* (TSF) */
171252784Sjilles#endif
172252784Sjilles#if __POSIX_VISIBLE >= 200112
173154250Sjasoneint	 posix_memalign(void **, size_t, size_t); /* (ADV) */
17493032Simpint	 setenv(const char *, const char *, int);
175171195Sscfint	 unsetenv(const char *);
176103728Swollman#endif
1771539Srgrimes
178189349Sdas#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
179189349Sdasint	 getsubopt(char **, char *const *, char **);
180189349Sdas#ifndef _MKDTEMP_DECLARED
181189349Sdaschar	*mkdtemp(char *);
182189349Sdas#define	_MKDTEMP_DECLARED
183189349Sdas#endif
184189349Sdas#ifndef _MKSTEMP_DECLARED
185189349Sdasint	 mkstemp(char *);
186189349Sdas#define	_MKSTEMP_DECLARED
187189349Sdas#endif
188189349Sdas#endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
189189349Sdas
190103728Swollman/*
191103728Swollman * The only changes to the XSI namespace in revision 6 were the deletion
192103728Swollman * of the ttyslot() and valloc() functions, which FreeBSD never declared
193103728Swollman * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
194103728Swollman * FreeBSD also does not have, and mktemp(), are to be deleted.
195103728Swollman */
196103728Swollman#if __XSI_VISIBLE
197103728Swollman/* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
198153707Strhodeslong	 a64l(const char *);
19993032Simpdouble	 drand48(void);
200103766Sbde/* char	*ecvt(double, int, int * __restrict, int * __restrict); */
20193032Simpdouble	 erand48(unsigned short[3]);
202103766Sbde/* char	*fcvt(double, int, int * __restrict, int * __restrict); */
203103766Sbde/* char	*gcvt(double, int, int * __restrict, int * __restrict); */
204108574Sjmallettint	 grantpt(int);
205103728Swollmanchar	*initstate(unsigned long /* XSI requires u_int */, char *, long);
20693032Simplong	 jrand48(unsigned short[3]);
207153707Strhodeschar	*l64a(long);
20893032Simpvoid	 lcong48(unsigned short[7]);
20993032Simplong	 lrand48(void);
210189782Sdas#if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600)
211103728Swollmanchar	*mktemp(char *);
212103766Sbde#define	_MKTEMP_DECLARED
213103728Swollman#endif
21493032Simplong	 mrand48(void);
21593032Simplong	 nrand48(unsigned short[3]);
216108574Sjmallettint	 posix_openpt(int);
217108574Sjmallettchar	*ptsname(int);
218171195Sscfint	 putenv(char *);
219103728Swollmanlong	 random(void);
2207865Sbdeunsigned short
22193032Simp	*seed48(unsigned short[3]);
222103728Swollman#ifndef _SETKEY_DECLARED
223103728Swollmanint	 setkey(const char *);
224103728Swollman#define	_SETKEY_DECLARED
225103728Swollman#endif
226103728Swollmanchar	*setstate(/* const */ char *);
22793032Simpvoid	 srand48(long);
228103728Swollmanvoid	 srandom(unsigned long);
229108574Sjmallettint	 unlockpt(int);
230103728Swollman#endif /* __XSI_VISIBLE */
2314749Sats
232103728Swollman#if __BSD_VISIBLE
233234370Sjasoneextern const char *malloc_conf;
234234370Sjasoneextern void (*malloc_message)(void *, const char *);
235103728Swollman
236116397Sdes/*
237116397Sdes * The alloca() function can't be implemented in C, and on some
238116397Sdes * platforms it can't be implemented at all as a callable function.
239116397Sdes * The GNU C compiler provides a built-in alloca() which we can use;
240116397Sdes * in all other cases, provide a prototype, mainly to pacify various
241116397Sdes * incarnations of lint.  On platforms where alloca() is not in libc,
242116397Sdes * programs which use it will fail to link when compiled with non-GNU
243116397Sdes * compilers.
244116397Sdes */
245116831Sobrien#if __GNUC__ >= 2 || defined(__INTEL_COMPILER)
246116831Sobrien#undef  alloca	/* some GNU bits try to get cute and define this on their own */
247116397Sdes#define alloca(sz) __builtin_alloca(sz)
248116831Sobrien#elif defined(lint)
249116831Sobrienvoid	*alloca(size_t);
250116397Sdes#endif
251116397Sdes
252189820Sdasvoid	 abort2(const char *, int, void **) __dead2;
25341927Sdt__uint32_t
25493032Simp	 arc4random(void);
255180658Sachevoid	 arc4random_addrandom(unsigned char *, int);
256180658Sachevoid	 arc4random_buf(void *, size_t);
25793032Simpvoid	 arc4random_stir(void);
258180689Sache__uint32_t
259180689Sache	 arc4random_uniform(__uint32_t);
260108445Sobrienchar	*getbsize(int *, long *);
261103728Swollman					/* getcap(3) functions */
26293032Simpchar	*cgetcap(char *, const char *, int);
26393032Simpint	 cgetclose(void);
26493032Simpint	 cgetent(char **, char **, const char *);
26593032Simpint	 cgetfirst(char **, char **);
26693032Simpint	 cgetmatch(const char *, const char *);
26793032Simpint	 cgetnext(char **, char **);
26893032Simpint	 cgetnum(char *, const char *, long *);
26993032Simpint	 cgetset(const char *);
27093032Simpint	 cgetstr(char *, const char *, char **);
27193032Simpint	 cgetustr(char *, const char *, char **);
2721539Srgrimes
27393032Simpint	 daemon(int, int);
274150052Sstefanfchar	*devname(__dev_t, __mode_t);
275150052Sstefanfchar 	*devname_r(__dev_t, __mode_t, char *, int);
276188497Sedchar	*fdevname(int);
277188497Sedchar 	*fdevname_r(int, char *, int);
27893032Simpint	 getloadavg(double [], int);
279228468Sedconst char *
28093032Simp	 getprogname(void);
2811539Srgrimes
28293032Simpint	 heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
283153707Strhodesint	 l64a_r(long, char *, int);
28493032Simpint	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
285254151Sjillesint	 mkostemp(char *, int);
286254151Sjillesint	 mkostemps(char *, int, int);
287103164Swollmanvoid	 qsort_r(void *, size_t, size_t, void *,
288103164Swollman	    int (*)(void *, const void *, const void *));
28993032Simpint	 radixsort(const unsigned char **, int, const unsigned char *,
29093032Simp	    unsigned);
29193032Simpvoid    *reallocf(void *, size_t);
292139922Stjrint	 rpmatch(const char *);
29393032Simpvoid	 setprogname(const char *);
294103164Swollmanint	 sradixsort(const unsigned char **, int, const unsigned char *,
295103164Swollman	    unsigned);
29693032Simpvoid	 sranddev(void);
29793032Simpvoid	 srandomdev(void);
298156707Sandrelong long
299156707Sandre	strtonum(const char *, long long, long long, const char **);
300103728Swollman
301103728Swollman/* Deprecated interfaces, to be removed in FreeBSD 6.0. */
302103766Sbde__int64_t
303103766Sbde	 strtoq(const char *, char **, int);
30441927Sdt__uint64_t
30593032Simp	 strtouq(const char *, char **, int);
306126136Sache
307126136Sacheextern char *suboptarg;			/* getsubopt(3) external variable */
308103728Swollman#endif /* __BSD_VISIBLE */
3091539Srgrimes__END_DECLS
3101539Srgrimes
3117865Sbde#endif /* !_STDLIB_H_ */
312