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 #include <sys/cdefs.h>
63 
64 #include <_types.h>
65 #if !defined(_ANSI_SOURCE)
66 #include <sys/wait.h>
67 #if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
68 #include <alloca.h>
69 #endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
70 #endif /* !_ANSI_SOURCE */
71 
72 /* DO NOT REMOVE THIS COMMENT: fixincludes needs to see:
73  * _GCC_SIZE_T */
74 #include <sys/_types/_size_t.h>
75 
76 #if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
77 #include <sys/_types/_ct_rune_t.h>
78 #include <sys/_types/_rune_t.h>
79 #endif	/* !_ANSI_SOURCE && (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
80 
81 #include <sys/_types/_wchar_t.h>
82 
83 typedef struct {
84 	int quot;		/* quotient */
85 	int rem;		/* remainder */
86 } div_t;
87 
88 typedef struct {
89 	long quot;		/* quotient */
90 	long rem;		/* remainder */
91 } ldiv_t;
92 
93 #if !__DARWIN_NO_LONG_LONG
94 typedef struct {
95 	long long quot;
96 	long long rem;
97 } lldiv_t;
98 #endif /* !__DARWIN_NO_LONG_LONG */
99 
100 #include <sys/_types/_null.h>
101 
102 #define	EXIT_FAILURE	1
103 #define	EXIT_SUCCESS	0
104 
105 #define	RAND_MAX	0x7fffffff
106 
107 #ifdef _USE_EXTENDED_LOCALES_
108 #include <_xlocale.h>
109 #endif /* _USE_EXTENDED_LOCALES_ */
110 
111 #ifndef MB_CUR_MAX
112 #ifdef _USE_EXTENDED_LOCALES_
113 #define	MB_CUR_MAX	(___mb_cur_max())
114 #ifndef MB_CUR_MAX_L
115 #define	MB_CUR_MAX_L(x)	(___mb_cur_max_l(x))
116 #endif /* !MB_CUR_MAX_L */
117 #else /* !_USE_EXTENDED_LOCALES_ */
118 extern int __mb_cur_max;
119 #define	MB_CUR_MAX	__mb_cur_max
120 #endif /* _USE_EXTENDED_LOCALES_ */
121 #endif /* MB_CUR_MAX */
122 
123 #if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)) \
124     && defined(_USE_EXTENDED_LOCALES_) && !defined(MB_CUR_MAX_L)
125 #define	MB_CUR_MAX_L(x)	(___mb_cur_max_l(x))
126 #endif
127 
128 #include <malloc/_malloc.h>
129 
130 __BEGIN_DECLS
131 void	 abort(void) __cold __dead2;
132 int	 abs(int) __pure2;
133 int	 atexit(void (* _Nonnull)(void));
134 double	 atof(const char *);
135 int	 atoi(const char *);
136 long	 atol(const char *);
137 #if !__DARWIN_NO_LONG_LONG
138 long long
139 	 atoll(const char *);
140 #endif /* !__DARWIN_NO_LONG_LONG */
141 void	*bsearch(const void *__key, const void *__base, size_t __nel,
142 	    size_t __width, int (* _Nonnull __compar)(const void *, const void *));
143 /* calloc is now declared in _malloc.h */
144 div_t	 div(int, int) __pure2;
145 void	 exit(int) __dead2;
146 /* free is now declared in _malloc.h */
147 char	*getenv(const char *);
148 long	 labs(long) __pure2;
149 ldiv_t	 ldiv(long, long) __pure2;
150 #if !__DARWIN_NO_LONG_LONG
151 long long
152 	 llabs(long long);
153 lldiv_t	 lldiv(long long, long long);
154 #endif /* !__DARWIN_NO_LONG_LONG */
155 /* malloc is now declared in _malloc.h */
156 int	 mblen(const char *__s, size_t __n);
157 size_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
158 int	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
159 /* posix_memalign is now declared in _malloc.h */
160 void	 qsort(void *__base, size_t __nel, size_t __width,
161 	    int (* _Nonnull __compar)(const void *, const void *));
162 int	 rand(void) __swift_unavailable("Use arc4random instead.");
163 /* realloc is now declared in _malloc.h */
164 void	 srand(unsigned) __swift_unavailable("Use arc4random instead.");
165 double	 strtod(const char *, char **) __DARWIN_ALIAS(strtod);
166 float	 strtof(const char *, char **) __DARWIN_ALIAS(strtof);
167 long	 strtol(const char *__str, char **__endptr, int __base);
168 long double
169 	 strtold(const char *, char **);
170 #if !__DARWIN_NO_LONG_LONG
171 long long
172 	 strtoll(const char *__str, char **__endptr, int __base);
173 #endif /* !__DARWIN_NO_LONG_LONG */
174 unsigned long
175 	 strtoul(const char *__str, char **__endptr, int __base);
176 #if !__DARWIN_NO_LONG_LONG
177 unsigned long long
178 	 strtoull(const char *__str, char **__endptr, int __base);
179 #endif /* !__DARWIN_NO_LONG_LONG */
180 
181 __swift_unavailable("Use posix_spawn APIs or NSTask instead. (On iOS, process spawning is unavailable.)")
182 __API_AVAILABLE(macos(10.0)) __IOS_PROHIBITED
183 __WATCHOS_PROHIBITED __TVOS_PROHIBITED
184 int	 system(const char *) __DARWIN_ALIAS_C(system);
185 
186 
187 size_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
188 int	 wctomb(char *, wchar_t);
189 
190 #ifndef _ANSI_SOURCE
191 void	_Exit(int) __dead2;
192 long	 a64l(const char *);
193 double	 drand48(void);
194 char	*ecvt(double, int, int *__restrict, int *__restrict); /* LEGACY */
195 double	 erand48(unsigned short[3]);
196 char	*fcvt(double, int, int *__restrict, int *__restrict); /* LEGACY */
197 char	*gcvt(double, int, char *); /* LEGACY */
198 int	 getsubopt(char **, char * const *, char **);
199 int	 grantpt(int);
200 #if __DARWIN_UNIX03
201 char	*initstate(unsigned, char *, size_t); /* no  __DARWIN_ALIAS needed */
202 #else /* !__DARWIN_UNIX03 */
203 char	*initstate(unsigned long, char *, long);
204 #endif /* __DARWIN_UNIX03 */
205 long	 jrand48(unsigned short[3]) __swift_unavailable("Use arc4random instead.");
206 char	*l64a(long);
207 void	 lcong48(unsigned short[7]);
208 long	 lrand48(void) __swift_unavailable("Use arc4random instead.");
209 char	*mktemp(char *);
210 int	 mkstemp(char *);
211 long	 mrand48(void) __swift_unavailable("Use arc4random instead.");
212 long	 nrand48(unsigned short[3]) __swift_unavailable("Use arc4random instead.");
213 int	 posix_openpt(int);
214 char	*ptsname(int);
215 
216 #if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
217 int ptsname_r(int fildes, char *buffer, size_t buflen) __API_AVAILABLE(macos(10.13.4), ios(11.3), tvos(11.3), watchos(4.3));
218 #endif
219 
220 int	 putenv(char *) __DARWIN_ALIAS(putenv);
221 long	 random(void) __swift_unavailable("Use arc4random instead.");
222 int	 rand_r(unsigned *) __swift_unavailable("Use arc4random instead.");
223 #if (__DARWIN_UNIX03 && !defined(_POSIX_C_SOURCE)) || defined(_DARWIN_C_SOURCE) || defined(_DARWIN_BETTER_REALPATH)
224 char	*realpath(const char * __restrict, char * __restrict) __DARWIN_EXTSN(realpath);
225 #else /* (!__DARWIN_UNIX03 || _POSIX_C_SOURCE) && !_DARWIN_C_SOURCE && !_DARWIN_BETTER_REALPATH */
226 char	*realpath(const char * __restrict, char * __restrict) __DARWIN_ALIAS(realpath);
227 #endif /* (__DARWIN_UNIX03 && _POSIX_C_SOURCE) || _DARWIN_C_SOURCE || _DARWIN_BETTER_REALPATH */
228 unsigned short
229 	*seed48(unsigned short[3]);
230 int	 setenv(const char * __name, const char * __value, int __overwrite) __DARWIN_ALIAS(setenv);
231 #if __DARWIN_UNIX03
232 void	 setkey(const char *) __DARWIN_ALIAS(setkey);
233 #else /* !__DARWIN_UNIX03 */
234 int	 setkey(const char *);
235 #endif /* __DARWIN_UNIX03 */
236 char	*setstate(const char *);
237 void	 srand48(long);
238 #if __DARWIN_UNIX03
239 void	 srandom(unsigned);
240 #else /* !__DARWIN_UNIX03 */
241 void	 srandom(unsigned long);
242 #endif /* __DARWIN_UNIX03 */
243 int	 unlockpt(int);
244 #if __DARWIN_UNIX03
245 int	 unsetenv(const char *) __DARWIN_ALIAS(unsetenv);
246 #else /* !__DARWIN_UNIX03 */
247 void	 unsetenv(const char *);
248 #endif /* __DARWIN_UNIX03 */
249 #endif	/* !_ANSI_SOURCE */
250 
251 #if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
252 #include <machine/types.h>
253 #include <sys/_types/_dev_t.h>
254 #include <sys/_types/_mode_t.h>
255 #include <_types/_uint32_t.h>
256 
257 uint32_t arc4random(void);
258 void	 arc4random_addrandom(unsigned char * /*dat*/, int /*datlen*/)
259     __OSX_DEPRECATED(10.0, 10.12, "use arc4random_stir")
260     __IOS_DEPRECATED(2.0, 10.0, "use arc4random_stir")
261     __TVOS_DEPRECATED(2.0, 10.0, "use arc4random_stir")
262     __WATCHOS_DEPRECATED(1.0, 3.0, "use arc4random_stir");
263 void	 arc4random_buf(void * __buf, size_t __nbytes) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
264 void	 arc4random_stir(void);
265 uint32_t
266 	 arc4random_uniform(uint32_t __upper_bound) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
267 #ifdef __BLOCKS__
268 int	 atexit_b(void (^ _Nonnull)(void)) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
269 
270 #ifdef __BLOCKS__
271 #if __has_attribute(noescape)
272 #define __bsearch_noescape __attribute__((__noescape__))
273 #else
274 #define __bsearch_noescape
275 #endif
276 #endif /* __BLOCKS__ */
277 void	*bsearch_b(const void *__key, const void *__base, size_t __nel,
278 	    size_t __width, int (^ _Nonnull __compar)(const void *, const void *) __bsearch_noescape)
279 	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
280 #endif /* __BLOCKS__ */
281 
282 	 /* getcap(3) functions */
283 char	*cgetcap(char *, const char *, int);
284 int	 cgetclose(void);
285 int	 cgetent(char **, char **, const char *);
286 int	 cgetfirst(char **, char **);
287 int	 cgetmatch(const char *, const char *);
288 int	 cgetnext(char **, char **);
289 int	 cgetnum(char *, const char *, long *);
290 int	 cgetset(const char *);
291 int	 cgetstr(char *, const char *, char **);
292 int	 cgetustr(char *, const char *, char **);
293 
294 int	 daemon(int, int) __DARWIN_1050(daemon) __OSX_AVAILABLE_BUT_DEPRECATED_MSG(__MAC_10_0, __MAC_10_5, __IPHONE_2_0, __IPHONE_2_0, "Use posix_spawn APIs instead.") __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
295 char	*devname(dev_t, mode_t);
296 char	*devname_r(dev_t, mode_t, char *buf, int len);
297 char	*getbsize(int *, long *);
298 int	 getloadavg(double [], int);
299 const char
300 	*getprogname(void);
301 void	 setprogname(const char *);
302 
303 #ifdef __BLOCKS__
304 #if __has_attribute(noescape)
305 #define __sort_noescape __attribute__((__noescape__))
306 #else
307 #define __sort_noescape
308 #endif
309 #endif /* __BLOCKS__ */
310 
311 int	 heapsort(void *__base, size_t __nel, size_t __width,
312 	    int (* _Nonnull __compar)(const void *, const void *));
313 #ifdef __BLOCKS__
314 int	 heapsort_b(void *__base, size_t __nel, size_t __width,
315 	    int (^ _Nonnull __compar)(const void *, const void *) __sort_noescape)
316 	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
317 #endif /* __BLOCKS__ */
318 int	 mergesort(void *__base, size_t __nel, size_t __width,
319 	    int (* _Nonnull __compar)(const void *, const void *));
320 #ifdef __BLOCKS__
321 int	 mergesort_b(void *__base, size_t __nel, size_t __width,
322 	    int (^ _Nonnull __compar)(const void *, const void *) __sort_noescape)
323 	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
324 #endif /* __BLOCKS__ */
325 void	 psort(void *__base, size_t __nel, size_t __width,
326 	    int (* _Nonnull __compar)(const void *, const void *))
327 	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
328 #ifdef __BLOCKS__
329 void	 psort_b(void *__base, size_t __nel, size_t __width,
330 	    int (^ _Nonnull __compar)(const void *, const void *) __sort_noescape)
331 	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
332 #endif /* __BLOCKS__ */
333 void	 psort_r(void *__base, size_t __nel, size_t __width, void *,
334 	    int (* _Nonnull __compar)(void *, const void *, const void *))
335 	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
336 #ifdef __BLOCKS__
337 void	 qsort_b(void *__base, size_t __nel, size_t __width,
338 	    int (^ _Nonnull __compar)(const void *, const void *) __sort_noescape)
339 	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
340 #endif /* __BLOCKS__ */
341 void	 qsort_r(void *__base, size_t __nel, size_t __width, void *,
342 	    int (* _Nonnull __compar)(void *, const void *, const void *));
343 int	 radixsort(const unsigned char **__base, int __nel, const unsigned char *__table,
344 	    unsigned __endbyte);
345 int	rpmatch(const char *)
346 	__API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), watchos(6.0));
347 int	 sradixsort(const unsigned char **__base, int __nel, const unsigned char *__table,
348 	    unsigned __endbyte);
349 void	 sranddev(void);
350 void	 srandomdev(void);
351 void	*reallocf(void *__ptr, size_t __size) __alloc_size(2);
352 long long
353 	strtonum(const char *__numstr, long long __minval, long long __maxval, const char **__errstrp)
354 	__API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0), watchos(7.0));
355 #if !__DARWIN_NO_LONG_LONG
356 long long
357 	 strtoq(const char *__str, char **__endptr, int __base);
358 unsigned long long
359 	 strtouq(const char *__str, char **__endptr, int __base);
360 #endif /* !__DARWIN_NO_LONG_LONG */
361 extern char *suboptarg;		/* getsubopt(3) external variable */
362 /* valloc is now declared in _malloc.h */
363 #endif	/* !_ANSI_SOURCE && !_POSIX_SOURCE */
364 
365 /* Poison the following routines if -fshort-wchar is set */
366 #if !defined(__cplusplus) && defined(__WCHAR_MAX__) && __WCHAR_MAX__ <= 0xffffU
367 #pragma GCC poison mbstowcs mbtowc wcstombs wctomb
368 #endif
369 __END_DECLS
370 
371 #ifdef _USE_EXTENDED_LOCALES_
372 #include <xlocale/_stdlib.h>
373 #endif /* _USE_EXTENDED_LOCALES_ */
374 
375 #endif /* _STDLIB_H_ */