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