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