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