The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | */ |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 28 | |
| 29 | #ifndef _STDLIB_H |
| 30 | #define _STDLIB_H |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 31 | |
| 32 | #include <sys/cdefs.h> |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 33 | #include <xlocale.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 34 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | #include <alloca.h> |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 36 | #include <malloc.h> |
| 37 | #include <stddef.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 38 | |
| 39 | __BEGIN_DECLS |
| 40 | |
| 41 | #define EXIT_FAILURE 1 |
| 42 | #define EXIT_SUCCESS 0 |
| 43 | |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 44 | __noreturn void abort(void); |
| 45 | __noreturn void exit(int); |
| 46 | __noreturn void _Exit(int) __INTRODUCED_IN(21); |
| 47 | int atexit(void (*)(void)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 48 | |
Josh Gao | 95fa26e | 2016-06-10 16:33:05 -0700 | [diff] [blame] | 49 | int at_quick_exit(void (*)(void)) __INTRODUCED_IN(21); |
| 50 | void quick_exit(int) __noreturn __INTRODUCED_IN(21); |
Dan Albert | b8425c5 | 2014-04-29 17:49:06 -0700 | [diff] [blame] | 51 | |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 52 | char* getenv(const char*); |
| 53 | int putenv(char*); |
| 54 | int setenv(const char*, const char*, int); |
| 55 | int unsetenv(const char*); |
| 56 | int clearenv(void); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 57 | |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 58 | char* mkdtemp(char*); |
| 59 | char* mktemp(char*) __attribute__((deprecated("mktemp is unsafe, use mkstemp or tmpfile instead"))); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 60 | |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 61 | int mkostemp64(char*, int) __INTRODUCED_IN(23); |
| 62 | int mkostemp(char*, int) __INTRODUCED_IN(23); |
| 63 | int mkostemps64(char*, int, int) __INTRODUCED_IN(23); |
| 64 | int mkostemps(char*, int, int) __INTRODUCED_IN(23); |
| 65 | int mkstemp64(char*) __INTRODUCED_IN(21); |
| 66 | int mkstemp(char*); |
| 67 | int mkstemps64(char*, int) __INTRODUCED_IN(23); |
| 68 | int mkstemps(char*, int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 69 | |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 70 | long strtol(const char *, char **, int); |
| 71 | long long strtoll(const char *, char **, int); |
| 72 | unsigned long strtoul(const char *, char **, int); |
| 73 | unsigned long long strtoull(const char *, char **, int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 74 | |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 75 | int posix_memalign(void** memptr, size_t alignment, size_t size) __INTRODUCED_IN(16); |
Ken Sumrall | 85aad90 | 2011-12-14 20:50:01 -0800 | [diff] [blame] | 76 | |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 77 | double strtod(const char*, char**); |
| 78 | long double strtold(const char*, char**) __INTRODUCED_IN(21); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 79 | |
Josh Gao | 2e8e5e6 | 2017-04-20 12:58:31 -0700 | [diff] [blame] | 80 | unsigned long strtoul_l(const char*, char**, int, locale_t) __INTRODUCED_IN(26); |
Dan Albert | dfb5ce4 | 2014-07-09 22:51:34 +0000 | [diff] [blame] | 81 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 82 | int atoi(const char*) __attribute_pure__; |
| 83 | long atol(const char*) __attribute_pure__; |
| 84 | long long atoll(const char*) __attribute_pure__; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 85 | |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 86 | char * realpath(const char *path, char *resolved) __overloadable |
| 87 | __RENAME_CLANG(realpath); |
| 88 | int system(const char *string); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 89 | |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 90 | void* bsearch(const void* key, const void* base0, size_t nmemb, size_t size, |
| 91 | int (*compar)(const void*, const void*)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 92 | |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 93 | void qsort(void*, size_t, size_t, int (*)(const void*, const void*)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 94 | |
Yabin Cui | a39f939 | 2014-10-28 12:04:02 -0700 | [diff] [blame] | 95 | uint32_t arc4random(void); |
| 96 | uint32_t arc4random_uniform(uint32_t); |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame] | 97 | void arc4random_buf(void*, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 98 | |
| 99 | #define RAND_MAX 0x7fffffff |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 100 | |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 101 | int rand_r(unsigned int*) __INTRODUCED_IN(21); |
Elliott Hughes | a0beeea | 2014-06-12 11:48:04 -0700 | [diff] [blame] | 102 | |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 103 | double drand48(void); |
| 104 | double erand48(unsigned short[3]); |
| 105 | long jrand48(unsigned short[3]); |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 106 | void lcong48(unsigned short[7]) __INTRODUCED_IN(23); |
Elliott Hughes | 274afe8 | 2014-11-06 12:40:08 -0800 | [diff] [blame] | 107 | long lrand48(void); |
| 108 | long mrand48(void); |
| 109 | long nrand48(unsigned short[3]); |
| 110 | unsigned short* seed48(unsigned short[3]); |
| 111 | void srand48(long); |
| 112 | |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 113 | char* initstate(unsigned int, char*, size_t) __INTRODUCED_IN(21); |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 114 | char* setstate(char*) __INTRODUCED_IN(21); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 115 | |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 116 | int getpt(void); |
Josh Gao | 14adff1 | 2016-04-29 12:00:55 -0700 | [diff] [blame] | 117 | int posix_openpt(int) __INTRODUCED_IN(21); |
Elliott Hughes | a381fe8 | 2014-12-09 20:30:23 -0800 | [diff] [blame] | 118 | char* ptsname(int); |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 119 | int ptsname_r(int, char*, size_t); |
| 120 | int unlockpt(int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 121 | |
Josh Gao | 2e8e5e6 | 2017-04-20 12:58:31 -0700 | [diff] [blame] | 122 | int getsubopt(char**, char* const*, char**) __INTRODUCED_IN(26); |
Elliott Hughes | df143f8 | 2016-04-04 17:34:04 -0700 | [diff] [blame] | 123 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 124 | typedef struct { |
| 125 | int quot; |
| 126 | int rem; |
| 127 | } div_t; |
| 128 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 129 | div_t div(int, int) __attribute_const__; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 130 | |
| 131 | typedef struct { |
| 132 | long int quot; |
| 133 | long int rem; |
| 134 | } ldiv_t; |
| 135 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 136 | ldiv_t ldiv(long, long) __attribute_const__; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 137 | |
| 138 | typedef struct { |
| 139 | long long int quot; |
| 140 | long long int rem; |
| 141 | } lldiv_t; |
| 142 | |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 143 | lldiv_t lldiv(long long, long long) __attribute_const__; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 144 | |
Elliott Hughes | 692207e | 2014-02-28 16:23:27 -0800 | [diff] [blame] | 145 | /* BSD compatibility. */ |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 146 | const char* getprogname(void) __INTRODUCED_IN(21); |
| 147 | void setprogname(const char*) __INTRODUCED_IN(21); |
Elliott Hughes | 692207e | 2014-02-28 16:23:27 -0800 | [diff] [blame] | 148 | |
Josh Gao | 2e8e5e6 | 2017-04-20 12:58:31 -0700 | [diff] [blame] | 149 | int mblen(const char*, size_t) __INTRODUCED_IN(26) __VERSIONER_NO_GUARD; |
Elliott Hughes | 6f6f905 | 2016-04-28 14:54:52 -0700 | [diff] [blame] | 150 | size_t mbstowcs(wchar_t*, const char*, size_t); |
Dan Albert | cb0b143 | 2016-09-06 16:51:00 -0700 | [diff] [blame] | 151 | int mbtowc(wchar_t*, const char*, size_t) __INTRODUCED_IN(21) __VERSIONER_NO_GUARD; |
| 152 | int wctomb(char*, wchar_t) __INTRODUCED_IN(21) __VERSIONER_NO_GUARD; |
| 153 | |
Elliott Hughes | 6f6f905 | 2016-04-28 14:54:52 -0700 | [diff] [blame] | 154 | size_t wcstombs(char*, const wchar_t*, size_t); |
David 'Digit' Turner | bb5581a | 2010-10-09 17:56:55 +0200 | [diff] [blame] | 155 | |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 156 | #if __ANDROID_API__ >= __ANDROID_API_L__ |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 157 | size_t __ctype_get_mb_cur_max(void) __INTRODUCED_IN(21); |
Dan Albert | 224ff04 | 2014-08-14 13:56:51 -0700 | [diff] [blame] | 158 | #define MB_CUR_MAX __ctype_get_mb_cur_max() |
Dan Albert | cb0b143 | 2016-09-06 16:51:00 -0700 | [diff] [blame] | 159 | #else |
| 160 | /* |
Dan Albert | 5c15b8c | 2017-02-21 15:54:58 -0800 | [diff] [blame] | 161 | * Pre-L we didn't have any locale support and so we were always the POSIX |
| 162 | * locale. POSIX specifies that MB_CUR_MAX for the POSIX locale is 1: |
| 163 | * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdlib.h.html |
Dan Albert | cb0b143 | 2016-09-06 16:51:00 -0700 | [diff] [blame] | 164 | */ |
Dan Albert | 5c15b8c | 2017-02-21 15:54:58 -0800 | [diff] [blame] | 165 | #define MB_CUR_MAX 1 |
Dan Albert | cb0b143 | 2016-09-06 16:51:00 -0700 | [diff] [blame] | 166 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 167 | |
Daniel Micay | 3244d9f | 2015-04-18 13:04:19 -0400 | [diff] [blame] | 168 | #if defined(__BIONIC_FORTIFY) |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 169 | #define __realpath_buf_too_small_str \ |
| 170 | "realpath output parameter must be NULL or a >= PATH_MAX bytes buffer" |
| 171 | |
| 172 | /* PATH_MAX is unavailable without polluting the namespace, but it's always 4096 on Linux */ |
| 173 | #define __PATH_MAX 4096 |
| 174 | |
| 175 | #if defined(__clang__) |
| 176 | |
| 177 | __BIONIC_ERROR_FUNCTION_VISIBILITY |
| 178 | char* realpath(const char* path, char* resolved) __overloadable |
| 179 | __enable_if(__bos(resolved) != __BIONIC_FORTIFY_UNKNOWN_SIZE && |
| 180 | __bos(resolved) < __PATH_MAX, __realpath_buf_too_small_str) |
| 181 | __errorattr(__realpath_buf_too_small_str); |
| 182 | |
| 183 | /* No need for a FORTIFY version; the only things we can catch are at |
| 184 | * compile-time. |
| 185 | */ |
| 186 | |
| 187 | #else /* defined(__clang__) */ |
Daniel Micay | 3244d9f | 2015-04-18 13:04:19 -0400 | [diff] [blame] | 188 | |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 189 | char* __realpath_real(const char*, char*) __RENAME(realpath); |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 190 | __errordecl(__realpath_size_error, __realpath_buf_too_small_str); |
Daniel Micay | 3244d9f | 2015-04-18 13:04:19 -0400 | [diff] [blame] | 191 | |
Daniel Micay | 3244d9f | 2015-04-18 13:04:19 -0400 | [diff] [blame] | 192 | __BIONIC_FORTIFY_INLINE |
| 193 | char* realpath(const char* path, char* resolved) { |
| 194 | size_t bos = __bos(resolved); |
| 195 | |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 196 | if (bos != __BIONIC_FORTIFY_UNKNOWN_SIZE && bos < __PATH_MAX) { |
Daniel Micay | 3244d9f | 2015-04-18 13:04:19 -0400 | [diff] [blame] | 197 | __realpath_size_error(); |
| 198 | } |
| 199 | |
| 200 | return __realpath_real(path, resolved); |
| 201 | } |
Daniel Micay | 3244d9f | 2015-04-18 13:04:19 -0400 | [diff] [blame] | 202 | |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 203 | #endif /* defined(__clang__) */ |
| 204 | |
| 205 | #undef __PATH_MAX |
| 206 | #undef __realpath_buf_too_small_str |
Daniel Micay | 3244d9f | 2015-04-18 13:04:19 -0400 | [diff] [blame] | 207 | #endif /* defined(__BIONIC_FORTIFY) */ |
| 208 | |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 209 | #if __ANDROID_API__ >= __ANDROID_API_L__ |
Josh Gao | b6a4a4c | 2016-07-26 16:34:40 -0700 | [diff] [blame] | 210 | float strtof(const char*, char**) __INTRODUCED_IN(21); |
Elliott Hughes | 95fa061 | 2016-09-28 12:29:52 -0700 | [diff] [blame] | 211 | double atof(const char*) __attribute_pure__ __INTRODUCED_IN(21); |
| 212 | int abs(int) __attribute_const__ __INTRODUCED_IN(21); |
| 213 | long labs(long) __attribute_const__ __INTRODUCED_IN(21); |
| 214 | long long llabs(long long) __attribute_const__ __INTRODUCED_IN(21); |
Josh Gao | b6a4a4c | 2016-07-26 16:34:40 -0700 | [diff] [blame] | 215 | int rand(void) __INTRODUCED_IN(21); |
| 216 | void srand(unsigned int) __INTRODUCED_IN(21); |
| 217 | long random(void) __INTRODUCED_IN(21); |
| 218 | void srandom(unsigned int) __INTRODUCED_IN(21); |
| 219 | int grantpt(int) __INTRODUCED_IN(21); |
Josh Gao | 6cd9fb0 | 2016-09-23 14:06:05 -0700 | [diff] [blame] | 220 | |
| 221 | long long strtoll_l(const char*, char**, int, locale_t) __INTRODUCED_IN(21); |
| 222 | unsigned long long strtoull_l(const char*, char**, int, locale_t) __INTRODUCED_IN(21); |
| 223 | long double strtold_l(const char*, char**, locale_t) __INTRODUCED_IN(21); |
Josh Gao | 8778d64 | 2016-07-22 15:26:36 -0700 | [diff] [blame] | 224 | #else |
| 225 | // Implemented as static inlines before 21. |
| 226 | #endif |
| 227 | |
Elliott Hughes | 4cae5c3 | 2017-07-10 11:51:00 -0700 | [diff] [blame] | 228 | #if __ANDROID_API__ >= __ANDROID_API_O__ |
Josh Gao | 2e8e5e6 | 2017-04-20 12:58:31 -0700 | [diff] [blame] | 229 | double strtod_l(const char*, char**, locale_t) __INTRODUCED_IN(26); |
| 230 | float strtof_l(const char*, char**, locale_t) __INTRODUCED_IN(26); |
| 231 | long strtol_l(const char*, char**, int, locale_t) __INTRODUCED_IN(26); |
Josh Gao | 6cd9fb0 | 2016-09-23 14:06:05 -0700 | [diff] [blame] | 232 | #else |
Elliott Hughes | 4cae5c3 | 2017-07-10 11:51:00 -0700 | [diff] [blame] | 233 | // Implemented as static inlines before 26. |
Josh Gao | 6cd9fb0 | 2016-09-23 14:06:05 -0700 | [diff] [blame] | 234 | #endif |
| 235 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 236 | __END_DECLS |
| 237 | |
Josh Gao | b8e1b705 | 2016-04-13 17:18:20 -0700 | [diff] [blame] | 238 | #include <android/legacy_stdlib_inlines.h> |
| 239 | |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 240 | #endif /* _STDLIB_H */ |