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)
160 /* Allocate a block that will be freed when the calling function exits. */
161 extern PTR EXFUN(__alloca, (size_t __size));
162 extern PTR EXFUN(alloca, (size_t __size));
165 #define __alloca(size) __builtin_alloca(size)
169 #define alloca(size) __alloca(size)
170 #endif /* Optimizing. */
171 #endif /* Use GNU, BSD, or misc. */
174 /* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
175 extern PTR EXFUN(valloc, (size_t __size));
181 /* The `volatile' keyword tells GCC that a function never returns. */
182 #define __NORETURN __volatile
186 #endif /* __NORETURN not defined. */
188 /* Abort execution and generate a core-dump. */
189 extern __NORETURN void EXFUN(abort, (NOARGS));
192 /* Register a function to be called when `exit' is called. */
193 extern int EXFUN(atexit, (void (*__func)(NOARGS)));
196 /* Register a function to be called with the status
197 given to `exit' and the given argument. */
198 extern int EXFUN(on_exit, (void (*__func)(int __status, PTR __arg),
202 /* Call all functions registered with `atexit' and `on_exit',
203 in the reverse of the order in which they were registered
204 perform stdio cleanup, and terminate program execution with STATUS. */
205 extern __NORETURN void EXFUN(exit, (int __status));
208 /* Return the value of envariable NAME, or NULL if it doesn't exist. */
209 extern char *EXFUN(getenv, (CONST char *__name));
212 /* The SVID says this is in <stdio.h>, but this seems a better place. */
213 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
214 If there is no `=', remove NAME from the environment. */
215 extern int EXFUN(putenv, (CONST char *__string));
218 /* Execute the given line as a shell command. */
219 extern int EXFUN(system, (CONST char *__command));
222 /* Shorthand for type of comparison functions. */
223 typedef int (*__compar_fn_t) (CONST PTR, CONST PTR);
226 #define comparison_fn_t __compar_fn_t
229 /* Do a binary search for KEY in BASE, which consists of NMEMB elements
230 of SIZE bytes each, using COMPAR to perform the comparisons. */
231 extern PTR EXFUN(bsearch, (CONST PTR __key, CONST PTR __base,
232 size_t __nmemb, size_t __size,
233 __compar_fn_t __compar));
235 /* Sort NMEMB elements of BASE, of SIZE bytes each,
236 using COMPAR to perform the comparisons. */
237 extern void EXFUN(qsort, (PTR __base, size_t __nmemb, size_t __size,
238 __compar_fn_t __compar));
243 /* The `const' keyword tells GCC that a function's return value is
244 based solely on its arguments, and there are no side-effects. */
245 #define __CONSTVALUE __const
249 #endif /* __CONSTVALUE not defined. */
251 /* Return the absolute value of X. */
252 extern __CONSTVALUE int EXFUN(abs, (int __x));
253 extern __CONSTVALUE long int EXFUN(labs, (long int __x));
255 #if defined(__GNUC__) && defined(__OPTIMIZE__)
256 #define abs(x) __builtin_abs(x)
257 #define labs(x) __builtin_labs(x)
258 #endif /* GCC and optimizing. */
261 /* Return the `div_t' or `ldiv_t' representation
262 of the value of NUMER over DENOM. */
263 /* GCC may have built-ins for these someday. */
264 extern __CONSTVALUE div_t EXFUN(div, (int __numer, int __denom));
265 extern __CONSTVALUE ldiv_t EXFUN(ldiv, (long int __numer, long int __denom));
268 /* Return the length of the multibyte character
269 in S, which is no longer than N. */
270 extern int EXFUN(mblen, (CONST char *__s, size_t __n));
271 /* Return the length of the given multibyte character,
272 putting its `wchar_t' representation in *PWC. */
273 extern int EXFUN(mbtowc, (wchar_t *__pwc, CONST char *__s, size_t __n));
274 /* Put the multibyte character represented
275 by WCHAR in S, returning its length. */
276 extern int EXFUN(wctomb, (char *__s, wchar_t __wchar));
279 #define mblen(s, n) mbtowc((wchar_t *) NULL, (s), (n))
280 #endif /* Optimizing. */
283 /* Convert a multibyte string to a wide char string. */
284 extern size_t EXFUN(mbstowcs, (wchar_t *__pwcs, CONST char *__s, size_t __n));
285 /* Convert a wide char string to multibyte string. */
286 extern size_t EXFUN(wcstombs, (char *__s, CONST wchar_t *__pwcs, size_t __n));
289 #endif /* stdlib.h */