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);
45 __noreturn void exit(int __status);
46 #if __ANDROID_API__ >= 21
47 __noreturn void _Exit(int __status) __INTRODUCED_IN(21);
48 #else
49 __noreturn void _Exit(int) __RENAME(_exit);
50 #endif
51 
52 int atexit(void (*__fn)(void));
53 
54 int at_quick_exit(void (*__fn)(void)) __INTRODUCED_IN(21);
55 void quick_exit(int __status) __noreturn __INTRODUCED_IN(21);
56 
57 char* getenv(const char* __name);
58 int putenv(char* __assignment);
59 int setenv(const char* __name, const char* __value, int __overwrite);
60 int unsetenv(const char* __name);
61 int clearenv(void);
62 
63 char* mkdtemp(char* __template);
64 char* mktemp(char* __template) __attribute__((deprecated("mktemp is unsafe, use mkstemp or tmpfile instead")));
65 
66 int mkostemp64(char* __template, int __flags) __INTRODUCED_IN(23);
67 int mkostemp(char* __template, int __flags) __INTRODUCED_IN(23);
68 int mkostemps64(char* __template, int __suffix_length, int __flags) __INTRODUCED_IN(23);
69 int mkostemps(char* __template, int __suffix_length, int __flags) __INTRODUCED_IN(23);
70 int mkstemp64(char* __template) __INTRODUCED_IN(21);
71 int mkstemp(char* __template);
72 int mkstemps64(char* __template, int __flags) __INTRODUCED_IN(23);
73 int mkstemps(char* __template, int __flags);
74 
75 long strtol(const char* __s, char** __end_ptr, int __base);
76 long long strtoll(const char* __s, char** __end_ptr, int __base);
77 unsigned long strtoul(const char* __s, char** __end_ptr, int __base);
78 unsigned long long strtoull(const char* __s, char** __end_ptr, int __base);
79 
80 int posix_memalign(void** __memptr, size_t __alignment, size_t __size) __INTRODUCED_IN(17);
81 
82 void* aligned_alloc(size_t __alignment, size_t __size) __INTRODUCED_IN(28);
83 
84 double strtod(const char* __s, char** __end_ptr);
85 long double strtold(const char* __s, char** __end_ptr) __RENAME_LDBL(strtod, 3, 21);
86 
87 unsigned long strtoul_l(const char* __s, char** __end_ptr, int __base, locale_t __l) __INTRODUCED_IN(26);
88 
89 int atoi(const char* __s) __attribute_pure__;
90 long atol(const char* __s) __attribute_pure__;
91 long long atoll(const char* __s) __attribute_pure__;
92 
93 __wur char* realpath(const char* __path, char* __resolved);
94 int system(const char* __command);
95 
96 void* bsearch(const void* __key, const void* __base, size_t __nmemb, size_t __size, int (*__comparator)(const void* __lhs, const void* __rhs));
97 
98 void qsort(void* __base, size_t __nmemb, size_t __size, int (*__comparator)(const void* __lhs, const void* __rhs));
99 
100 uint32_t arc4random(void);
101 uint32_t arc4random_uniform(uint32_t __upper_bound);
102 void arc4random_buf(void* __buf, size_t __n);
103 
104 #define RAND_MAX 0x7fffffff
105 
106 int rand_r(unsigned int* __seed_ptr) __INTRODUCED_IN(21);
107 
108 double drand48(void);
109 double erand48(unsigned short __xsubi[3]);
110 long jrand48(unsigned short __xsubi[3]);
111 void lcong48(unsigned short __param[7]) __INTRODUCED_IN(23);
112 long lrand48(void);
113 long mrand48(void);
114 long nrand48(unsigned short __xsubi[3]);
115 unsigned short* seed48(unsigned short __seed16v[3]);
116 void srand48(long __seed);
117 
118 char* initstate(unsigned int __seed, char* __state, size_t __n) __INTRODUCED_IN(21);
119 char* setstate(char* __state) __INTRODUCED_IN(21);
120 
121 int getpt(void);
122 int posix_openpt(int __flags) __INTRODUCED_IN(21);
123 char* ptsname(int __fd);
124 int ptsname_r(int __fd, char* __buf, size_t __n);
125 int unlockpt(int __fd);
126 
127 int getsubopt(char** __option, char* const* __tokens, char** __value_ptr) __INTRODUCED_IN(26);
128 
129 typedef struct {
130   int quot;
131   int rem;
132 } div_t;
133 
134 div_t div(int __numerator, int __denominator) __attribute_const__;
135 
136 typedef struct {
137   long int quot;
138   long int rem;
139 } ldiv_t;
140 
141 ldiv_t ldiv(long __numerator, long __denominator) __attribute_const__;
142 
143 typedef struct {
144   long long int quot;
145   long long int rem;
146 } lldiv_t;
147 
148 lldiv_t lldiv(long long __numerator, long long __denominator) __attribute_const__;
149 
150 /**
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  */
157 int getloadavg(double __averages[], int __n) __INTRODUCED_IN(29);
158 
159 /* BSD compatibility. */
160 const char* getprogname(void) __INTRODUCED_IN(21);
161 void setprogname(const char* __name) __INTRODUCED_IN(21);
162 
163 int mblen(const char* __s, size_t __n) __INTRODUCED_IN_NO_GUARD_FOR_NDK(26);
164 size_t mbstowcs(wchar_t* __dst, const char* __src, size_t __n);
165 int mbtowc(wchar_t* __wc_ptr, const char* __s, size_t __n) __INTRODUCED_IN_NO_GUARD_FOR_NDK(21);
166 int wctomb(char* __dst, wchar_t __wc) __INTRODUCED_IN_NO_GUARD_FOR_NDK(21);
167 
168 size_t wcstombs(char* __dst, const wchar_t* __src, size_t __n);
169 
170 #if __ANDROID_API__ >= 21
171 size_t __ctype_get_mb_cur_max(void) __INTRODUCED_IN(21);
172 #define MB_CUR_MAX __ctype_get_mb_cur_max()
173 #else
174 /*
175  * 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
178  */
179 #define MB_CUR_MAX 1
180 #endif
181 
182 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
183 #include <bits/fortify/stdlib.h>
184 #endif
185 
186 #if __ANDROID_API__ >= 19
187 int abs(int __x) __attribute_const__ __INTRODUCED_IN(19);
188 long labs(long __x) __attribute_const__ __INTRODUCED_IN(19);
189 long long llabs(long long __x) __attribute_const__ __INTRODUCED_IN(19);
190 #else
191 // Implemented as static inlines before 19.
192 #endif
193 
194 #if __ANDROID_API__ >= 21
195 float strtof(const char* __s, char** __end_ptr) __INTRODUCED_IN(21);
196 double atof(const char* __s) __attribute_pure__ __INTRODUCED_IN(21);
197 int rand(void) __INTRODUCED_IN(21);
198 void srand(unsigned int __seed) __INTRODUCED_IN(21);
199 long random(void) __INTRODUCED_IN(21);
200 void srandom(unsigned int __seed) __INTRODUCED_IN(21);
201 int grantpt(int __fd) __INTRODUCED_IN(21);
202 
203 long long strtoll_l(const char* __s, char** __end_ptr, int __base, locale_t __l) __INTRODUCED_IN(21);
204 unsigned long long strtoull_l(const char* __s, char** __end_ptr, int __base, locale_t __l) __INTRODUCED_IN(21);
205 long double strtold_l(const char* __s, char** __end_ptr, locale_t __l) __INTRODUCED_IN(21);
206 #else
207 // Implemented as static inlines before 21.
208 #endif
209 
210 #if __ANDROID_API__ >= 26
211 double strtod_l(const char* __s, char** __end_ptr, locale_t __l) __INTRODUCED_IN(26);
212 float strtof_l(const char* __s, char** __end_ptr, locale_t __l) __INTRODUCED_IN(26);
213 long strtol_l(const char* __s, char** __end_ptr, int, locale_t __l) __INTRODUCED_IN(26);
214 #else
215 // Implemented as static inlines before 26.
216 #endif
217 
218 __END_DECLS
219 
220 #include <android/legacy_stdlib_inlines.h>
221 
222 #endif /* _STDLIB_H */
223