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