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#ifndef _STDLIB_H_
29#define _STDLIB_H_
30
31#include <sys/cdefs.h>
32
33/* wchar_t is required in stdlib.h according to POSIX.
34 * note that defining __need_wchar_t prevents stddef.h
35 * to define all other symbols it does normally */
36#define __need_wchar_t
37#include <stddef.h>
38
39#include <stddef.h>
40#include <string.h>
41#include <alloca.h>
42#include <strings.h>
43#include <memory.h>
44
45__BEGIN_DECLS
46
47#define EXIT_FAILURE 1
48#define EXIT_SUCCESS 0
49
50extern __noreturn void exit(int);
51extern __noreturn void abort(void);
52extern int atexit(void (*)(void));
53
54extern char *getenv(const char *);
55extern int putenv(const char *);
56extern int setenv(const char *, const char *, int);
57extern int unsetenv(const char *);
58extern int clearenv(void);
59
60extern char *mkdtemp(char *);
61extern char *mktemp (char *);
62extern int mkstemp (char *);
63
64extern long strtol(const char *, char **, int);
65extern long long strtoll(const char *, char **, int);
66extern unsigned long strtoul(const char *, char **, int);
67extern unsigned long long strtoull(const char *, char **, int);
68extern double strtod(const char *nptr, char **endptr);
69
70extern int posix_memalign(void **memptr, size_t alignment, size_t size);
71
72static __inline__ float strtof(const char *nptr, char **endptr)
73{
74    return (float)strtod(nptr, endptr);
75}
76
77extern int atoi(const char *) __purefunc;
78extern long atol(const char *) __purefunc;
79extern long long atoll(const char *) __purefunc;
80
81static __inline__ double atof(const char *nptr)
82{
83    return (strtod(nptr, NULL));
84}
85
86static __inline__ int abs(int __n) {
87    return (__n < 0) ? -__n : __n;
88}
89
90static __inline__ long labs(long __n) {
91    return (__n < 0L) ? -__n : __n;
92}
93
94static __inline__ long long llabs(long long __n) {
95    return (__n < 0LL) ? -__n : __n;
96}
97
98extern char * realpath(const char *path, char *resolved);
99extern int system(const char * string);
100
101extern void * bsearch(const void *key, const void *base0,
102	size_t nmemb, size_t size,
103	int (*compar)(const void *, const void *));
104
105extern void qsort(void *, size_t, size_t, int (*)(const void *, const void *));
106
107extern long jrand48(unsigned short *);
108extern long mrand48(void);
109extern long nrand48(unsigned short *);
110extern long lrand48(void);
111extern unsigned short *seed48(unsigned short*);
112extern double erand48(unsigned short xsubi[3]);
113extern double drand48(void);
114extern void srand48(long);
115extern unsigned int arc4random(void);
116extern void arc4random_stir(void);
117extern void arc4random_addrandom(unsigned char *, int);
118
119#define RAND_MAX 0x7fffffff
120static __inline__ int rand(void) {
121    return (int)lrand48();
122}
123static __inline__ void srand(unsigned int __s) {
124    srand48(__s);
125}
126static __inline__ long random(void)
127{
128    return lrand48();
129}
130static __inline__ void srandom(unsigned int __s)
131{
132    srand48(__s);
133}
134
135/* Basic PTY functions.  These only work if devpts is mounted! */
136
137extern int    unlockpt(int);
138extern char*  ptsname(int);
139extern int    ptsname_r(int, char*, size_t);
140extern int    getpt(void);
141
142static __inline__ int grantpt(int __fd __attribute((unused)))
143{
144  (void)__fd;
145  return 0;     /* devpts does this all for us! */
146}
147
148typedef struct {
149    int  quot;
150    int  rem;
151} div_t;
152
153extern div_t   div(int, int);
154
155typedef struct {
156    long int  quot;
157    long int  rem;
158} ldiv_t;
159
160extern ldiv_t   ldiv(long, long);
161
162typedef struct {
163    long long int  quot;
164    long long int  rem;
165} lldiv_t;
166
167extern lldiv_t   lldiv(long long, long long);
168
169#if 1 /* MISSING FROM BIONIC - ENABLED FOR STLPort and libstdc++-v3 */
170/* make STLPort happy */
171extern int      mblen(const char *, size_t);
172extern size_t   mbstowcs(wchar_t *, const char *, size_t);
173extern int      mbtowc(wchar_t *, const char *, size_t);
174
175/* Likewise, make libstdc++-v3 happy.  */
176extern int	wctomb(char *, wchar_t);
177extern size_t	wcstombs(char *, const wchar_t *, size_t);
178#endif /* MISSING */
179
180#define MB_CUR_MAX 1
181
182#if 0 /* MISSING FROM BIONIC */
183extern int on_exit(void (*)(int, void *), void *);
184#endif /* MISSING */
185
186__END_DECLS
187
188#endif /* _STDLIB_H_ */
189