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, 1992 Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
20 * ANSI Standard: 4.10 GENERAL UTILITIES <stdlib.h>
30 /* Get size_t, wchar_t and NULL from <stddef.h>. */
32 #define __need_wchar_t
39 /* Get machine-dependent HUGE_VAL value (returned on overflow). */
43 /* Returned by `div'. */
46 int quot; /* Quotient. */
47 int rem; /* Remainder. */
50 /* Returned by `ldiv'. */
53 long int quot; /* Quotient. */
54 long int rem; /* Remainder. */
58 /* The largest number rand will return (same as INT_MAX). */
59 #define RAND_MAX 2147483647
62 /* We define these the same for all machines.
63 Changes from this to the outside world should be done in `_exit'. */
64 #define EXIT_FAILURE 1 /* Failing exit status. */
65 #define EXIT_SUCCESS 0 /* Successful exit status. */
68 /* Maximum length of a multibyte character in the current locale.
69 This is just one until the fancy locale support is finished. */
73 /* Convert a string to a floating-point number. */
74 extern double atof __P ((__const char *__nptr));
75 /* Convert a string to an integer. */
76 extern int atoi __P ((__const char *__nptr));
77 /* Convert a string to a long integer. */
78 extern long int atol __P ((__const char *__nptr));
80 /* Convert a string to a floating-point number. */
81 extern double strtod __P ((__const char *__nptr, char **__endptr));
82 /* Convert a string to a long integer. */
83 extern long int strtol __P ((__const char *__nptr, char **__endptr,
85 /* Convert a string to an unsigned long integer. */
86 extern unsigned long int strtoul __P ((__const char *__nptr,
87 char **__endptr, int __base));
90 #define atof(nptr) strtod((nptr), (char **) NULL)
91 #define atoi(nptr) ((int) atol(nptr))
92 #define atol(nptr) strtol((nptr), (char **) NULL, 10)
93 #endif /* Optimizing. */
96 /* Return a random integer between 0 and RAND_MAX inclusive. */
97 extern int rand __P ((void));
98 /* Seed the random number generator with the given number. */
99 extern void srand __P ((unsigned int __seed));
101 /* These are the functions that actually do things. The `random', `srandom',
102 `initstate' and `setstate' functions are those from BSD Unices.
103 The `rand' and `srand' functions are required by the ANSI standard.
104 We provide both interfaces to the same random number generator. */
105 /* Return a random long integer between 0 and RAND_MAX inclusive. */
106 extern long int __random __P ((void));
107 /* Seed the random number generator with the given number. */
108 extern void __srandom __P ((unsigned int __seed));
110 /* Initialize the random number generator to use state buffer STATEBUF,
111 of length STATELEN, and seed it with SEED. Optimal lengths are 8, 16,
112 32, 64, 128 and 256, the bigger the better; values less than 8 will
113 cause an error and values greater than 256 will be rounded down. */
114 extern __ptr_t __initstate __P ((unsigned int __seed, __ptr_t __statebuf,
116 /* Switch the random number generator to state buffer STATEBUF,
117 which should have been previously initialized by `initstate'. */
118 extern __ptr_t __setstate __P ((__ptr_t __statebuf));
121 extern long int random __P ((void));
122 extern void srandom __P ((unsigned int __seed));
123 extern __ptr_t initstate __P ((unsigned int __seed, __ptr_t __statebuf,
125 extern __ptr_t setstate __P ((__ptr_t __statebuf));
128 #define random() __random()
129 #define srandom(seed) __srandom(seed)
130 #define initstate(s, b, n) __initstate((s), (b), (n))
131 #define setstate(state) __setstate(state)
132 #endif /* Optimizing. */
133 #endif /* Use BSD. */
136 #define rand() ((int) __random())
137 #define srand(seed) __srandom(seed)
138 #endif /* Optimizing. */
141 /* Allocate SIZE bytes of memory. */
142 extern __ptr_t malloc __P ((size_t __size));
143 /* Re-allocate the previously allocated block
144 in __ptr_t, making the new block SIZE bytes long. */
145 extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size));
146 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
147 extern __ptr_t calloc __P ((size_t __nmemb, size_t __size));
148 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
149 extern void free __P ((__ptr_t __ptr));
152 /* Free a block. An alias for `free'. (Sun Unices). */
153 extern void cfree __P ((__ptr_t __ptr));
156 #define cfree(ptr) free(ptr)
157 #endif /* Optimizing. */
158 #endif /* Use misc. */
160 #if defined(__USE_GNU) || defined(__USE_BSD) || defined(__USE_MISC)
162 #endif /* Use GNU, BSD, or misc. */
165 /* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
166 extern __ptr_t valloc __P ((size_t __size));
172 /* The `volatile' keyword tells GCC that a function never returns. */
173 #define __NORETURN __volatile
177 #endif /* __NORETURN not defined. */
179 /* Abort execution and generate a core-dump. */
180 extern __NORETURN void abort __P ((void));
183 /* Register a function to be called when `exit' is called. */
184 extern int atexit __P ((void (*__func) (void)));
187 /* Register a function to be called with the status
188 given to `exit' and the given argument. */
189 extern int on_exit __P ((void (*__func) (int __status, __ptr_t __arg),
193 /* Call all functions registered with `atexit' and `on_exit',
194 in the reverse of the order in which they were registered
195 perform stdio cleanup, and terminate program execution with STATUS. */
196 extern __NORETURN void exit __P ((int __status));
199 /* Return the value of envariable NAME, or NULL if it doesn't exist. */
200 extern char *getenv __P ((__const char *__name));
203 /* The SVID says this is in <stdio.h>, but this seems a better place. */
204 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
205 If there is no `=', remove NAME from the environment. */
206 extern int putenv __P ((__const char *__string));
210 /* Set NAME to VALUE in the environment.
211 If REPLACE is nonzero, overwrite an existing value. */
212 extern int setenv __P ((__const char *__name, __const char *__value,
216 /* Execute the given line as a shell command. */
217 extern int system __P ((__const char *__command));
220 /* Shorthand for type of comparison functions. */
221 typedef int (*__compar_fn_t) (__const __ptr_t, __const __ptr_t);
224 typedef __compar_fn_t comparison_fn_t;
227 /* Do a binary search for KEY in BASE, which consists of NMEMB elements
228 of SIZE bytes each, using COMPAR to perform the comparisons. */
229 extern __ptr_t bsearch __P ((__const __ptr_t __key, __const __ptr_t __base,
230 size_t __nmemb, size_t __size,
231 __compar_fn_t __compar));
233 /* Sort NMEMB elements of BASE, of SIZE bytes each,
234 using COMPAR to perform the comparisons. */
235 extern void qsort __P ((__ptr_t __base, size_t __nmemb, size_t __size,
236 __compar_fn_t __compar));
241 /* The `const' keyword tells GCC that a function's return value is
242 based solely on its arguments, and there are no side-effects. */
243 #define __CONSTVALUE __const
247 #endif /* __CONSTVALUE not defined. */
249 /* Return the absolute value of X. */
250 extern __CONSTVALUE int abs __P ((int __x));
251 extern __CONSTVALUE long int labs __P ((long int __x));
254 /* Return the `div_t' or `ldiv_t' representation
255 of the value of NUMER over DENOM. */
256 /* GCC may have built-ins for these someday. */
257 extern __CONSTVALUE div_t div __P ((int __numer, int __denom));
258 extern __CONSTVALUE ldiv_t ldiv __P ((long int __numer, long int __denom));
261 /* Return the length of the multibyte character
262 in S, which is no longer than N. */
263 extern int mblen __P ((__const char *__s, size_t __n));
264 /* Return the length of the given multibyte character,
265 putting its `wchar_t' representation in *PWC. */
266 extern int mbtowc __P ((wchar_t * __pwc, __const char *__s, size_t __n));
267 /* Put the multibyte character represented
268 by WCHAR in S, returning its length. */
269 extern int wctomb __P ((char *__s, wchar_t __wchar));
272 #define mblen(s, n) mbtowc((wchar_t *) NULL, (s), (n))
273 #endif /* Optimizing. */
276 /* Convert a multibyte string to a wide char string. */
277 extern size_t mbstowcs __P ((wchar_t * __pwcs, __const char *__s, size_t __n));
278 /* Convert a wide char string to multibyte string. */
279 extern size_t wcstombs __P ((char *__s, __const wchar_t * __pwcs, size_t __n));
284 #endif /* stdlib.h */