1*dfc5f6feSguenther /* $OpenBSD: stdlib.h,v 1.78 2024/08/03 20:09:24 guenther Exp $ */ 2336c580aSderaadt /* $NetBSD: stdlib.h,v 1.25 1995/12/27 21:19:08 jtc Exp $ */ 3df930be7Sderaadt 4df930be7Sderaadt /*- 5df930be7Sderaadt * Copyright (c) 1990 The Regents of the University of California. 6df930be7Sderaadt * All rights reserved. 7df930be7Sderaadt * 8df930be7Sderaadt * Redistribution and use in source and binary forms, with or without 9df930be7Sderaadt * modification, are permitted provided that the following conditions 10df930be7Sderaadt * are met: 11df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright 12df930be7Sderaadt * notice, this list of conditions and the following disclaimer. 13df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright 14df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the 15df930be7Sderaadt * documentation and/or other materials provided with the distribution. 16e33d3bd3Smillert * 3. Neither the name of the University nor the names of its contributors 17df930be7Sderaadt * may be used to endorse or promote products derived from this software 18df930be7Sderaadt * without specific prior written permission. 19df930be7Sderaadt * 20df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30df930be7Sderaadt * SUCH DAMAGE. 31df930be7Sderaadt * 32df930be7Sderaadt * @(#)stdlib.h 5.13 (Berkeley) 6/4/91 33df930be7Sderaadt */ 34df930be7Sderaadt 35df930be7Sderaadt #ifndef _STDLIB_H_ 36df930be7Sderaadt #define _STDLIB_H_ 37df930be7Sderaadt 3852a0e603Smillert #include <sys/cdefs.h> 396ecde746Smillert #include <sys/_null.h> 40c916d948Smillert #include <machine/_types.h> 41c916d948Smillert #if __BSD_VISIBLE /* for quad_t, etc. (XXX - use protected types) */ 42df930be7Sderaadt #include <sys/types.h> 43df930be7Sderaadt #endif 44df930be7Sderaadt 45c916d948Smillert #ifndef _SIZE_T_DEFINED_ 46c916d948Smillert #define _SIZE_T_DEFINED_ 47c916d948Smillert typedef __size_t size_t; 48df930be7Sderaadt #endif 49df930be7Sderaadt 50c8fdc41eSespie /* in C++, wchar_t is a built-in type */ 51c916d948Smillert #if !defined(_WCHAR_T_DEFINED_) && !defined(__cplusplus) 52c916d948Smillert #define _WCHAR_T_DEFINED_ 53c916d948Smillert typedef __wchar_t wchar_t; 54df930be7Sderaadt #endif 55df930be7Sderaadt 56df930be7Sderaadt typedef struct { 57df930be7Sderaadt int quot; /* quotient */ 58df930be7Sderaadt int rem; /* remainder */ 59df930be7Sderaadt } div_t; 60df930be7Sderaadt 61df930be7Sderaadt typedef struct { 62df930be7Sderaadt long quot; /* quotient */ 63df930be7Sderaadt long rem; /* remainder */ 64df930be7Sderaadt } ldiv_t; 65df930be7Sderaadt 66aa522acbSmillert #if __ISO_C_VISIBLE >= 1999 67aa522acbSmillert typedef struct { 68aa522acbSmillert long long quot; /* quotient */ 69aa522acbSmillert long long rem; /* remainder */ 70aa522acbSmillert } lldiv_t; 71aa522acbSmillert #endif 72aa522acbSmillert 7352a0e603Smillert #if __BSD_VISIBLE 74df930be7Sderaadt typedef struct { 75df930be7Sderaadt quad_t quot; /* quotient */ 76df930be7Sderaadt quad_t rem; /* remainder */ 77df930be7Sderaadt } qdiv_t; 78df930be7Sderaadt #endif 79df930be7Sderaadt 80df930be7Sderaadt #define EXIT_FAILURE 1 81df930be7Sderaadt #define EXIT_SUCCESS 0 82df930be7Sderaadt 83df930be7Sderaadt #define RAND_MAX 0x7fffffff 84df930be7Sderaadt 853a628b46Sschwarze #define MB_CUR_MAX __mb_cur_max() 86df930be7Sderaadt 870028afb6Smillert /* 880028afb6Smillert * Some header files may define an abs macro. 890028afb6Smillert * If defined, undef it to prevent a syntax error and issue a warning. 900028afb6Smillert */ 910028afb6Smillert #ifdef abs 920028afb6Smillert #undef abs 930028afb6Smillert #warning abs macro collides with abs() prototype, undefining 940028afb6Smillert #endif 950028afb6Smillert 96df930be7Sderaadt __BEGIN_DECLS 97c72b5b24Smillert __dead void abort(void); 98c72b5b24Smillert int abs(int); 99f3c3a9c6Smillert int atexit(void (*)(void)); 100c72b5b24Smillert double atof(const char *); 101c72b5b24Smillert int atoi(const char *); 102c72b5b24Smillert long atol(const char *); 103f3c3a9c6Smillert void *bsearch(const void *, const void *, size_t, size_t, 104f3c3a9c6Smillert int (*)(const void *, const void *)); 105c72b5b24Smillert void *calloc(size_t, size_t); 106c72b5b24Smillert div_t div(int, int); 107c72b5b24Smillert __dead void exit(int); 1085f37d0e7Smillert __dead void _Exit(int); 109c72b5b24Smillert void free(void *); 110c72b5b24Smillert char *getenv(const char *); 111c72b5b24Smillert long labs(long); 112c72b5b24Smillert ldiv_t ldiv(long, long); 113c72b5b24Smillert void *malloc(size_t); 114e1603b91Sderaadt #if __BSD_VISIBLE 1156a20801cSmillert void freezero(void *, size_t) 1166a20801cSmillert __attribute__ ((__bounded__(__buffer__,1,2))); 117e03a3151Sotto void *calloc_conceal(size_t, size_t); 118e03a3151Sotto void *malloc_conceal(size_t); 11914537fffSotto void *recallocarray(void *, size_t, size_t, size_t); 120e1603b91Sderaadt #endif /* __BSD_VISIBLE */ 121f3c3a9c6Smillert void qsort(void *, size_t, size_t, int (*)(const void *, const void *)); 122c72b5b24Smillert int rand(void); 123c72b5b24Smillert void *realloc(void *, size_t); 124c72b5b24Smillert void srand(unsigned); 125f7510a6eSderaadt void srand_deterministic(unsigned); 126e58af031Stedu double strtod(const char *__restrict, char **__restrict); 127e58af031Stedu float strtof(const char *__restrict, char **__restrict); 128e58af031Stedu long strtol(const char *__restrict, char **__restrict, int); 1297b36286aSmartynas long double 130e58af031Stedu strtold(const char *__restrict, char **__restrict); 131df930be7Sderaadt unsigned long 132e58af031Stedu strtoul(const char *__restrict, char **__restrict, int); 133c72b5b24Smillert int system(const char *); 134df930be7Sderaadt 1353a628b46Sschwarze size_t __mb_cur_max(void); 136c72b5b24Smillert int mblen(const char *, size_t); 137c72b5b24Smillert size_t mbstowcs(wchar_t *, const char *, size_t); 138c72b5b24Smillert int wctomb(char *, wchar_t); 139c72b5b24Smillert int mbtowc(wchar_t *, const char *, size_t); 140c72b5b24Smillert size_t wcstombs(char *, const wchar_t *, size_t); 141df930be7Sderaadt 14252a0e603Smillert /* 14352a0e603Smillert * IEEE Std 1003.1c-95, also adopted by X/Open CAE Spec Issue 5 Version 2 14452a0e603Smillert */ 145b10cd188Sguenther #if __BSD_VISIBLE || __POSIX_VISIBLE >= 199506 || defined(_REENTRANT) 14652a0e603Smillert int rand_r(unsigned int *); 14752a0e603Smillert #endif 14852a0e603Smillert 14952a0e603Smillert #if __BSD_VISIBLE || __XPG_VISIBLE >= 400 15052a0e603Smillert double drand48(void); 15152a0e603Smillert double erand48(unsigned short[3]); 15252a0e603Smillert long jrand48(unsigned short[3]); 15352a0e603Smillert void lcong48(unsigned short[7]); 154f7510a6eSderaadt void lcong48_deterministic(unsigned short[7]); 15552a0e603Smillert long lrand48(void); 15652a0e603Smillert long mrand48(void); 15752a0e603Smillert long nrand48(unsigned short[3]); 15852a0e603Smillert unsigned short *seed48(unsigned short[3]); 159f7510a6eSderaadt unsigned short *seed48_deterministic(unsigned short[3]); 16052a0e603Smillert void srand48(long); 161f7510a6eSderaadt void srand48_deterministic(long); 16252a0e603Smillert 163471b62eeSmillert int putenv(char *); 16452a0e603Smillert #endif 16552a0e603Smillert 166b10cd188Sguenther /* 167b10cd188Sguenther * XSI functions marked LEGACY in IEEE Std 1003.1-2001 (POSIX) and 168b10cd188Sguenther * removed in IEEE Std 1003.1-2008 169b10cd188Sguenther */ 170b10cd188Sguenther #if __BSD_VISIBLE || __XPG_VISIBLE < 700 171b10cd188Sguenther char *ecvt(double, int, int *, int *); 172b10cd188Sguenther char *fcvt(double, int, int *, int *); 173b10cd188Sguenther char *gcvt(double, int, char *); 174b10cd188Sguenther #if __BSD_VISIBLE || __XPG_VISIBLE >= 420 175b10cd188Sguenther char *mktemp(char *); 176b10cd188Sguenther #endif 177b10cd188Sguenther #endif /* __BSD_VISIBLE || __XPG_VISIBLE < 700 */ 178b10cd188Sguenther 17952a0e603Smillert #if __BSD_VISIBLE || __XPG_VISIBLE >= 420 18052a0e603Smillert long a64l(const char *); 18152a0e603Smillert char *l64a(long); 18252a0e603Smillert 18352a0e603Smillert char *initstate(unsigned int, char *, size_t) 18452a0e603Smillert __attribute__((__bounded__ (__string__,2,3))); 18552a0e603Smillert long random(void); 186b10cd188Sguenther char *setstate(char *); 18752a0e603Smillert void srandom(unsigned int); 188f7510a6eSderaadt void srandom_deterministic(unsigned int); 18952a0e603Smillert 1900ad647d6Smartynas char *realpath(const char *, char *) 1910ad647d6Smartynas __attribute__((__bounded__ (__minbytes__,2,1024))); 19252a0e603Smillert 193b10cd188Sguenther /* 194b10cd188Sguenther * XSI functions marked LEGACY in XPG5 and removed in IEEE Std 1003.1-2001 195b10cd188Sguenther */ 196b10cd188Sguenther #if __BSD_VISIBLE || __XPG_VISIBLE < 600 19752a0e603Smillert int ttyslot(void); 19852a0e603Smillert void *valloc(size_t); /* obsoleted by malloc() */ 199b10cd188Sguenther #endif 20052a0e603Smillert #endif /* __BSD_VISIBLE || __XPG_VISIBLE >= 420 */ 20152a0e603Smillert 20252a0e603Smillert /* 203b10cd188Sguenther * 4.4BSD, then XSI in XPG4.2, then added to POSIX base in IEEE Std 1003.1-2008 204b10cd188Sguenther */ 205b10cd188Sguenther #if __BSD_VISIBLE || __XPG_VISIBLE >= 420 || __POSIX_VISIBLE >= 200809 206b10cd188Sguenther int mkstemp(char *); 207b10cd188Sguenther #endif 208b10cd188Sguenther 209b10cd188Sguenther /* 21052a0e603Smillert * ISO C99 21152a0e603Smillert */ 212aa522acbSmillert #if __ISO_C_VISIBLE >= 1999 21352a0e603Smillert long long 21452a0e603Smillert atoll(const char *); 21552a0e603Smillert long long 21652a0e603Smillert llabs(long long); 2172994e40dSdjm lldiv_t 2182994e40dSdjm lldiv(long long, long long); 21952a0e603Smillert long long 220e58af031Stedu strtoll(const char *__restrict, char **__restrict, int); 22152a0e603Smillert unsigned long long 222e58af031Stedu strtoull(const char *__restrict, char **__restrict, int); 22352a0e603Smillert #endif 22452a0e603Smillert 2257a9442ebSotto #if __ISO_C_VISIBLE >= 2011 2267a9442ebSotto void * 2277a9442ebSotto aligned_alloc(size_t, size_t); 2287a9442ebSotto #endif 2297a9442ebSotto 23052a0e603Smillert /* 23152a0e603Smillert * The Open Group Base Specifications, Issue 6; IEEE Std 1003.1-2001 (POSIX) 23252a0e603Smillert */ 233b10cd188Sguenther #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112 234b10cd188Sguenther int posix_memalign(void **, size_t, size_t); 23552a0e603Smillert int setenv(const char *, const char *, int); 236471b62eeSmillert int unsetenv(const char *); 23752a0e603Smillert #endif 2388d2ec7eaSmillert #if __XPG_VISIBLE >= 420 || __POSIX_VISIBLE >= 200112 2398d2ec7eaSmillert char *ptsname(int); 2408d2ec7eaSmillert int grantpt(int); 2418d2ec7eaSmillert int unlockpt(int); 2428d2ec7eaSmillert #endif 2438d2ec7eaSmillert #if __POSIX_VISIBLE >= 200112 2448d2ec7eaSmillert int posix_openpt(int); 2458d2ec7eaSmillert #endif 24652a0e603Smillert 247b10cd188Sguenther /* 248b10cd188Sguenther * The Open Group Base Specifications, Issue 7; IEEE Std 1003.1-2008 (POSIX) 249b10cd188Sguenther */ 250b10cd188Sguenther #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809 251b10cd188Sguenther char *mkdtemp(char *); 252b10cd188Sguenther #endif 253b10cd188Sguenther 2549a476230Smillert #if __XPG_VISIBLE >= 420 || __POSIX_VISIBLE >= 200809 2556290d6dbSmillert int getsubopt(char **, char * const *, char **); 2566290d6dbSmillert #endif 2576290d6dbSmillert 258ed42a740Sguenther /* 259*dfc5f6feSguenther * The Open Group Base Specifications, Issue 8 260ed42a740Sguenther */ 261*dfc5f6feSguenther #if __POSIX_VISIBLE >= 202405 || __BSD_VISIBLE 262ed42a740Sguenther int mkostemp(char *, int); 263*dfc5f6feSguenther void *reallocarray(void *, size_t, size_t); 264ed42a740Sguenther #endif 265ed42a740Sguenther 26652a0e603Smillert #if __BSD_VISIBLE 267af4f502bStedu #define alloca(n) __builtin_alloca(n) 268df930be7Sderaadt 269c72b5b24Smillert char *getbsize(int *, long *); 270c72b5b24Smillert char *cgetcap(char *, const char *, int); 271c72b5b24Smillert int cgetclose(void); 272c72b5b24Smillert int cgetent(char **, char **, const char *); 273c72b5b24Smillert int cgetfirst(char **, char **); 274c72b5b24Smillert int cgetmatch(char *, const char *); 275c72b5b24Smillert int cgetnext(char **, char **); 276c72b5b24Smillert int cgetnum(char *, const char *, long *); 277c72b5b24Smillert int cgetset(const char *); 278c72b5b24Smillert int cgetusedb(int); 279c72b5b24Smillert int cgetstr(char *, const char *, char **); 280c72b5b24Smillert int cgetustr(char *, const char *, char **); 281df930be7Sderaadt 282c72b5b24Smillert int daemon(int, int); 2838fb4bdfcSmillert char *devname(dev_t, mode_t); 284c72b5b24Smillert int getloadavg(double [], int); 285df930be7Sderaadt 286c88a5da8Sajacoutot const char * 287c88a5da8Sajacoutot getprogname(void); 288c88a5da8Sajacoutot void setprogname(const char *); 289c88a5da8Sajacoutot 290df930be7Sderaadt extern char *suboptarg; /* getsubopt(3) external variable */ 291df930be7Sderaadt 2921aa88f2bSmillert char * mkdtemps(char *, int); 29352a0e603Smillert int mkstemps(char *, int); 294ed42a740Sguenther int mkostemps(char *, int, int); 29552a0e603Smillert 296f3c3a9c6Smillert int heapsort(void *, size_t, size_t, int (*)(const void *, const void *)); 297f3c3a9c6Smillert int mergesort(void *, size_t, size_t, int (*)(const void *, const void *)); 298f3c3a9c6Smillert int radixsort(const unsigned char **, int, const unsigned char *, 299f3c3a9c6Smillert unsigned); 300f3c3a9c6Smillert int sradixsort(const unsigned char **, int, const unsigned char *, 301f3c3a9c6Smillert unsigned); 302df930be7Sderaadt 303c72b5b24Smillert void srandomdev(void); 30452a0e603Smillert long long 30552a0e603Smillert strtonum(const char *, long long, long long, const char **); 306df930be7Sderaadt 307c72b5b24Smillert void setproctitle(const char *, ...) 3084cfa8f05Skrw __attribute__((__format__ (__printf__, 1, 2))); 309df930be7Sderaadt 310c72b5b24Smillert quad_t qabs(quad_t); 311c72b5b24Smillert qdiv_t qdiv(quad_t, quad_t); 312e58af031Stedu quad_t strtoq(const char *__restrict, char **__restrict, int); 313e58af031Stedu u_quad_t strtouq(const char *__restrict, char **__restrict, int); 314df930be7Sderaadt 315e5388d05Sbeck uint32_t arc4random(void); 316e5388d05Sbeck uint32_t arc4random_uniform(uint32_t); 317f7b5bfc7Sotto void arc4random_buf(void *, size_t) 318c1664c68Stom __attribute__((__bounded__ (__buffer__,1,2))); 319f7b5bfc7Sotto 32052a0e603Smillert #endif /* __BSD_VISIBLE */ 321df930be7Sderaadt 322df930be7Sderaadt __END_DECLS 323df930be7Sderaadt 324df930be7Sderaadt #endif /* _STDLIB_H_ */ 325