2 * Copyright (c) 2000, 2002 - 2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1990, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
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.
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
55 * @(#)stdlib.h 8.5 (Berkeley) 5/19/95
61 #include <Availability.h>
64 #if !defined(_ANSI_SOURCE)
66 #if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
68 #endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
69 #endif /* !_ANSI_SOURCE */
71 /* DO NOT REMOVE THIS COMMENT: fixincludes needs to see:
73 #include <sys/_types/_size_t.h>
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) */
80 #include <sys/_types/_wchar_t.h>
83 int quot
; /* quotient */
84 int rem
; /* remainder */
88 long quot
; /* quotient */
89 long rem
; /* remainder */
92 #if !__DARWIN_NO_LONG_LONG
97 #endif /* !__DARWIN_NO_LONG_LONG */
99 #include <sys/_types/_null.h>
101 #define EXIT_FAILURE 1
102 #define EXIT_SUCCESS 0
104 #define RAND_MAX 0x7fffffff
106 #ifdef _USE_EXTENDED_LOCALES_
107 #include <_xlocale.h>
108 #endif /* _USE_EXTENDED_LOCALES_ */
111 #ifdef _USE_EXTENDED_LOCALES_
112 #define MB_CUR_MAX (___mb_cur_max())
114 #define MB_CUR_MAX_L(x) (___mb_cur_max_l(x))
115 #endif /* !MB_CUR_MAX_L */
116 #else /* !_USE_EXTENDED_LOCALES_ */
117 extern int __mb_cur_max
;
118 #define MB_CUR_MAX __mb_cur_max
119 #endif /* _USE_EXTENDED_LOCALES_ */
120 #endif /* MB_CUR_MAX */
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))
127 #include "libc_private.h"
128 /* f must be a literal string */
129 #define LIBC_ABORT(f,...) abort_report_np("%s:%s:%u: " f, __FILE__, __func__, __LINE__, ## __VA_ARGS__)
133 #if __has_attribute(alloc_size)
134 #define __alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
136 #define __alloc_size(...)
138 #endif // __alloc_size
141 void abort(void) __dead2
;
142 int abs(int) __pure2
;
143 int atexit(void (* _Nonnull
)(void));
144 double atof(const char *);
145 int atoi(const char *);
146 long atol(const char *);
147 #if !__DARWIN_NO_LONG_LONG
150 #endif /* !__DARWIN_NO_LONG_LONG */
151 void *bsearch(const void *__key
, const void *__base
, size_t __nel
,
152 size_t __width
, int (* _Nonnull __compar
)(const void *, const void *));
153 void *calloc(size_t __count
, size_t __size
) __result_use_check
__alloc_size(1,2);
154 div_t div(int, int) __pure2
;
155 void exit(int) __dead2
;
157 char *getenv(const char *);
158 long labs(long) __pure2
;
159 ldiv_t ldiv(long, long) __pure2
;
160 #if !__DARWIN_NO_LONG_LONG
163 lldiv_t
lldiv(long long, long long);
164 #endif /* !__DARWIN_NO_LONG_LONG */
165 void *malloc(size_t __size
) __result_use_check
__alloc_size(1);
166 int mblen(const char *__s
, size_t __n
);
167 size_t mbstowcs(wchar_t * __restrict
, const char * __restrict
, size_t);
168 int mbtowc(wchar_t * __restrict
, const char * __restrict
, size_t);
169 int posix_memalign(void **__memptr
, size_t __alignment
, size_t __size
) __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_0
);
170 void qsort(void *__base
, size_t __nel
, size_t __width
,
171 int (* _Nonnull __compar
)(const void *, const void *));
172 int rand(void) __swift_unavailable("Use arc4random instead.");
173 void *realloc(void *__ptr
, size_t __size
) __result_use_check
__alloc_size(2);
174 void srand(unsigned) __swift_unavailable("Use arc4random instead.");
175 double strtod(const char *, char **) __DARWIN_ALIAS(strtod
);
176 float strtof(const char *, char **) __DARWIN_ALIAS(strtof
);
177 long strtol(const char *__str
, char **__endptr
, int __base
);
179 strtold(const char *, char **);
180 #if !__DARWIN_NO_LONG_LONG
182 strtoll(const char *__str
, char **__endptr
, int __base
);
183 #endif /* !__DARWIN_NO_LONG_LONG */
185 strtoul(const char *__str
, char **__endptr
, int __base
);
186 #if !__DARWIN_NO_LONG_LONG
188 strtoull(const char *__str
, char **__endptr
, int __base
);
189 #endif /* !__DARWIN_NO_LONG_LONG */
191 #ifndef LIBC_ALIAS_SYSTEM
194 #if TARGET_OS_EMBEDDED
195 #define __swift_unavailable_on(osx_msg, ios_msg) __swift_unavailable(ios_msg)
197 #define __swift_unavailable_on(osx_msg, ios_msg) __swift_unavailable(osx_msg)
200 __swift_unavailable_on("Use posix_spawn APIs or NSTask instead.", "Process spawning is unavailable")
201 __API_AVAILABLE(macos(10.0)) __IOS_PROHIBITED
202 __WATCHOS_PROHIBITED __TVOS_PROHIBITED
203 int system(const char *) __DARWIN_ALIAS_C(system
);
205 #else /* LIBC_ALIAS_SYSTEM */
206 int system(const char *) LIBC_ALIAS_C(system
);
207 #endif /* !LIBC_ALIAS_SYSTEM */
210 #undef __swift_unavailable_on
212 size_t wcstombs(char * __restrict
, const wchar_t * __restrict
, size_t);
213 int wctomb(char *, wchar_t);
216 void _Exit(int) __dead2
;
217 long a64l(const char *);
218 double drand48(void);
219 char *ecvt(double, int, int *__restrict
, int *__restrict
); /* LEGACY */
220 double erand48(unsigned short[3]);
221 char *fcvt(double, int, int *__restrict
, int *__restrict
); /* LEGACY */
222 char *gcvt(double, int, char *); /* LEGACY */
223 int getsubopt(char **, char * const *, char **);
226 char *initstate(unsigned, char *, size_t); /* no __DARWIN_ALIAS needed */
227 #else /* !__DARWIN_UNIX03 */
228 char *initstate(unsigned long, char *, long);
229 #endif /* __DARWIN_UNIX03 */
230 long jrand48(unsigned short[3]) __swift_unavailable("Use arc4random instead.");
232 void lcong48(unsigned short[7]);
233 long lrand48(void) __swift_unavailable("Use arc4random instead.");
234 char *mktemp(char *);
236 long mrand48(void) __swift_unavailable("Use arc4random instead.");
237 long nrand48(unsigned short[3]) __swift_unavailable("Use arc4random instead.");
238 int posix_openpt(int);
241 #if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
242 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));
246 #ifndef LIBC_ALIAS_PUTENV
248 int putenv(char *) __DARWIN_ALIAS(putenv
);
250 #else /* LIBC_ALIAS_PUTENV */
251 int putenv(char *) LIBC_ALIAS(putenv
);
252 #endif /* !LIBC_ALIAS_PUTENV */
254 long random(void) __swift_unavailable("Use arc4random instead.");
255 int rand_r(unsigned *) __swift_unavailable("Use arc4random instead.");
258 #ifndef LIBC_ALIAS_REALPATH
259 char *realpath(const char * __restrict
, char * __restrict
) __DARWIN_EXTSN(realpath
);
260 #else /* LIBC_ALIAS_REALPATH */
261 #ifdef VARIANT_DARWINEXTSN
262 char *realpath(const char * __restrict
, char * __restrict
) LIBC_EXTSN(realpath
);
263 #else /* !VARIANT_DARWINEXTSN */
264 char *realpath(const char * __restrict
, char * __restrict
) LIBC_ALIAS(realpath
);
265 #endif /* VARIANT_DARWINEXTSN */
266 #endif /* !LIBC_ALIAS_REALPATH */
267 #else /* !__LIBC__ */
269 #if (__DARWIN_UNIX03 && !defined(_POSIX_C_SOURCE)) || defined(_DARWIN_C_SOURCE) || defined(_DARWIN_BETTER_REALPATH)
270 char *realpath(const char * __restrict
, char * __restrict
) __DARWIN_EXTSN(realpath
);
271 #else /* (!__DARWIN_UNIX03 || _POSIX_C_SOURCE) && !_DARWIN_C_SOURCE && !_DARWIN_BETTER_REALPATH */
272 char *realpath(const char * __restrict
, char * __restrict
) __DARWIN_ALIAS(realpath
);
273 #endif /* (__DARWIN_UNIX03 && _POSIX_C_SOURCE) || _DARWIN_C_SOURCE || _DARWIN_BETTER_REALPATH */
275 #endif /* __LIBC__ */
278 *seed48(unsigned short[3]);
280 #ifndef LIBC_ALIAS_SETENV
282 int setenv(const char * __name
, const char * __value
, int __overwrite
) __DARWIN_ALIAS(setenv
);
284 #else /* LIBC_ALIAS_SETENV */
285 int setenv(const char * __name
, const char * __value
, int __overwrite
) LIBC_ALIAS(setenv
);
286 #endif /* !LIBC_ALIAS_SETENV */
290 #ifndef LIBC_ALIAS_SETKEY
292 void setkey(const char *) __DARWIN_ALIAS(setkey
);
294 #else /* LIBC_ALIAS_SETKEY */
295 void setkey(const char *) LIBC_ALIAS(setkey
);
296 #endif /* !LIBC_ALIAS_SETKEY */
298 #else /* !__DARWIN_UNIX03 */
299 int setkey(const char *);
300 #endif /* __DARWIN_UNIX03 */
301 char *setstate(const char *);
304 void srandom(unsigned);
305 #else /* !__DARWIN_UNIX03 */
306 void srandom(unsigned long);
307 #endif /* __DARWIN_UNIX03 */
311 #ifndef LIBC_ALIAS_UNSETENV
313 int unsetenv(const char *) __DARWIN_ALIAS(unsetenv
);
315 #else /* LIBC_ALIAS_UNSETENV */
316 int unsetenv(const char *) LIBC_ALIAS(unsetenv
);
317 #endif /* !LIBC_ALIAS_UNSETENV */
319 #else /* !__DARWIN_UNIX03 */
320 void unsetenv(const char *);
321 #endif /* __DARWIN_UNIX03 */
322 #endif /* !_ANSI_SOURCE */
324 #if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
325 #include <machine/types.h>
327 #include <sys/_types/_dev_t.h>
328 #include <sys/_types/_mode_t.h>
329 #include <_types/_uint32_t.h>
331 uint32_t arc4random(void);
332 void arc4random_addrandom(unsigned char * /*dat*/, int /*datlen*/)
333 __OSX_DEPRECATED(10.0, 10.12, "use arc4random_stir")
334 __IOS_DEPRECATED(2.0, 10.0, "use arc4random_stir")
335 __TVOS_DEPRECATED(2.0, 10.0, "use arc4random_stir")
336 __WATCHOS_DEPRECATED(1.0, 3.0, "use arc4random_stir");
337 void arc4random_buf(void * __buf
, size_t __nbytes
) __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_4_3
);
338 void arc4random_stir(void);
340 arc4random_uniform(uint32_t __upper_bound
) __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_4_3
);
342 int atexit_b(void (^ _Nonnull
)(void)) __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
343 void *bsearch_b(const void *__key
, const void *__base
, size_t __nel
,
344 size_t __width
, int (^ _Nonnull __compar
)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
345 #endif /* __BLOCKS__ */
347 /* getcap(3) functions */
348 char *cgetcap(char *, const char *, int);
350 int cgetent(char **, char **, const char *);
351 int cgetfirst(char **, char **);
352 int cgetmatch(const char *, const char *);
353 int cgetnext(char **, char **);
354 int cgetnum(char *, const char *, long *);
355 int cgetset(const char *);
356 int cgetstr(char *, const char *, char **);
357 int cgetustr(char *, const char *, char **);
359 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
;
360 char *devname(dev_t
, mode_t
);
361 char *devname_r(dev_t
, mode_t
, char *buf
, int len
);
362 char *getbsize(int *, long *);
363 int getloadavg(double [], int);
367 int heapsort(void *__base
, size_t __nel
, size_t __width
,
368 int (* _Nonnull __compar
)(const void *, const void *));
370 int heapsort_b(void *__base
, size_t __nel
, size_t __width
,
371 int (^ _Nonnull __compar
)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
372 #endif /* __BLOCKS__ */
373 int mergesort(void *__base
, size_t __nel
, size_t __width
,
374 int (* _Nonnull __compar
)(const void *, const void *));
376 int mergesort_b(void *__base
, size_t __nel
, size_t __width
,
377 int (^ _Nonnull __compar
)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
378 #endif /* __BLOCKS__ */
379 void psort(void *__base
, size_t __nel
, size_t __width
,
380 int (* _Nonnull __compar
)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
382 void psort_b(void *__base
, size_t __nel
, size_t __width
,
383 int (^ _Nonnull __compar
)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
384 #endif /* __BLOCKS__ */
385 void psort_r(void *__base
, size_t __nel
, size_t __width
, void *,
386 int (* _Nonnull __compar
)(void *, const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
388 void qsort_b(void *__base
, size_t __nel
, size_t __width
,
389 int (^ _Nonnull __compar
)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
390 #endif /* __BLOCKS__ */
391 void qsort_r(void *__base
, size_t __nel
, size_t __width
, void *,
392 int (* _Nonnull __compar
)(void *, const void *, const void *));
393 int radixsort(const unsigned char **__base
, int __nel
, const unsigned char *__table
,
395 void setprogname(const char *);
396 int sradixsort(const unsigned char **__base
, int __nel
, const unsigned char *__table
,
399 void srandomdev(void);
400 void *reallocf(void *__ptr
, size_t __size
) __alloc_size(2);
401 #if !__DARWIN_NO_LONG_LONG
403 strtoq(const char *__str
, char **__endptr
, int __base
);
405 strtouq(const char *__str
, char **__endptr
, int __base
);
406 #endif /* !__DARWIN_NO_LONG_LONG */
407 extern char *suboptarg
; /* getsubopt(3) external variable */
408 void *valloc(size_t) __alloc_size(1);
409 #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
411 /* Poison the following routines if -fshort-wchar is set */
412 #if !defined(__cplusplus) && defined(__WCHAR_MAX__) && __WCHAR_MAX__ <= 0xffffU
413 #pragma GCC poison mbstowcs mbtowc wcstombs wctomb
417 #ifdef _USE_EXTENDED_LOCALES_
418 #include <xlocale/_stdlib.h>
419 #endif /* _USE_EXTENDED_LOCALES_ */
421 #endif /* _STDLIB_H_ */