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 <sys/cdefs.h>
33 #include <xlocale.h>
34 
35 #include <alloca.h>
36 #include <malloc.h>
37 #include <stddef.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);
46 __noreturn void _Exit(int) __INTRODUCED_IN(21);
47 int atexit(void (*)(void));
48 
49 int at_quick_exit(void (*)(void)) __INTRODUCED_IN(21);
50 void quick_exit(int) __noreturn __INTRODUCED_IN(21);
51 
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);
57 
58 char* mkdtemp(char*);
59 char* mktemp(char*) __attribute__((deprecated("mktemp is unsafe, use mkstemp or tmpfile instead")));
60 
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);
69 
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);
74 
75 int posix_memalign(void** memptr, size_t alignment, size_t size) __INTRODUCED_IN(16);
76 
77 double strtod(const char*, char**);
78 long double strtold(const char*, char**) __INTRODUCED_IN(21);
79 
80 unsigned long strtoul_l(const char*, char**, int, locale_t) __INTRODUCED_IN(26);
81 
82 int atoi(const char*) __attribute_pure__;
83 long atol(const char*) __attribute_pure__;
84 long long atoll(const char*) __attribute_pure__;
85 
86 char * realpath(const char *path, char *resolved) __overloadable
87         __RENAME_CLANG(realpath);
88 int system(const char *string);
89 
90 void* bsearch(const void* key, const void* base0, size_t nmemb, size_t size,
91               int (*compar)(const void*, const void*));
92 
93 void qsort(void*, size_t, size_t, int (*)(const void*, const void*));
94 
95 uint32_t arc4random(void);
96 uint32_t arc4random_uniform(uint32_t);
97 void arc4random_buf(void*, size_t);
98 
99 #define RAND_MAX 0x7fffffff
100 
101 int rand_r(unsigned int*) __INTRODUCED_IN(21);
102 
103 double drand48(void);
104 double erand48(unsigned short[3]);
105 long jrand48(unsigned short[3]);
106 void lcong48(unsigned short[7]) __INTRODUCED_IN(23);
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 
113 char* initstate(unsigned int, char*, size_t) __INTRODUCED_IN(21);
114 char* setstate(char*) __INTRODUCED_IN(21);
115 
116 int getpt(void);
117 int posix_openpt(int) __INTRODUCED_IN(21);
118 char* ptsname(int);
119 int ptsname_r(int, char*, size_t);
120 int unlockpt(int);
121 
122 int getsubopt(char**, char* const*, char**) __INTRODUCED_IN(26);
123 
124 typedef struct {
125     int  quot;
126     int  rem;
127 } div_t;
128 
129 div_t div(int, int) __attribute_const__;
130 
131 typedef struct {
132     long int  quot;
133     long int  rem;
134 } ldiv_t;
135 
136 ldiv_t ldiv(long, long) __attribute_const__;
137 
138 typedef struct {
139     long long int  quot;
140     long long int  rem;
141 } lldiv_t;
142 
143 lldiv_t lldiv(long long, long long) __attribute_const__;
144 
145 /* BSD compatibility. */
146 const char* getprogname(void) __INTRODUCED_IN(21);
147 void setprogname(const char*) __INTRODUCED_IN(21);
148 
149 int mblen(const char*, size_t) __INTRODUCED_IN(26) __VERSIONER_NO_GUARD;
150 size_t mbstowcs(wchar_t*, const char*, size_t);
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 
154 size_t wcstombs(char*, const wchar_t*, size_t);
155 
156 #if __ANDROID_API__ >= __ANDROID_API_L__
157 size_t __ctype_get_mb_cur_max(void) __INTRODUCED_IN(21);
158 #define MB_CUR_MAX __ctype_get_mb_cur_max()
159 #else
160 /*
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
164  */
165 #define MB_CUR_MAX 1
166 #endif
167 
168 #if defined(__BIONIC_FORTIFY)
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__) */
188 
189 char* __realpath_real(const char*, char*) __RENAME(realpath);
190 __errordecl(__realpath_size_error, __realpath_buf_too_small_str);
191 
192 __BIONIC_FORTIFY_INLINE
realpath(const char * path,char * resolved)193 char* realpath(const char* path, char* resolved) {
194     size_t bos = __bos(resolved);
195 
196     if (bos != __BIONIC_FORTIFY_UNKNOWN_SIZE && bos < __PATH_MAX) {
197         __realpath_size_error();
198     }
199 
200     return __realpath_real(path, resolved);
201 }
202 
203 #endif /* defined(__clang__) */
204 
205 #undef __PATH_MAX
206 #undef __realpath_buf_too_small_str
207 #endif /* defined(__BIONIC_FORTIFY) */
208 
209 #if __ANDROID_API__ >= __ANDROID_API_L__
210 float strtof(const char*, char**) __INTRODUCED_IN(21);
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);
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);
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);
224 #else
225 // Implemented as static inlines before 21.
226 #endif
227 
228 #if __ANDROID_API__ >= __ANDROID_API_FUTURE__
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);
232 #else
233 // Implemented as static inlines.
234 #endif
235 
236 __END_DECLS
237 
238 #include <android/legacy_stdlib_inlines.h>
239 
240 #endif /* _STDLIB_H */
241