blob: 06ed3f46a3146b65be92ddf6c79407dcea971ce3 [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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <alloca.h>
Elliott Hughes21b56eb2017-10-20 17:57:17 -070033#include <bits/wait.h>
Elliott Hughes76f89162015-01-26 13:34:58 -080034#include <malloc.h>
35#include <stddef.h>
Elliott Hughes21b56eb2017-10-20 17:57:17 -070036#include <sys/cdefs.h>
37#include <xlocale.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038
39__BEGIN_DECLS
40
41#define EXIT_FAILURE 1
42#define EXIT_SUCCESS 0
43
Elliott Hughes504d0482021-06-07 11:20:28 -070044__noreturn void abort(void) __attribute__((__nomerge__));
Elliott Hughesfaa74342017-08-11 17:34:44 -070045__noreturn void exit(int __status);
Elliott Hughes95c6cd72019-12-20 13:26:14 -080046#if __ANDROID_API__ >= 21
Elliott Hughesfaa74342017-08-11 17:34:44 -070047__noreturn void _Exit(int __status) __INTRODUCED_IN(21);
Elliott Hughes2f94a292017-09-18 14:40:01 -070048#else
49__noreturn void _Exit(int) __RENAME(_exit);
50#endif
51
zijunzhao5a918d92022-11-28 21:05:55 +000052int atexit(void (* _Nonnull __fn)(void));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
zijunzhao5a918d92022-11-28 21:05:55 +000054int at_quick_exit(void (* _Nonnull __fn)(void)) __INTRODUCED_IN(21);
Elliott Hughesfaa74342017-08-11 17:34:44 -070055void quick_exit(int __status) __noreturn __INTRODUCED_IN(21);
Dan Albertb8425c52014-04-29 17:49:06 -070056
zijunzhao5a918d92022-11-28 21:05:55 +000057char* _Nullable getenv(const char* _Nonnull __name);
58int putenv(char* _Nonnull __assignment);
59int setenv(const char* _Nonnull __name, const char* _Nonnull __value, int __overwrite);
60int unsetenv(const char* _Nonnull __name);
Elliott Hughes3b2096a2016-07-22 18:57:12 -070061int clearenv(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062
zijunzhao5a918d92022-11-28 21:05:55 +000063char* _Nullable mkdtemp(char* _Nonnull __template);
64char* _Nullable mktemp(char* _Nonnull __template) __attribute__((deprecated("mktemp is unsafe, use mkstemp or tmpfile instead")));
Elliott Hughes31165ed2014-09-23 17:34:29 -070065
zijunzhao5a918d92022-11-28 21:05:55 +000066int mkostemp64(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23);
67int mkostemp(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23);
68int mkostemps64(char* _Nonnull __template, int __suffix_length, int __flags) __INTRODUCED_IN(23);
69int mkostemps(char* _Nonnull __template, int __suffix_length, int __flags) __INTRODUCED_IN(23);
70int mkstemp64(char* _Nonnull __template) __INTRODUCED_IN(21);
71int mkstemp(char* _Nonnull __template);
72int mkstemps64(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23);
73int mkstemps(char* _Nonnull __template, int __flags);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074
zijunzhao5a918d92022-11-28 21:05:55 +000075long strtol(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
76long long strtoll(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
77unsigned long strtoul(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
78unsigned long long strtoull(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079
zijunzhao5a918d92022-11-28 21:05:55 +000080int posix_memalign(void* _Nullable * _Nullable __memptr, size_t __alignment, size_t __size) __INTRODUCED_IN(17);
Ken Sumrall85aad902011-12-14 20:50:01 -080081
zijunzhao5a918d92022-11-28 21:05:55 +000082void* _Nullable aligned_alloc(size_t __alignment, size_t __size) __INTRODUCED_IN(28);
Christopher Ferriscae21a92018-02-05 18:14:55 -080083
zijunzhao5a918d92022-11-28 21:05:55 +000084double strtod(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr);
85long double strtold(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr) __RENAME_LDBL(strtod, 3, 21);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
zijunzhao5a918d92022-11-28 21:05:55 +000087unsigned long strtoul_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l) __INTRODUCED_IN(26);
Dan Albertdfb5ce42014-07-09 22:51:34 +000088
zijunzhao5a918d92022-11-28 21:05:55 +000089int atoi(const char* _Nonnull __s) __attribute_pure__;
90long atol(const char* _Nonnull __s) __attribute_pure__;
91long long atoll(const char* _Nonnull __s) __attribute_pure__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092
zijunzhao5a918d92022-11-28 21:05:55 +000093__wur char* _Nullable realpath(const char* _Nonnull __path, char* _Nullable __resolved);
94int system(const char* _Nonnull __command);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
zijunzhao5a918d92022-11-28 21:05:55 +000096void* _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 -080097
zijunzhao5a918d92022-11-28 21:05:55 +000098void qsort(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 -080099
Yabin Cuia39f9392014-10-28 12:04:02 -0700100uint32_t arc4random(void);
Elliott Hughesfaa74342017-08-11 17:34:44 -0700101uint32_t arc4random_uniform(uint32_t __upper_bound);
zijunzhao5a918d92022-11-28 21:05:55 +0000102void arc4random_buf(void* _Nonnull __buf, size_t __n);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103
104#define RAND_MAX 0x7fffffff
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700105
zijunzhao5a918d92022-11-28 21:05:55 +0000106int rand_r(unsigned int* _Nonnull __seed_ptr) __INTRODUCED_IN(21);
Elliott Hughesa0beeea2014-06-12 11:48:04 -0700107
Elliott Hughes274afe82014-11-06 12:40:08 -0800108double drand48(void);
zijunzhao5a918d92022-11-28 21:05:55 +0000109double erand48(unsigned short __xsubi[_Nonnull 3]);
110long jrand48(unsigned short __xsubi[_Nonnull 3]);
111void lcong48(unsigned short __param[_Nonnull 7]) __INTRODUCED_IN(23);
Elliott Hughes274afe82014-11-06 12:40:08 -0800112long lrand48(void);
113long mrand48(void);
zijunzhao5a918d92022-11-28 21:05:55 +0000114long nrand48(unsigned short __xsubi[_Nonnull 3]);
115unsigned short* _Nonnull seed48(unsigned short __seed16v[_Nonnull 3]);
Elliott Hughesfaa74342017-08-11 17:34:44 -0700116void srand48(long __seed);
Elliott Hughes274afe82014-11-06 12:40:08 -0800117
zijunzhao5a918d92022-11-28 21:05:55 +0000118char* _Nullable initstate(unsigned int __seed, char* _Nonnull __state, size_t __n) __INTRODUCED_IN(21);
119char* _Nullable setstate(char* _Nonnull __state) __INTRODUCED_IN(21);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120
Elliott Hughes49167062014-07-25 17:24:00 -0700121int getpt(void);
Elliott Hughesfaa74342017-08-11 17:34:44 -0700122int posix_openpt(int __flags) __INTRODUCED_IN(21);
zijunzhao5a918d92022-11-28 21:05:55 +0000123char* _Nullable ptsname(int __fd);
124int ptsname_r(int __fd, char* _Nonnull __buf, size_t __n);
Elliott Hughesfaa74342017-08-11 17:34:44 -0700125int unlockpt(int __fd);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126
zijunzhao5a918d92022-11-28 21:05:55 +0000127int 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 -0700128
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129typedef struct {
Elliott Hughesfaa74342017-08-11 17:34:44 -0700130 int quot;
131 int rem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132} div_t;
133
Elliott Hughesfaa74342017-08-11 17:34:44 -0700134div_t div(int __numerator, int __denominator) __attribute_const__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135
136typedef struct {
Elliott Hughesfaa74342017-08-11 17:34:44 -0700137 long int quot;
138 long int rem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139} ldiv_t;
140
Elliott Hughesfaa74342017-08-11 17:34:44 -0700141ldiv_t ldiv(long __numerator, long __denominator) __attribute_const__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142
143typedef struct {
Elliott Hughesfaa74342017-08-11 17:34:44 -0700144 long long int quot;
145 long long int rem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146} lldiv_t;
147
Elliott Hughesfaa74342017-08-11 17:34:44 -0700148lldiv_t lldiv(long long __numerator, long long __denominator) __attribute_const__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700150/**
151 * [getloadavg(3)](http://man7.org/linux/man-pages/man3/getloadavg.3.html) queries the
152 * number of runnable processes averaged over time. The Linux kernel supports averages
153 * over the last 1, 5, and 15 minutes.
154 *
155 * Returns the number of samples written to `__averages` (at most 3), and returns -1 on failure.
156 */
zijunzhao5a918d92022-11-28 21:05:55 +0000157int getloadavg(double __averages[_Nonnull], int __n) __INTRODUCED_IN(29);
Elliott Hughes2d0b28b2018-10-23 11:23:00 -0700158
Elliott Hughes692207e2014-02-28 16:23:27 -0800159/* BSD compatibility. */
zijunzhao5a918d92022-11-28 21:05:55 +0000160const char* _Nullable getprogname(void) __INTRODUCED_IN(21);
161void setprogname(const char* _Nonnull __name) __INTRODUCED_IN(21);
Elliott Hughes692207e2014-02-28 16:23:27 -0800162
zijunzhao5a918d92022-11-28 21:05:55 +0000163int mblen(const char* _Nullable __s, size_t __n) __INTRODUCED_IN_NO_GUARD_FOR_NDK(26);
164size_t mbstowcs(wchar_t* _Nullable __dst, const char* _Nullable __src, size_t __n) __INTRODUCED_IN_NO_GUARD_FOR_NDK(21);
165int mbtowc(wchar_t* _Nullable __wc_ptr, const char* _Nullable __s, size_t __n) __INTRODUCED_IN_NO_GUARD_FOR_NDK(21);
166int wctomb(char* _Nullable __dst, wchar_t __wc) __INTRODUCED_IN_NO_GUARD_FOR_NDK(21);
Dan Albertcb0b1432016-09-06 16:51:00 -0700167
zijunzhao5a918d92022-11-28 21:05:55 +0000168size_t wcstombs(char* _Nullable __dst, const wchar_t* _Nullable __src, size_t __n) __INTRODUCED_IN_NO_GUARD_FOR_NDK(21);
David 'Digit' Turnerbb5581a2010-10-09 17:56:55 +0200169
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800170#if __ANDROID_API__ >= 21
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700171size_t __ctype_get_mb_cur_max(void) __INTRODUCED_IN(21);
Dan Albert224ff042014-08-14 13:56:51 -0700172#define MB_CUR_MAX __ctype_get_mb_cur_max()
Dan Albertcb0b1432016-09-06 16:51:00 -0700173#else
174/*
Dan Albert5c15b8c2017-02-21 15:54:58 -0800175 * Pre-L we didn't have any locale support and so we were always the POSIX
176 * locale. POSIX specifies that MB_CUR_MAX for the POSIX locale is 1:
177 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdlib.h.html
Dan Albertcb0b1432016-09-06 16:51:00 -0700178 */
Dan Albert5c15b8c2017-02-21 15:54:58 -0800179#define MB_CUR_MAX 1
Dan Albertcb0b1432016-09-06 16:51:00 -0700180#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181
George Burgess IVb97049c2017-07-24 15:05:05 -0700182#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
183#include <bits/fortify/stdlib.h>
184#endif
Daniel Micay3244d9f2015-04-18 13:04:19 -0400185
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800186#if __ANDROID_API__ >= 19
Ryan Prichard51a7fe82018-01-08 16:18:48 -0800187int abs(int __x) __attribute_const__ __INTRODUCED_IN(19);
188long labs(long __x) __attribute_const__ __INTRODUCED_IN(19);
189long long llabs(long long __x) __attribute_const__ __INTRODUCED_IN(19);
190#else
191// Implemented as static inlines before 19.
192#endif
193
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800194#if __ANDROID_API__ >= 21
zijunzhao5a918d92022-11-28 21:05:55 +0000195float strtof(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr) __INTRODUCED_IN(21);
196double atof(const char* _Nonnull __s) __attribute_pure__ __INTRODUCED_IN(21);
Josh Gaob6a4a4c2016-07-26 16:34:40 -0700197int rand(void) __INTRODUCED_IN(21);
Elliott Hughesfaa74342017-08-11 17:34:44 -0700198void srand(unsigned int __seed) __INTRODUCED_IN(21);
Josh Gaob6a4a4c2016-07-26 16:34:40 -0700199long random(void) __INTRODUCED_IN(21);
Elliott Hughesfaa74342017-08-11 17:34:44 -0700200void srandom(unsigned int __seed) __INTRODUCED_IN(21);
201int grantpt(int __fd) __INTRODUCED_IN(21);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700202
zijunzhao5a918d92022-11-28 21:05:55 +0000203long long strtoll_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l) __INTRODUCED_IN(21);
204unsigned long long strtoull_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l) __INTRODUCED_IN(21);
205long double strtold_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l) __INTRODUCED_IN(21);
Josh Gao8778d642016-07-22 15:26:36 -0700206#else
207// Implemented as static inlines before 21.
208#endif
209
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800210#if __ANDROID_API__ >= 26
zijunzhao5a918d92022-11-28 21:05:55 +0000211double strtod_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l) __INTRODUCED_IN(26);
212float strtof_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l) __INTRODUCED_IN(26);
213long strtol_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int, locale_t _Nonnull __l) __INTRODUCED_IN(26);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700214#else
Elliott Hughes4cae5c32017-07-10 11:51:00 -0700215// Implemented as static inlines before 26.
Josh Gao6cd9fb02016-09-23 14:06:05 -0700216#endif
217
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218__END_DECLS
219
Josh Gaob8e1b7052016-04-13 17:18:20 -0700220#include <android/legacy_stdlib_inlines.h>
221
Elliott Hughes76f89162015-01-26 13:34:58 -0800222#endif /* _STDLIB_H */