xref: /haiku/headers/posix/stdlib.h (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 /*
2  * Copyright 2002-2021 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _STDLIB_H_
6 #define _STDLIB_H_
7 
8 
9 #include <alloca.h>
10 #include <div_t.h>
11 #include <limits.h>
12 #include <stddef.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <wchar_t.h>
16 
17 #define RAND_MAX      0x7fffffff
18 #define MB_CUR_MAX    (__ctype_get_mb_cur_max())
19 
20 #define EXIT_SUCCESS  0
21 #define EXIT_FAILURE  1
22 
23 /* random data structures (for thread-safe random numbers) */
24 struct random_data  {
25     int *fptr;
26     int *rptr;
27     int *state;
28     int rand_type;
29     int rand_deg;
30     int rand_sep;
31     int *end_ptr;
32 };
33 
34 struct drand48_data  {
35     unsigned short int x[3];
36     unsigned short int a[3];
37     unsigned short int c;
38     unsigned short int old_x[3];
39     int init;
40 };
41 
42 
43 #ifdef  __cplusplus
44 extern "C" {
45 #endif
46 
47 /* memory allocation (see malloc.h for additional defines & prototypes) */
48 extern void		*calloc(size_t numElements, size_t size);
49 extern void		free(void *pointer);
50 extern void		*malloc(size_t size);
51 extern int		posix_memalign(void **_pointer, size_t alignment, size_t size);
52 extern void 	*aligned_alloc(size_t alignment, size_t size) _ALIGNED_BY_ARG(1);
53 extern void		*realloc(void *oldPointer, size_t newSize);
54 
55 /* process termination */
56 extern void		abort(void) __attribute__((noreturn));
57 extern int		atexit(void (*func)(void));
58 extern int		atfork(void (*func)(void));
59 extern void		exit(int) __attribute__((noreturn));
60 extern void		_Exit(int) __attribute__((noreturn));
61 
62 /* misc functions */
63 extern char		*realpath(const char *path, char *resolved);
64 
65 extern int		system(const char *command);
66 
67 extern char		*mktemp(char *name);
68 extern char		*mkdtemp(char *templat);
69 extern int		mkstemp(char *templat);
70 
71 extern char		*ecvt(double value, int digits, int *_decimalPoint, int *_sign);
72 extern char		*fcvt(double value, int precision, int *_decimalPoint,
73 					int *_sign);
74 extern char		*gcvt(double value, int digits, char *buffer);
75 
76 extern char		*l64a(long n);
77 extern long		a64l(const char *string);
78 
79 /* environment variables */
80 extern char		**environ;
81 extern int		clearenv(void);
82 extern char		*getenv(const char *name);
83 extern int		putenv(char *string);
84 extern int		setenv(char const *name, char const *value, int rewrite);
85 extern int		unsetenv(const char *name);
86 
87 /* ASCII string to number conversion */
88 extern double			atof(const char *string);
89 extern int				atoi(const char *string);
90 extern long				atol(const char *string);
91 extern long long int	atoll(const char *string);
92 extern unsigned int 	atoui(const char *string);
93 extern unsigned long	atoul(const char *string);
94 
95 extern double			strtod(const char *string, char **end);
96 extern long double		strtold(const char *string, char **end);
97 extern float			strtof(const char *string, char **end);
98 extern long				strtol(const char *string, char **end, int base);
99 extern unsigned long	strtoul(const char *string, char **end, int base);
100 extern long long		strtoll(const char *string, char **end, int base);
101 extern unsigned long long strtoull(const char *string, char **end, int base);
102 
103 /* random number generation */
104 extern void		srand(unsigned int seed);
105 extern int		rand(void);
106 extern int		random(void);
107 extern void		srandom(unsigned int seed);
108 extern int		rand_r(unsigned int *seed);
109 extern int		random_r(struct random_data *data, int *result);
110 extern int		srandom_r(unsigned int seed, struct random_data *data);
111 extern char		*initstate(unsigned int seed, char *state, size_t size);
112 extern char		*setstate(char *state);
113 extern int		initstate_r(unsigned int seed, void *stateBuffer,
114 					size_t stateLength, struct random_data *data);
115 extern int		setstate_r(void *stateBuffer, struct random_data *data);
116 
117 extern double	drand48(void);
118 extern double	erand48(unsigned short int xsubi[3]);
119 extern long		lrand48(void);
120 extern long		nrand48(unsigned short int xsubi[3]);
121 extern long 	mrand48(void);
122 extern long		jrand48(unsigned short int xsubi[3]);
123 extern void		srand48(long int seed);
124 extern unsigned short *seed48(unsigned short int seed16v[3]);
125 extern void		lcong48(unsigned short int param[7]);
126 
127 extern int		drand48_r(struct drand48_data *data, double *result);
128 extern int		erand48_r(unsigned short int xsubi[3],
129 					struct drand48_data *data, double *result);
130 extern int		lrand48_r(struct drand48_data *data, long int *result);
131 extern int		nrand48_r(unsigned short int xsubi[3],
132 					struct drand48_data *data, long int *result);
133 extern int		mrand48_r(struct drand48_data *data, long int *result);
134 extern int		jrand48_r(unsigned short int xsubi[3],
135 					struct drand48_data *data, long int *result);
136 extern int		srand48_r(long int seed, struct drand48_data *data);
137 extern int		seed48_r(unsigned short int seed16v[3],
138 					struct drand48_data *data);
139 extern int		lcong48_r(unsigned short int param[7],
140 					struct drand48_data *data);
141 
142 /* search and sort functions */
143 typedef int (*_compare_function)(const void *, const void *);
144 
145 extern void		*bsearch(const void *key, const void *base, size_t numElements,
146 					size_t sizeOfElement, _compare_function);
147 extern int		heapsort(void *base, size_t numElements, size_t sizeOfElement,
148 					_compare_function);
149 extern int		mergesort(void *base, size_t numElements, size_t sizeOfElement,
150 					_compare_function);
151 extern void		qsort(void *base, size_t numElements, size_t sizeOfElement,
152 					_compare_function);
153 extern int		radixsort(u_char const **base, int numElements,
154 					u_char const *table, u_int endByte);
155 extern int		sradixsort(u_char const **base, int numElements,
156 					u_char const *table, u_int endByte);
157 
158 /* misc math functions */
159 extern int		abs(int number);
160 extern long		labs(long number);
161 extern long long llabs(long long number);
162 
163 extern div_t	div(int numerator,  int denominator);
164 extern ldiv_t	ldiv(long numerator, long denominator);
165 extern lldiv_t	lldiv(long long numerator, long long denominator);
166 
167 /* wide & multibyte string functions */
168 extern int		mblen(const char *string, size_t maxSize);
169 extern int		mbtowc(wchar_t *pwc, const char *string, size_t maxSize);
170 extern int		wctomb(char *string, wchar_t wchar);
171 extern size_t	mbstowcs(wchar_t *pwcs, const char *string, size_t maxSize);
172 extern size_t	wcstombs(char *string, const wchar_t *pwcs, size_t maxSize);
173 
174 /* crypt */
175 extern void 	setkey(const char *key);
176 
177 /* sub-option parsing */
178 extern int		getsubopt(char **optionp, char * const *keylistp,
179 					char **valuep);
180 
181 /* pty functions */
182 extern int		posix_openpt(int openFlags);
183 extern int		grantpt(int masterFD);
184 extern char*	ptsname(int masterFD);
185 extern int		unlockpt(int masterFD);
186 
187 /* internal accessor to value for MB_CUR_MAX */
188 extern unsigned short __ctype_get_mb_cur_max(void);
189 
190 #ifdef __cplusplus
191 } /* extern "C" */
192 #endif
193 
194 #endif /* _STDLIB_H_ */
195