1/*
2 * Copyright (c) 2000, 2002 - 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*-
24 * Copyright (c) 1990, 1993
25 *	The Regents of the University of California.  All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 *    notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 *    notice, this list of conditions and the following disclaimer in the
34 *    documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 *    must display the following acknowledgement:
37 *	This product includes software developed by the University of
38 *	California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 *    may be used to endorse or promote products derived from this software
41 *    without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 *
55 *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
56 */
57
58#ifndef _STDLIB_H_
59#define _STDLIB_H_
60
61#include <Availability.h>
62
63#include <_types.h>
64#if !defined(_ANSI_SOURCE)
65#include <sys/wait.h>
66#if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
67#include <alloca.h>
68#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
69#endif /* !_ANSI_SOURCE */
70
71/* DO NOT REMOVE THIS COMMENT: fixincludes needs to see:
72 * _GCC_SIZE_T */
73#include <sys/_types/_size_t.h>
74
75#if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
76#include <sys/_types/_ct_rune_t.h>
77#include <sys/_types/_rune_t.h>
78#endif	/* !_ANSI_SOURCE && (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
79
80#include <sys/_types/_wchar_t.h>
81
82typedef struct {
83	int quot;		/* quotient */
84	int rem;		/* remainder */
85} div_t;
86
87typedef struct {
88	long quot;		/* quotient */
89	long rem;		/* remainder */
90} ldiv_t;
91
92#if !__DARWIN_NO_LONG_LONG
93typedef struct {
94	long long quot;
95	long long rem;
96} lldiv_t;
97#endif /* !__DARWIN_NO_LONG_LONG */
98
99#include <sys/_types/_null.h>
100
101#define	EXIT_FAILURE	1
102#define	EXIT_SUCCESS	0
103
104#define	RAND_MAX	0x7fffffff
105
106#ifdef _USE_EXTENDED_LOCALES_
107#include <_xlocale.h>
108#endif /* _USE_EXTENDED_LOCALES_ */
109
110#ifndef MB_CUR_MAX
111#ifdef _USE_EXTENDED_LOCALES_
112#define	MB_CUR_MAX	(___mb_cur_max())
113#ifndef MB_CUR_MAX_L
114#define	MB_CUR_MAX_L(x)	(___mb_cur_max_l(x))
115#endif /* !MB_CUR_MAX_L */
116#else /* !_USE_EXTENDED_LOCALES_ */
117extern int __mb_cur_max;
118#define	MB_CUR_MAX	__mb_cur_max
119#endif /* _USE_EXTENDED_LOCALES_ */
120#endif /* MB_CUR_MAX */
121
122#if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)) \
123    && defined(_USE_EXTENDED_LOCALES_) && !defined(MB_CUR_MAX_L)
124#define	MB_CUR_MAX_L(x)	(___mb_cur_max_l(x))
125#endif
126//Begin-Libc
127/* f must be a literal string */
128#define LIBC_ABORT(f,...)	abort_report_np("%s:%s:%u: " f, __FILE__, __func__, __LINE__, ## __VA_ARGS__)
129//End-Libc
130
131__BEGIN_DECLS
132void	 abort(void) __dead2;
133//Begin-Libc
134__private_extern__
135void	 abort_report_np(const char *, ...) __dead2 __printflike(1, 2);
136//End-Libc
137int	 abs(int) __pure2;
138int	 atexit(void (*)(void));
139double	 atof(const char *);
140int	 atoi(const char *);
141long	 atol(const char *);
142#if !__DARWIN_NO_LONG_LONG
143long long
144	 atoll(const char *);
145#endif /* !__DARWIN_NO_LONG_LONG */
146void	*bsearch(const void *, const void *, size_t,
147	    size_t, int (*)(const void *, const void *));
148void	*calloc(size_t, size_t);
149div_t	 div(int, int) __pure2;
150void	 exit(int) __dead2;
151void	 free(void *);
152char	*getenv(const char *);
153long	 labs(long) __pure2;
154ldiv_t	 ldiv(long, long) __pure2;
155#if !__DARWIN_NO_LONG_LONG
156long long
157	 llabs(long long);
158lldiv_t	 lldiv(long long, long long);
159#endif /* !__DARWIN_NO_LONG_LONG */
160void	*malloc(size_t);
161int	 mblen(const char *, size_t);
162size_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
163int	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
164int 	 posix_memalign(void **, size_t, size_t) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_0);
165void	 qsort(void *, size_t, size_t,
166	    int (*)(const void *, const void *));
167int	 rand(void);
168void	*realloc(void *, size_t);
169void	 srand(unsigned);
170double	 strtod(const char *, char **) __DARWIN_ALIAS(strtod);
171float	 strtof(const char *, char **) __DARWIN_ALIAS(strtof);
172long	 strtol(const char *, char **, int);
173long double
174	 strtold(const char *, char **);
175#if !__DARWIN_NO_LONG_LONG
176long long
177	 strtoll(const char *, char **, int);
178#endif /* !__DARWIN_NO_LONG_LONG */
179unsigned long
180	 strtoul(const char *, char **, int);
181#if !__DARWIN_NO_LONG_LONG
182unsigned long long
183	 strtoull(const char *, char **, int);
184#endif /* !__DARWIN_NO_LONG_LONG */
185//Begin-Libc
186#ifndef LIBC_ALIAS_SYSTEM
187//End-Libc
188int	 system(const char *) __DARWIN_ALIAS_C(system);
189//Begin-Libc
190#else /* LIBC_ALIAS_SYSTEM */
191int	 system(const char *) LIBC_ALIAS_C(system);
192#endif /* !LIBC_ALIAS_SYSTEM */
193//End-Libc
194size_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
195int	 wctomb(char *, wchar_t);
196
197#ifndef _ANSI_SOURCE
198void	_Exit(int) __dead2;
199long	 a64l(const char *);
200double	 drand48(void);
201char	*ecvt(double, int, int *__restrict, int *__restrict); /* LEGACY */
202double	 erand48(unsigned short[3]);
203char	*fcvt(double, int, int *__restrict, int *__restrict); /* LEGACY */
204char	*gcvt(double, int, char *); /* LEGACY */
205int	 getsubopt(char **, char * const *, char **);
206int	 grantpt(int);
207#if __DARWIN_UNIX03
208char	*initstate(unsigned, char *, size_t); /* no  __DARWIN_ALIAS needed */
209#else /* !__DARWIN_UNIX03 */
210char	*initstate(unsigned long, char *, long);
211#endif /* __DARWIN_UNIX03 */
212long	 jrand48(unsigned short[3]);
213char	*l64a(long);
214void	 lcong48(unsigned short[7]);
215long	 lrand48(void);
216char	*mktemp(char *);
217int	 mkstemp(char *);
218long	 mrand48(void);
219long	 nrand48(unsigned short[3]);
220int	 posix_openpt(int);
221char	*ptsname(int);
222//Begin-Libc
223#ifndef LIBC_ALIAS_PUTENV
224//End-Libc
225int	 putenv(char *) __DARWIN_ALIAS(putenv);
226//Begin-Libc
227#else /* LIBC_ALIAS_PUTENV */
228int	 putenv(char *) LIBC_ALIAS(putenv);
229#endif /* !LIBC_ALIAS_PUTENV */
230//End-Libc
231long	 random(void);
232int	 rand_r(unsigned *);
233//Begin-Libc
234#ifdef __LIBC__
235#ifndef LIBC_ALIAS_REALPATH
236char	*realpath(const char * __restrict, char * __restrict) __DARWIN_EXTSN(realpath);
237#else /* LIBC_ALIAS_REALPATH */
238#ifdef VARIANT_DARWINEXTSN
239char	*realpath(const char * __restrict, char * __restrict) LIBC_EXTSN(realpath);
240#else /* !VARIANT_DARWINEXTSN */
241char	*realpath(const char * __restrict, char * __restrict) LIBC_ALIAS(realpath);
242#endif /* VARIANT_DARWINEXTSN */
243#endif /* !LIBC_ALIAS_REALPATH */
244#else /* !__LIBC__ */
245//End-Libc
246#if (__DARWIN_UNIX03 && !defined(_POSIX_C_SOURCE)) || defined(_DARWIN_C_SOURCE) || defined(_DARWIN_BETTER_REALPATH)
247char	*realpath(const char * __restrict, char * __restrict) __DARWIN_EXTSN(realpath);
248#else /* (!__DARWIN_UNIX03 || _POSIX_C_SOURCE) && !_DARWIN_C_SOURCE && !_DARWIN_BETTER_REALPATH */
249char	*realpath(const char * __restrict, char * __restrict) __DARWIN_ALIAS(realpath);
250#endif /* (__DARWIN_UNIX03 && _POSIX_C_SOURCE) || _DARWIN_C_SOURCE || _DARWIN_BETTER_REALPATH */
251//Begin-Libc
252#endif /* __LIBC__ */
253//End-Libc
254unsigned short
255	*seed48(unsigned short[3]);
256//Begin-Libc
257#ifndef LIBC_ALIAS_SETENV
258//End-Libc
259int	 setenv(const char *, const char *, int) __DARWIN_ALIAS(setenv);
260//Begin-Libc
261#else /* LIBC_ALIAS_SETENV */
262int	 setenv(const char *, const char *, int) LIBC_ALIAS(setenv);
263#endif /* !LIBC_ALIAS_SETENV */
264//End-Libc
265#if __DARWIN_UNIX03
266//Begin-Libc
267#ifndef LIBC_ALIAS_SETKEY
268//End-Libc
269void	 setkey(const char *) __DARWIN_ALIAS(setkey);
270//Begin-Libc
271#else /* LIBC_ALIAS_SETKEY */
272void	 setkey(const char *) LIBC_ALIAS(setkey);
273#endif /* !LIBC_ALIAS_SETKEY */
274//End-Libc
275#else /* !__DARWIN_UNIX03 */
276int	 setkey(const char *);
277#endif /* __DARWIN_UNIX03 */
278char	*setstate(const char *);
279void	 srand48(long);
280#if __DARWIN_UNIX03
281void	 srandom(unsigned);
282#else /* !__DARWIN_UNIX03 */
283void	 srandom(unsigned long);
284#endif /* __DARWIN_UNIX03 */
285int	 unlockpt(int);
286#if __DARWIN_UNIX03
287//Begin-Libc
288#ifndef LIBC_ALIAS_UNSETENV
289//End-Libc
290int	 unsetenv(const char *) __DARWIN_ALIAS(unsetenv);
291//Begin-Libc
292#else /* LIBC_ALIAS_UNSETENV */
293int	 unsetenv(const char *) LIBC_ALIAS(unsetenv);
294#endif /* !LIBC_ALIAS_UNSETENV */
295//End-Libc
296#else /* !__DARWIN_UNIX03 */
297void	 unsetenv(const char *);
298#endif /* __DARWIN_UNIX03 */
299#endif	/* !_ANSI_SOURCE */
300
301#if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
302#include <machine/types.h>
303
304#include <sys/_types/_dev_t.h>
305#include <sys/_types/_mode_t.h>
306
307u_int32_t	arc4random(void);
308void	 arc4random_addrandom(unsigned char * /*dat*/, int /*datlen*/);
309void	 arc4random_buf(void * /*buf*/, size_t /*nbytes*/) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
310void	 arc4random_stir(void);
311u_int32_t
312	 arc4random_uniform(u_int32_t /*upper_bound*/) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
313#ifdef __BLOCKS__
314int	 atexit_b(void (^)(void)) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
315void	*bsearch_b(const void *, const void *, size_t,
316	    size_t, int (^)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
317#endif /* __BLOCKS__ */
318
319	 /* getcap(3) functions */
320char	*cgetcap(char *, const char *, int);
321int	 cgetclose(void);
322int	 cgetent(char **, char **, const char *);
323int	 cgetfirst(char **, char **);
324int	 cgetmatch(const char *, const char *);
325int	 cgetnext(char **, char **);
326int	 cgetnum(char *, const char *, long *);
327int	 cgetset(const char *);
328int	 cgetstr(char *, const char *, char **);
329int	 cgetustr(char *, const char *, char **);
330
331int	 daemon(int, int) __DARWIN_1050(daemon) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_5, __IPHONE_2_0, __IPHONE_2_0);
332char	*devname(dev_t, mode_t);
333char	*devname_r(dev_t, mode_t, char *buf, int len);
334char	*getbsize(int *, long *);
335int	 getloadavg(double [], int);
336const char
337	*getprogname(void);
338
339int	 heapsort(void *, size_t, size_t,
340	    int (*)(const void *, const void *));
341#ifdef __BLOCKS__
342int	 heapsort_b(void *, size_t, size_t,
343	    int (^)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
344#endif /* __BLOCKS__ */
345int	 mergesort(void *, size_t, size_t,
346	    int (*)(const void *, const void *));
347#ifdef __BLOCKS__
348int	 mergesort_b(void *, size_t, size_t,
349	    int (^)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
350#endif /* __BLOCKS__ */
351void	 psort(void *, size_t, size_t,
352	    int (*)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
353#ifdef __BLOCKS__
354void	 psort_b(void *, size_t, size_t,
355	    int (^)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
356#endif /* __BLOCKS__ */
357void	 psort_r(void *, size_t, size_t, void *,
358	    int (*)(void *, const void *, const void *))  __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
359#ifdef __BLOCKS__
360void	 qsort_b(void *, size_t, size_t,
361	    int (^)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
362#endif /* __BLOCKS__ */
363void	 qsort_r(void *, size_t, size_t, void *,
364	    int (*)(void *, const void *, const void *));
365int	 radixsort(const unsigned char **, int, const unsigned char *,
366	    unsigned);
367void	 setprogname(const char *);
368int	 sradixsort(const unsigned char **, int, const unsigned char *,
369	    unsigned);
370void	 sranddev(void);
371void	 srandomdev(void);
372void	*reallocf(void *, size_t);
373#if !__DARWIN_NO_LONG_LONG
374long long
375	 strtoq(const char *, char **, int);
376unsigned long long
377	 strtouq(const char *, char **, int);
378#endif /* !__DARWIN_NO_LONG_LONG */
379extern char *suboptarg;		/* getsubopt(3) external variable */
380void	*valloc(size_t);
381#endif	/* !_ANSI_SOURCE && !_POSIX_SOURCE */
382
383/* Poison the following routines if -fshort-wchar is set */
384#if !defined(__cplusplus) && defined(__WCHAR_MAX__) && __WCHAR_MAX__ <= 0xffffU
385#pragma GCC poison mbstowcs mbtowc wcstombs wctomb
386#endif
387__END_DECLS
388
389#ifdef _USE_EXTENDED_LOCALES_
390#include <xlocale/_stdlib.h>
391#endif /* _USE_EXTENDED_LOCALES_ */
392
393#endif /* _STDLIB_H_ */
394