forked from phoenix-rtos/libphoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib.h
240 lines (133 loc) · 6.23 KB
/
stdlib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Phoenix-RTOS
*
* libphoenix
*
* stdlib.h
*
* Copyright 2017 Phoenix Systems
* Author: Pawel Pisarczyk
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/
#ifndef _LIBPHOENIX_STDLIB_H_
#define _LIBPHOENIX_STDLIB_H_
#include <arch.h>
#include <alloca.h>
#define RAND_MAX 2147483647
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
#define ATEXIT_MAX 32
typedef struct {
int quot;
int rem;
} div_t;
typedef struct {
long int quot;
long int rem;
} ldiv_t;
/* Converts the string pointed to, by the argument str to a floating-point number (type double). */
extern double atof(const char *str);
/* Converts the string pointed to, by the argument str to an integer (type int). */
extern int atoi(const char *str);
/* Converts the string pointed to, by the argument str to a long integer (type long int). */
extern long int atol(const char *str);
/* Converts the string pointed to, by the argument str to a floating-point number (type double). */
extern double strtod(const char *str, char **endptr);
/* Converts the string pointed to, by the argument str to a long integer (type long int). */
extern long int strtol(const char *str, char **endptr, int base);
/* Converts the string pointed to, by the argument str to an unsigned long integer (type unsigned long int). */
extern unsigned long int strtoul(const char *str, char **endptr, int base);
/* Converts the string pointed to, by the argument str to a long long integer (type long long int). */
extern long long int strtoll(const char *nptr, char **endptr, int base);
/* Converts the string pointed to, by the argument str to an unsigned long long integer (type unsigned long long int). */
extern unsigned long long int strtoull(const char *nptr, char **endptr, int base);
/* Allocates the requested memory and returns a pointer to it. */
extern void *calloc(size_t nitems, size_t size);
/* Deallocates the memory previously allocated by a call to calloc, malloc, or realloc. */
extern void free(void *ptr);
/* Allocates the requested memory and returns a pointer to it. */
extern void *malloc(size_t size);
/* Attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc. */
extern void *realloc(void *ptr, size_t size);
#define reallocf realloc /* TODO: free on fail */
/* Causes an abnormal program termination. */
extern void abort(void);
/* Causes the specified function func to be called when the program terminates normally. */
extern int atexit(void (*func)(void));
/* Causes the program to terminate normally.*/
extern void exit(int status) __attribute__((noreturn));
/* Causes the program to terminate without cleanup.*/
extern void _Exit(int status);
/* Removes variable from the environment. */
int unsetenv(const char *name);
/* Adds or changes value of environment variable. */
int putenv(char *string);
/* Removes all variables set in the environment. */
int clearenv(void);
/* Searches for the environment string pointed to by name and returns the associated value to the string. */
extern char *getenv(const char *name);
/* Adds or changes environment variable. If name already exists in environment, it's value is changed only if overwrite is non-zero. */
extern int setenv(const char *name, const char *value, int overwrite);
/* The command specified by string is passed to the host environment to be executed by the command processor. */
extern int system(const char *string);
/* Performs a binary search.*/
extern void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *));
/* Sorts an array. */
extern void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*));
/* Returns the absolute value of x. */
static inline int abs(int x)
{
return x >= 0 ? x : -x;
}
/* Divides numer (numerator) by denom (denominator). */
extern div_t div(int numer, int denom);
/* Returns the absolute value of x. */
static inline long int labs(long int x)
{
return x >= 0 ? x : -x;
}
/* Returns the absolute value of x. */
static inline long long int llabs(long long int x)
{
return x >= 0 ? x : -x;
}
/* Divides numer (numerator) by denom (denominator). */
extern ldiv_t ldiv(long int numer, long int denom);
/* Returns a pseudo-random number in the range of 0 to RAND_MAX (reentrant version) */
extern int rand_r(unsigned int *seed);
/* Returns a pseudo-random number in the range of 0 to RAND_MAX. */
extern int rand(void);
/* This function seeds the random number generator used by the function rand. */
extern void srand(unsigned int seed);
/* Returns the length of a multibyte character pointed to by the argument str. */
extern int mblen(const char *str, size_t n);
/* Converts the string of multibyte characters pointed to by the argument str to the array pointed to by pwcs. */
extern size_t mbstowcs(wchar_t *pwcs, const char *str, size_t n);
/* Examines the multibyte character pointed to by the argument str. */
extern int mbtowc(wchar_t *pwc, const char *str, size_t n);
/* Converts the codes stored in the array pwcs to multibyte characters and stores them in the string str. */
extern size_t wcstombs(char *str, const wchar_t *pwcs, size_t n);
/* Examines the code which corresponds to a multibyte character given by the argument wchar. */
extern int wctomb(char *str, wchar_t wchar);
/* Returns name of the slave pseudoterminal device corresponding to the master referred to by fd. */
extern char *ptsname(int fd);
/* Reentrant version of ptsname. */
extern int ptsname_r(int fd, char *buf, size_t buflen);
/* Changes the mode and owner of the slave pseudoterminal device correspongin to the master referred to by fd. */
extern int grantpt(int fd);
/* Unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by fd. */
extern int unlockpt(int fd);
/* Return canonicalized absolute path, to be deallocated with free() */
char *canonicalize_file_name(const char *path);
double strtod(const char *restrict nptr, char **restrict endptr);
/* random number generator */
extern long random(void);
extern void srandom(unsigned int seed);
/* make unique filename */
extern int mkstemp(char *template);
/* register a funtion to run at process termination */
extern int atexit(void (*func)(void));
#endif