blob: 2c918d4792b1fc3fe2110eae0d834cd7689e6255 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 Hughes76f89162015-01-26 13:34:58 -080028
29#ifndef _STDLIB_H
30#define _STDLIB_H
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031
Elliott Hughes414dd2d2024-10-16 14:48:30 +000032#include <sys/cdefs.h>
33
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <alloca.h>
Elliott Hughes21b56eb2017-10-20 17:57:17 -070035#include <bits/wait.h>
Elliott Hughes76f89162015-01-26 13:34:58 -080036#include <malloc.h>
37#include <stddef.h>
Elliott Hughes21b56eb2017-10-20 17:57:17 -070038#include <xlocale.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039
40__BEGIN_DECLS
41
42#define EXIT_FAILURE 1
43#define EXIT_SUCCESS 0
44
Elliott Hughes504d0482021-06-07 11:20:28 -070045__noreturn void abort(void) __attribute__((__nomerge__));
Elliott Hughesfaa74342017-08-11 17:34:44 -070046__noreturn void exit(int __status);
Elliott Hughes655e4302023-06-16 12:39:33 -070047__noreturn void _Exit(int __status);
Elliott Hughes2f94a292017-09-18 14:40:01 -070048
zijunzhao5a918d92022-11-28 21:05:55 +000049int atexit(void (* _Nonnull __fn)(void));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050
Elliott Hughes655e4302023-06-16 12:39:33 -070051int at_quick_exit(void (* _Nonnull __fn)(void));
52void quick_exit(int __status) __noreturn;
Dan Albertb8425c52014-04-29 17:49:06 -070053
zijunzhao5a918d92022-11-28 21:05:55 +000054char* _Nullable getenv(const char* _Nonnull __name);
55int putenv(char* _Nonnull __assignment);
56int setenv(const char* _Nonnull __name, const char* _Nonnull __value, int __overwrite);
57int unsetenv(const char* _Nonnull __name);
Elliott Hughes3b2096a2016-07-22 18:57:12 -070058int clearenv(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059
zijunzhao5a918d92022-11-28 21:05:55 +000060char* _Nullable mkdtemp(char* _Nonnull __template);
Elliott Hughesa1b5ca22024-04-22 20:10:53 +000061char* _Nullable mktemp(char* _Nonnull __template) __attribute__((__deprecated__("mktemp is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughes31165ed2014-09-23 17:34:29 -070062
zijunzhao5a918d92022-11-28 21:05:55 +000063int mkostemp64(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23);
64int mkostemp(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23);
65int mkostemps64(char* _Nonnull __template, int __suffix_length, int __flags) __INTRODUCED_IN(23);
66int mkostemps(char* _Nonnull __template, int __suffix_length, int __flags) __INTRODUCED_IN(23);
Elliott Hughes655e4302023-06-16 12:39:33 -070067int mkstemp64(char* _Nonnull __template);
zijunzhao5a918d92022-11-28 21:05:55 +000068int mkstemp(char* _Nonnull __template);
69int mkstemps64(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23);
70int mkstemps(char* _Nonnull __template, int __flags);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071
Elliott Hughes655e4302023-06-16 12:39:33 -070072int posix_memalign(void* _Nullable * _Nullable __memptr, size_t __alignment, size_t __size);
Ken Sumrall85aad902011-12-14 20:50:01 -080073
Elliott Hughesc9f344e2024-08-20 15:28:38 +000074/**
75 * [aligned_alloc(3)](https://man7.org/linux/man-pages/man3/aligned_alloc.3.html)
76 * allocates the given number of bytes with the given alignment.
77 *
78 * Returns a pointer to the allocated memory on success and returns a null
79 * pointer and sets `errno` on failure.
80 *
81 * Available since API level 28.
82 */
Elliott Hughesb19df892024-09-06 14:27:41 +000083__nodiscard void* _Nullable aligned_alloc(size_t __alignment, size_t __size) __INTRODUCED_IN(28);
Christopher Ferriscae21a92018-02-05 18:14:55 -080084
Elliott Hughesb19df892024-09-06 14:27:41 +000085__nodiscard char* _Nullable realpath(const char* _Nonnull __path, char* _Nullable __resolved);
Elliott Hughescc87eec2023-10-26 21:28:12 +000086
87/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000088 * [system(3)](https://man7.org/linux/man-pages/man3/system.3.html) executes
Elliott Hughescc87eec2023-10-26 21:28:12 +000089 * the given command in a new shell process.
90 *
91 * On Android, the special case of `system(NULL)` always returns 1,
92 * as specified by POSIX. Passing `NULL` to determine whether or
93 * not a shell is available is not portable. Callers should just try
94 * the command they actually want to run, since there are many reasons
95 * why it might fail, both temporarily (for lack of resources, say)
96 * or permanently (for lack of permission, say).
97 *
98 * Returns -1 and sets errno if process creation fails; returns a
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000099 * [waitpid(2)](https://man7.org/linux/man-pages/man2/waitpid.2.html)
Elliott Hughescc87eec2023-10-26 21:28:12 +0000100 * status otherwise.
101 */
zijunzhao5a918d92022-11-28 21:05:55 +0000102int system(const char* _Nonnull __command);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103
Elliott Hughesd4c54b42024-05-31 17:33:33 +0000104/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000105 * [bsearch(3)](https://man7.org/linux/man-pages/man3/bsearch.3.html) searches
Elliott Hughesd4c54b42024-05-31 17:33:33 +0000106 * a sorted array.
107 *
108 * Returns a pointer to a matching item on success,
109 * or NULL if no matching item is found.
110 */
Elliott Hughesb19df892024-09-06 14:27:41 +0000111__nodiscard 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));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112
Elliott Hughes5bae5722024-07-30 12:47:33 -0400113/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000114 * [qsort(3)](https://man7.org/linux/man-pages/man3/qsort.3.html) sorts an array
Elliott Hughes5bae5722024-07-30 12:47:33 -0400115 * of n elements each of the given size, using the given comparator.
116 */
117void qsort(void* _Nullable __array, size_t __n, size_t __size, int (* _Nonnull __comparator)(const void* _Nullable __lhs, const void* _Nullable __rhs));
118
119/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000120 * [qsort_r(3)](https://man7.org/linux/man-pages/man3/qsort_r.3.html) sorts an
Elliott Hughes5bae5722024-07-30 12:47:33 -0400121 * array of n elements each of the given size, using the given comparator,
122 * and passing the given context argument to the comparator.
123 *
124 * Available since API level 36.
125 */
126void qsort_r(void* _Nullable __array, size_t __n, size_t __size, int (* _Nonnull __comparator)(const void* _Nullable __lhs, const void* _Nullable __rhs, void* _Nullable __context), void* _Nullable __context) __INTRODUCED_IN(36);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127
Yabin Cuia39f9392014-10-28 12:04:02 -0700128uint32_t arc4random(void);
Elliott Hughesfaa74342017-08-11 17:34:44 -0700129uint32_t arc4random_uniform(uint32_t __upper_bound);
zijunzhao5a918d92022-11-28 21:05:55 +0000130void arc4random_buf(void* _Nonnull __buf, size_t __n);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131
132#define RAND_MAX 0x7fffffff
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700133
Elliott Hughes655e4302023-06-16 12:39:33 -0700134int rand_r(unsigned int* _Nonnull __seed_ptr);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700135
Elliott Hughes274afe82014-11-06 12:40:08 -0800136double drand48(void);
zijunzhao5a918d92022-11-28 21:05:55 +0000137double erand48(unsigned short __xsubi[_Nonnull 3]);
138long jrand48(unsigned short __xsubi[_Nonnull 3]);
139void lcong48(unsigned short __param[_Nonnull 7]) __INTRODUCED_IN(23);
Elliott Hughes274afe82014-11-06 12:40:08 -0800140long lrand48(void);
141long mrand48(void);
zijunzhao5a918d92022-11-28 21:05:55 +0000142long nrand48(unsigned short __xsubi[_Nonnull 3]);
143unsigned short* _Nonnull seed48(unsigned short __seed16v[_Nonnull 3]);
Elliott Hughesfaa74342017-08-11 17:34:44 -0700144void srand48(long __seed);
Elliott Hughes274afe82014-11-06 12:40:08 -0800145
Elliott Hughes655e4302023-06-16 12:39:33 -0700146char* _Nullable initstate(unsigned int __seed, char* _Nonnull __state, size_t __n);
147char* _Nullable setstate(char* _Nonnull __state);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148
Elliott Hughes49167062014-07-25 17:24:00 -0700149int getpt(void);
Elliott Hughes655e4302023-06-16 12:39:33 -0700150int posix_openpt(int __flags);
zijunzhao5a918d92022-11-28 21:05:55 +0000151char* _Nullable ptsname(int __fd);
152int ptsname_r(int __fd, char* _Nonnull __buf, size_t __n);
Elliott Hughesfaa74342017-08-11 17:34:44 -0700153int unlockpt(int __fd);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800154
zijunzhao5a918d92022-11-28 21:05:55 +0000155int getsubopt(char* _Nonnull * _Nonnull __option, char* _Nonnull const* _Nonnull __tokens, char* _Nullable * _Nonnull __value_ptr) __INTRODUCED_IN(26);
Elliott Hughesdf143f82016-04-04 17:34:04 -0700156
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157typedef struct {
Elliott Hughesfaa74342017-08-11 17:34:44 -0700158 int quot;
159 int rem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160} div_t;
161
Elliott Hughesfaa74342017-08-11 17:34:44 -0700162div_t div(int __numerator, int __denominator) __attribute_const__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163
164typedef struct {
Elliott Hughesfaa74342017-08-11 17:34:44 -0700165 long int quot;
166 long int rem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167} ldiv_t;
168
Elliott Hughesfaa74342017-08-11 17:34:44 -0700169ldiv_t ldiv(long __numerator, long __denominator) __attribute_const__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170
171typedef struct {
Elliott Hughesfaa74342017-08-11 17:34:44 -0700172 long long int quot;
173 long long int rem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174} lldiv_t;
175
Elliott Hughesfaa74342017-08-11 17:34:44 -0700176lldiv_t lldiv(long long __numerator, long long __denominator) __attribute_const__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800177
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700178/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000179 * [getloadavg(3)](https://man7.org/linux/man-pages/man3/getloadavg.3.html) queries the
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700180 * number of runnable processes averaged over time. The Linux kernel supports averages
181 * over the last 1, 5, and 15 minutes.
182 *
183 * Returns the number of samples written to `__averages` (at most 3), and returns -1 on failure.
184 */
zijunzhao5a918d92022-11-28 21:05:55 +0000185int getloadavg(double __averages[_Nonnull], int __n) __INTRODUCED_IN(29);
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700186
Elliott Hughes692207e2014-02-28 16:23:27 -0800187/* BSD compatibility. */
Elliott Hughes655e4302023-06-16 12:39:33 -0700188const char* _Nullable getprogname(void);
189void setprogname(const char* _Nonnull __name);
Elliott Hughes692207e2014-02-28 16:23:27 -0800190
Elliott Hughes2f9fe8c2024-08-13 13:42:43 +0000191int mblen(const char* _Nullable __s, size_t __n) __INTRODUCED_IN(26);
Elliott Hughes2bdeff42023-06-20 14:32:58 -0700192size_t mbstowcs(wchar_t* _Nullable __dst, const char* _Nullable __src, size_t __n);
193int mbtowc(wchar_t* _Nullable __wc_ptr, const char* _Nullable __s, size_t __n);
194int wctomb(char* _Nullable __dst, wchar_t __wc);
Dan Albertcb0b1432016-09-06 16:51:00 -0700195
Elliott Hughes2bdeff42023-06-20 14:32:58 -0700196size_t wcstombs(char* _Nullable __dst, const wchar_t* _Nullable __src, size_t __n);
David 'Digit' Turnerbb5581a2010-10-09 17:56:55 +0200197
Elliott Hughes655e4302023-06-16 12:39:33 -0700198size_t __ctype_get_mb_cur_max(void);
Dan Albert224ff042014-08-14 13:56:51 -0700199#define MB_CUR_MAX __ctype_get_mb_cur_max()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
George Burgess IVb97049c2017-07-24 15:05:05 -0700201#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
202#include <bits/fortify/stdlib.h>
203#endif
Daniel Micay3244d9f2015-04-18 13:04:19 -0400204
Elliott Hughes655e4302023-06-16 12:39:33 -0700205int abs(int __x) __attribute_const__;
206long labs(long __x) __attribute_const__;
207long long llabs(long long __x) __attribute_const__;
Ryan Prichard51a7fe82018-01-08 16:18:48 -0800208
Elliott Hughes655e4302023-06-16 12:39:33 -0700209int rand(void);
210void srand(unsigned int __seed);
211long random(void);
212void srandom(unsigned int __seed);
213int grantpt(int __fd);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700214
Elliott Hughes14a55882024-08-23 13:39:13 +0000215/**
216 * [atof(3)](https://man7.org/linux/man-pages/man3/atof.3.html) converts a
217 * string to a double.
218 *
219 * Returns the double; use strtof() or strtod() if you need to detect errors.
220 */
221double atof(const char* _Nonnull __s) __attribute_pure__;
222
223/**
224 * [atoi(3)](https://man7.org/linux/man-pages/man3/atoi.3.html) converts a
225 * string to an int.
226 *
227 * Returns the int or 0 on error; use strtol() if you need to detect errors.
228 */
229int atoi(const char* _Nonnull __s) __attribute_pure__;
230
231/**
232 * [atol(3)](https://man7.org/linux/man-pages/man3/atol.3.html) converts a
233 * string to a long.
234 *
235 * Returns the long or 0 on error; use strtol() if you need to detect errors.
236 */
237long atol(const char* _Nonnull __s) __attribute_pure__;
238
239/**
240 * [atoll(3)](https://man7.org/linux/man-pages/man3/atoll.3.html) converts a
241 * string to a long long.
242 *
243 * Returns the long long or 0 on error; use strtol() if you need to detect errors.
244 */
245long long atoll(const char* _Nonnull __s) __attribute_pure__;
246
247/**
248 * [strtol(3)](https://man7.org/linux/man-pages/man3/strtol.3.html) converts a
249 * string to a long.
250 *
251 * Returns the long.
252 * `__end_ptr` is set to the last character in `__s` that was converted.
253 * errno is set to ERANGE if the result overflowed or underflowed.
254 */
255long strtol(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
256
257/** Equivalent to strtol() on Android. */
258long strtol_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int, locale_t _Nonnull __l) __RENAME(strtol);
259
260/**
261 * [strtoll(3)](https://man7.org/linux/man-pages/man3/strtoll.3.html) converts a
262 * string to a long long.
263 *
264 * Returns the long long.
265 * `__end_ptr` is set to the last character in `__s` that was converted.
266 * errno is set to ERANGE if the result overflowed or underflowed.
267 */
268long long strtoll(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
269
270/** Equivalent to strtoll() on Android. */
Elliott Hughes655e4302023-06-16 12:39:33 -0700271long long strtoll_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l);
Elliott Hughes14a55882024-08-23 13:39:13 +0000272
273/**
274 * [strtoul(3)](https://man7.org/linux/man-pages/man3/strtoul.3.html) converts a
275 * string to an unsigned long.
276 *
277 * Returns the unsigned long.
278 * `__end_ptr` is set to the last character in `__s` that was converted.
279 * errno is set to ERANGE if the result overflowed or underflowed.
280 */
281unsigned long strtoul(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
282
283/** Equivalent to strtoul() on Android. */
284unsigned long strtoul_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l) __RENAME(strtoul);
285
286/**
287 * [strtoull(3)](https://man7.org/linux/man-pages/man3/strtoull.3.html) converts a
288 * string to an unsigned long long.
289 *
290 * Returns the unsigned long long.
291 * `__end_ptr` is set to the last character in `__s` that was converted.
292 * errno is set to ERANGE if the result overflowed or underflowed.
293 */
294unsigned long long strtoull(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
295
296/** Equivalent to strtoull() on Android. */
Elliott Hughes655e4302023-06-16 12:39:33 -0700297unsigned long long strtoull_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l);
Elliott Hughes14a55882024-08-23 13:39:13 +0000298
299/**
300 * [strtof(3)](https://man7.org/linux/man-pages/man3/strtof.3.html) converts a
301 * string to a float.
302 *
303 * Returns the float.
304 * `__end_ptr` is set to the last character in `__s` that was converted.
305 * errno is set to ERANGE if the result overflowed or underflowed.
306 */
307float strtof(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr);
308
309/**
310 * [strtod(3)](https://man7.org/linux/man-pages/man3/strtod.3.html) converts a
311 * string to a double.
312 *
313 * Returns the double.
314 * `__end_ptr` is set to the last character in `__s` that was converted.
315 * errno is set to ERANGE if the result overflowed or underflowed.
316 */
317double strtod(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr);
318
319/**
320 * [strtold(3)](https://man7.org/linux/man-pages/man3/strtold.3.html) converts a
321 * string to a long double.
322 *
323 * Returns the long double.
324 * `__end_ptr` is set to the last character in `__s` that was converted.
325 * errno is set to ERANGE if the result overflowed or underflowed.
326 */
327long double strtold(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr);
328
329/** Equivalent to strtold() on Android. */
Elliott Hughes655e4302023-06-16 12:39:33 -0700330long double strtold_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l);
Josh Gao8778d642016-07-22 15:26:36 -0700331
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800332#if __ANDROID_API__ >= 26
Elliott Hughes14a55882024-08-23 13:39:13 +0000333/** Equivalent to strtod() on Android. */
zijunzhao5a918d92022-11-28 21:05:55 +0000334double strtod_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l) __INTRODUCED_IN(26);
Elliott Hughes14a55882024-08-23 13:39:13 +0000335/** Equivalent to strtof() on Android. */
zijunzhao5a918d92022-11-28 21:05:55 +0000336float strtof_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l) __INTRODUCED_IN(26);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700337#else
Elliott Hughes4cae5c32017-07-10 11:51:00 -0700338// Implemented as static inlines before 26.
Josh Gao6cd9fb02016-09-23 14:06:05 -0700339#endif
340
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800341__END_DECLS
342
Josh Gaob8e1b7052016-04-13 17:18:20 -0700343#include <android/legacy_stdlib_inlines.h>
344
Elliott Hughes76f89162015-01-26 13:34:58 -0800345#endif /* _STDLIB_H */