1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef _STDLIB_H_ 33 #define _STDLIB_H_ 34 35 #include <sys/cdefs.h> 36 #include <sys/_null.h> 37 #include <sys/_types.h> 38 39 __NULLABILITY_PRAGMA_PUSH 40 41 #ifndef _SIZE_T_DECLARED 42 typedef __size_t size_t; 43 #define _SIZE_T_DECLARED 44 #endif 45 46 #ifndef __cplusplus 47 #ifndef _WCHAR_T_DECLARED 48 typedef ___wchar_t wchar_t; 49 #define _WCHAR_T_DECLARED 50 #endif 51 #endif 52 53 typedef struct { 54 int quot; /* quotient */ 55 int rem; /* remainder */ 56 } div_t; 57 58 typedef struct { 59 long quot; 60 long rem; 61 } ldiv_t; 62 63 #define EXIT_FAILURE 1 64 #define EXIT_SUCCESS 0 65 66 /* 67 * I.e., INT_MAX; rand(3) returns a signed integer but must produce output in 68 * the range [0, RAND_MAX], so half of the possible output range is unused. 69 */ 70 #define RAND_MAX 0x7fffffff 71 72 __BEGIN_DECLS 73 #ifdef _XLOCALE_H_ 74 #include <xlocale/_stdlib.h> 75 #endif 76 extern int __mb_cur_max; 77 extern int ___mb_cur_max(void); 78 #define MB_CUR_MAX ((size_t)___mb_cur_max()) 79 80 _Noreturn void abort(void) __noexcept; 81 int abs(int) __pure2; 82 int atexit(void (* _Nonnull)(void)) __noexcept; 83 double atof(const char *); 84 int atoi(const char *); 85 long atol(const char *); 86 void *bsearch(const void *, const void *, size_t, 87 size_t, int (*)(const void * _Nonnull, const void *)); 88 void *calloc(size_t, size_t) __malloc_like __result_use_check 89 __alloc_size2(1, 2); 90 div_t div(int, int) __pure2; 91 _Noreturn void exit(int); 92 void free(void *); 93 char *getenv(const char *); 94 long labs(long) __pure2; 95 ldiv_t ldiv(long, long) __pure2; 96 void *malloc(size_t) __malloc_like __result_use_check __alloc_size(1); 97 int mblen(const char *, size_t); 98 size_t mbstowcs(wchar_t * __restrict , const char * __restrict, size_t); 99 int mbtowc(wchar_t * __restrict, const char * __restrict, size_t); 100 void qsort(void *, size_t, size_t, 101 int (* _Nonnull)(const void *, const void *)); 102 int rand(void); 103 void *realloc(void *, size_t) __result_use_check __alloc_size(2); 104 void srand(unsigned); 105 double strtod(const char * __restrict, char ** __restrict); 106 float strtof(const char * __restrict, char ** __restrict); 107 long strtol(const char * __restrict, char ** __restrict, int); 108 long double 109 strtold(const char * __restrict, char ** __restrict); 110 unsigned long 111 strtoul(const char * __restrict, char ** __restrict, int); 112 int system(const char *); 113 int wctomb(char *, wchar_t); 114 size_t wcstombs(char * __restrict, const wchar_t * __restrict, size_t); 115 116 /* 117 * Functions added in C99 which we make conditionally available in the 118 * BSD^C89 namespace if the compiler supports `long long'. 119 * The #if test is more complicated than it ought to be because 120 * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long' 121 * is not supported in the compilation environment (which therefore means 122 * that it can't really be ISO C99). 123 * 124 * (The only other extension made by C99 in this header is _Exit().) 125 */ 126 #if __ISO_C_VISIBLE >= 1999 || defined(__cplusplus) 127 #ifdef __LONG_LONG_SUPPORTED 128 /* LONGLONG */ 129 typedef struct { 130 long long quot; 131 long long rem; 132 } lldiv_t; 133 134 /* LONGLONG */ 135 long long 136 atoll(const char *); 137 /* LONGLONG */ 138 long long 139 llabs(long long) __pure2; 140 /* LONGLONG */ 141 lldiv_t lldiv(long long, long long) __pure2; 142 /* LONGLONG */ 143 long long 144 strtoll(const char * __restrict, char ** __restrict, int); 145 /* LONGLONG */ 146 unsigned long long 147 strtoull(const char * __restrict, char ** __restrict, int); 148 #endif /* __LONG_LONG_SUPPORTED */ 149 150 _Noreturn void _Exit(int) __noexcept; 151 #endif /* __ISO_C_VISIBLE >= 1999 */ 152 153 /* 154 * If we're in a mode greater than C99, expose C11 functions. 155 */ 156 #if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L 157 void * aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1) 158 __alloc_size(2); 159 int at_quick_exit(void (*)(void)) __noexcept; 160 _Noreturn void 161 quick_exit(int) /* __noexcept -- not ready ABI issues? */; 162 #endif /* __ISO_C_VISIBLE >= 2011 */ 163 /* 164 * Extensions made by POSIX relative to C. 165 */ 166 #if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE 167 char *realpath(const char * __restrict, char * __restrict); 168 #endif 169 #if __POSIX_VISIBLE >= 199506 170 int rand_r(unsigned *); /* (TSF) */ 171 #endif 172 #if __POSIX_VISIBLE >= 200112 173 int posix_memalign(void **, size_t, size_t); /* (ADV) */ 174 int setenv(const char *, const char *, int); 175 int unsetenv(const char *); 176 #endif 177 178 #if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE 179 int getsubopt(char **, char *const *, char **); 180 #ifndef _MKDTEMP_DECLARED 181 char *mkdtemp(char *); 182 #define _MKDTEMP_DECLARED 183 #endif 184 #ifndef _MKSTEMP_DECLARED 185 int mkstemp(char *); 186 #define _MKSTEMP_DECLARED 187 #endif 188 #endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */ 189 190 /* 191 * The only changes to the XSI namespace in revision 6 were the deletion 192 * of the ttyslot() and valloc() functions, which FreeBSD never declared 193 * in this header. For revision 7, ecvt(), fcvt(), and gcvt(), which 194 * FreeBSD also does not have, and mktemp(), are to be deleted. 195 */ 196 #if __XSI_VISIBLE 197 /* XXX XSI requires pollution from <sys/wait.h> here. We'd rather not. */ 198 long a64l(const char *); 199 double drand48(void); 200 /* char *ecvt(double, int, int * __restrict, int * __restrict); */ 201 double erand48(unsigned short[3]); 202 /* char *fcvt(double, int, int * __restrict, int * __restrict); */ 203 /* char *gcvt(double, int, int * __restrict, int * __restrict); */ 204 char *initstate(unsigned int, char *, size_t); 205 long jrand48(unsigned short[3]); 206 char *l64a(long); 207 void lcong48(unsigned short[7]); 208 long lrand48(void); 209 #if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600) 210 char *mktemp(char *); 211 #define _MKTEMP_DECLARED 212 #endif 213 long mrand48(void); 214 long nrand48(unsigned short[3]); 215 int putenv(char *); 216 long random(void); 217 unsigned short 218 *seed48(unsigned short[3]); 219 char *setstate(/* const */ char *); 220 void srand48(long); 221 void srandom(unsigned int); 222 #endif /* __XSI_VISIBLE */ 223 224 #if __XSI_VISIBLE 225 int grantpt(int); 226 int posix_openpt(int); 227 char *ptsname(int); 228 int unlockpt(int); 229 #endif /* __XSI_VISIBLE */ 230 #if __BSD_VISIBLE 231 /* ptsname_r will be included in POSIX issue 8 */ 232 int ptsname_r(int, char *, size_t); 233 #endif 234 235 #if __BSD_VISIBLE 236 extern const char *malloc_conf; 237 extern void (*malloc_message)(void *, const char *); 238 239 /* 240 * The alloca() function can't be implemented in C, and on some 241 * platforms it can't be implemented at all as a callable function. 242 * The GNU C compiler provides a built-in alloca() which we can use. 243 * On platforms where alloca() is not in libc, programs which use it 244 * will fail to link when compiled with non-GNU compilers. 245 */ 246 #if __GNUC__ >= 2 247 #undef alloca /* some GNU bits try to get cute and define this on their own */ 248 #define alloca(sz) __builtin_alloca(sz) 249 #endif 250 251 void abort2(const char *, int, void **) __dead2; 252 __uint32_t 253 arc4random(void); 254 void arc4random_buf(void *, size_t); 255 __uint32_t 256 arc4random_uniform(__uint32_t); 257 258 #ifdef __BLOCKS__ 259 int atexit_b(void (^ _Nonnull)(void)); 260 void *bsearch_b(const void *, const void *, size_t, 261 size_t, int (^ _Nonnull)(const void *, const void *)); 262 #endif 263 char *getbsize(int *, long *); 264 /* getcap(3) functions */ 265 char *cgetcap(char *, const char *, int); 266 int cgetclose(void); 267 int cgetent(char **, char **, const char *); 268 int cgetfirst(char **, char **); 269 int cgetmatch(const char *, const char *); 270 int cgetnext(char **, char **); 271 int cgetnum(char *, const char *, long *); 272 int cgetset(const char *); 273 int cgetstr(char *, const char *, char **); 274 int cgetustr(char *, const char *, char **); 275 276 int clearenv(void); 277 278 int daemon(int, int); 279 int daemonfd(int, int); 280 char *devname(__dev_t, __mode_t); 281 char *devname_r(__dev_t, __mode_t, char *, int); 282 char *fdevname(int); 283 char *fdevname_r(int, char *, int); 284 int getloadavg(double [], int); 285 const char * 286 getprogname(void); 287 288 int heapsort(void *, size_t, size_t, 289 int (* _Nonnull)(const void *, const void *)); 290 #ifdef __BLOCKS__ 291 int heapsort_b(void *, size_t, size_t, 292 int (^ _Nonnull)(const void *, const void *)); 293 void qsort_b(void *, size_t, size_t, 294 int (^ _Nonnull)(const void *, const void *)); 295 #endif 296 int l64a_r(long, char *, int); 297 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *)); 298 #ifdef __BLOCKS__ 299 int mergesort_b(void *, size_t, size_t, int (^)(const void *, const void *)); 300 #endif 301 int mkostemp(char *, int); 302 int mkostemps(char *, int, int); 303 int mkostempsat(int, char *, int, int); 304 void qsort_r(void *, size_t, size_t, 305 int (*)(const void *, const void *, void *), void *); 306 int radixsort(const unsigned char **, int, const unsigned char *, 307 unsigned); 308 void *reallocarray(void *, size_t, size_t) __result_use_check 309 __alloc_size2(2, 3); 310 void *reallocf(void *, size_t) __result_use_check __alloc_size(2); 311 int rpmatch(const char *); 312 char *secure_getenv(const char *); 313 void setprogname(const char *); 314 int sradixsort(const unsigned char **, int, const unsigned char *, 315 unsigned); 316 void srandomdev(void); 317 long long 318 strtonum(const char *, long long, long long, const char **); 319 320 /* Deprecated interfaces, to be removed. */ 321 __int64_t 322 strtoq(const char *, char **, int); 323 __uint64_t 324 strtouq(const char *, char **, int); 325 326 /* 327 * In FreeBSD 14, the prototype of qsort_r() was modified to comply with 328 * POSIX. The standardized qsort_r()'s order of last two parameters was 329 * changed, and the comparator function is now taking thunk as its last 330 * parameter, and both are different from the ones expected by the historical 331 * FreeBSD qsort_r() interface. 332 * 333 * Apply a workaround where we explicitly link against the historical interface, 334 * qsort_r@FBSD_1.0, in case when qsort_r() is called with the last parameter 335 * with a function pointer that exactly matches the historical FreeBSD qsort_r() 336 * comparator signature, so applications written for the historical interface 337 * can continue to work without modification. Toolchains that don't support 338 * symbol versioning don't define __sym_compat, so only provide this symbol in 339 * supported environments. 340 */ 341 #ifdef __sym_compat 342 #if defined(__generic) || defined(__cplusplus) 343 void __qsort_r_compat(void *, size_t, size_t, void *, 344 int (*)(void *, const void *, const void *)); 345 __sym_compat(qsort_r, __qsort_r_compat, FBSD_1.0); 346 #endif 347 #endif 348 #if defined(__generic) && !defined(__cplusplus) 349 #define qsort_r(base, nel, width, arg4, arg5) \ 350 __generic(arg5, int (*)(void *, const void *, const void *), \ 351 __qsort_r_compat, qsort_r)(base, nel, width, arg4, arg5) 352 #elif defined(__cplusplus) && defined(__sym_compat) 353 __END_DECLS 354 extern "C++" { 355 static inline void qsort_r(void *base, size_t nmemb, size_t size, 356 void *thunk, int (*compar)(void *, const void *, const void *)) { 357 __qsort_r_compat(base, nmemb, size, thunk, compar); 358 } 359 } 360 __BEGIN_DECLS 361 #endif 362 363 extern char *suboptarg; /* getsubopt(3) external variable */ 364 #endif /* __BSD_VISIBLE */ 365 366 #if __EXT1_VISIBLE 367 368 #ifndef _RSIZE_T_DEFINED 369 #define _RSIZE_T_DEFINED 370 typedef size_t rsize_t; 371 #endif 372 373 #ifndef _ERRNO_T_DEFINED 374 #define _ERRNO_T_DEFINED 375 typedef int errno_t; 376 #endif 377 378 /* K.3.6 */ 379 typedef void (*constraint_handler_t)(const char * __restrict, 380 void * __restrict, errno_t); 381 /* K.3.6.1.1 */ 382 constraint_handler_t set_constraint_handler_s(constraint_handler_t handler); 383 /* K.3.6.1.2 */ 384 _Noreturn void abort_handler_s(const char * __restrict, void * __restrict, 385 errno_t); 386 /* K3.6.1.3 */ 387 void ignore_handler_s(const char * __restrict, void * __restrict, errno_t); 388 /* K.3.6.3.2 */ 389 errno_t qsort_s(void *, rsize_t, rsize_t, 390 int (*)(const void *, const void *, void *), void *); 391 #endif /* __EXT1_VISIBLE */ 392 393 __END_DECLS 394 __NULLABILITY_PRAGMA_POP 395 396 #endif /* !_STDLIB_H_ */ 397