xref: /aosp_15_r20/bionic/libc/include/stdlib.h (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
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 
34 #include <alloca.h>
35 #include <bits/wait.h>
36 #include <malloc.h>
37 #include <stddef.h>
38 #include <xlocale.h>
39 
40 __BEGIN_DECLS
41 
42 #define EXIT_FAILURE 1
43 #define EXIT_SUCCESS 0
44 
45 __noreturn void abort(void) __attribute__((__nomerge__));
46 __noreturn void exit(int __status);
47 __noreturn void _Exit(int __status);
48 
49 int atexit(void (* _Nonnull __fn)(void));
50 
51 int at_quick_exit(void (* _Nonnull __fn)(void));
52 void quick_exit(int __status) __noreturn;
53 
54 char* _Nullable getenv(const char* _Nonnull __name);
55 int putenv(char* _Nonnull __assignment);
56 int setenv(const char* _Nonnull __name, const char* _Nonnull __value, int __overwrite);
57 int unsetenv(const char* _Nonnull __name);
58 int clearenv(void);
59 
60 char* _Nullable mkdtemp(char* _Nonnull __template);
61 char* _Nullable mktemp(char* _Nonnull __template) __attribute__((__deprecated__("mktemp is unsafe, use mkstemp or tmpfile instead")));
62 
63 
64 #if __BIONIC_AVAILABILITY_GUARD(23)
65 int mkostemp64(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23);
66 int mkostemp(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23);
67 int mkostemps64(char* _Nonnull __template, int __suffix_length, int __flags) __INTRODUCED_IN(23);
68 int mkostemps(char* _Nonnull __template, int __suffix_length, int __flags) __INTRODUCED_IN(23);
69 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */
70 
71 int mkstemp64(char* _Nonnull __template);
72 int mkstemp(char* _Nonnull __template);
73 
74 #if __BIONIC_AVAILABILITY_GUARD(23)
75 int mkstemps64(char* _Nonnull __template, int __flags) __INTRODUCED_IN(23);
76 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */
77 
78 int mkstemps(char* _Nonnull __template, int __flags);
79 
80 int posix_memalign(void* _Nullable * _Nullable __memptr, size_t __alignment, size_t __size);
81 
82 /**
83  * [aligned_alloc(3)](https://man7.org/linux/man-pages/man3/aligned_alloc.3.html)
84  * allocates the given number of bytes with the given alignment.
85  *
86  * Returns a pointer to the allocated memory on success and returns a null
87  * pointer and sets `errno` on failure.
88  *
89  * Available since API level 28.
90  */
91 
92 #if __BIONIC_AVAILABILITY_GUARD(28)
93 __nodiscard void* _Nullable aligned_alloc(size_t __alignment, size_t __size) __INTRODUCED_IN(28);
94 #endif /* __BIONIC_AVAILABILITY_GUARD(28) */
95 
96 
97 __nodiscard char* _Nullable realpath(const char* _Nonnull __path, char* _Nullable __resolved);
98 
99 /**
100  * [system(3)](https://man7.org/linux/man-pages/man3/system.3.html) executes
101  * the given command in a new shell process.
102  *
103  * On Android, the special case of `system(NULL)` always returns 1,
104  * as specified by POSIX. Passing `NULL` to determine whether or
105  * not a shell is available is not portable. Callers should just try
106  * the command they actually want to run, since there are many reasons
107  * why it might fail, both temporarily (for lack of resources, say)
108  * or permanently (for lack of permission, say).
109  *
110  * Returns -1 and sets errno if process creation fails; returns a
111  * [waitpid(2)](https://man7.org/linux/man-pages/man2/waitpid.2.html)
112  * status otherwise.
113  */
114 int system(const char* _Nonnull __command);
115 
116 /**
117  * [bsearch(3)](https://man7.org/linux/man-pages/man3/bsearch.3.html) searches
118  * a sorted array.
119  *
120  * Returns a pointer to a matching item on success,
121  * or NULL if no matching item is found.
122  */
123 __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));
124 
125 /**
126  * [qsort(3)](https://man7.org/linux/man-pages/man3/qsort.3.html) sorts an array
127  * of n elements each of the given size, using the given comparator.
128  */
129 void qsort(void* _Nullable __array, size_t __n, size_t __size, int (* _Nonnull __comparator)(const void* _Nullable __lhs, const void* _Nullable __rhs));
130 
131 /**
132  * [qsort_r(3)](https://man7.org/linux/man-pages/man3/qsort_r.3.html) sorts an
133  * array of n elements each of the given size, using the given comparator,
134  * and passing the given context argument to the comparator.
135  *
136  * Available since API level 36.
137  */
138 
139 #if __BIONIC_AVAILABILITY_GUARD(36)
140 void 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);
141 #endif /* __BIONIC_AVAILABILITY_GUARD(36) */
142 
143 
144 uint32_t arc4random(void);
145 uint32_t arc4random_uniform(uint32_t __upper_bound);
146 void arc4random_buf(void* _Nonnull __buf, size_t __n);
147 
148 #define RAND_MAX 0x7fffffff
149 
150 int rand_r(unsigned int* _Nonnull __seed_ptr);
151 
152 double drand48(void);
153 double erand48(unsigned short __xsubi[_Nonnull 3]);
154 long jrand48(unsigned short __xsubi[_Nonnull 3]);
155 
156 #if __BIONIC_AVAILABILITY_GUARD(23)
157 void lcong48(unsigned short __param[_Nonnull 7]) __INTRODUCED_IN(23);
158 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */
159 
160 long lrand48(void);
161 long mrand48(void);
162 long nrand48(unsigned short __xsubi[_Nonnull 3]);
163 unsigned short* _Nonnull seed48(unsigned short __seed16v[_Nonnull 3]);
164 void srand48(long __seed);
165 
166 char* _Nullable initstate(unsigned int __seed, char* _Nonnull __state, size_t __n);
167 char* _Nullable setstate(char* _Nonnull __state);
168 
169 int getpt(void);
170 int posix_openpt(int __flags);
171 char* _Nullable ptsname(int __fd);
172 int ptsname_r(int __fd, char* _Nonnull __buf, size_t __n);
173 int unlockpt(int __fd);
174 
175 
176 #if __BIONIC_AVAILABILITY_GUARD(26)
177 int getsubopt(char* _Nonnull * _Nonnull __option, char* _Nonnull const* _Nonnull __tokens, char* _Nullable * _Nonnull __value_ptr) __INTRODUCED_IN(26);
178 #endif /* __BIONIC_AVAILABILITY_GUARD(26) */
179 
180 
181 typedef struct {
182   int quot;
183   int rem;
184 } div_t;
185 
186 div_t div(int __numerator, int __denominator) __attribute_const__;
187 
188 typedef struct {
189   long int quot;
190   long int rem;
191 } ldiv_t;
192 
193 ldiv_t ldiv(long __numerator, long __denominator) __attribute_const__;
194 
195 typedef struct {
196   long long int quot;
197   long long int rem;
198 } lldiv_t;
199 
200 lldiv_t lldiv(long long __numerator, long long __denominator) __attribute_const__;
201 
202 /**
203  * [getloadavg(3)](https://man7.org/linux/man-pages/man3/getloadavg.3.html) queries the
204  * number of runnable processes averaged over time. The Linux kernel supports averages
205  * over the last 1, 5, and 15 minutes.
206  *
207  * Returns the number of samples written to `__averages` (at most 3), and returns -1 on failure.
208  */
209 
210 #if __BIONIC_AVAILABILITY_GUARD(29)
211 int getloadavg(double __averages[_Nonnull], int __n) __INTRODUCED_IN(29);
212 #endif /* __BIONIC_AVAILABILITY_GUARD(29) */
213 
214 
215 /* BSD compatibility. */
216 const char* _Nullable getprogname(void);
217 void setprogname(const char* _Nonnull __name);
218 
219 
220 #if __BIONIC_AVAILABILITY_GUARD(26)
221 int mblen(const char* _Nullable __s, size_t __n) __INTRODUCED_IN(26);
222 #endif /* __BIONIC_AVAILABILITY_GUARD(26) */
223 
224 size_t mbstowcs(wchar_t* _Nullable __dst, const char* _Nullable __src, size_t __n);
225 int mbtowc(wchar_t* _Nullable __wc_ptr, const char*  _Nullable __s, size_t __n);
226 int wctomb(char* _Nullable __dst, wchar_t __wc);
227 
228 size_t wcstombs(char* _Nullable __dst, const wchar_t* _Nullable __src, size_t __n);
229 
230 size_t __ctype_get_mb_cur_max(void);
231 #define MB_CUR_MAX __ctype_get_mb_cur_max()
232 
233 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
234 #include <bits/fortify/stdlib.h>
235 #endif
236 
237 int abs(int __x) __attribute_const__;
238 long labs(long __x) __attribute_const__;
239 long long llabs(long long __x) __attribute_const__;
240 
241 int rand(void);
242 void srand(unsigned int __seed);
243 long random(void);
244 void srandom(unsigned int __seed);
245 int grantpt(int __fd);
246 
247 /**
248  * [atof(3)](https://man7.org/linux/man-pages/man3/atof.3.html) converts a
249  * string to a double.
250  *
251  * Returns the double; use strtof() or strtod() if you need to detect errors.
252  */
253 double atof(const char* _Nonnull __s) __attribute_pure__;
254 
255 /**
256  * [atoi(3)](https://man7.org/linux/man-pages/man3/atoi.3.html) converts a
257  * string to an int.
258  *
259  * Returns the int or 0 on error; use strtol() if you need to detect errors.
260  */
261 int atoi(const char* _Nonnull __s) __attribute_pure__;
262 
263 /**
264  * [atol(3)](https://man7.org/linux/man-pages/man3/atol.3.html) converts a
265  * string to a long.
266  *
267  * Returns the long or 0 on error; use strtol() if you need to detect errors.
268  */
269 long atol(const char* _Nonnull __s) __attribute_pure__;
270 
271 /**
272  * [atoll(3)](https://man7.org/linux/man-pages/man3/atoll.3.html) converts a
273  * string to a long long.
274  *
275  * Returns the long long or 0 on error; use strtol() if you need to detect errors.
276  */
277 long long atoll(const char* _Nonnull __s) __attribute_pure__;
278 
279 /**
280  * [strtol(3)](https://man7.org/linux/man-pages/man3/strtol.3.html) converts a
281  * string to a long.
282  *
283  * Returns the long.
284  * `__end_ptr` is set to the last character in `__s` that was converted.
285  * errno is set to ERANGE if the result overflowed or underflowed.
286  */
287 long strtol(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
288 
289 /** Equivalent to strtol() on Android. */
290 long strtol_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int, locale_t _Nonnull __l) __RENAME(strtol);
291 
292 /**
293  * [strtoll(3)](https://man7.org/linux/man-pages/man3/strtoll.3.html) converts a
294  * string to a long long.
295  *
296  * Returns the long long.
297  * `__end_ptr` is set to the last character in `__s` that was converted.
298  * errno is set to ERANGE if the result overflowed or underflowed.
299  */
300 long long strtoll(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
301 
302 /** Equivalent to strtoll() on Android. */
303 long long strtoll_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l);
304 
305 /**
306  * [strtoul(3)](https://man7.org/linux/man-pages/man3/strtoul.3.html) converts a
307  * string to an unsigned long.
308  *
309  * Returns the unsigned long.
310  * `__end_ptr` is set to the last character in `__s` that was converted.
311  * errno is set to ERANGE if the result overflowed or underflowed.
312  */
313 unsigned long strtoul(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
314 
315 /** Equivalent to strtoul() on Android. */
316 unsigned long strtoul_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l) __RENAME(strtoul);
317 
318 /**
319  * [strtoull(3)](https://man7.org/linux/man-pages/man3/strtoull.3.html) converts a
320  * string to an unsigned long long.
321  *
322  * Returns the unsigned long long.
323  * `__end_ptr` is set to the last character in `__s` that was converted.
324  * errno is set to ERANGE if the result overflowed or underflowed.
325  */
326 unsigned long long strtoull(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base);
327 
328 /** Equivalent to strtoull() on Android. */
329 unsigned long long strtoull_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, int __base, locale_t _Nonnull __l);
330 
331 /**
332  * [strtof(3)](https://man7.org/linux/man-pages/man3/strtof.3.html) converts a
333  * string to a float.
334  *
335  * Returns the float.
336  * `__end_ptr` is set to the last character in `__s` that was converted.
337  * errno is set to ERANGE if the result overflowed or underflowed.
338  */
339 float strtof(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr);
340 
341 /**
342  * [strtod(3)](https://man7.org/linux/man-pages/man3/strtod.3.html) converts a
343  * string to a double.
344  *
345  * Returns the double.
346  * `__end_ptr` is set to the last character in `__s` that was converted.
347  * errno is set to ERANGE if the result overflowed or underflowed.
348  */
349 double strtod(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr);
350 
351 /**
352  * [strtold(3)](https://man7.org/linux/man-pages/man3/strtold.3.html) converts a
353  * string to a long double.
354  *
355  * Returns the long double.
356  * `__end_ptr` is set to the last character in `__s` that was converted.
357  * errno is set to ERANGE if the result overflowed or underflowed.
358  */
359 long double strtold(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr);
360 
361 /** Equivalent to strtold() on Android. */
362 long double strtold_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l);
363 
364 #if __ANDROID_API__ >= 26
365 /** Equivalent to strtod() on Android. */
366 double strtod_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l) __INTRODUCED_IN(26);
367 /** Equivalent to strtof() on Android. */
368 float strtof_l(const char* _Nonnull __s, char* _Nullable * _Nullable __end_ptr, locale_t _Nonnull __l) __INTRODUCED_IN(26);
369 #else
370 // Implemented as static inlines before 26.
371 #endif
372 
373 __END_DECLS
374 
375 #include <android/legacy_stdlib_inlines.h>
376 
377 #endif /* _STDLIB_H */
378