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>
62 #include <sys/cdefs.h>
65 #if !defined(_ANSI_SOURCE)
66 #ifndef UNIFDEF_DRIVERKIT
68 #endif /* UNIFDEF_DRIVERKIT */
69 #if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
71 #endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
72 #endif /* !_ANSI_SOURCE */
74 /* DO NOT REMOVE THIS COMMENT: fixincludes needs to see:
76 #include <sys/_types/_size_t.h>
78 #if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
79 #include <sys/_types/_ct_rune_t.h>
80 #include <sys/_types/_rune_t.h>
81 #endif /* !_ANSI_SOURCE && (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
83 #include <sys/_types/_wchar_t.h>
86 int quot
; /* quotient */
87 int rem
; /* remainder */
91 long quot
; /* quotient */
92 long rem
; /* remainder */
95 #if !__DARWIN_NO_LONG_LONG
100 #endif /* !__DARWIN_NO_LONG_LONG */
102 #include <sys/_types/_null.h>
104 #ifndef UNIFDEF_DRIVERKIT
105 #define EXIT_FAILURE 1
106 #define EXIT_SUCCESS 0
107 #endif /* UNIFDEF_DRIVERKIT */
109 #define RAND_MAX 0x7fffffff
111 #ifdef _USE_EXTENDED_LOCALES_
112 #include <_xlocale.h>
113 #endif /* _USE_EXTENDED_LOCALES_ */
116 #ifdef _USE_EXTENDED_LOCALES_
117 #define MB_CUR_MAX (___mb_cur_max())
119 #define MB_CUR_MAX_L(x) (___mb_cur_max_l(x))
120 #endif /* !MB_CUR_MAX_L */
121 #else /* !_USE_EXTENDED_LOCALES_ */
122 extern int __mb_cur_max
;
123 #define MB_CUR_MAX __mb_cur_max
124 #endif /* _USE_EXTENDED_LOCALES_ */
125 #endif /* MB_CUR_MAX */
127 #if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)) \
128 && defined(_USE_EXTENDED_LOCALES_) && !defined(MB_CUR_MAX_L)
129 #define MB_CUR_MAX_L(x) (___mb_cur_max_l(x))
132 #include "libc_private.h"
133 /* f must be a literal string */
134 #define LIBC_ABORT(f,...) abort_report_np("%s:%s:%u: " f, __FILE__, __func__, __LINE__, ## __VA_ARGS__)
137 #ifndef UNIFDEF_DRIVERKIT
138 #include <malloc/_malloc.h>
139 #endif /* UNIFDEF_DRIVERKIT */
142 void abort(void) __cold __dead2
;
143 int abs(int) __pure2
;
144 #ifndef UNIFDEF_DRIVERKIT
145 int atexit(void (* _Nonnull
)(void));
146 #endif /* UNIFDEF_DRIVERKIT */
147 double atof(const char *);
148 int atoi(const char *);
149 long atol(const char *);
150 #if !__DARWIN_NO_LONG_LONG
153 #endif /* !__DARWIN_NO_LONG_LONG */
154 void *bsearch(const void *__key
, const void *__base
, size_t __nel
,
155 size_t __width
, int (* _Nonnull __compar
)(const void *, const void *));
156 #ifndef UNIFDEF_DRIVERKIT
157 /* calloc is now declared in _malloc.h */
158 #endif /* UNIFDEF_DRIVERKIT */
159 div_t div(int, int) __pure2
;
160 #ifndef UNIFDEF_DRIVERKIT
161 void exit(int) __dead2
;
162 /* free is now declared in _malloc.h */
163 char *getenv(const char *);
164 #endif /* UNIFDEF_DRIVERKIT */
165 long labs(long) __pure2
;
166 ldiv_t ldiv(long, long) __pure2
;
167 #if !__DARWIN_NO_LONG_LONG
170 lldiv_t
lldiv(long long, long long);
171 #endif /* !__DARWIN_NO_LONG_LONG */
172 #ifndef UNIFDEF_DRIVERKIT
173 /* malloc is now declared in _malloc.h */
174 #endif /* UNIFDEF_DRIVERKIT */
175 int mblen(const char *__s
, size_t __n
);
176 size_t mbstowcs(wchar_t * __restrict
, const char * __restrict
, size_t);
177 int mbtowc(wchar_t * __restrict
, const char * __restrict
, size_t);
178 #ifndef UNIFDEF_DRIVERKIT
179 /* posix_memalign is now declared in _malloc.h */
180 #endif /* UNIFDEF_DRIVERKIT */
181 void qsort(void *__base
, size_t __nel
, size_t __width
,
182 int (* _Nonnull __compar
)(const void *, const void *));
183 #ifndef UNIFDEF_DRIVERKIT
184 int rand(void) __swift_unavailable("Use arc4random instead.");
185 /* realloc is now declared in _malloc.h */
186 void srand(unsigned) __swift_unavailable("Use arc4random instead.");
187 #endif /* UNIFDEF_DRIVERKIT */
188 double strtod(const char *, char **) __DARWIN_ALIAS(strtod
);
189 float strtof(const char *, char **) __DARWIN_ALIAS(strtof
);
190 long strtol(const char *__str
, char **__endptr
, int __base
);
192 strtold(const char *, char **);
193 #if !__DARWIN_NO_LONG_LONG
195 strtoll(const char *__str
, char **__endptr
, int __base
);
196 #endif /* !__DARWIN_NO_LONG_LONG */
198 strtoul(const char *__str
, char **__endptr
, int __base
);
199 #if !__DARWIN_NO_LONG_LONG
201 strtoull(const char *__str
, char **__endptr
, int __base
);
202 #endif /* !__DARWIN_NO_LONG_LONG */
203 #ifndef UNIFDEF_DRIVERKIT
205 #ifndef LIBC_ALIAS_SYSTEM
208 #if TARGET_OS_EMBEDDED
209 #define __swift_unavailable_on(osx_msg, ios_msg) __swift_unavailable(ios_msg)
211 #define __swift_unavailable_on(osx_msg, ios_msg) __swift_unavailable(osx_msg)
214 __swift_unavailable_on("Use posix_spawn APIs or NSTask instead.", "Process spawning is unavailable")
215 __API_AVAILABLE(macos(10.0)) __IOS_PROHIBITED
216 __WATCHOS_PROHIBITED __TVOS_PROHIBITED
217 int system(const char *) __DARWIN_ALIAS_C(system
);
219 #else /* LIBC_ALIAS_SYSTEM */
220 int system(const char *) LIBC_ALIAS_C(system
);
221 #endif /* !LIBC_ALIAS_SYSTEM */
224 #undef __swift_unavailable_on
225 #endif /* UNIFDEF_DRIVERKIT */
227 size_t wcstombs(char * __restrict
, const wchar_t * __restrict
, size_t);
228 int wctomb(char *, wchar_t);
230 #ifndef UNIFDEF_DRIVERKIT
232 void _Exit(int) __dead2
;
233 long a64l(const char *);
234 double drand48(void);
235 char *ecvt(double, int, int *__restrict
, int *__restrict
); /* LEGACY */
236 double erand48(unsigned short[3]);
237 char *fcvt(double, int, int *__restrict
, int *__restrict
); /* LEGACY */
238 char *gcvt(double, int, char *); /* LEGACY */
239 int getsubopt(char **, char * const *, char **);
242 char *initstate(unsigned, char *, size_t); /* no __DARWIN_ALIAS needed */
243 #else /* !__DARWIN_UNIX03 */
244 char *initstate(unsigned long, char *, long);
245 #endif /* __DARWIN_UNIX03 */
246 long jrand48(unsigned short[3]) __swift_unavailable("Use arc4random instead.");
248 void lcong48(unsigned short[7]);
249 long lrand48(void) __swift_unavailable("Use arc4random instead.");
250 char *mktemp(char *);
252 long mrand48(void) __swift_unavailable("Use arc4random instead.");
253 long nrand48(unsigned short[3]) __swift_unavailable("Use arc4random instead.");
254 int posix_openpt(int);
257 #if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
258 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));
262 #ifndef LIBC_ALIAS_PUTENV
264 int putenv(char *) __DARWIN_ALIAS(putenv
);
266 #else /* LIBC_ALIAS_PUTENV */
267 int putenv(char *) LIBC_ALIAS(putenv
);
268 #endif /* !LIBC_ALIAS_PUTENV */
270 long random(void) __swift_unavailable("Use arc4random instead.");
271 int rand_r(unsigned *) __swift_unavailable("Use arc4random instead.");
274 #ifndef LIBC_ALIAS_REALPATH
275 char *realpath(const char * __restrict
, char * __restrict
) __DARWIN_EXTSN(realpath
);
276 #else /* LIBC_ALIAS_REALPATH */
277 #ifdef VARIANT_DARWINEXTSN
278 char *realpath(const char * __restrict
, char * __restrict
) LIBC_EXTSN(realpath
);
279 #else /* !VARIANT_DARWINEXTSN */
280 char *realpath(const char * __restrict
, char * __restrict
) LIBC_ALIAS(realpath
);
281 #endif /* VARIANT_DARWINEXTSN */
282 #endif /* !LIBC_ALIAS_REALPATH */
283 #else /* !__LIBC__ */
285 #if (__DARWIN_UNIX03 && !defined(_POSIX_C_SOURCE)) || defined(_DARWIN_C_SOURCE) || defined(_DARWIN_BETTER_REALPATH)
286 char *realpath(const char * __restrict
, char * __restrict
) __DARWIN_EXTSN(realpath
);
287 #else /* (!__DARWIN_UNIX03 || _POSIX_C_SOURCE) && !_DARWIN_C_SOURCE && !_DARWIN_BETTER_REALPATH */
288 char *realpath(const char * __restrict
, char * __restrict
) __DARWIN_ALIAS(realpath
);
289 #endif /* (__DARWIN_UNIX03 && _POSIX_C_SOURCE) || _DARWIN_C_SOURCE || _DARWIN_BETTER_REALPATH */
291 #endif /* __LIBC__ */
294 *seed48(unsigned short[3]);
296 #ifndef LIBC_ALIAS_SETENV
298 int setenv(const char * __name
, const char * __value
, int __overwrite
) __DARWIN_ALIAS(setenv
);
300 #else /* LIBC_ALIAS_SETENV */
301 int setenv(const char * __name
, const char * __value
, int __overwrite
) LIBC_ALIAS(setenv
);
302 #endif /* !LIBC_ALIAS_SETENV */
306 #ifndef LIBC_ALIAS_SETKEY
308 void setkey(const char *) __DARWIN_ALIAS(setkey
);
310 #else /* LIBC_ALIAS_SETKEY */
311 void setkey(const char *) LIBC_ALIAS(setkey
);
312 #endif /* !LIBC_ALIAS_SETKEY */
314 #else /* !__DARWIN_UNIX03 */
315 int setkey(const char *);
316 #endif /* __DARWIN_UNIX03 */
317 char *setstate(const char *);
320 void srandom(unsigned);
321 #else /* !__DARWIN_UNIX03 */
322 void srandom(unsigned long);
323 #endif /* __DARWIN_UNIX03 */
327 #ifndef LIBC_ALIAS_UNSETENV
329 int unsetenv(const char *) __DARWIN_ALIAS(unsetenv
);
331 #else /* LIBC_ALIAS_UNSETENV */
332 int unsetenv(const char *) LIBC_ALIAS(unsetenv
);
333 #endif /* !LIBC_ALIAS_UNSETENV */
335 #else /* !__DARWIN_UNIX03 */
336 void unsetenv(const char *);
337 #endif /* __DARWIN_UNIX03 */
338 #endif /* !_ANSI_SOURCE */
339 #endif /* UNIFDEF_DRIVERKIT */
341 #if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
342 #include <machine/types.h>
343 #ifndef UNIFDEF_DRIVERKIT
344 #include <sys/_types/_dev_t.h>
345 #include <sys/_types/_mode_t.h>
346 #endif /* UNIFDEF_DRIVERKIT */
347 #include <_types/_uint32_t.h>
349 uint32_t arc4random(void);
350 #ifndef UNIFDEF_DRIVERKIT
351 void arc4random_addrandom(unsigned char * /*dat*/, int /*datlen*/)
352 __OSX_DEPRECATED(10.0, 10.12, "use arc4random_stir")
353 __IOS_DEPRECATED(2.0, 10.0, "use arc4random_stir")
354 __TVOS_DEPRECATED(2.0, 10.0, "use arc4random_stir")
355 __WATCHOS_DEPRECATED(1.0, 3.0, "use arc4random_stir");
356 #endif /* UNIFDEF_DRIVERKIT */
357 void arc4random_buf(void * __buf
, size_t __nbytes
) __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_4_3
);
358 void arc4random_stir(void);
360 arc4random_uniform(uint32_t __upper_bound
) __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_4_3
);
362 #ifndef UNIFDEF_DRIVERKIT
363 int atexit_b(void (^ _Nonnull
)(void)) __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
364 #endif /* UNIFDEF_DRIVERKIT */
365 void *bsearch_b(const void *__key
, const void *__base
, size_t __nel
,
366 size_t __width
, int (^ _Nonnull __compar
)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
367 #endif /* __BLOCKS__ */
369 #ifndef UNIFDEF_DRIVERKIT
370 /* getcap(3) functions */
371 char *cgetcap(char *, const char *, int);
373 int cgetent(char **, char **, const char *);
374 int cgetfirst(char **, char **);
375 int cgetmatch(const char *, const char *);
376 int cgetnext(char **, char **);
377 int cgetnum(char *, const char *, long *);
378 int cgetset(const char *);
379 int cgetstr(char *, const char *, char **);
380 int cgetustr(char *, const char *, char **);
382 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
;
383 char *devname(dev_t
, mode_t
);
384 char *devname_r(dev_t
, mode_t
, char *buf
, int len
);
385 char *getbsize(int *, long *);
386 int getloadavg(double [], int);
389 void setprogname(const char *);
390 #endif /* UNIFDEF_DRIVERKIT */
393 #if __has_attribute(noescape)
394 #define __sort_noescape __attribute__((__noescape__))
396 #define __sort_noescape
398 #endif /* __BLOCKS__ */
400 int heapsort(void *__base
, size_t __nel
, size_t __width
,
401 int (* _Nonnull __compar
)(const void *, const void *));
403 int heapsort_b(void *__base
, size_t __nel
, size_t __width
,
404 int (^ _Nonnull __compar
)(const void *, const void *) __sort_noescape
)
405 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
406 #endif /* __BLOCKS__ */
407 int mergesort(void *__base
, size_t __nel
, size_t __width
,
408 int (* _Nonnull __compar
)(const void *, const void *));
410 int mergesort_b(void *__base
, size_t __nel
, size_t __width
,
411 int (^ _Nonnull __compar
)(const void *, const void *) __sort_noescape
)
412 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
413 #endif /* __BLOCKS__ */
414 #ifndef UNIFDEF_DRIVERKIT
415 void psort(void *__base
, size_t __nel
, size_t __width
,
416 int (* _Nonnull __compar
)(const void *, const void *))
417 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
419 void psort_b(void *__base
, size_t __nel
, size_t __width
,
420 int (^ _Nonnull __compar
)(const void *, const void *) __sort_noescape
)
421 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
422 #endif /* __BLOCKS__ */
423 void psort_r(void *__base
, size_t __nel
, size_t __width
, void *,
424 int (* _Nonnull __compar
)(void *, const void *, const void *))
425 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
426 #endif /* UNIFDEF_DRIVERKIT */
428 void qsort_b(void *__base
, size_t __nel
, size_t __width
,
429 int (^ _Nonnull __compar
)(const void *, const void *) __sort_noescape
)
430 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_3_2
);
431 #endif /* __BLOCKS__ */
432 void qsort_r(void *__base
, size_t __nel
, size_t __width
, void *,
433 int (* _Nonnull __compar
)(void *, const void *, const void *));
434 int radixsort(const unsigned char **__base
, int __nel
, const unsigned char *__table
,
436 int rpmatch(const char *)
437 __API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), watchos(6.0));
438 int sradixsort(const unsigned char **__base
, int __nel
, const unsigned char *__table
,
440 #ifndef UNIFDEF_DRIVERKIT
442 void srandomdev(void);
443 void *reallocf(void *__ptr
, size_t __size
) __alloc_size(2);
444 #endif /* UNIFDEF_DRIVERKIT */
445 #if !__DARWIN_NO_LONG_LONG
447 strtoq(const char *__str
, char **__endptr
, int __base
);
449 strtouq(const char *__str
, char **__endptr
, int __base
);
450 #endif /* !__DARWIN_NO_LONG_LONG */
451 #ifndef UNIFDEF_DRIVERKIT
452 extern char *suboptarg
; /* getsubopt(3) external variable */
453 /* valloc is now declared in _malloc.h */
454 #endif /* UNIFDEF_DRIVERKIT */
455 #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
457 /* Poison the following routines if -fshort-wchar is set */
458 #if !defined(__cplusplus) && defined(__WCHAR_MAX__) && __WCHAR_MAX__ <= 0xffffU
459 #pragma GCC poison mbstowcs mbtowc wcstombs wctomb
463 #ifdef _USE_EXTENDED_LOCALES_
464 #include <xlocale/_stdlib.h>
465 #endif /* _USE_EXTENDED_LOCALES_ */
467 #endif /* _STDLIB_H_ */