Fri Jan 26 12:20:45 1996 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / stdlib / stdlib.h
blob7c9c68974b937993fd053e80a0d4236482a9c2f9
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the, 1992 Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
20 * ANSI Standard: 4.10 GENERAL UTILITIES <stdlib.h>
23 #ifndef _STDLIB_H
25 #define _STDLIB_H 1
26 #include <features.h>
28 /* Get size_t, wchar_t and NULL from <stddef.h>. */
29 #define __need_size_t
30 #define __need_wchar_t
31 #define __need_NULL
32 #include <stddef.h>
34 #define __need_Emath
35 #include <errno.h>
37 __BEGIN_DECLS
39 /* Returned by `div'. */
40 typedef struct
42 int quot; /* Quotient. */
43 int rem; /* Remainder. */
44 } div_t;
46 /* Returned by `ldiv'. */
47 typedef struct
49 long int quot; /* Quotient. */
50 long int rem; /* Remainder. */
51 } ldiv_t;
54 /* The largest number rand will return (same as INT_MAX). */
55 #define RAND_MAX 2147483647
58 /* We define these the same for all machines.
59 Changes from this to the outside world should be done in `_exit'. */
60 #define EXIT_FAILURE 1 /* Failing exit status. */
61 #define EXIT_SUCCESS 0 /* Successful exit status. */
64 /* Maximum length of a multibyte character in the current locale.
65 This is just one until the fancy locale support is finished. */
66 #define MB_CUR_MAX 1
69 /* Convert a string to a floating-point number. */
70 extern double atof __P ((__const char *__nptr));
71 /* Convert a string to an integer. */
72 extern int atoi __P ((__const char *__nptr));
73 /* Convert a string to a long integer. */
74 extern long int atol __P ((__const char *__nptr));
76 /* Convert a string to a floating-point number. */
77 extern double strtod __P ((__const char *__nptr, char **__endptr));
79 #ifdef __USE_GNU
80 /* Likewise for `float' and `long double' sizes of floating-point numbers. */
81 extern float strtof __P ((__const char *__nptr, char **__endptr));
82 extern __long_double_t strtold __P ((__const char *__nptr, char **__endptr));
83 #endif
85 /* Convert a string to a long integer. */
86 extern long int strtol __P ((__const char *__nptr, char **__endptr,
87 int __base));
88 /* Convert a string to an unsigned long integer. */
89 extern unsigned long int strtoul __P ((__const char *__nptr,
90 char **__endptr, int __base));
92 #if defined (__GNUC__) && defined (__USE_BSD)
93 /* Convert a string to a quadword integer. */
94 extern long long int strtoq __P ((__const char *__nptr, char **__endptr,
95 int __base));
96 /* Convert a string to an unsigned quadword integer. */
97 extern unsigned long long int strtouq __P ((__const char *__nptr,
98 char **__endptr, int __base));
99 #endif /* GCC and use BSD. */
102 /* The internal entry points for `strtoX' take an extra flag argument
103 saying whether or not to parse locale-dependent number grouping. */
105 extern double __strtod_internal (__const char *__nptr,
106 char **__endptr, int __group);
107 extern float __strtof_internal (__const char *__nptr, char **__endptr,
108 int __group);
109 extern __long_double_t __strtold_internal (__const char *__nptr,
110 char **__endptr, int __group);
111 extern long int __strtol_internal (__const char *__nptr, char **__endptr,
112 int __base, int __group);
113 extern unsigned long int __strtoul_internal (__const char *__nptr,
114 char **__endptr, int __base,
115 int __group);
116 extern long long int __strtoq_internal (__const char *__nptr, char **__endptr,
117 int __base, int __group);
118 extern unsigned long long int __strtouq_internal (__const char *__nptr,
119 char **__endptr, int __base,
120 int __group);
122 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
123 /* Define inline functions which call the internal entry points. */
125 extern __inline double strtod (__const char *__nptr, char **__endptr)
126 { return __strtod_internal (__nptr, __endptr, 0); }
127 extern __inline long int strtol (__const char *__nptr,
128 char **__endptr, int __base)
129 { return __strtol_internal (__nptr, __endptr, __base, 0); }
130 extern __inline unsigned long int strtoul (__const char *__nptr,
131 char **__endptr, int __base)
132 { return __strtoul_internal (__nptr, __endptr, __base, 0); }
134 #ifdef __USE_GNU
135 extern __inline float strtof (__const char *__nptr, char **__endptr)
136 { return __strtof_internal (__nptr, __endptr, 0); }
137 extern __inline __long_double_t strtold (__const char *__nptr, char **__endptr)
138 { return __strtold_internal (__nptr, __endptr, 0); }
139 #endif
141 #ifdef __USE_BSD
142 extern __inline long long int strtoq (__const char *__nptr, char **__endptr,
143 int __base)
144 { return __strtoq_internal (__nptr, __endptr, __base, 0); }
145 extern __inline unsigned long long int strtouq (__const char *__nptr,
146 char **__endptr, int __base)
147 { return __strtouq_internal (__nptr, __endptr, __base, 0); }
148 #endif
150 extern __inline double atof (__const char *__nptr)
151 { return strtod (__nptr, (char **) NULL); }
152 extern __inline int atoi (__const char *__nptr)
153 { return (int) strtol (__nptr, (char **) NULL, 10); }
154 extern __inline long int atol (__const char *__nptr)
155 { return strtol (__nptr, (char **) NULL, 10); }
156 #endif /* Optimizing GCC >=2. */
159 #ifdef __USE_SVID
160 /* Convert N to base 64 using the digits "./0-9A-Za-z", least-significant
161 digit first. Returns a pointer to static storage overwritten by the
162 next call. */
163 extern char *l64a __P ((long int __n));
165 /* Read a number from a string in base 64 as above. */
166 extern long int a64l __P ((const char *));
167 #endif
170 /* Return a random integer between 0 and RAND_MAX inclusive. */
171 extern int rand __P ((void));
172 /* Seed the random number generator with the given number. */
173 extern void srand __P ((unsigned int __seed));
175 /* These are the functions that actually do things. The `random', `srandom',
176 `initstate' and `setstate' functions are those from BSD Unices.
177 The `rand' and `srand' functions are required by the ANSI standard.
178 We provide both interfaces to the same random number generator. */
179 /* Return a random long integer between 0 and RAND_MAX inclusive. */
180 extern long int __random __P ((void));
181 /* Seed the random number generator with the given number. */
182 extern void __srandom __P ((unsigned int __seed));
184 /* Initialize the random number generator to use state buffer STATEBUF,
185 of length STATELEN, and seed it with SEED. Optimal lengths are 8, 16,
186 32, 64, 128 and 256, the bigger the better; values less than 8 will
187 cause an error and values greater than 256 will be rounded down. */
188 extern __ptr_t __initstate __P ((unsigned int __seed, __ptr_t __statebuf,
189 size_t __statelen));
190 /* Switch the random number generator to state buffer STATEBUF,
191 which should have been previously initialized by `initstate'. */
192 extern __ptr_t __setstate __P ((__ptr_t __statebuf));
194 #ifdef __USE_BSD
195 extern long int random __P ((void));
196 extern void srandom __P ((unsigned int __seed));
197 extern __ptr_t initstate __P ((unsigned int __seed, __ptr_t __statebuf,
198 size_t __statelen));
199 extern __ptr_t setstate __P ((__ptr_t __statebuf));
201 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
202 extern __inline long int random (void)
203 { return __random(); }
204 extern __inline void srandom (unsigned int __seed)
205 { __srandom(__seed); }
206 extern __inline __ptr_t initstate (unsigned int __seed,
207 __ptr_t __statebuf, size_t __statelen)
208 { return __initstate (__seed, __statebuf, __statelen); }
209 extern __inline __ptr_t setstate (__ptr_t __statebuf)
210 { return __setstate (__statebuf); }
211 #endif /* Optimizing GCC >=2. */
213 #ifdef __USE_REENTRANT
214 /* Reentrant versions of the `random' family of functions.
215 These functions all use the following data structure to contain
216 state, rather than global state variables. */
218 struct random_data
220 long int *fptr; /* Front pointer. */
221 long int *rptr; /* Rear pointer. */
222 long int *state; /* Array of state values. */
223 int rand_type; /* Type of random number generator. */
224 int rand_deg; /* Degree of random number generator. */
225 int rand_sep; /* Distance between front and rear. */
226 long int *end_ptr; /* Pointer behind state table. */
229 extern int __random_r __P ((struct random_data *__buf, long int *__result));
230 extern int __srandom_r __P ((unsigned int __seed, struct random_data *__buf));
231 extern int __initstate_r __P ((unsigned int __seed, __ptr_t __statebuf,
232 size_t __statelen, struct random_data *__buf));
233 extern int __setstate_r __P ((__ptr_t __statebuf, struct random_data *__buf));
235 extern int random_r __P ((struct random_data *__buf, long int *__result));
236 extern int srandom_r __P ((unsigned int __seed, struct random_data *__buf));
237 extern int initstate_r __P ((unsigned int __seed, __ptr_t __statebuf,
238 size_t __statelen, struct random_data *__buf));
239 extern int setstate_r __P ((__ptr_t __statebuf, struct random_data *__buf));
240 #endif /* __USE_REENTRANT. */
241 #endif /* Use BSD. */
244 #ifdef __USE_SVID
245 /* System V style 48-bit random number generator functions. */
247 /* Data structure for communication with thread safe versions. */
248 struct drand48_data
250 unsigned short int X[3]; /* Current state. */
251 unsigned short int a[3]; /* Factor in congruential formula. */
252 unsigned short int c; /* Additive const. in congruential formula. */
253 unsigned short int old_X[3]; /* Old state. */
254 int init; /* Flag for initializing. */
257 /* Return non-negative, double-precision floating-point value in [0.0,1.0). */
258 extern double drand48 __P ((void));
259 extern int drand48_r __P ((struct drand48_data *__buffer, double *__result));
260 extern double erand48 __P ((unsigned short int __xsubi[3]));
261 extern int erand48_r __P ((unsigned short int __xsubi[3],
262 struct drand48_data *__buffer, double *__result));
263 /* Return non-negative, long integer in [0,2^31). */
264 extern long lrand48 __P ((void));
265 extern int lrand48_r __P ((struct drand48_data *__buffer, long *__result));
266 extern long nrand48 __P ((unsigned short int __xsubi[3]));
267 extern int nrand48_r __P ((unsigned short int __xsubi[3],
268 struct drand48_data *__buffer, long *__result));
269 /* Return signed, long integers in [-2^31,2^31). */
270 extern long mrand48 __P ((void));
271 extern int mrand48_r __P ((struct drand48_data *__buffer, long *__result));
272 extern long jrand48 __P ((unsigned short int __xsubi[3]));
273 extern int jrand48_r __P ((unsigned short int __xsubi[3],
274 struct drand48_data *__buffer, long *__result));
275 /* Seed random number generator. */
276 extern void srand48 __P ((long __seedval));
277 extern int srand48_r __P ((long __seedval, struct drand48_data *__buffer));
278 extern unsigned short int *seed48 __P ((unsigned short int __seed16v[3]));
279 extern int seed48_r __P ((unsigned short int __seed16v[3],
280 struct drand48_data *__buffer));
281 extern void lcong48 __P ((unsigned short int __param[7]));
282 extern int lcong48_r __P ((unsigned short int __param[7],
283 struct drand48_data *__buffer));
285 /* Internal function to compute next state of the generator. */
286 extern int __drand48_iterate __P ((unsigned short int __xsubi[3],
287 struct drand48_data *__buffer));
288 #endif /* __USE_SVID. */
291 /* Allocate SIZE bytes of memory. */
292 extern __ptr_t malloc __P ((size_t __size));
293 /* Re-allocate the previously allocated block
294 in __ptr_t, making the new block SIZE bytes long. */
295 extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size));
296 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
297 extern __ptr_t calloc __P ((size_t __nmemb, size_t __size));
298 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
299 extern void free __P ((__ptr_t __ptr));
301 #ifdef __USE_MISC
302 /* Free a block. An alias for `free'. (Sun Unices). */
303 extern void cfree __P ((__ptr_t __ptr));
304 #endif /* Use misc. */
306 #if defined(__USE_GNU) || defined(__USE_BSD) || defined(__USE_MISC)
307 #include <alloca.h>
308 #endif /* Use GNU, BSD, or misc. */
310 #ifdef __USE_BSD
311 /* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
312 extern __ptr_t valloc __P ((size_t __size));
313 #endif
316 /* Abort execution and generate a core-dump. */
317 extern void abort __P ((void)) __attribute__ ((__noreturn__));
320 /* Register a function to be called when `exit' is called. */
321 extern int atexit __P ((void (*__func) (void)));
323 #ifdef __USE_MISC
324 /* Register a function to be called with the status
325 given to `exit' and the given argument. */
326 extern int on_exit __P ((void (*__func) (int __status, __ptr_t __arg),
327 __ptr_t __arg));
328 #endif
330 /* Call all functions registered with `atexit' and `on_exit',
331 in the reverse of the order in which they were registered
332 perform stdio cleanup, and terminate program execution with STATUS. */
333 extern void exit __P ((int __status)) __attribute__ ((__noreturn__));
336 /* Return the value of envariable NAME, or NULL if it doesn't exist. */
337 extern char *getenv __P ((__const char *__name));
339 #ifdef __USE_SVID
340 /* The SVID says this is in <stdio.h>, but this seems a better place. */
341 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
342 If there is no `=', remove NAME from the environment. */
343 extern int putenv __P ((__const char *__string));
344 #endif
346 #ifdef __USE_BSD
347 /* Set NAME to VALUE in the environment.
348 If REPLACE is nonzero, overwrite an existing value. */
349 extern int setenv __P ((__const char *__name, __const char *__value,
350 int __replace));
352 /* Remove the variable NAME from the environment. */
353 extern void unsetenv __P ((__const char *__name));
354 #endif
356 /* Execute the given line as a shell command. */
357 extern int system __P ((__const char *__command));
360 /* Shorthand for type of comparison functions. */
361 #ifndef __COMPAR_FN_T
362 #define __COMPAR_FN_T
363 typedef int (*__compar_fn_t) __P ((__const __ptr_t, __const __ptr_t));
364 #endif
366 #ifdef __USE_GNU
367 typedef __compar_fn_t comparison_fn_t;
368 #endif
370 /* Do a binary search for KEY in BASE, which consists of NMEMB elements
371 of SIZE bytes each, using COMPAR to perform the comparisons. */
372 extern __ptr_t bsearch __P ((__const __ptr_t __key, __const __ptr_t __base,
373 size_t __nmemb, size_t __size,
374 __compar_fn_t __compar));
376 /* Sort NMEMB elements of BASE, of SIZE bytes each,
377 using COMPAR to perform the comparisons. */
378 extern void qsort __P ((__ptr_t __base, size_t __nmemb, size_t __size,
379 __compar_fn_t __compar));
382 /* Return the absolute value of X. */
383 extern int abs __P ((int __x)) __attribute__ ((__const__));
384 extern long int labs __P ((long int __x)) __attribute__ ((__const__));
387 /* Return the `div_t' or `ldiv_t' representation
388 of the value of NUMER over DENOM. */
389 /* GCC may have built-ins for these someday. */
390 extern div_t div __P ((int __numer, int __denom)) __attribute__ ((__const__));
391 extern ldiv_t ldiv __P ((long int __numer, long int __denom)) __attribute__ ((__const__));
394 #ifdef __USE_SVID
395 /* Convert floating point numbers to strings. The returned values are
396 valid only until another call to the same function. */
398 /* Convert VALUE to a string with NDIGIT digits and return a pointer to
399 this. Set *DECPT with the position of the decimal character and *SIGN
400 with the sign of the number. */
401 char *ecvt __P ((double __value, int __ndigit, int *__decpt, int *sign));
403 /* Convert VALUE to a string rounded to NDIGIT decimal digits. Set *DECPT
404 with the position of the decimal character and *SIGN with the sign of
405 the number. */
406 char *fcvt __P ((double __value, int __ndigit, int *__decpt, int *sign));
408 /* If possible convert VALUE to a string with NDIGIT significant digits.
409 Otherwise use exponential representation. The resulting string will
410 be written to BUF. */
411 char *gcvt __P ((double __value, int __ndigit, char *__buf));
413 /* Reentrant version of the functions above which provide their own
414 buffers. */
415 int ecvt_r __P ((double __value, int __ndigit, int *__decpt, int *sign,
416 char *__buf, size_t __len));
417 int fcvt_r __P ((double __value, int __ndigit, int *__decpt, int *sign,
418 char *__buf, size_t __len));
419 #endif
422 /* Return the length of the multibyte character
423 in S, which is no longer than N. */
424 extern int mblen __P ((__const char *__s, size_t __n));
425 /* Return the length of the given multibyte character,
426 putting its `wchar_t' representation in *PWC. */
427 extern int mbtowc __P ((wchar_t * __pwc, __const char *__s, size_t __n));
428 /* Put the multibyte character represented
429 by WCHAR in S, returning its length. */
430 extern int wctomb __P ((char *__s, wchar_t __wchar));
432 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
433 extern __inline int mblen (__const char *__s, size_t __n)
434 { return mbtowc ((wchar_t *) NULL, __s, __n); }
435 #endif /* Optimizing GCC >=2. */
438 /* Convert a multibyte string to a wide char string. */
439 extern size_t mbstowcs __P ((wchar_t * __pwcs, __const char *__s, size_t __n));
440 /* Convert a wide char string to multibyte string. */
441 extern size_t wcstombs __P ((char *__s, __const wchar_t * __pwcs, size_t __n));
444 __END_DECLS
446 #endif /* stdlib.h */