1 /* 2 * Copyright 2002-2006, 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 17 #define RAND_MAX 0x7fffffff 18 #define MB_CUR_MAX 1 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 nmemb, size_t size); 49 extern void free(void *pointer); 50 extern void *malloc(size_t size); 51 extern void *realloc(void * ptr, size_t size); 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 59 /* misc functions */ 60 extern char *realpath(const char *path, char *resolved); 61 62 extern int daemon(int nochdir, int noclose); 63 extern int system(const char *command); 64 65 extern char *mktemp(char *name); 66 extern int mkstemp(char *templat); 67 68 /* environment variables */ 69 extern char **environ; 70 extern char *getenv(const char *name); 71 extern int putenv(const char *string); 72 extern int setenv(char const *name, char const *value, int rewrite); 73 extern int unsetenv(const char *name); 74 75 /* ASCII string to number conversion */ 76 extern double atof(const char *string); 77 extern int atoi(const char *string); 78 extern long atol(const char *string); 79 extern long long int atoll(const char *string); 80 extern unsigned int atoui(const char *string); 81 extern unsigned long atoul(const char *string); 82 83 extern double strtod(const char *string, char **end); 84 extern long strtol(const char *string, char **end, int base); 85 extern unsigned long strtoul(const char *string, char **end, int base); 86 extern long long strtoll(const char *string, char **end, int base); 87 extern unsigned long long strtoull(const char *string, char **end, int base); 88 89 /* random number generation */ 90 extern void srand(unsigned int seed); 91 extern int rand(void); 92 extern int random(void); 93 extern void srandom(unsigned int seed); 94 extern int rand_r(unsigned int *seed); 95 extern int random_r(struct random_data *data, int *result); 96 extern int srandom_r(unsigned int seed, struct random_data *data); 97 extern char *initstate(unsigned int seed, char *state, size_t size); 98 extern char *setstate(char *state); 99 extern int initstate_r(unsigned int seed, void *stateBuffer, 100 size_t stateLength, struct random_data *data); 101 extern int setstate_r(void *stateBuffer, struct random_data *data); 102 103 extern double drand48(void); 104 extern double erand48(unsigned short int xsubi[3]); 105 extern long lrand48(void); 106 extern long nrand48(unsigned short int xsubi[3]); 107 extern long mrand48(void); 108 extern long jrand48(unsigned short int xsubi[3]); 109 extern void srand48(long int seed); 110 extern unsigned short *seed48(unsigned short int seed16v[3]); 111 extern void lcong48(unsigned short int param[7]); 112 113 extern int drand48_r(struct drand48_data *data, double *result); 114 extern int erand48_r(unsigned short int xsubi[3], 115 struct drand48_data *data, double *result); 116 extern int lrand48_r(struct drand48_data *data, long int *result); 117 extern int nrand48_r(unsigned short int xsubi[3], 118 struct drand48_data *data, long int *result); 119 extern int mrand48_r(struct drand48_data *data, long int *result); 120 extern int jrand48_r(unsigned short int xsubi[3], 121 struct drand48_data *data, long int *result); 122 extern int srand48_r(long int seed, struct drand48_data *data); 123 extern int seed48_r(unsigned short int seed16v[3], 124 struct drand48_data *data); 125 extern int lcong48_r(unsigned short int param[7], 126 struct drand48_data *data); 127 128 /* search and sort functions */ 129 typedef int (*_compare_function)(const void *, const void *); 130 131 extern void *bsearch(const void *key, const void *base, size_t numElements, 132 size_t sizeOfElement, _compare_function); 133 extern int heapsort(void *base, size_t numElements, size_t sizeOfElement, _compare_function); 134 extern int mergesort(void *base, size_t numElements, size_t sizeOfElement, _compare_function); 135 extern void qsort(void *base, size_t numElements, size_t sizeOfElement, _compare_function); 136 extern int radixsort(u_char const **base, int numElements, u_char const *table, u_int endByte); 137 extern int sradixsort(u_char const **base, int numElements, u_char const *table, u_int endByte); 138 139 /* misc math functions */ 140 extern int abs(int number); 141 extern long labs(long number); 142 extern long long llabs(long long number); 143 144 extern div_t div(int numerator, int denominator); 145 extern ldiv_t ldiv(long numerator, long denominator); 146 extern lldiv_t lldiv(long long numerator, long long denominator); 147 148 /* wide & multibyte string functions */ 149 extern int mblen(const char *string, size_t maxSize); 150 extern int mbtowc(wchar_t *pwc, const char *string, size_t maxSize); 151 extern int wctomb(char *string, wchar_t wchar); 152 extern size_t mbstowcs(wchar_t *pwcs, const char *string, size_t maxSize); 153 extern size_t wcstombs(char *string, const wchar_t *pwcs, size_t maxSize); 154 155 /* crypt */ 156 extern void setkey(const char *key); 157 158 #ifdef __cplusplus 159 } /* extern "C" */ 160 #endif 161 162 #endif /* _STDLIB_H_ */ 163