1 /* Copyright (C) 1991, 1992 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
37 /* Get HUGE_VAL (returned by strtod on overflow) from <float.h>. */
38 #define __need_HUGE_VAL
42 /* Returned by `div'. */
45 int quot; /* Quotient. */
46 int rem; /* Remainder. */
49 /* Returned by `ldiv'. */
52 long int quot; /* Quotient. */
53 long int rem; /* Remainder. */
57 /* The largest number rand will return (same as INT_MAX). */
58 #define RAND_MAX 2147483647
61 /* We define these the same for all machines.
62 Changes from this to the outside world should be done in `_exit'. */
63 #define EXIT_FAILURE 1 /* Failing exit status. */
64 #define EXIT_SUCCESS 0 /* Successful exit status. */
67 /* Maximum length of a multibyte character in the current locale.
68 This is just one until the fancy locale support is finished. */
72 /* Convert a string to a floating-point number. */
73 extern double EXFUN(atof, (CONST char *__nptr));
74 /* Convert a string to an integer. */
75 extern int EXFUN(atoi, (CONST char *__nptr));
76 /* Convert a string to a long integer. */
77 extern long int EXFUN(atol, (CONST char *__nptr));
79 /* Convert a string to a floating-point number. */
80 extern double EXFUN(strtod, (CONST char *__nptr, char **__endptr));
81 /* Convert a string to a long integer. */
82 extern long int EXFUN(strtol, (CONST char *__nptr, char **__endptr,
84 /* Convert a string to an unsigned long integer. */
85 extern unsigned long int EXFUN(strtoul, (CONST char *__nptr,
86 char **__endptr, int __base));
89 #define atof(nptr) strtod((nptr), (char **) NULL)
90 #define atoi(nptr) ((int) atol(nptr))
91 #define atol(nptr) strtol((nptr), (char **) NULL, 10)
92 #endif /* Optimizing. */
95 /* Return a random integer between 0 and RAND_MAX inclusive. */
96 extern int EXFUN(rand, (NOARGS));
97 /* Seed the random number generator with the given number. */
98 extern void EXFUN(srand, (unsigned int __seed));
100 /* These are the functions that actually do things. The `random', `srandom',
101 `initstate' and `setstate' functions are those from BSD Unices.
102 The `rand' and `srand' functions are required by the ANSI standard.
103 We provide both interfaces to the same random number generator. */
104 /* Return a random long integer between 0 and RAND_MAX inclusive. */
105 extern long int EXFUN(__random, (NOARGS));
106 /* Seed the random number generator with the given number. */
107 extern void EXFUN(__srandom, (unsigned int __seed));
109 /* Initialize the random number generator to use state buffer STATEBUF,
110 of length STATELEN, and seed it with SEED. Optimal lengths are 8, 16,
111 32, 64, 128 and 256, the bigger the better; values less than 8 will
112 cause an error and values greater than 256 will be rounded down. */
113 extern PTR EXFUN(__initstate, (unsigned int __seed, PTR __statebuf,
115 /* Switch the random number generator to state buffer STATEBUF,
116 which should have been previously initialized by `initstate'. */
117 extern PTR EXFUN(__setstate, (PTR __statebuf));
120 extern long int EXFUN(random, (NOARGS));
121 extern void EXFUN(srandom, (unsigned int __seed));
122 extern PTR EXFUN(initstate, (unsigned int __seed, PTR __statebuf,
124 extern PTR EXFUN(setstate, (PTR __statebuf));
127 #define random() __random()
128 #define srandom(seed) __srandom(seed)
129 #define initstate(s, b, n) __initstate((s), (b), (n))
130 #define setstate(state) __setstate(state)
131 #endif /* Optimizing. */
132 #endif /* Use BSD. */
135 #define rand() ((int) __random())
136 #define srand(seed) __srandom(seed)
137 #endif /* Optimizing. */
140 /* Allocate SIZE bytes of memory. */
141 extern PTR EXFUN(malloc, (size_t __size));
142 /* Re-allocate the previously allocated block
143 in PTR, making the new block SIZE bytes long. */
144 extern PTR EXFUN(realloc, (PTR __ptr, size_t __size));
145 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
146 extern PTR EXFUN(calloc, (size_t __nmemb, size_t __size));
147 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
148 extern void EXFUN(free, (PTR __ptr));
151 /* Free a block. An alias for `free'. (Sun Unices). */
152 extern void EXFUN(cfree, (PTR __ptr));
155 #define cfree(ptr) free(ptr)
156 #endif /* Optimizing. */
157 #endif /* Use misc. */
159 #if defined(__USE_GNU) || defined(__USE_BSD) || defined(__USE_MISC)
161 #endif /* Use GNU, BSD, or misc. */
164 /* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
165 extern PTR EXFUN(valloc, (size_t __size));
171 /* The `volatile' keyword tells GCC that a function never returns. */
172 #define __NORETURN __volatile
176 #endif /* __NORETURN not defined. */
178 /* Abort execution and generate a core-dump. */
179 extern __NORETURN void EXFUN(abort, (NOARGS));
182 /* Register a function to be called when `exit' is called. */
183 extern int EXFUN(atexit, (void (*__func)(NOARGS)));
186 /* Register a function to be called with the status
187 given to `exit' and the given argument. */
188 extern int EXFUN(on_exit, (void (*__func)(int __status, PTR __arg),
192 /* Call all functions registered with `atexit' and `on_exit',
193 in the reverse of the order in which they were registered
194 perform stdio cleanup, and terminate program execution with STATUS. */
195 extern __NORETURN void EXFUN(exit, (int __status));
198 /* Return the value of envariable NAME, or NULL if it doesn't exist. */
199 extern char *EXFUN(getenv, (CONST char *__name));
202 /* The SVID says this is in <stdio.h>, but this seems a better place. */
203 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
204 If there is no `=', remove NAME from the environment. */
205 extern int EXFUN(putenv, (CONST char *__string));
208 /* Execute the given line as a shell command. */
209 extern int EXFUN(system, (CONST char *__command));
212 /* Shorthand for type of comparison functions. */
213 typedef int (*__compar_fn_t) (CONST PTR, CONST PTR);
216 #define comparison_fn_t __compar_fn_t
219 /* Do a binary search for KEY in BASE, which consists of NMEMB elements
220 of SIZE bytes each, using COMPAR to perform the comparisons. */
221 extern PTR EXFUN(bsearch, (CONST PTR __key, CONST PTR __base,
222 size_t __nmemb, size_t __size,
223 __compar_fn_t __compar));
225 /* Sort NMEMB elements of BASE, of SIZE bytes each,
226 using COMPAR to perform the comparisons. */
227 extern void EXFUN(qsort, (PTR __base, size_t __nmemb, size_t __size,
228 __compar_fn_t __compar));
233 /* The `const' keyword tells GCC that a function's return value is
234 based solely on its arguments, and there are no side-effects. */
235 #define __CONSTVALUE __const
239 #endif /* __CONSTVALUE not defined. */
241 /* Return the absolute value of X. */
242 extern __CONSTVALUE int EXFUN(abs, (int __x));
243 extern __CONSTVALUE long int EXFUN(labs, (long int __x));
245 #if defined(__GNUC__) && defined(__OPTIMIZE__)
246 #define abs(x) __builtin_abs(x)
247 #define labs(x) __builtin_labs(x)
248 #endif /* GCC and optimizing. */
251 /* Return the `div_t' or `ldiv_t' representation
252 of the value of NUMER over DENOM. */
253 /* GCC may have built-ins for these someday. */
254 extern __CONSTVALUE div_t EXFUN(div, (int __numer, int __denom));
255 extern __CONSTVALUE ldiv_t EXFUN(ldiv, (long int __numer, long int __denom));
258 /* Return the length of the multibyte character
259 in S, which is no longer than N. */
260 extern int EXFUN(mblen, (CONST char *__s, size_t __n));
261 /* Return the length of the given multibyte character,
262 putting its `wchar_t' representation in *PWC. */
263 extern int EXFUN(mbtowc, (wchar_t *__pwc, CONST char *__s, size_t __n));
264 /* Put the multibyte character represented
265 by WCHAR in S, returning its length. */
266 extern int EXFUN(wctomb, (char *__s, wchar_t __wchar));
269 #define mblen(s, n) mbtowc((wchar_t *) NULL, (s), (n))
270 #endif /* Optimizing. */
273 /* Convert a multibyte string to a wide char string. */
274 extern size_t EXFUN(mbstowcs, (wchar_t *__pwcs, CONST char *__s, size_t __n));
275 /* Convert a wide char string to multibyte string. */
276 extern size_t EXFUN(wcstombs, (char *__s, CONST wchar_t *__pwcs, size_t __n));
279 #endif /* stdlib.h */