1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #ifndef _STDLIB_H 30 #define _STDLIB_H 31 32 #include <alloca.h> 33 #include <bits/wait.h> 34 #include <malloc.h> 35 #include <stddef.h> 36 #include <sys/cdefs.h> 37 #include <xlocale.h> 38 39 __BEGIN_DECLS 40 41 #define EXIT_FAILURE 1 42 #define EXIT_SUCCESS 0 43 44 __noreturn void abort(void) __attribute__((__nomerge__)); 45 __noreturn void exit(int __status); 46 __noreturn void _Exit(int __status); 47 48 int atexit(void (* _Nonnull __fn)(void)); 49 50 int at_quick_exit(void (* _Nonnull __fn)(void)); 51 void quick_exit(int __status) __noreturn; 52 53 char* _Nullable getenv(const char* _Nonnull __name); 54 int putenv(char* _Nonnull __assignment); 55 int setenv(const char* _Nonnull __name, const char* _Nonnull __value, int __overwrite); 56 int unsetenv(const char* _Nonnull __name); 57 int clearenv(void); 58 59 char* _Nullable mkdtemp(char* _Nonnull __template); 60 char* _Nullable mktemp(char* _Nonnull __template) __attribute__((__deprecated__("mktemp is unsafe, use mkstemp or tmpfile instead"))); 61 62 int mkostemp64(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23); 63 int mkostemp(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23); 64 int mkostemps64(char* _Nonnull __template, int __suffix_length, int __flags) __INTRODUCED_IN(23); 65 int mkostemps(char* _Nonnull __template, int __suffix_length, int __flags) __INTRODUCED_IN(23); 66 int mkstemp64(char* _Nonnull __template); 67 int mkstemp(char* _Nonnull __template); 68 int mkstemps64(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23); 69 int mkstemps(char* _Nonnull __template, int __flags); 70 71 long strtol(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base); 72 long long strtoll(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base); 73 unsigned long strtoul(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base); 74 unsigned long long strtoull(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base); 75 76 int posix_memalign(void* _Nullable * _Nullable __memptr, size_t __alignment, size_t __size); 77 78 void* _Nullable aligned_alloc(size_t __alignment, size_t __size) __INTRODUCED_IN(28); 79 80 double strtod(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr); 81 long double strtold(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr); 82 83 unsigned long strtoul_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l) __INTRODUCED_IN(26); 84 85 int atoi(const char* _Nonnull __s) __attribute_pure__; 86 long atol(const char* _Nonnull __s) __attribute_pure__; 87 long long atoll(const char* _Nonnull __s) __attribute_pure__; 88 89 __wur char* _Nullable realpath(const char* _Nonnull __path, char* _Nullable __resolved); 90 91 /** 92 * [system(3)](http://man7.org/linux/man-pages/man3/system.3.html) executes 93 * the given command in a new shell process. 94 * 95 * On Android, the special case of `system(NULL)` always returns 1, 96 * as specified by POSIX. Passing `NULL` to determine whether or 97 * not a shell is available is not portable. Callers should just try 98 * the command they actually want to run, since there are many reasons 99 * why it might fail, both temporarily (for lack of resources, say) 100 * or permanently (for lack of permission, say). 101 * 102 * Returns -1 and sets errno if process creation fails; returns a 103 * [waitpid(2)](http://man7.org/linux/man-pages/man2/waitpid.2.html) 104 * status otherwise. 105 */ 106 int system(const char* _Nonnull __command); 107 108 /** 109 * [bsearch(3)](http://man7.org/linux/man-pages/man3/bsearch.3.html) searches 110 * a sorted array. 111 * 112 * Returns a pointer to a matching item on success, 113 * or NULL if no matching item is found. 114 */ 115 __wur void* _Nullable bsearch(const void* _Nonnull __key, const void* _Nullable __base, size_t __nmemb, size_t __size, int (* _Nonnull __comparator)(const void* _Nonnull __lhs, const void* _Nonnull __rhs)); 116 117 void qsort(void* _Nullable __base, size_t __nmemb, size_t __size, int (* _Nonnull __comparator)(const void* _Nullable __lhs, const void* _Nullable __rhs)); 118 119 uint32_t arc4random(void); 120 uint32_t arc4random_uniform(uint32_t __upper_bound); 121 void arc4random_buf(void* _Nonnull __buf, size_t __n); 122 123 #define RAND_MAX 0x7fffffff 124 125 int rand_r(unsigned int* _Nonnull __seed_ptr); 126 127 double drand48(void); 128 double erand48(unsigned short __xsubi[_Nonnull 3]); 129 long jrand48(unsigned short __xsubi[_Nonnull 3]); 130 void lcong48(unsigned short __param[_Nonnull 7]) __INTRODUCED_IN(23); 131 long lrand48(void); 132 long mrand48(void); 133 long nrand48(unsigned short __xsubi[_Nonnull 3]); 134 unsigned short* _Nonnull seed48(unsigned short __seed16v[_Nonnull 3]); 135 void srand48(long __seed); 136 137 char* _Nullable initstate(unsigned int __seed, char* _Nonnull __state, size_t __n); 138 char* _Nullable setstate(char* _Nonnull __state); 139 140 int getpt(void); 141 int posix_openpt(int __flags); 142 char* _Nullable ptsname(int __fd); 143 int ptsname_r(int __fd, char* _Nonnull __buf, size_t __n); 144 int unlockpt(int __fd); 145 146 int getsubopt(char* _Nonnull * _Nonnull __option, char* _Nonnull const* _Nonnull __tokens, char* _Nullable * _Nonnull __value_ptr) __INTRODUCED_IN(26); 147 148 typedef struct { 149 int quot; 150 int rem; 151 } div_t; 152 153 div_t div(int __numerator, int __denominator) __attribute_const__; 154 155 typedef struct { 156 long int quot; 157 long int rem; 158 } ldiv_t; 159 160 ldiv_t ldiv(long __numerator, long __denominator) __attribute_const__; 161 162 typedef struct { 163 long long int quot; 164 long long int rem; 165 } lldiv_t; 166 167 lldiv_t lldiv(long long __numerator, long long __denominator) __attribute_const__; 168 169 /** 170 * [getloadavg(3)](http://man7.org/linux/man-pages/man3/getloadavg.3.html) queries the 171 * number of runnable processes averaged over time. The Linux kernel supports averages 172 * over the last 1, 5, and 15 minutes. 173 * 174 * Returns the number of samples written to `__averages` (at most 3), and returns -1 on failure. 175 */ 176 int getloadavg(double __averages[_Nonnull], int __n) __INTRODUCED_IN(29); 177 178 /* BSD compatibility. */ 179 const char* _Nullable getprogname(void); 180 void setprogname(const char* _Nonnull __name); 181 182 int mblen(const char* _Nullable __s, size_t __n) __INTRODUCED_IN_NO_GUARD_FOR_NDK(26); 183 size_t mbstowcs(wchar_t* _Nullable __dst, const char* _Nullable __src, size_t __n); 184 int mbtowc(wchar_t* _Nullable __wc_ptr, const char* _Nullable __s, size_t __n); 185 int wctomb(char* _Nullable __dst, wchar_t __wc); 186 187 size_t wcstombs(char* _Nullable __dst, const wchar_t* _Nullable __src, size_t __n); 188 189 size_t __ctype_get_mb_cur_max(void); 190 #define MB_CUR_MAX __ctype_get_mb_cur_max() 191 192 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS) 193 #include <bits/fortify/stdlib.h> 194 #endif 195 196 int abs(int __x) __attribute_const__; 197 long labs(long __x) __attribute_const__; 198 long long llabs(long long __x) __attribute_const__; 199 200 float strtof(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr); 201 double atof(const char* _Nonnull __s) __attribute_pure__; 202 int rand(void); 203 void srand(unsigned int __seed); 204 long random(void); 205 void srandom(unsigned int __seed); 206 int grantpt(int __fd); 207 208 long long strtoll_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l); 209 unsigned long long strtoull_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l); 210 long double strtold_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l); 211 212 #if __ANDROID_API__ >= 26 213 double strtod_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l) __INTRODUCED_IN(26); 214 float strtof_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l) __INTRODUCED_IN(26); 215 long strtol_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int, locale_t _Nonnull __l) __INTRODUCED_IN(26); 216 #else 217 // Implemented as static inlines before 26. 218 #endif 219 220 __END_DECLS 221 222 #include <android/legacy_stdlib_inlines.h> 223 224 #endif /* _STDLIB_H */ 225