xref: /haiku/headers/posix/stdlib.h (revision 610f99c838cb661ff85377789ffd3ad4ff672a08)
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 #ifdef _ISOC11_SOURCE
53 extern void 	*aligned_alloc(size_t alignment, size_t size) _ALIGNED_BY_ARG(1);
54 #endif
55 extern void		*realloc(void *oldPointer, size_t newSize);
56 
57 /* process termination */
58 extern void		abort(void) __attribute__((noreturn));
59 extern int		atexit(void (*func)(void));
60 extern int		atfork(void (*func)(void));
61 extern void		exit(int) __attribute__((noreturn));
62 extern void		_Exit(int) __attribute__((noreturn));
63 
64 /* misc functions */
65 extern char		*realpath(const char *path, char *resolved);
66 
67 extern int		system(const char *command);
68 
69 extern char		*mktemp(char *name);
70 extern char		*mkdtemp(char *templat);
71 extern int		mkstemp(char *templat);
72 
73 extern char		*ecvt(double value, int digits, int *_decimalPoint, int *_sign);
74 extern char		*fcvt(double value, int precision, int *_decimalPoint,
75 					int *_sign);
76 extern char		*gcvt(double value, int digits, char *buffer);
77 
78 extern char		*l64a(long n);
79 extern long		a64l(const char *string);
80 
81 /* environment variables */
82 extern char		**environ;
83 extern int		clearenv(void);
84 extern char		*getenv(const char *name);
85 extern int		putenv(char *string);
86 extern int		setenv(char const *name, char const *value, int rewrite);
87 extern int		unsetenv(const char *name);
88 
89 /* ASCII string to number conversion */
90 extern double			atof(const char *string);
91 extern int				atoi(const char *string);
92 extern long				atol(const char *string);
93 extern long long int	atoll(const char *string);
94 extern unsigned int 	atoui(const char *string);
95 extern unsigned long	atoul(const char *string);
96 
97 extern double			strtod(const char *string, char **end);
98 extern long double		strtold(const char *string, char **end);
99 extern float			strtof(const char *string, char **end);
100 extern long				strtol(const char *string, char **end, int base);
101 extern unsigned long	strtoul(const char *string, char **end, int base);
102 extern long long		strtoll(const char *string, char **end, int base);
103 extern unsigned long long strtoull(const char *string, char **end, int base);
104 
105 /* random number generation */
106 extern void		srand(unsigned int seed);
107 extern int		rand(void);
108 extern int		random(void);
109 extern void		srandom(unsigned int seed);
110 extern int		rand_r(unsigned int *seed);
111 extern int		random_r(struct random_data *data, int *result);
112 extern int		srandom_r(unsigned int seed, struct random_data *data);
113 extern char		*initstate(unsigned int seed, char *state, size_t size);
114 extern char		*setstate(char *state);
115 extern int		initstate_r(unsigned int seed, void *stateBuffer,
116 					size_t stateLength, struct random_data *data);
117 extern int		setstate_r(void *stateBuffer, struct random_data *data);
118 
119 extern double	drand48(void);
120 extern double	erand48(unsigned short int xsubi[3]);
121 extern long		lrand48(void);
122 extern long		nrand48(unsigned short int xsubi[3]);
123 extern long 	mrand48(void);
124 extern long		jrand48(unsigned short int xsubi[3]);
125 extern void		srand48(long int seed);
126 extern unsigned short *seed48(unsigned short int seed16v[3]);
127 extern void		lcong48(unsigned short int param[7]);
128 
129 extern int		drand48_r(struct drand48_data *data, double *result);
130 extern int		erand48_r(unsigned short int xsubi[3],
131 					struct drand48_data *data, double *result);
132 extern int		lrand48_r(struct drand48_data *data, long int *result);
133 extern int		nrand48_r(unsigned short int xsubi[3],
134 					struct drand48_data *data, long int *result);
135 extern int		mrand48_r(struct drand48_data *data, long int *result);
136 extern int		jrand48_r(unsigned short int xsubi[3],
137 					struct drand48_data *data, long int *result);
138 extern int		srand48_r(long int seed, struct drand48_data *data);
139 extern int		seed48_r(unsigned short int seed16v[3],
140 					struct drand48_data *data);
141 extern int		lcong48_r(unsigned short int param[7],
142 					struct drand48_data *data);
143 
144 /* search and sort functions */
145 typedef int (*_compare_function)(const void *, const void *);
146 
147 extern void		*bsearch(const void *key, const void *base, size_t numElements,
148 					size_t sizeOfElement, _compare_function);
149 extern int		heapsort(void *base, size_t numElements, size_t sizeOfElement,
150 					_compare_function);
151 extern int		mergesort(void *base, size_t numElements, size_t sizeOfElement,
152 					_compare_function);
153 extern void		qsort(void *base, size_t numElements, size_t sizeOfElement,
154 					_compare_function);
155 extern int		radixsort(u_char const **base, int numElements,
156 					u_char const *table, u_int endByte);
157 extern int		sradixsort(u_char const **base, int numElements,
158 					u_char const *table, u_int endByte);
159 
160 /* misc math functions */
161 extern int		abs(int number);
162 extern long		labs(long number);
163 extern long long llabs(long long number);
164 
165 extern div_t	div(int numerator,  int denominator);
166 extern ldiv_t	ldiv(long numerator, long denominator);
167 extern lldiv_t	lldiv(long long numerator, long long denominator);
168 
169 /* wide & multibyte string functions */
170 extern int		mblen(const char *string, size_t maxSize);
171 extern int		mbtowc(wchar_t *pwc, const char *string, size_t maxSize);
172 extern int		wctomb(char *string, wchar_t wchar);
173 extern size_t	mbstowcs(wchar_t *pwcs, const char *string, size_t maxSize);
174 extern size_t	wcstombs(char *string, const wchar_t *pwcs, size_t maxSize);
175 
176 /* crypt */
177 extern void 	setkey(const char *key);
178 
179 /* sub-option parsing */
180 extern int		getsubopt(char **optionp, char * const *keylistp,
181 					char **valuep);
182 
183 /* pty functions */
184 extern int		posix_openpt(int openFlags);
185 extern int		grantpt(int masterFD);
186 extern char*	ptsname(int masterFD);
187 extern int		unlockpt(int masterFD);
188 
189 /* internal accessor to value for MB_CUR_MAX */
190 extern unsigned short __ctype_get_mb_cur_max(void);
191 
192 #ifdef __cplusplus
193 } /* extern "C" */
194 #endif
195 
196 #endif /* _STDLIB_H_ */
197