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)
162 /* Allocate a block that will be freed when the calling function exits. */
163 extern PTR EXFUN(__alloca, (size_t __size));
164 extern PTR EXFUN(alloca, (size_t __size));
167 #define __alloca(size) __builtin_alloca(size)
171 #define alloca(size) __alloca(size)
172 #endif /* Optimizing. */
173 #endif /* Use GNU, BSD, or misc. */
176 /* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
177 extern PTR EXFUN(valloc, (size_t __size));
183 /* The `volatile' keyword tells GCC that a function never returns. */
184 #define __NORETURN __volatile
188 #endif /* __NORETURN not defined. */
190 /* Abort execution and generate a core-dump. */
191 extern __NORETURN void EXFUN(abort, (NOARGS));
194 /* Register a function to be called when `exit' is called. */
195 extern int EXFUN(atexit, (void (*__func)(NOARGS)));
198 /* Register a function to be called with the status
199 given to `exit' and the given argument. */
200 extern int EXFUN(on_exit, (void (*__func)(int __status, PTR __arg),
204 /* Call all functions registered with `atexit' and `on_exit',
205 in the reverse of the order in which they were registered
206 perform stdio cleanup, and terminate program execution with STATUS. */
207 extern __NORETURN void EXFUN(exit, (int __status));
210 /* Return the value of envariable NAME, or NULL if it doesn't exist. */
211 extern char *EXFUN(getenv, (CONST char *__name));
214 /* The SVID says this is in <stdio.h>, but this seems a better place. */
215 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
216 If there is no `=', remove NAME from the environment. */
217 extern int EXFUN(putenv, (CONST char *__string));
220 /* Execute the given line as a shell command. */
221 extern int EXFUN(system, (CONST char *__command));
224 /* Shorthand for type of comparison functions. */
225 typedef int (*__compar_fn_t) (CONST PTR, CONST PTR);
228 #define comparison_fn_t __compar_fn_t
231 /* Do a binary search for KEY in BASE, which consists of NMEMB elements
232 of SIZE bytes each, using COMPAR to perform the comparisons. */
233 extern PTR EXFUN(bsearch, (CONST PTR __key, CONST PTR __base,
234 size_t __nmemb, size_t __size,
235 __compar_fn_t __compar));
237 /* Sort NMEMB elements of BASE, of SIZE bytes each,
238 using COMPAR to perform the comparisons. */
239 extern void EXFUN(qsort, (PTR __base, size_t __nmemb, size_t __size,
240 __compar_fn_t __compar));
245 /* The `const' keyword tells GCC that a function's return value is
246 based solely on its arguments, and there are no side-effects. */
247 #define __CONSTVALUE __const
251 #endif /* __CONSTVALUE not defined. */
253 /* Return the absolute value of X. */
254 extern __CONSTVALUE int EXFUN(abs, (int __x));
255 extern __CONSTVALUE long int EXFUN(labs, (long int __x));
257 #if defined(__GNUC__) && defined(__OPTIMIZE__)
258 #define abs(x) __builtin_abs(x)
259 #define labs(x) __builtin_labs(x)
260 #endif /* GCC and optimizing. */
263 /* Return the `div_t' or `ldiv_t' representation
264 of the value of NUMER over DENOM. */
265 /* GCC may have built-ins for these someday. */
266 extern __CONSTVALUE div_t EXFUN(div, (int __numer, int __denom));
267 extern __CONSTVALUE ldiv_t EXFUN(ldiv, (long int __numer, long int __denom));
270 /* Return the length of the multibyte character
271 in S, which is no longer than N. */
272 extern int EXFUN(mblen, (CONST char *__s, size_t __n));
273 /* Return the length of the given multibyte character,
274 putting its `wchar_t' representation in *PWC. */
275 extern int EXFUN(mbtowc, (wchar_t *__pwc, CONST char *__s, size_t __n));
276 /* Put the multibyte character represented
277 by WCHAR in S, returning its length. */
278 extern int EXFUN(wctomb, (char *__s, wchar_t __wchar));
281 #define mblen(s, n) mbtowc((wchar_t *) NULL, (s), (n))
282 #endif /* Optimizing. */
285 /* Convert a multibyte string to a wide char string. */
286 extern size_t EXFUN(mbstowcs, (wchar_t *__pwcs, CONST char *__s, size_t __n));
287 /* Convert a wide char string to multibyte string. */
288 extern size_t EXFUN(wcstombs, (char *__s, CONST wchar_t *__pwcs, size_t __n));
291 #endif /* stdlib.h */