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 Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
20 * ANSI Standard: 4.10 GENERAL UTILITIES <stdlib.h>
28 /* Get size_t, wchar_t and NULL from <stddef.h>. */
30 #define __need_wchar_t
39 /* Returned by `div'. */
42 int quot
; /* Quotient. */
43 int rem
; /* Remainder. */
46 /* Returned by `ldiv'. */
49 long int quot
; /* Quotient. */
50 long int rem
; /* Remainder. */
54 /* Returned by `lldiv'. */
57 long long int quot
; /* Quotient. */
58 long long int rem
; /* Remainder. */
63 /* The largest number rand will return (same as INT_MAX). */
64 #define RAND_MAX 2147483647
67 /* We define these the same for all machines.
68 Changes from this to the outside world should be done in `_exit'. */
69 #define EXIT_FAILURE 1 /* Failing exit status. */
70 #define EXIT_SUCCESS 0 /* Successful exit status. */
73 /* Maximum length of a multibyte character in the current locale. */
74 #define MB_CUR_MAX (__ctype_get_mb_cur_max ())
75 extern int __ctype_get_mb_cur_max
__P ((void));
78 /* Convert a string to a floating-point number. */
79 extern double atof
__P ((__const
char *__nptr
));
80 /* Convert a string to an integer. */
81 extern int atoi
__P ((__const
char *__nptr
));
82 /* Convert a string to a long integer. */
83 extern long int atol
__P ((__const
char *__nptr
));
85 /* Convert a string to a floating-point number. */
86 extern double strtod
__P ((__const
char *__nptr
, char **__endptr
));
89 /* Likewise for `float' and `long double' sizes of floating-point numbers. */
90 extern float strtof
__P ((__const
char *__nptr
, char **__endptr
));
91 extern __long_double_t strtold
__P ((__const
char *__nptr
, char **__endptr
));
94 /* Convert a string to a long integer. */
95 extern long int strtol
__P ((__const
char *__nptr
, char **__endptr
,
97 /* Convert a string to an unsigned long integer. */
98 extern unsigned long int strtoul
__P ((__const
char *__nptr
,
99 char **__endptr
, int __base
));
101 #if defined (__GNUC__) && defined (__USE_BSD)
102 /* Convert a string to a quadword integer. */
103 extern long long int strtoq
__P ((__const
char *__nptr
, char **__endptr
,
105 /* Convert a string to an unsigned quadword integer. */
106 extern unsigned long long int strtouq
__P ((__const
char *__nptr
,
107 char **__endptr
, int __base
));
108 #endif /* GCC and use BSD. */
110 #if defined (__GNUC__) && defined (__USE_MISC)
111 /* Convert a string to a quadword integer. */
112 extern long long int strtoll
__P ((__const
char *__nptr
, char **__endptr
,
114 /* Convert a string to an unsigned quadword integer. */
115 extern unsigned long long int strtoull
__P ((__const
char *__nptr
,
116 char **__endptr
, int __base
));
117 #endif /* GCC and use MISC. */
121 /* The internal entry points for `strtoX' take an extra flag argument
122 saying whether or not to parse locale-dependent number grouping. */
124 extern double __strtod_internal
__P ((__const
char *__nptr
,
125 char **__endptr
, int __group
));
126 extern float __strtof_internal
__P ((__const
char *__nptr
, char **__endptr
,
128 extern __long_double_t __strtold_internal
__P ((__const
char *__nptr
,
129 char **__endptr
, int __group
));
130 extern long int __strtol_internal
__P ((__const
char *__nptr
, char **__endptr
,
131 int __base
, int __group
));
132 extern unsigned long int __strtoul_internal
__P ((__const
char *__nptr
,
133 char **__endptr
, int __base
,
136 extern long long int __strtoq_internal
__P ((__const
char *__nptr
,
137 char **__endptr
, int __base
,
139 extern unsigned long long int __strtouq_internal
__P ((__const
char *__nptr
,
145 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
146 /* Define inline functions which call the internal entry points. */
148 extern __inline
double strtod (__const
char *__nptr
, char **__endptr
)
149 { return __strtod_internal (__nptr
, __endptr
, 0); }
150 extern __inline
long int strtol (__const
char *__nptr
,
151 char **__endptr
, int __base
)
152 { return __strtol_internal (__nptr
, __endptr
, __base
, 0); }
153 extern __inline
unsigned long int strtoul (__const
char *__nptr
,
154 char **__endptr
, int __base
)
155 { return __strtoul_internal (__nptr
, __endptr
, __base
, 0); }
158 extern __inline
float strtof (__const
char *__nptr
, char **__endptr
)
159 { return __strtof_internal (__nptr
, __endptr
, 0); }
160 extern __inline __long_double_t
strtold (__const
char *__nptr
, char **__endptr
)
161 { return __strtold_internal (__nptr
, __endptr
, 0); }
165 extern __inline
long long int strtoq (__const
char *__nptr
, char **__endptr
,
167 { return __strtoq_internal (__nptr
, __endptr
, __base
, 0); }
168 extern __inline
unsigned long long int strtouq (__const
char *__nptr
,
169 char **__endptr
, int __base
)
170 { return __strtouq_internal (__nptr
, __endptr
, __base
, 0); }
174 extern __inline
long long int strtoll (__const
char *__nptr
, char **__endptr
,
176 { return __strtoq_internal (__nptr
, __endptr
, __base
, 0); }
177 extern __inline
unsigned long long int strtoull (__const
char *__nptr
,
178 char **__endptr
, int __base
)
179 { return __strtouq_internal (__nptr
, __endptr
, __base
, 0); }
182 extern __inline
double atof (__const
char *__nptr
)
183 { return strtod (__nptr
, (char **) NULL
); }
184 extern __inline
int atoi (__const
char *__nptr
)
185 { return (int) strtol (__nptr
, (char **) NULL
, 10); }
186 extern __inline
long int atol (__const
char *__nptr
)
187 { return strtol (__nptr
, (char **) NULL
, 10); }
188 #endif /* Optimizing GCC >=2. */
192 /* Convert N to base 64 using the digits "./0-9A-Za-z", least-significant
193 digit first. Returns a pointer to static storage overwritten by the
195 extern char *l64a
__P ((long int __n
));
197 /* Read a number from a string in base 64 as above. */
198 extern long int a64l
__P ((const char *));
202 /* Return a random integer between 0 and RAND_MAX inclusive. */
203 extern int rand
__P ((void));
204 /* Seed the random number generator with the given number. */
205 extern void srand
__P ((unsigned int __seed
));
209 #include <sys/types.h> /* we need int32_t... */
211 /* These are the functions that actually do things. The `random', `srandom',
212 `initstate' and `setstate' functions are those from BSD Unices.
213 The `rand' and `srand' functions are required by the ANSI standard.
214 We provide both interfaces to the same random number generator. */
215 /* Return a random long integer between 0 and RAND_MAX inclusive. */
216 extern int32_t __random
__P ((void));
217 /* Seed the random number generator with the given number. */
218 extern void __srandom
__P ((unsigned int __seed
));
220 /* Initialize the random number generator to use state buffer STATEBUF,
221 of length STATELEN, and seed it with SEED. Optimal lengths are 8, 16,
222 32, 64, 128 and 256, the bigger the better; values less than 8 will
223 cause an error and values greater than 256 will be rounded down. */
224 extern __ptr_t __initstate
__P ((unsigned int __seed
, __ptr_t __statebuf
,
226 /* Switch the random number generator to state buffer STATEBUF,
227 which should have been previously initialized by `initstate'. */
228 extern __ptr_t __setstate
__P ((__ptr_t __statebuf
));
230 extern long int random
__P ((void));
231 extern void srandom
__P ((unsigned int __seed
));
232 extern __ptr_t initstate
__P ((unsigned int __seed
, __ptr_t __statebuf
,
234 extern __ptr_t setstate
__P ((__ptr_t __statebuf
));
236 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
237 extern __inline
long int random (void)
238 { return __random(); }
239 extern __inline
void srandom (unsigned int __seed
)
240 { __srandom(__seed
); }
241 extern __inline __ptr_t
initstate (unsigned int __seed
,
242 __ptr_t __statebuf
, size_t __statelen
)
243 { return __initstate (__seed
, __statebuf
, __statelen
); }
244 extern __inline __ptr_t
setstate (__ptr_t __statebuf
)
245 { return __setstate (__statebuf
); }
246 #endif /* Optimizing GCC >=2. */
248 #ifdef __USE_REENTRANT
249 /* Reentrant versions of the `random' family of functions.
250 These functions all use the following data structure to contain
251 state, rather than global state variables. */
255 int32_t *fptr
; /* Front pointer. */
256 int32_t *rptr
; /* Rear pointer. */
257 int32_t *state
; /* Array of state values. */
258 int rand_type
; /* Type of random number generator. */
259 int rand_deg
; /* Degree of random number generator. */
260 int rand_sep
; /* Distance between front and rear. */
261 int32_t *end_ptr
; /* Pointer behind state table. */
264 extern int __random_r
__P ((struct random_data
*__buf
, int32_t *__result
));
265 extern int random_r
__P ((struct random_data
*__buf
, int32_t *__result
));
266 extern int __srandom_r
__P ((unsigned int __seed
, struct random_data
*__buf
));
267 extern int __initstate_r
__P ((unsigned int __seed
, __ptr_t __statebuf
,
268 size_t __statelen
, struct random_data
*__buf
));
269 extern int __setstate_r
__P ((__ptr_t __statebuf
, struct random_data
*__buf
));
271 extern int srandom_r
__P ((unsigned int __seed
, struct random_data
*__buf
));
272 extern int initstate_r
__P ((unsigned int __seed
, __ptr_t __statebuf
,
273 size_t __statelen
, struct random_data
*__buf
));
274 extern int setstate_r
__P ((__ptr_t __statebuf
, struct random_data
*__buf
));
275 #endif /* __USE_REENTRANT. */
276 #endif /* Use BSD. */
280 /* System V style 48-bit random number generator functions. */
282 /* Data structure for communication with thread safe versions. */
285 unsigned short int X
[3]; /* Current state. */
286 unsigned short int a
[3]; /* Factor in congruential formula. */
287 unsigned short int c
; /* Additive const. in congruential formula. */
288 unsigned short int old_X
[3]; /* Old state. */
289 int init
; /* Flag for initializing. */
292 /* Return non-negative, double-precision floating-point value in [0.0,1.0). */
293 extern double drand48
__P ((void));
294 extern int drand48_r
__P ((struct drand48_data
*__buffer
, double *__result
));
295 extern double erand48
__P ((unsigned short int __xsubi
[3]));
296 extern int erand48_r
__P ((unsigned short int __xsubi
[3],
297 struct drand48_data
*__buffer
, double *__result
));
298 /* Return non-negative, long integer in [0,2^31). */
299 extern long lrand48
__P ((void));
300 extern int lrand48_r
__P ((struct drand48_data
*__buffer
, long *__result
));
301 extern long nrand48
__P ((unsigned short int __xsubi
[3]));
302 extern int nrand48_r
__P ((unsigned short int __xsubi
[3],
303 struct drand48_data
*__buffer
, long *__result
));
304 /* Return signed, long integers in [-2^31,2^31). */
305 extern long mrand48
__P ((void));
306 extern int mrand48_r
__P ((struct drand48_data
*__buffer
, long *__result
));
307 extern long jrand48
__P ((unsigned short int __xsubi
[3]));
308 extern int jrand48_r
__P ((unsigned short int __xsubi
[3],
309 struct drand48_data
*__buffer
, long *__result
));
310 /* Seed random number generator. */
311 extern void srand48
__P ((long __seedval
));
312 extern int srand48_r
__P ((long __seedval
, struct drand48_data
*__buffer
));
313 extern unsigned short int *seed48
__P ((unsigned short int __seed16v
[3]));
314 extern int seed48_r
__P ((unsigned short int __seed16v
[3],
315 struct drand48_data
*__buffer
));
316 extern void lcong48
__P ((unsigned short int __param
[7]));
317 extern int lcong48_r
__P ((unsigned short int __param
[7],
318 struct drand48_data
*__buffer
));
320 /* Internal function to compute next state of the generator. */
321 extern int __drand48_iterate
__P ((unsigned short int __xsubi
[3],
322 struct drand48_data
*__buffer
));
323 #endif /* __USE_SVID. */
326 /* Allocate SIZE bytes of memory. */
327 extern __ptr_t malloc
__P ((size_t __size
));
328 /* Re-allocate the previously allocated block
329 in __ptr_t, making the new block SIZE bytes long. */
330 extern __ptr_t realloc
__P ((__ptr_t __ptr
, size_t __size
));
331 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
332 extern __ptr_t calloc
__P ((size_t __nmemb
, size_t __size
));
333 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
334 extern void free
__P ((__ptr_t __ptr
));
337 /* Free a block. An alias for `free'. (Sun Unices). */
338 extern void cfree
__P ((__ptr_t __ptr
));
339 #endif /* Use misc. */
341 #if defined(__USE_GNU) || defined(__USE_BSD) || defined(__USE_MISC)
343 #endif /* Use GNU, BSD, or misc. */
346 /* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
347 extern __ptr_t valloc
__P ((size_t __size
));
351 /* Abort execution and generate a core-dump. */
352 extern void abort
__P ((void)) __attribute__ ((__noreturn__
));
355 /* Register a function to be called when `exit' is called. */
356 extern int atexit
__P ((void (*__func
) (void)));
359 /* Register a function to be called with the status
360 given to `exit' and the given argument. */
361 extern int on_exit
__P ((void (*__func
) (int __status
, __ptr_t __arg
),
365 /* Call all functions registered with `atexit' and `on_exit',
366 in the reverse of the order in which they were registered
367 perform stdio cleanup, and terminate program execution with STATUS. */
368 extern void exit
__P ((int __status
)) __attribute__ ((__noreturn__
));
371 /* Return the value of envariable NAME, or NULL if it doesn't exist. */
372 extern char *getenv
__P ((__const
char *__name
));
375 /* The SVID says this is in <stdio.h>, but this seems a better place. */
376 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
377 If there is no `=', remove NAME from the environment. */
378 extern int putenv
__P ((__const
char *__string
));
382 /* Set NAME to VALUE in the environment.
383 If REPLACE is nonzero, overwrite an existing value. */
384 extern int setenv
__P ((__const
char *__name
, __const
char *__value
,
387 /* Remove the variable NAME from the environment. */
388 extern void unsetenv
__P ((__const
char *__name
));
391 /* Execute the given line as a shell command. */
392 extern int system
__P ((__const
char *__command
));
396 /* Return a malloc'd string containing the canonical absolute name of the
397 named file. The last file name component need not exist, and may be a
398 symlink to a nonexistent file. */
399 extern char *canonicalize_file_name
__P ((__const
char *__name
));
403 /* Return the canonical absolute name of file NAME. The last file name
404 component need not exist, and may be a symlink to a nonexistent file.
405 If RESOLVED is null, the result is malloc'd; otherwise, if the canonical
406 name is PATH_MAX chars or more, returns null with `errno' set to
407 ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars, returns the
409 extern char *realpath
__P ((__const
char *__name
, char *__resolved
));
413 /* Shorthand for type of comparison functions. */
414 #ifndef __COMPAR_FN_T
415 #define __COMPAR_FN_T
416 typedef int (*__compar_fn_t
) __P ((__const __ptr_t
, __const __ptr_t
));
420 typedef __compar_fn_t comparison_fn_t
;
423 /* Do a binary search for KEY in BASE, which consists of NMEMB elements
424 of SIZE bytes each, using COMPAR to perform the comparisons. */
425 extern __ptr_t bsearch
__P ((__const __ptr_t __key
, __const __ptr_t __base
,
426 size_t __nmemb
, size_t __size
,
427 __compar_fn_t __compar
));
429 /* Sort NMEMB elements of BASE, of SIZE bytes each,
430 using COMPAR to perform the comparisons. */
431 extern void qsort
__P ((__ptr_t __base
, size_t __nmemb
, size_t __size
,
432 __compar_fn_t __compar
));
435 /* Return the absolute value of X. */
436 extern int abs
__P ((int __x
)) __attribute__ ((__const__
));
437 extern long int labs
__P ((long int __x
)) __attribute__ ((__const__
));
439 extern long long int llabs
__P ((long long int __x
)) __attribute__ ((__const__
));
443 /* Return the `div_t', `ldiv_t' or `lldiv_t' representation
444 of the value of NUMER over DENOM. */
445 /* GCC may have built-ins for these someday. */
446 extern div_t div
__P ((int __numer
, int __denom
)) __attribute__ ((__const__
));
447 extern ldiv_t ldiv
__P ((long int __numer
, long int __denom
)) __attribute__ ((__const__
));
449 extern lldiv_t lldiv
__P ((long long int __numer
, long long int __denom
)) __attribute__ ((__const__
));
454 /* Convert floating point numbers to strings. The returned values are
455 valid only until another call to the same function. */
457 /* Convert VALUE to a string with NDIGIT digits and return a pointer to
458 this. Set *DECPT with the position of the decimal character and *SIGN
459 with the sign of the number. */
460 char *ecvt
__P ((double __value
, int __ndigit
, int *__decpt
, int *sign
));
462 /* Convert VALUE to a string rounded to NDIGIT decimal digits. Set *DECPT
463 with the position of the decimal character and *SIGN with the sign of
465 char *fcvt
__P ((double __value
, int __ndigit
, int *__decpt
, int *sign
));
467 /* If possible convert VALUE to a string with NDIGIT significant digits.
468 Otherwise use exponential representation. The resulting string will
469 be written to BUF. */
470 char *gcvt
__P ((double __value
, int __ndigit
, char *__buf
));
472 /* Reentrant version of the functions above which provide their own
474 int ecvt_r
__P ((double __value
, int __ndigit
, int *__decpt
, int *sign
,
475 char *__buf
, size_t __len
));
476 int fcvt_r
__P ((double __value
, int __ndigit
, int *__decpt
, int *sign
,
477 char *__buf
, size_t __len
));
481 /* Return the length of the multibyte character
482 in S, which is no longer than N. */
483 extern int mblen
__P ((__const
char *__s
, size_t __n
));
484 /* Return the length of the given multibyte character,
485 putting its `wchar_t' representation in *PWC. */
486 extern int mbtowc
__P ((wchar_t * __pwc
, __const
char *__s
, size_t __n
));
487 /* Put the multibyte character represented
488 by WCHAR in S, returning its length. */
489 extern int wctomb
__P ((char *__s
, wchar_t __wchar
));
491 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
492 extern __inline
int mblen (__const
char *__s
, size_t __n
)
493 { return mbtowc ((wchar_t *) NULL
, __s
, __n
); }
494 #endif /* Optimizing GCC >=2. */
497 /* Convert a multibyte string to a wide char string. */
498 extern size_t mbstowcs
__P ((wchar_t * __pwcs
, __const
char *__s
, size_t __n
));
499 /* Convert a wide char string to multibyte string. */
500 extern size_t wcstombs
__P ((char *__s
, __const
wchar_t * __pwcs
, size_t __n
));
504 /* Determine whether the string value of RESPONSE matches the affirmation
505 or negative response expression as specified by the LC_MESSAGES category
506 in the program's current locale. Returns 1 if affirmative, 0 if
507 negative, and -1 if not matching. */
508 extern int rpmatch
__P ((__const
char *__response
));
514 #endif /* stdlib.h */