Update from main archive 961219
[glibc.git] / stdlib / stdlib.h
blob235b2f0b59bcef91f9401327eb25d1e9f26dedd5
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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
20 * ISO S 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 __BEGIN_DECLS
36 /* Returned by `div'. */
37 typedef struct
39 int quot; /* Quotient. */
40 int rem; /* Remainder. */
41 } div_t;
43 /* Returned by `ldiv'. */
44 typedef struct
46 long int quot; /* Quotient. */
47 long int rem; /* Remainder. */
48 } ldiv_t;
50 #ifdef __USE_GNU
51 /* Returned by `lldiv'. */
52 typedef struct
54 long long int quot; /* Quotient. */
55 long long int rem; /* Remainder. */
56 } lldiv_t;
57 #endif
60 /* The largest number rand will return (same as INT_MAX). */
61 #define RAND_MAX 2147483647
64 /* We define these the same for all machines.
65 Changes from this to the outside world should be done in `_exit'. */
66 #define EXIT_FAILURE 1 /* Failing exit status. */
67 #define EXIT_SUCCESS 0 /* Successful exit status. */
70 /* Maximum length of a multibyte character in the current locale. */
71 #define MB_CUR_MAX (__ctype_get_mb_cur_max ())
72 extern int __ctype_get_mb_cur_max __P ((void));
75 /* Convert a string to a floating-point number. */
76 extern double atof __P ((__const char *__nptr));
77 /* Convert a string to an integer. */
78 extern int atoi __P ((__const char *__nptr));
79 /* Convert a string to a long integer. */
80 extern long int atol __P ((__const char *__nptr));
82 /* Convert a string to a floating-point number. */
83 extern double strtod __P ((__const char *__nptr, char **__endptr));
85 #ifdef __USE_GNU
86 /* Likewise for `float' and `long double' sizes of floating-point numbers. */
87 extern float strtof __P ((__const char *__nptr, char **__endptr));
88 extern __long_double_t strtold __P ((__const char *__nptr, char **__endptr));
89 #endif
91 /* Convert a string to a long integer. */
92 extern long int strtol __P ((__const char *__nptr, char **__endptr,
93 int __base));
94 /* Convert a string to an unsigned long integer. */
95 extern unsigned long int strtoul __P ((__const char *__nptr,
96 char **__endptr, int __base));
98 #if defined (__GNUC__) && defined (__USE_BSD)
99 /* Convert a string to a quadword integer. */
100 extern long long int strtoq __P ((__const char *__nptr, char **__endptr,
101 int __base));
102 /* Convert a string to an unsigned quadword integer. */
103 extern unsigned long long int strtouq __P ((__const char *__nptr,
104 char **__endptr, int __base));
105 #endif /* GCC and use BSD. */
107 #if defined (__GNUC__) && defined (__USE_MISC)
108 /* Convert a string to a quadword integer. */
109 extern long long int strtoll __P ((__const char *__nptr, char **__endptr,
110 int __base));
111 /* Convert a string to an unsigned quadword integer. */
112 extern unsigned long long int strtoull __P ((__const char *__nptr,
113 char **__endptr, int __base));
114 #endif /* GCC and use MISC. */
118 /* The internal entry points for `strtoX' take an extra flag argument
119 saying whether or not to parse locale-dependent number grouping. */
121 extern double __strtod_internal __P ((__const char *__nptr,
122 char **__endptr, int __group));
123 extern float __strtof_internal __P ((__const char *__nptr, char **__endptr,
124 int __group));
125 extern __long_double_t __strtold_internal __P ((__const char *__nptr,
126 char **__endptr, int __group));
127 extern long int __strtol_internal __P ((__const char *__nptr, char **__endptr,
128 int __base, int __group));
129 extern unsigned long int __strtoul_internal __P ((__const char *__nptr,
130 char **__endptr, int __base,
131 int __group));
132 #ifdef __GNUC__
133 extern long long int __strtoq_internal __P ((__const char *__nptr,
134 char **__endptr, int __base,
135 int __group));
136 extern unsigned long long int __strtouq_internal __P ((__const char *__nptr,
137 char **__endptr,
138 int __base,
139 int __group));
140 #endif /* GCC */
142 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
143 /* Define inline functions which call the internal entry points. */
145 extern __inline double strtod (__const char *__nptr, char **__endptr)
146 { return __strtod_internal (__nptr, __endptr, 0); }
147 extern __inline long int strtol (__const char *__nptr,
148 char **__endptr, int __base)
149 { return __strtol_internal (__nptr, __endptr, __base, 0); }
150 extern __inline unsigned long int strtoul (__const char *__nptr,
151 char **__endptr, int __base)
152 { return __strtoul_internal (__nptr, __endptr, __base, 0); }
154 #ifdef __USE_GNU
155 extern __inline float strtof (__const char *__nptr, char **__endptr)
156 { return __strtof_internal (__nptr, __endptr, 0); }
157 extern __inline __long_double_t strtold (__const char *__nptr, char **__endptr)
158 { return __strtold_internal (__nptr, __endptr, 0); }
159 #endif
161 #ifdef __USE_BSD
162 extern __inline long long int strtoq (__const char *__nptr, char **__endptr,
163 int __base)
164 { return __strtoq_internal (__nptr, __endptr, __base, 0); }
165 extern __inline unsigned long long int strtouq (__const char *__nptr,
166 char **__endptr, int __base)
167 { return __strtouq_internal (__nptr, __endptr, __base, 0); }
168 #endif
170 #ifdef __USE_MISC
171 extern __inline long long int strtoll (__const char *__nptr, char **__endptr,
172 int __base)
173 { return __strtoq_internal (__nptr, __endptr, __base, 0); }
174 extern __inline unsigned long long int strtoull (__const char *__nptr,
175 char **__endptr, int __base)
176 { return __strtouq_internal (__nptr, __endptr, __base, 0); }
177 #endif
179 extern __inline double atof (__const char *__nptr)
180 { return strtod (__nptr, (char **) NULL); }
181 extern __inline int atoi (__const char *__nptr)
182 { return (int) strtol (__nptr, (char **) NULL, 10); }
183 extern __inline long int atol (__const char *__nptr)
184 { return strtol (__nptr, (char **) NULL, 10); }
185 #endif /* Optimizing GCC >=2. */
188 #if defined(__USE_SVID) || defined(__USE_XOPEN_EXTENDED)
189 /* Convert N to base 64 using the digits "./0-9A-Za-z", least-significant
190 digit first. Returns a pointer to static storage overwritten by the
191 next call. */
192 extern char *l64a __P ((long int __n));
194 /* Read a number from a string S in base 64 as above. */
195 extern long int a64l __P ((__const char *__s));
198 #include <sys/types.h> /* we need int32_t... */
200 /* These are the functions that actually do things. The `random', `srandom',
201 `initstate' and `setstate' functions are those from BSD Unices.
202 The `rand' and `srand' functions are required by the ANSI standard.
203 We provide both interfaces to the same random number generator. */
204 /* Return a random long integer between 0 and RAND_MAX inclusive. */
205 extern int32_t __random __P ((void));
206 extern int32_t random __P ((void));
208 /* Seed the random number generator with the given number. */
209 extern void __srandom __P ((unsigned int __seed));
210 extern void srandom __P ((unsigned int __seed));
212 /* Initialize the random number generator to use state buffer STATEBUF,
213 of length STATELEN, and seed it with SEED. Optimal lengths are 8, 16,
214 32, 64, 128 and 256, the bigger the better; values less than 8 will
215 cause an error and values greater than 256 will be rounded down. */
216 extern __ptr_t __initstate __P ((unsigned int __seed, __ptr_t __statebuf,
217 size_t __statelen));
218 extern __ptr_t initstate __P ((unsigned int __seed, __ptr_t __statebuf,
219 size_t __statelen));
221 /* Switch the random number generator to state buffer STATEBUF,
222 which should have been previously initialized by `initstate'. */
223 extern __ptr_t __setstate __P ((__ptr_t __statebuf));
224 extern __ptr_t setstate __P ((__ptr_t __statebuf));
227 #ifdef __USE_REENTRANT
228 /* Reentrant versions of the `random' family of functions.
229 These functions all use the following data structure to contain
230 state, rather than global state variables. */
232 struct random_data
234 int32_t *fptr; /* Front pointer. */
235 int32_t *rptr; /* Rear pointer. */
236 int32_t *state; /* Array of state values. */
237 int rand_type; /* Type of random number generator. */
238 int rand_deg; /* Degree of random number generator. */
239 int rand_sep; /* Distance between front and rear. */
240 int32_t *end_ptr; /* Pointer behind state table. */
243 extern int __random_r __P ((struct random_data *__buf, int32_t *__result));
244 extern int random_r __P ((struct random_data *__buf, int32_t *__result));
246 extern int __srandom_r __P ((unsigned int __seed, struct random_data *__buf));
247 extern int srandom_r __P ((unsigned int __seed, struct random_data *__buf));
249 extern int __initstate_r __P ((unsigned int __seed, __ptr_t __statebuf,
250 size_t __statelen, struct random_data *__buf));
251 extern int initstate_r __P ((unsigned int __seed, __ptr_t __statebuf,
252 size_t __statelen, struct random_data *__buf));
254 extern int __setstate_r __P ((__ptr_t __statebuf, struct random_data *__buf));
255 extern int setstate_r __P ((__ptr_t __statebuf, struct random_data *__buf));
256 #endif /* Use reentrant. */
257 #endif /* Use BSD. */
260 /* Return a random integer between 0 and RAND_MAX inclusive. */
261 extern int rand __P ((void));
262 /* Seed the random number generator with the given number. */
263 extern void srand __P ((unsigned int __seed));
265 #ifdef __USE_REENTRANT
266 /* Reentrant interface according to POSIX.1. */
267 extern int __rand_r __P ((unsigned int *__seed));
268 extern int rand_r __P ((unsigned int *__seed));
269 #endif
272 #if defined(__USE_SVID) || defined(__USE_XOPEN)
273 /* System V style 48-bit random number generator functions. */
275 /* Return non-negative, double-precision floating-point value in [0.0,1.0). */
276 extern double drand48 __P ((void));
277 extern double erand48 __P ((unsigned short int __xsubi[3]));
279 /* Return non-negative, long integer in [0,2^31). */
280 extern long lrand48 __P ((void));
281 extern long nrand48 __P ((unsigned short int __xsubi[3]));
283 /* Return signed, long integers in [-2^31,2^31). */
284 extern long mrand48 __P ((void));
285 extern long jrand48 __P ((unsigned short int __xsubi[3]));
287 /* Seed random number generator. */
288 extern void srand48 __P ((long __seedval));
289 extern unsigned short int *seed48 __P ((unsigned short int __seed16v[3]));
290 extern void lcong48 __P ((unsigned short int __param[7]));
292 /* Data structure for communication with thread safe versions. */
293 struct drand48_data
295 unsigned short int X[3]; /* Current state. */
296 unsigned short int a[3]; /* Factor in congruential formula. */
297 unsigned short int c; /* Additive const. in congruential formula. */
298 unsigned short int old_X[3]; /* Old state. */
299 int init; /* Flag for initializing. */
302 #ifdef __USE_REENTRANT
303 /* Return non-negative, double-precision floating-point value in [0.0,1.0). */
304 extern int drand48_r __P ((struct drand48_data *__buffer, double *__result));
305 extern int erand48_r __P ((unsigned short int __xsubi[3],
306 struct drand48_data *__buffer, double *__result));
308 /* Return non-negative, long integer in [0,2^31). */
309 extern int lrand48_r __P ((struct drand48_data *__buffer, long *__result));
310 extern int nrand48_r __P ((unsigned short int __xsubi[3],
311 struct drand48_data *__buffer, long *__result));
313 /* Return signed, long integers in [-2^31,2^31). */
314 extern int mrand48_r __P ((struct drand48_data *__buffer, long *__result));
315 extern int jrand48_r __P ((unsigned short int __xsubi[3],
316 struct drand48_data *__buffer, long *__result));
318 /* Seed random number generator. */
319 extern int srand48_r __P ((long __seedval, struct drand48_data *__buffer));
320 extern int seed48_r __P ((unsigned short int __seed16v[3],
321 struct drand48_data *__buffer));
322 extern int lcong48_r __P ((unsigned short int __param[7],
323 struct drand48_data *__buffer));
324 #endif /* Use reentrant. */
326 /* Internal function to compute next state of the generator. */
327 extern int __drand48_iterate __P ((unsigned short int __xsubi[3],
328 struct drand48_data *__buffer));
329 #endif /* Use SVID or X/Open. */
332 /* Allocate SIZE bytes of memory. */
333 extern __ptr_t malloc __P ((size_t __size));
334 /* Re-allocate the previously allocated block
335 in __ptr_t, making the new block SIZE bytes long. */
336 extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size));
337 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
338 extern __ptr_t calloc __P ((size_t __nmemb, size_t __size));
339 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
340 extern void free __P ((__ptr_t __ptr));
342 #ifdef __USE_MISC
343 /* Free a block. An alias for `free'. (Sun Unices). */
344 extern void cfree __P ((__ptr_t __ptr));
345 #endif /* Use misc. */
347 #if defined(__USE_GNU) || defined(__USE_BSD) || defined(__USE_MISC)
348 #include <alloca.h>
349 #endif /* Use GNU, BSD, or misc. */
351 #if defined(__USE_BSD) || defined(__USE_XOPEN_EXTENDED)
352 /* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
353 extern __ptr_t valloc __P ((size_t __size));
354 #endif
357 /* Abort execution and generate a core-dump. */
358 extern void abort __P ((void)) __attribute__ ((__noreturn__));
361 /* Register a function to be called when `exit' is called. */
362 extern int atexit __P ((void (*__func) (void)));
364 #ifdef __USE_MISC
365 /* Register a function to be called with the status
366 given to `exit' and the given argument. */
367 extern int __on_exit __P ((void (*__func) (int __status, __ptr_t __arg),
368 __ptr_t __arg));
369 extern int on_exit __P ((void (*__func) (int __status, __ptr_t __arg),
370 __ptr_t __arg));
371 #endif
373 /* Call all functions registered with `atexit' and `on_exit',
374 in the reverse of the order in which they were registered
375 perform stdio cleanup, and terminate program execution with STATUS. */
376 extern void exit __P ((int __status)) __attribute__ ((__noreturn__));
379 /* Return the value of envariable NAME, or NULL if it doesn't exist. */
380 extern char *getenv __P ((__const char *__name));
382 /* This function is similar to the above but returns NULL if the
383 programs is running with SUID or SGID enabled. */
384 extern char *__secure_getenv __P ((__const char *__name));
386 #if defined(__USE_SVID) || defined(__USE_XOPEN)
387 /* The SVID says this is in <stdio.h>, but this seems a better place. */
388 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
389 If there is no `=', remove NAME from the environment. */
390 extern int putenv __P ((__const char *__string));
391 #endif
393 #ifdef __USE_BSD
394 /* Set NAME to VALUE in the environment.
395 If REPLACE is nonzero, overwrite an existing value. */
396 extern int setenv __P ((__const char *__name, __const char *__value,
397 int __replace));
399 /* Remove the variable NAME from the environment. */
400 extern void unsetenv __P ((__const char *__name));
401 #endif
403 #ifdef __USE_MISC
404 /* The `clearenv' was planned to be added to POSIX.1 but probably
405 never made it. Nevertheless the POSIX.9 standard (POSIX bindings
406 for Fortran 77) requires this function. */
407 extern int clearenv __P ((void));
408 #endif
411 #if defined(__USE_MISC) || defined(__USE_XOPEN_EXTENDED)
412 /* Generate a unique temporary file name from TEMPLATE.
413 The last six characters of TEMPLATE must be "XXXXXX";
414 they are replaced with a string that makes the file name unique.
415 Returns TEMPLATE, or a null pointer if it cannot get a unique file name. */
416 extern char *mktemp __P ((char *__template));
418 /* Generate a unique temporary file name from TEMPLATE.
419 The last six characters of TEMPLATE must be "XXXXXX";
420 they are replaced with a string that makes the filename unique.
421 Returns a file descriptor open on the file for reading and writing,
422 or -1 if it cannot create a uniquely-named file. */
423 extern int mkstemp __P ((char *__template));
424 #endif
427 /* Execute the given line as a shell command. */
428 extern int system __P ((__const char *__command));
431 #ifdef __USE_GNU
432 /* Return a malloc'd string containing the canonical absolute name of the
433 named file. The last file name component need not exist, and may be a
434 symlink to a nonexistent file. */
435 extern char *canonicalize_file_name __P ((__const char *__name));
436 #endif
438 #if defined(__USE_BSD) || defined(__USE_XOPEN_EXTENDED)
439 /* Return the canonical absolute name of file NAME. The last file name
440 component need not exist, and may be a symlink to a nonexistent file.
441 If RESOLVED is null, the result is malloc'd; otherwise, if the canonical
442 name is PATH_MAX chars or more, returns null with `errno' set to
443 ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars, returns the
444 name in RESOLVED. */
445 extern char *realpath __P ((__const char *__name, char *__resolved));
446 #endif
449 /* Shorthand for type of comparison functions. */
450 #ifndef __COMPAR_FN_T
451 #define __COMPAR_FN_T
452 typedef int (*__compar_fn_t) __P ((__const __ptr_t, __const __ptr_t));
453 #endif
455 #ifdef __USE_GNU
456 typedef __compar_fn_t comparison_fn_t;
457 #endif
459 /* Do a binary search for KEY in BASE, which consists of NMEMB elements
460 of SIZE bytes each, using COMPAR to perform the comparisons. */
461 extern __ptr_t bsearch __P ((__const __ptr_t __key, __const __ptr_t __base,
462 size_t __nmemb, size_t __size,
463 __compar_fn_t __compar));
465 /* Sort NMEMB elements of BASE, of SIZE bytes each,
466 using COMPAR to perform the comparisons. */
467 extern void qsort __P ((__ptr_t __base, size_t __nmemb, size_t __size,
468 __compar_fn_t __compar));
471 /* Return the absolute value of X. */
472 extern int abs __P ((int __x)) __attribute__ ((__const__));
473 extern long int labs __P ((long int __x)) __attribute__ ((__const__));
474 #ifdef __USE_GNU
475 extern long long int llabs __P ((long long int __x)) __attribute__ ((__const__));
476 #endif
479 /* Return the `div_t', `ldiv_t' or `lldiv_t' representation
480 of the value of NUMER over DENOM. */
481 /* GCC may have built-ins for these someday. */
482 extern div_t div __P ((int __numer, int __denom)) __attribute__ ((__const__));
483 extern ldiv_t ldiv __P ((long int __numer, long int __denom)) __attribute__ ((__const__));
484 #ifdef __USE_GNU
485 extern lldiv_t lldiv __P ((long long int __numer, long long int __denom)) __attribute__ ((__const__));
486 #endif
489 #if defined(__USE_SVID) || defined(__USE_XOPEN_EXTENDED)
490 /* Convert floating point numbers to strings. The returned values are
491 valid only until another call to the same function. */
493 /* Convert VALUE to a string with NDIGIT digits and return a pointer to
494 this. Set *DECPT with the position of the decimal character and *SIGN
495 with the sign of the number. */
496 extern char *ecvt __P ((double __value, int __ndigit, int *__decpt,
497 int *__sign));
499 /* Convert VALUE to a string rounded to NDIGIT decimal digits. Set *DECPT
500 with the position of the decimal character and *SIGN with the sign of
501 the number. */
502 extern char *fcvt __P ((double __value, int __ndigit, int *__decpt,
503 int *__sign));
505 /* If possible convert VALUE to a string with NDIGIT significant digits.
506 Otherwise use exponential representation. The resulting string will
507 be written to BUF. */
508 extern char *gcvt __P ((double __value, int __ndigit, char *__buf));
510 /* Long double versions of above functions. */
511 extern char *qecvt __P ((__long_double_t __value, int __ndigit, int *__decpt,
512 int *__sign));
513 extern char *qfcvt __P ((__long_double_t __value, int __ndigit, int *__decpt,
514 int *__sign));
515 extern char *qgcvt __P ((__long_double_t __value, int __ndigit, char *__buf));
518 #ifdef __USE_REENTRANT
519 /* Reentrant version of the functions above which provide their own
520 buffers. */
521 extern int ecvt_r __P ((double __value, int __ndigit, int *__decpt,
522 int *__sign, char *__buf, size_t __len));
523 extern int fcvt_r __P ((double __value, int __ndigit, int *__decpt,
524 int *__sign, char *__buf, size_t __len));
526 extern int qecvt_r __P ((__long_double_t __value, int __ndigit, int *__decpt,
527 int *__sign, char *__buf, size_t __len));
528 extern int qfcvt_r __P ((__long_double_t __value, int __ndigit, int *__decpt,
529 int *__sign, char *__buf, size_t __len));
530 #endif /* reentrant */
531 #endif /* use MISC || use X/Open Unix */
534 /* Return the length of the multibyte character
535 in S, which is no longer than N. */
536 extern int mblen __P ((__const char *__s, size_t __n));
537 /* Return the length of the given multibyte character,
538 putting its `wchar_t' representation in *PWC. */
539 extern int mbtowc __P ((wchar_t *__pwc, __const char *__s, size_t __n));
540 /* Put the multibyte character represented
541 by WCHAR in S, returning its length. */
542 extern int wctomb __P ((char *__s, wchar_t __wchar));
544 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
545 extern __inline int mblen (__const char *__s, size_t __n)
546 { return mbtowc ((wchar_t *) NULL, __s, __n); }
547 #endif /* Optimizing GCC >=2. */
550 /* Convert a multibyte string to a wide char string. */
551 extern size_t mbstowcs __P ((wchar_t *__pwcs, __const char *__s, size_t __n));
552 /* Convert a wide char string to multibyte string. */
553 extern size_t wcstombs __P ((char *__s, __const wchar_t *__pwcs, size_t __n));
556 #ifdef __USE_SVID
557 /* Determine whether the string value of RESPONSE matches the affirmation
558 or negative response expression as specified by the LC_MESSAGES category
559 in the program's current locale. Returns 1 if affirmative, 0 if
560 negative, and -1 if not matching. */
561 extern int rpmatch __P ((__const char *__response));
562 #endif
565 #ifdef __USE_XOPEN_EXTENDED
566 /* Parse comma separated suboption from *OPTIONP and match against
567 strings in TOKENS. If found return index and set *VALUEP to
568 optional value introduced by an equal sign. If the suboption is
569 not part of TOKENS return in *VALUEP beginning of unknown
570 suboption. On exit *OPTIONP is set to the beginning of the next
571 otken or at the terminating NUL character. */
572 extern int getsubopt __P ((char **__optionp, __const char *__const *__tokens,
573 char **__valuep));
574 #endif
577 #ifdef __USE_XOPEN
579 /* Setup DES tables according KEY. */
580 extern void setkey __P ((__const char *__key));
581 #endif
584 __END_DECLS
586 #endif /* stdlib.h */