stdlib.h revision 254151
12742Swollman/*-
22742Swollman * Copyright (c) 1990, 1993
32742Swollman *	The Regents of the University of California.  All rights reserved.
42742Swollman *
52742Swollman * Redistribution and use in source and binary forms, with or without
62742Swollman * modification, are permitted provided that the following conditions
72742Swollman * are met:
82742Swollman * 1. Redistributions of source code must retain the above copyright
92742Swollman *    notice, this list of conditions and the following disclaimer.
102742Swollman * 2. Redistributions in binary form must reproduce the above copyright
112742Swollman *    notice, this list of conditions and the following disclaimer in the
122742Swollman *    documentation and/or other materials provided with the distribution.
132742Swollman * 3. Neither the name of the University nor the names of its contributors
142742Swollman *    may be used to endorse or promote products derived from this software
152742Swollman *    without specific prior written permission.
162742Swollman *
172742Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
182742Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192742Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
202742Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
212742Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
222742Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
232742Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
242742Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
252742Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
262742Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272742Swollman * SUCH DAMAGE.
282742Swollman *
292742Swollman *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
302742Swollman * $FreeBSD: head/include/stdlib.h 254151 2013-08-09 17:24:23Z jilles $
312742Swollman */
322742Swollman
332742Swollman#ifndef _STDLIB_H_
342742Swollman#define	_STDLIB_H_
352742Swollman
362742Swollman#include <sys/cdefs.h>
372742Swollman#include <sys/_null.h>
382742Swollman#include <sys/_types.h>
392742Swollman
402742Swollman#if __BSD_VISIBLE
412742Swollman#ifndef _RUNE_T_DECLARED
422742Swollmantypedef	__rune_t	rune_t;
432742Swollman#define	_RUNE_T_DECLARED
442742Swollman#endif
452742Swollman#endif
462742Swollman
472742Swollman#ifndef _SIZE_T_DECLARED
482742Swollmantypedef	__size_t	size_t;
492742Swollman#define	_SIZE_T_DECLARED
502742Swollman#endif
512742Swollman
522742Swollman#ifndef	__cplusplus
532742Swollman#ifndef _WCHAR_T_DECLARED
542742Swollmantypedef	__wchar_t	wchar_t;
552742Swollman#define	_WCHAR_T_DECLARED
562742Swollman#endif
572742Swollman#endif
582742Swollman
592742Swollmantypedef struct {
602742Swollman	int	quot;		/* quotient */
612742Swollman	int	rem;		/* remainder */
622742Swollman} div_t;
632742Swollman
642742Swollmantypedef struct {
652742Swollman	long	quot;
662742Swollman	long	rem;
672742Swollman} ldiv_t;
682742Swollman
692742Swollman#define	EXIT_FAILURE	1
702742Swollman#define	EXIT_SUCCESS	0
712742Swollman
722742Swollman#define	RAND_MAX	0x7ffffffd
732742Swollman
742742Swollman__BEGIN_DECLS
752742Swollman#ifdef _XLOCALE_H_
762742Swollman#include <xlocale/_stdlib.h>
772742Swollman#endif
782742Swollmanextern int __mb_cur_max;
792742Swollmanextern int ___mb_cur_max(void);
802742Swollman#define	MB_CUR_MAX	(___mb_cur_max())
812742Swollman
822742Swollman_Noreturn void	 abort(void);
832742Swollmanint	 abs(int) __pure2;
842742Swollmanint	 atexit(void (*)(void));
852742Swollmandouble	 atof(const char *);
862742Swollmanint	 atoi(const char *);
872742Swollmanlong	 atol(const char *);
882742Swollmanvoid	*bsearch(const void *, const void *, size_t,
892742Swollman	    size_t, int (*)(const void *, const void *));
902742Swollmanvoid	*calloc(size_t, size_t) __malloc_like;
912742Swollmandiv_t	 div(int, int) __pure2;
922742Swollman_Noreturn void	 exit(int);
932742Swollmanvoid	 free(void *);
942742Swollmanchar	*getenv(const char *);
952742Swollmanlong	 labs(long) __pure2;
962742Swollmanldiv_t	 ldiv(long, long) __pure2;
972742Swollmanvoid	*malloc(size_t) __malloc_like;
982742Swollmanint	 mblen(const char *, size_t);
992742Swollmansize_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
1002742Swollmanint	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
1012742Swollmanvoid	 qsort(void *, size_t, size_t,
1022742Swollman	    int (*)(const void *, const void *));
1032742Swollmanint	 rand(void);
1042742Swollmanvoid	*realloc(void *, size_t);
1052742Swollmanvoid	 srand(unsigned);
1062742Swollmandouble	 strtod(const char * __restrict, char ** __restrict);
1072742Swollmanfloat	 strtof(const char * __restrict, char ** __restrict);
1082742Swollmanlong	 strtol(const char * __restrict, char ** __restrict, int);
1092742Swollmanlong double
1102742Swollman	 strtold(const char * __restrict, char ** __restrict);
1112742Swollmanunsigned long
1122742Swollman	 strtoul(const char * __restrict, char ** __restrict, int);
1132742Swollmanint	 system(const char *);
1142742Swollmanint	 wctomb(char *, wchar_t);
1152742Swollmansize_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
1162742Swollman
1172742Swollman/*
1182742Swollman * Functions added in C99 which we make conditionally available in the
1192742Swollman * BSD^C89 namespace if the compiler supports `long long'.
1202742Swollman * The #if test is more complicated than it ought to be because
1212742Swollman * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
1222742Swollman * is not supported in the compilation environment (which therefore means
1232742Swollman * that it can't really be ISO C99).
1242742Swollman *
1252742Swollman * (The only other extension made by C99 in thie header is _Exit().)
1262742Swollman */
1272742Swollman#if __ISO_C_VISIBLE >= 1999
1282742Swollman#ifdef __LONG_LONG_SUPPORTED
1292742Swollman/* LONGLONG */
1302742Swollmantypedef struct {
1312742Swollman	long long quot;
1322742Swollman	long long rem;
1332742Swollman} lldiv_t;
1342742Swollman
1352742Swollman/* LONGLONG */
1362742Swollmanlong long
1372742Swollman	 atoll(const char *);
1382742Swollman/* LONGLONG */
1392742Swollmanlong long
1402742Swollman	 llabs(long long) __pure2;
1412742Swollman/* LONGLONG */
1422742Swollmanlldiv_t	 lldiv(long long, long long) __pure2;
1432742Swollman/* LONGLONG */
1442742Swollmanlong long
1452742Swollman	 strtoll(const char * __restrict, char ** __restrict, int);
1462742Swollman/* LONGLONG */
1472742Swollmanunsigned long long
1482742Swollman	 strtoull(const char * __restrict, char ** __restrict, int);
1492742Swollman#endif /* __LONG_LONG_SUPPORTED */
1502742Swollman
1512742Swollman_Noreturn void	 _Exit(int);
1522742Swollman#endif /* __ISO_C_VISIBLE >= 1999 */
1532742Swollman
1542742Swollman/*
1552742Swollman * If we're in a mode greater than C99, expose C11 functions.
1562742Swollman */
1572742Swollman#if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L
1582742Swollmanvoid *	aligned_alloc(size_t, size_t) __malloc_like;
1592742Swollmanint	at_quick_exit(void (*)(void));
1602742Swollman_Noreturn void
1612742Swollman	quick_exit(int);
1622742Swollman#endif /* __ISO_C_VISIBLE >= 2011 */
1632742Swollman/*
1642742Swollman * Extensions made by POSIX relative to C.
1652742Swollman */
1662742Swollman#if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE
1672742Swollmanchar	*realpath(const char * __restrict, char * __restrict);
1682742Swollman#endif
1692742Swollman#if __POSIX_VISIBLE >= 199506
1702742Swollmanint	 rand_r(unsigned *);			/* (TSF) */
1712742Swollman#endif
1722742Swollman#if __POSIX_VISIBLE >= 200112
1732742Swollmanint	 posix_memalign(void **, size_t, size_t); /* (ADV) */
1742742Swollmanint	 setenv(const char *, const char *, int);
1752742Swollmanint	 unsetenv(const char *);
1762742Swollman#endif
1772742Swollman
1782742Swollman#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
1792742Swollmanint	 getsubopt(char **, char *const *, char **);
1802742Swollman#ifndef _MKDTEMP_DECLARED
1812742Swollmanchar	*mkdtemp(char *);
1822742Swollman#define	_MKDTEMP_DECLARED
1832742Swollman#endif
1842742Swollman#ifndef _MKSTEMP_DECLARED
1852742Swollmanint	 mkstemp(char *);
1862742Swollman#define	_MKSTEMP_DECLARED
1872742Swollman#endif
1882742Swollman#endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
1892742Swollman
1902742Swollman/*
1912742Swollman * The only changes to the XSI namespace in revision 6 were the deletion
1922742Swollman * of the ttyslot() and valloc() functions, which FreeBSD never declared
1932742Swollman * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
1942742Swollman * FreeBSD also does not have, and mktemp(), are to be deleted.
1952742Swollman */
1962742Swollman#if __XSI_VISIBLE
1972742Swollman/* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
1982742Swollmanlong	 a64l(const char *);
1992742Swollmandouble	 drand48(void);
2002742Swollman/* char	*ecvt(double, int, int * __restrict, int * __restrict); */
2012742Swollmandouble	 erand48(unsigned short[3]);
2022742Swollman/* char	*fcvt(double, int, int * __restrict, int * __restrict); */
2032742Swollman/* char	*gcvt(double, int, int * __restrict, int * __restrict); */
2042742Swollmanint	 grantpt(int);
2052742Swollmanchar	*initstate(unsigned long /* XSI requires u_int */, char *, long);
2062742Swollmanlong	 jrand48(unsigned short[3]);
2072742Swollmanchar	*l64a(long);
2082742Swollmanvoid	 lcong48(unsigned short[7]);
2092742Swollmanlong	 lrand48(void);
2102742Swollman#if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600)
2112742Swollmanchar	*mktemp(char *);
2122742Swollman#define	_MKTEMP_DECLARED
2132742Swollman#endif
2142742Swollmanlong	 mrand48(void);
2152742Swollmanlong	 nrand48(unsigned short[3]);
2162742Swollmanint	 posix_openpt(int);
2172742Swollmanchar	*ptsname(int);
2182742Swollmanint	 putenv(char *);
2192742Swollmanlong	 random(void);
2202742Swollmanunsigned short
2212742Swollman	*seed48(unsigned short[3]);
2222742Swollman#ifndef _SETKEY_DECLARED
2232742Swollmanint	 setkey(const char *);
2242742Swollman#define	_SETKEY_DECLARED
2252742Swollman#endif
2262742Swollmanchar	*setstate(/* const */ char *);
2272742Swollmanvoid	 srand48(long);
2282742Swollmanvoid	 srandom(unsigned long);
2292742Swollmanint	 unlockpt(int);
2302742Swollman#endif /* __XSI_VISIBLE */
2312742Swollman
2322742Swollman#if __BSD_VISIBLE
2332742Swollmanextern const char *malloc_conf;
2342742Swollmanextern void (*malloc_message)(void *, const char *);
2352742Swollman
2362742Swollman/*
2372742Swollman * The alloca() function can't be implemented in C, and on some
2382742Swollman * platforms it can't be implemented at all as a callable function.
2392742Swollman * The GNU C compiler provides a built-in alloca() which we can use;
2402742Swollman * in all other cases, provide a prototype, mainly to pacify various
2412742Swollman * incarnations of lint.  On platforms where alloca() is not in libc,
2422742Swollman * programs which use it will fail to link when compiled with non-GNU
2432742Swollman * compilers.
2442742Swollman */
2452742Swollman#if __GNUC__ >= 2 || defined(__INTEL_COMPILER)
2462742Swollman#undef  alloca	/* some GNU bits try to get cute and define this on their own */
2472742Swollman#define alloca(sz) __builtin_alloca(sz)
2482742Swollman#elif defined(lint)
2492742Swollmanvoid	*alloca(size_t);
2502742Swollman#endif
2512742Swollman
2522742Swollmanvoid	 abort2(const char *, int, void **) __dead2;
2532742Swollman__uint32_t
2542742Swollman	 arc4random(void);
2552742Swollmanvoid	 arc4random_addrandom(unsigned char *, int);
2562742Swollmanvoid	 arc4random_buf(void *, size_t);
2572742Swollmanvoid	 arc4random_stir(void);
2582742Swollman__uint32_t
2592742Swollman	 arc4random_uniform(__uint32_t);
2602742Swollmanchar	*getbsize(int *, long *);
2612742Swollman					/* getcap(3) functions */
2622742Swollmanchar	*cgetcap(char *, const char *, int);
2632742Swollmanint	 cgetclose(void);
2642742Swollmanint	 cgetent(char **, char **, const char *);
2652742Swollmanint	 cgetfirst(char **, char **);
2662742Swollmanint	 cgetmatch(const char *, const char *);
2672742Swollmanint	 cgetnext(char **, char **);
2682742Swollmanint	 cgetnum(char *, const char *, long *);
2692742Swollmanint	 cgetset(const char *);
2702742Swollmanint	 cgetstr(char *, const char *, char **);
2712742Swollmanint	 cgetustr(char *, const char *, char **);
2722742Swollman
2732742Swollmanint	 daemon(int, int);
2742742Swollmanchar	*devname(__dev_t, __mode_t);
2752742Swollmanchar 	*devname_r(__dev_t, __mode_t, char *, int);
2762742Swollmanchar	*fdevname(int);
2772742Swollmanchar 	*fdevname_r(int, char *, int);
2782742Swollmanint	 getloadavg(double [], int);
2792742Swollmanconst char *
2802742Swollman	 getprogname(void);
2812742Swollman
2822742Swollmanint	 heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
2832742Swollmanint	 l64a_r(long, char *, int);
2842742Swollmanint	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
2852742Swollmanint	 mkostemp(char *, int);
2862742Swollmanint	 mkostemps(char *, int, int);
2872742Swollmanvoid	 qsort_r(void *, size_t, size_t, void *,
2882742Swollman	    int (*)(void *, const void *, const void *));
2892742Swollmanint	 radixsort(const unsigned char **, int, const unsigned char *,
2902742Swollman	    unsigned);
2912742Swollmanvoid    *reallocf(void *, size_t);
2922742Swollmanint	 rpmatch(const char *);
2932742Swollmanvoid	 setprogname(const char *);
2942742Swollmanint	 sradixsort(const unsigned char **, int, const unsigned char *,
2952742Swollman	    unsigned);
2962742Swollmanvoid	 sranddev(void);
2972742Swollmanvoid	 srandomdev(void);
2982742Swollmanlong long
2992742Swollman	strtonum(const char *, long long, long long, const char **);
3002742Swollman
3012742Swollman/* Deprecated interfaces, to be removed in FreeBSD 6.0. */
3022742Swollman__int64_t
3032742Swollman	 strtoq(const char *, char **, int);
3042742Swollman__uint64_t
3052742Swollman	 strtouq(const char *, char **, int);
3062742Swollman
3072742Swollmanextern char *suboptarg;			/* getsubopt(3) external variable */
3082742Swollman#endif /* __BSD_VISIBLE */
3092742Swollman__END_DECLS
3102742Swollman
3112742Swollman#endif /* !_STDLIB_H_ */
3122742Swollman