stdlib.h revision 252784
12340Sbde/*-
250476Speter * Copyright (c) 1990, 1993
31638Srgrimes *	The Regents of the University of California.  All rights reserved.
42340Sbde *
578347Sobrien * Redistribution and use in source and binary forms, with or without
61638Srgrimes * modification, are permitted provided that the following conditions
7209024Simp * are met:
8209024Simp * 1. Redistributions of source code must retain the above copyright
9209024Simp *    notice, this list of conditions and the following disclaimer.
10215149Sdim * 2. Redistributions in binary form must reproduce the above copyright
11215149Sdim *    notice, this list of conditions and the following disclaimer in the
12215149Sdim *    documentation and/or other materials provided with the distribution.
13215149Sdim * 3. Neither the name of the University nor the names of its contributors
14215149Sdim *    may be used to endorse or promote products derived from this software
15209024Simp *    without specific prior written permission.
16239272Sgonzo *
17209024Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18209024Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19241298Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20241298Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21241298Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22241298Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2319343Ssteve * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2419343Ssteve * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2519343Ssteve * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2619343Ssteve * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2719343Ssteve * SUCH DAMAGE.
2819343Ssteve *
2919343Ssteve *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
3019343Ssteve * $FreeBSD: head/include/stdlib.h 252784 2013-07-05 14:16:04Z jilles $
3119343Ssteve */
3219343Ssteve
3319343Ssteve#ifndef _STDLIB_H_
3419343Ssteve#define	_STDLIB_H_
35117173Sru
3619343Ssteve#include <sys/cdefs.h>
371638Srgrimes#include <sys/_null.h>
382340Sbde#include <sys/_types.h>
3919343Ssteve
4019343Ssteve#if __BSD_VISIBLE
4119343Ssteve#ifndef _RUNE_T_DECLARED
42270484Sdestypedef	__rune_t	rune_t;
4319343Ssteve#define	_RUNE_T_DECLARED
442340Sbde#endif
45270484Sdes#endif
46270484Sdes
47270484Sdes#ifndef _SIZE_T_DECLARED
481638Srgrimestypedef	__size_t	size_t;
492340Sbde#define	_SIZE_T_DECLARED
502340Sbde#endif
51217100Skib
521638Srgrimes#ifndef	__cplusplus
5319343Ssteve#ifndef _WCHAR_T_DECLARED
5419343Sstevetypedef	__wchar_t	wchar_t;
55125445Sbde#define	_WCHAR_T_DECLARED
5619343Ssteve#endif
572340Sbde#endif
58209024Simp
59177865Sobrientypedef struct {
60173375Scognet	int	quot;		/* quotient */
61177865Sobrien	int	rem;		/* remainder */
6219343Ssteve} div_t;
63173850Sjb
64173850Sjbtypedef struct {
65173375Scognet	long	quot;
66173850Sjb	long	rem;
67202807Ssepotvin} ldiv_t;
681638Srgrimes
69179184Sjb#define	EXIT_FAILURE	1
70179184Sjb#define	EXIT_SUCCESS	0
71179184Sjb
72179184Sjb#define	RAND_MAX	0x7ffffffd
73179184Sjb
74212422Srpaulo__BEGIN_DECLS
75179184Sjb#ifdef _XLOCALE_H_
76179184Sjb#include <xlocale/_stdlib.h>
77179184Sjb#endif
78206082Snetchildextern int __mb_cur_max;
79206082Snetchildextern int ___mb_cur_max(void);
80206082Snetchild#define	MB_CUR_MAX	(___mb_cur_max())
81179184Sjb
82179184Sjb_Noreturn void	 abort(void);
832419Spaulint	 abs(int) __pure2;
84204024Smarcelint	 atexit(void (*)(void));
85202807Ssepotvindouble	 atof(const char *);
861638Srgrimesint	 atoi(const char *);
872340Sbdelong	 atol(const char *);
881638Srgrimesvoid	*bsearch(const void *, const void *, size_t,
89125119Sru	    size_t, int (*)(const void *, const void *));
902340Sbdevoid	*calloc(size_t, size_t) __malloc_like;
912340Sbdediv_t	 div(int, int) __pure2;
922340Sbde_Noreturn void	 exit(int);
932340Sbdevoid	 free(void *);
942340Sbdechar	*getenv(const char *);
952340Sbdelong	 labs(long) __pure2;
962340Sbdeldiv_t	 ldiv(long, long) __pure2;
972340Sbdevoid	*malloc(size_t) __malloc_like;
982340Sbdeint	 mblen(const char *, size_t);
992340Sbdesize_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
1001638Srgrimesint	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
101251748Ssjgvoid	 qsort(void *, size_t, size_t,
102251748Ssjg	    int (*)(const void *, const void *));
103251748Ssjgint	 rand(void);
104133369Shartivoid	*realloc(void *, size_t);
105251748Ssjgvoid	 srand(unsigned);
106251748Ssjgdouble	 strtod(const char * __restrict, char ** __restrict);
107251748Ssjgfloat	 strtof(const char * __restrict, char ** __restrict);
108251748Ssjglong	 strtol(const char * __restrict, char ** __restrict, int);
109133369Shartilong double
110133369Sharti	 strtold(const char * __restrict, char ** __restrict);
111133369Shartiunsigned long
112133369Sharti	 strtoul(const char * __restrict, char ** __restrict, int);
11319343Ssteveint	 system(const char *);
11419343Ssteveint	 wctomb(char *, wchar_t);
11519343Sstevesize_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
11619343Ssteve
1172340Sbde/*
1182340Sbde * Functions added in C99 which we make conditionally available in the
11919343Ssteve * BSD^C89 namespace if the compiler supports `long long'.
1202340Sbde * The #if test is more complicated than it ought to be because
1211638Srgrimes * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
1225257Sache * is not supported in the compilation environment (which therefore means
1235257Sache * that it can't really be ISO C99).
1242340Sbde *
1252340Sbde * (The only other extension made by C99 in thie header is _Exit().)
1261638Srgrimes */
1272340Sbde#if __ISO_C_VISIBLE >= 1999
1282340Sbde#ifdef __LONG_LONG_SUPPORTED
1291638Srgrimes/* LONGLONG */
1302340Sbdetypedef struct {
13192813Sru	long long quot;
13292491Smarkm	long long rem;
13392491Smarkm} lldiv_t;
134120485Smarkm
13592491Smarkm/* LONGLONG */
1361638Srgrimeslong long
1372340Sbde	 atoll(const char *);
1381638Srgrimes/* LONGLONG */
139234575Simplong long
140234575Simp	 llabs(long long) __pure2;
141234575Simp/* LONGLONG */
14236054Sbdelldiv_t	 lldiv(long long, long long) __pure2;
14336054Sbde/* LONGLONG */
14436054Sbdelong long
145244236Semaste	 strtoll(const char * __restrict, char ** __restrict, int);
146244236Semaste/* LONGLONG */
147294064Ssmhunsigned long long
148294064Ssmh	 strtoull(const char * __restrict, char ** __restrict, int);
1492340Sbde#endif /* __LONG_LONG_SUPPORTED */
1502340Sbde
1511638Srgrimes_Noreturn void	 _Exit(int);
1522340Sbde#endif /* __ISO_C_VISIBLE >= 1999 */
1532340Sbde
154244366Semaste/*
1551638Srgrimes * If we're in a mode greater than C99, expose C11 functions.
1562340Sbde */
1572340Sbde#if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L
1582340Sbdevoid *	aligned_alloc(size_t, size_t) __malloc_like;
15919343Ssteveint	at_quick_exit(void (*)(void));
16019343Ssteve_Noreturn void
16119343Ssteve	quick_exit(int);
1622340Sbde#endif /* __ISO_C_VISIBLE >= 2011 */
16319343Ssteve/*
1642340Sbde * Extensions made by POSIX relative to C.
16519343Ssteve */
166129163Sbde#if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE
16719343Sstevechar	*realpath(const char * __restrict, char * __restrict);
16819343Ssteve#endif
16919343Ssteve#if __POSIX_VISIBLE >= 199506
17019343Ssteveint	 rand_r(unsigned *);			/* (TSF) */
17119343Ssteve#endif
17219343Ssteve#if __POSIX_VISIBLE >= 200112
17319343Ssteveint	 posix_memalign(void **, size_t, size_t); /* (ADV) */
17411613Sbdeint	 setenv(const char *, const char *, int);
17519481Salexint	 unsetenv(const char *);
176228137Sfjoe#endif
17719343Ssteve
17819343Ssteve#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
17919481Salexint	 getsubopt(char **, char *const *, char **);
180228137Sfjoe#ifndef _MKDTEMP_DECLARED
18119343Sstevechar	*mkdtemp(char *);
18219343Ssteve#define	_MKDTEMP_DECLARED
183211243Swill#endif
18419343Ssteve#ifndef _MKSTEMP_DECLARED
18519343Ssteveint	 mkstemp(char *);
18619343Ssteve#define	_MKSTEMP_DECLARED
18719343Ssteve#endif
18819343Ssteve#endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
18919481Salex
190228137Sfjoe/*
19119343Ssteve * The only changes to the XSI namespace in revision 6 were the deletion
19219343Ssteve * of the ttyslot() and valloc() functions, which FreeBSD never declared
19319481Salex * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
194228137Sfjoe * FreeBSD also does not have, and mktemp(), are to be deleted.
19519343Ssteve */
19619343Ssteve#if __XSI_VISIBLE
19719481Salex/* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
19819481Salexlong	 a64l(const char *);
19939208Sobriendouble	 drand48(void);
20019343Ssteve/* char	*ecvt(double, int, int * __restrict, int * __restrict); */
201228137Sfjoedouble	 erand48(unsigned short[3]);
20219343Ssteve/* char	*fcvt(double, int, int * __restrict, int * __restrict); */
20319343Ssteve/* char	*gcvt(double, int, int * __restrict, int * __restrict); */
20419481Salexint	 grantpt(int);
20519481Salexchar	*initstate(unsigned long /* XSI requires u_int */, char *, long);
20639208Sobrienlong	 jrand48(unsigned short[3]);
20719343Sstevechar	*l64a(long);
208228137Sfjoevoid	 lcong48(unsigned short[7]);
20919343Sstevelong	 lrand48(void);
21019343Ssteve#if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600)
21119481Salexchar	*mktemp(char *);
21219343Ssteve#define	_MKTEMP_DECLARED
21319343Ssteve#endif
21419343Sstevelong	 mrand48(void);
21519481Salexlong	 nrand48(unsigned short[3]);
21619343Ssteveint	 posix_openpt(int);
21719343Sstevechar	*ptsname(int);
21819343Ssteveint	 putenv(char *);
21919481Salexlong	 random(void);
22019481Salexunsigned short
22139208Sobrien	*seed48(unsigned short[3]);
22219343Ssteve#ifndef _SETKEY_DECLARED
22319343Ssteveint	 setkey(const char *);
22419481Salex#define	_SETKEY_DECLARED
22519481Salex#endif
22639208Sobrienchar	*setstate(/* const */ char *);
22719343Sstevevoid	 srand48(long);
22819343Sstevevoid	 srandom(unsigned long);
22919343Ssteveint	 unlockpt(int);
23019343Ssteve#endif /* __XSI_VISIBLE */
23119343Ssteve
23211613Sbde#if __BSD_VISIBLE
233211243Swillextern const char *malloc_conf;
23411613Sbdeextern void (*malloc_message)(void *, const char *);
23511613Sbde
236103713Smarkm/*
237103713Smarkm * The alloca() function can't be implemented in C, and on some
238103713Smarkm * platforms it can't be implemented at all as a callable function.
239103713Smarkm * The GNU C compiler provides a built-in alloca() which we can use;
240103713Smarkm * in all other cases, provide a prototype, mainly to pacify various
241103713Smarkm * incarnations of lint.  On platforms where alloca() is not in libc,
242103713Smarkm * programs which use it will fail to link when compiled with non-GNU
243103713Smarkm * compilers.
24477817Sobrien */
24577817Sobrien#if __GNUC__ >= 2 || defined(__INTEL_COMPILER)
246228137Sfjoe#undef  alloca	/* some GNU bits try to get cute and define this on their own */
24777817Sobrien#define alloca(sz) __builtin_alloca(sz)
2481638Srgrimes#elif defined(lint)
249280329Sdimvoid	*alloca(size_t);
250228137Sfjoe#endif
2511638Srgrimes
25277817Sobrienvoid	 abort2(const char *, int, void **) __dead2;
25377817Sobrien__uint32_t
25477817Sobrien	 arc4random(void);
25536673Sdtvoid	 arc4random_addrandom(unsigned char *, int);
256280329Sdimvoid	 arc4random_buf(void *, size_t);
2571844Swollmanvoid	 arc4random_stir(void);
25836054Sbde__uint32_t
259280329Sdim	 arc4random_uniform(__uint32_t);
260228137Sfjoechar	*getbsize(int *, long *);
26136054Sbde					/* getcap(3) functions */
2621638Srgrimeschar	*cgetcap(char *, const char *, int);
263280329Sdimint	 cgetclose(void);
264228137Sfjoeint	 cgetent(char **, char **, const char *);
2651638Srgrimesint	 cgetfirst(char **, char **);
26677818Sobrienint	 cgetmatch(const char *, const char *);
26777818Sobrienint	 cgetnext(char **, char **);
26877818Sobrienint	 cgetnum(char *, const char *, long *);
26977818Sobrienint	 cgetset(const char *);
2701638Srgrimesint	 cgetstr(char *, const char *, char **);
271280329Sdimint	 cgetustr(char *, const char *, char **);
2721638Srgrimes
2733292Srgrimesint	 daemon(int, int);
274280329Sdimchar	*devname(__dev_t, __mode_t);
275228137Sfjoechar 	*devname_r(__dev_t, __mode_t, char *, int);
2763292Srgrimeschar	*fdevname(int);
277117173Sruchar 	*fdevname_r(int, char *, int);
278280329Sdimint	 getloadavg(double [], int);
279280329Sdimconst char *
280228137Sfjoe	 getprogname(void);
281117173Sru
282117173Sruint	 heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
2831638Srgrimesint	 l64a_r(long, char *, int);
284228137Sfjoeint	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
2851638Srgrimesvoid	 qsort_r(void *, size_t, size_t, void *,
28616068Sphk	    int (*)(void *, const void *, const void *));
2871638Srgrimesint	 radixsort(const unsigned char **, int, const unsigned char *,
28816068Sphk	    unsigned);
28916068Sphkvoid    *reallocf(void *, size_t);
29039208Sobrienint	 rpmatch(const char *);
291228137Sfjoevoid	 setprogname(const char *);
2921638Srgrimesint	 sradixsort(const unsigned char **, int, const unsigned char *,
2931638Srgrimes	    unsigned);
29415959Sphkvoid	 sranddev(void);
29515959Sphkvoid	 srandomdev(void);
29639208Sobrienlong long
297228137Sfjoe	strtonum(const char *, long long, long long, const char **);
2981638Srgrimes
29921582Ssteve/* Deprecated interfaces, to be removed in FreeBSD 6.0. */
3001638Srgrimes__int64_t
30121582Ssteve	 strtoq(const char *, char **, int);
30221582Ssteve__uint64_t
3031638Srgrimes	 strtouq(const char *, char **, int);
3041638Srgrimes
30515959Sphkextern char *suboptarg;			/* getsubopt(3) external variable */
3061638Srgrimes#endif /* __BSD_VISIBLE */
3071638Srgrimes__END_DECLS
30811613Sbde
309228137Sfjoe#endif /* !_STDLIB_H_ */
3101638Srgrimes