1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 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>
28 /* Get size_t, wchar_t and NULL from <stddef.h>. */
30 #define __need_wchar_t
39 /* Returned by `div'. */
42 int quot; /* Quotient. */
43 int rem; /* Remainder. */
46 /* Returned by `ldiv'. */
49 long int quot; /* Quotient. */
50 long int rem; /* Remainder. */
54 /* The largest number rand will return (same as INT_MAX). */
55 #define RAND_MAX 2147483647
58 /* We define these the same for all machines.
59 Changes from this to the outside world should be done in `_exit'. */
60 #define EXIT_FAILURE 1 /* Failing exit status. */
61 #define EXIT_SUCCESS 0 /* Successful exit status. */
64 /* Maximum length of a multibyte character in the current locale.
65 This is just one until the fancy locale support is finished. */
69 /* Convert a string to a floating-point number. */
70 extern double atof __P ((__const char *__nptr));
71 /* Convert a string to an integer. */
72 extern int atoi __P ((__const char *__nptr));
73 /* Convert a string to a long integer. */
74 extern long int atol __P ((__const char *__nptr));
76 /* Convert a string to a floating-point number. */
77 extern double strtod __P ((__const char *__nptr, char **__endptr));
78 /* Convert a string to a long integer. */
79 extern long int strtol __P ((__const char *__nptr, char **__endptr,
81 /* Convert a string to an unsigned long integer. */
82 extern unsigned long int strtoul __P ((__const char *__nptr,
83 char **__endptr, int __base));
85 #if defined (__GNUC__) && defined (__USE_BSD)
86 /* Convert a string to a quadword integer. */
87 extern long long int strtoq __P ((__const char *__nptr, char **__endptr,
89 /* Convert a string to an unsigned quadword integer. */
90 extern unsigned long long int strtouq __P ((__const char *__nptr,
91 char **__endptr, int __base));
92 #endif /* GCC and use BSD. */
94 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
95 extern __inline double atof (__const char *__nptr)
96 { return strtod(__nptr, (char **) NULL); }
97 extern __inline int atoi (__const char *__nptr)
98 { return (int) strtol (__nptr, (char **) NULL, 10); }
99 extern __inline long int atol (__const char *__nptr)
100 { return strtol (__nptr, (char **) NULL, 10); }
101 #endif /* Optimizing GCC >=2. */
104 /* Return a random integer between 0 and RAND_MAX inclusive. */
105 extern int rand __P ((void));
106 /* Seed the random number generator with the given number. */
107 extern void srand __P ((unsigned int __seed));
109 /* These are the functions that actually do things. The `random', `srandom',
110 `initstate' and `setstate' functions are those from BSD Unices.
111 The `rand' and `srand' functions are required by the ANSI standard.
112 We provide both interfaces to the same random number generator. */
113 /* Return a random long integer between 0 and RAND_MAX inclusive. */
114 extern long int __random __P ((void));
115 /* Seed the random number generator with the given number. */
116 extern void __srandom __P ((unsigned int __seed));
118 /* Initialize the random number generator to use state buffer STATEBUF,
119 of length STATELEN, and seed it with SEED. Optimal lengths are 8, 16,
120 32, 64, 128 and 256, the bigger the better; values less than 8 will
121 cause an error and values greater than 256 will be rounded down. */
122 extern __ptr_t __initstate __P ((unsigned int __seed, __ptr_t __statebuf,
124 /* Switch the random number generator to state buffer STATEBUF,
125 which should have been previously initialized by `initstate'. */
126 extern __ptr_t __setstate __P ((__ptr_t __statebuf));
129 extern long int random __P ((void));
130 extern void srandom __P ((unsigned int __seed));
131 extern __ptr_t initstate __P ((unsigned int __seed, __ptr_t __statebuf,
133 extern __ptr_t setstate __P ((__ptr_t __statebuf));
135 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
136 extern __inline long int random (void)
137 { return __random(); }
138 extern __inline void srandom (unsigned int __seed)
139 { __srandom(__seed); }
140 extern __inline __ptr_t initstate (unsigned int __seed,
141 __ptr_t __statebuf, size_t __statelen)
142 { return __initstate (__seed, __statebuf, __statelen); }
143 extern __inline __ptr_t setstate (__ptr_t __statebuf)
144 { return __setstate (__statebuf); }
145 #endif /* Optimizing GCC >=2. */
146 #endif /* Use BSD. */
149 /* Allocate SIZE bytes of memory. */
150 extern __ptr_t malloc __P ((size_t __size));
151 /* Re-allocate the previously allocated block
152 in __ptr_t, making the new block SIZE bytes long. */
153 extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size));
154 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
155 extern __ptr_t calloc __P ((size_t __nmemb, size_t __size));
156 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
157 extern void free __P ((__ptr_t __ptr));
160 /* Free a block. An alias for `free'. (Sun Unices). */
161 extern void cfree __P ((__ptr_t __ptr));
162 #endif /* Use misc. */
164 #if defined(__USE_GNU) || defined(__USE_BSD) || defined(__USE_MISC)
166 #endif /* Use GNU, BSD, or misc. */
169 /* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
170 extern __ptr_t valloc __P ((size_t __size));
174 /* Abort execution and generate a core-dump. */
175 extern void abort __P ((void)) __attribute__ ((__noreturn__));
178 /* Register a function to be called when `exit' is called. */
179 extern int atexit __P ((void (*__func) (void)));
182 /* Register a function to be called with the status
183 given to `exit' and the given argument. */
184 extern int on_exit __P ((void (*__func) (int __status, __ptr_t __arg),
188 /* Call all functions registered with `atexit' and `on_exit',
189 in the reverse of the order in which they were registered
190 perform stdio cleanup, and terminate program execution with STATUS. */
191 extern void exit __P ((int __status)) __attribute__ ((__noreturn__));
194 /* Return the value of envariable NAME, or NULL if it doesn't exist. */
195 extern char *getenv __P ((__const char *__name));
198 /* The SVID says this is in <stdio.h>, but this seems a better place. */
199 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
200 If there is no `=', remove NAME from the environment. */
201 extern int putenv __P ((__const char *__string));
205 /* Set NAME to VALUE in the environment.
206 If REPLACE is nonzero, overwrite an existing value. */
207 extern int setenv __P ((__const char *__name, __const char *__value,
211 /* Execute the given line as a shell command. */
212 extern int system __P ((__const char *__command));
215 /* Shorthand for type of comparison functions. */
216 typedef int (*__compar_fn_t) __P ((__const __ptr_t, __const __ptr_t));
219 typedef __compar_fn_t comparison_fn_t;
222 /* Do a binary search for KEY in BASE, which consists of NMEMB elements
223 of SIZE bytes each, using COMPAR to perform the comparisons. */
224 extern __ptr_t bsearch __P ((__const __ptr_t __key, __const __ptr_t __base,
225 size_t __nmemb, size_t __size,
226 __compar_fn_t __compar));
228 /* Sort NMEMB elements of BASE, of SIZE bytes each,
229 using COMPAR to perform the comparisons. */
230 extern void qsort __P ((__ptr_t __base, size_t __nmemb, size_t __size,
231 __compar_fn_t __compar));
236 /* The `const' keyword tells GCC that a function's return value is
237 based solely on its arguments, and there are no side-effects. */
238 #define __CONSTVALUE __const
242 #endif /* __CONSTVALUE not defined. */
244 /* Return the absolute value of X. */
245 extern __CONSTVALUE int abs __P ((int __x));
246 extern __CONSTVALUE long int labs __P ((long int __x));
249 /* Return the `div_t' or `ldiv_t' representation
250 of the value of NUMER over DENOM. */
251 /* GCC may have built-ins for these someday. */
252 extern __CONSTVALUE div_t div __P ((int __numer, int __denom));
253 extern __CONSTVALUE ldiv_t ldiv __P ((long int __numer, long int __denom));
256 /* Return the length of the multibyte character
257 in S, which is no longer than N. */
258 extern int mblen __P ((__const char *__s, size_t __n));
259 /* Return the length of the given multibyte character,
260 putting its `wchar_t' representation in *PWC. */
261 extern int mbtowc __P ((wchar_t * __pwc, __const char *__s, size_t __n));
262 /* Put the multibyte character represented
263 by WCHAR in S, returning its length. */
264 extern int wctomb __P ((char *__s, wchar_t __wchar));
266 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
267 extern __inline int mblen (__const char *__s, size_t __n)
268 { return mbtowc ((wchar_t *) NULL, __s, __n); }
269 #endif /* Optimizing GCC >=2. */
272 /* Convert a multibyte string to a wide char string. */
273 extern size_t mbstowcs __P ((wchar_t * __pwcs, __const char *__s, size_t __n));
274 /* Convert a wide char string to multibyte string. */
275 extern size_t wcstombs __P ((char *__s, __const wchar_t * __pwcs, size_t __n));
280 #endif /* stdlib.h */