Local rewriting.
[glibc/history.git] / stdlib / stdlib.h
blob13b2e424d9b95d4a6533d44818483d41b843d6cd
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>
23 #ifndef _STDLIB_H
25 #define _STDLIB_H 1
26 #include <features.h>
28 __BEGIN_DECLS
30 /* Get size_t, wchar_t and NULL from <stddef.h>. */
31 #define __need_size_t
32 #define __need_wchar_t
33 #define __need_NULL
34 #include <stddef.h>
36 #define __need_Emath
37 #include <errno.h>
39 /* Get machine-dependent HUGE_VAL value (returned on overflow). */
40 #include <huge_val.h>
43 /* Returned by `div'. */
44 typedef struct
46 int quot; /* Quotient. */
47 int rem; /* Remainder. */
48 } div_t;
50 /* Returned by `ldiv'. */
51 typedef struct
53 long int quot; /* Quotient. */
54 long int rem; /* Remainder. */
55 } ldiv_t;
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. */
70 #define MB_CUR_MAX 1
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,
84 int __base));
85 /* Convert a string to an unsigned long integer. */
86 extern unsigned long int strtoul __P ((__const char *__nptr,
87 char **__endptr, int __base));
89 #ifdef __OPTIMIZE__
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,
115 size_t __statelen));
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));
120 #ifdef __USE_BSD
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,
124 size_t __statelen));
125 extern __ptr_t setstate __P ((__ptr_t __statebuf));
127 #ifdef __OPTIMIZE__
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. */
135 #ifdef __OPTIMIZE__
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));
151 #ifdef __USE_MISC
152 /* Free a block. An alias for `free'. (Sun Unices). */
153 extern void cfree __P ((__ptr_t __ptr));
155 #ifdef __OPTIMIZE__
156 #define cfree(ptr) free(ptr)
157 #endif /* Optimizing. */
158 #endif /* Use misc. */
160 #if defined(__USE_GNU) || defined(__USE_BSD) || defined(__USE_MISC)
161 #include <alloca.h>
162 #endif /* Use GNU, BSD, or misc. */
164 #ifdef __USE_BSD
165 /* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
166 extern __ptr_t valloc __P ((size_t __size));
167 #endif
170 #ifndef __NORETURN
171 #ifdef __GNUC__
172 /* The `volatile' keyword tells GCC that a function never returns. */
173 #define __NORETURN __volatile
174 #else /* Not GCC. */
175 #define __NORETURN
176 #endif /* GCC. */
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)));
186 #ifdef __USE_MISC
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),
190 __ptr_t __arg));
191 #endif
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));
202 #ifdef __USE_SVID
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));
207 #endif
209 #ifdef __USE_BSD
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,
213 int __replace));
214 #endif
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);
223 #ifdef __GNUC__
224 #define comparison_fn_t __compar_fn_t
225 #endif
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));
239 #ifndef __CONSTVALUE
240 #ifdef __GNUC__
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
244 #else
245 #define __CONSTVALUE
246 #endif /* GCC. */
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));
253 #if defined(__GNUC__) && defined(__OPTIMIZE__)
254 #define abs(x) __builtin_abs(x)
255 #define labs(x) __builtin_labs(x)
256 #endif /* GCC and optimizing. */
259 /* Return the `div_t' or `ldiv_t' representation
260 of the value of NUMER over DENOM. */
261 /* GCC may have built-ins for these someday. */
262 extern __CONSTVALUE div_t div __P ((int __numer, int __denom));
263 extern __CONSTVALUE ldiv_t ldiv __P ((long int __numer, long int __denom));
266 /* Return the length of the multibyte character
267 in S, which is no longer than N. */
268 extern int mblen __P ((__const char *__s, size_t __n));
269 /* Return the length of the given multibyte character,
270 putting its `wchar_t' representation in *PWC. */
271 extern int mbtowc __P ((wchar_t * __pwc, __const char *__s, size_t __n));
272 /* Put the multibyte character represented
273 by WCHAR in S, returning its length. */
274 extern int wctomb __P ((char *__s, wchar_t __wchar));
276 #ifdef __OPTIMIZE__
277 #define mblen(s, n) mbtowc((wchar_t *) NULL, (s), (n))
278 #endif /* Optimizing. */
281 /* Convert a multibyte string to a wide char string. */
282 extern size_t mbstowcs __P ((wchar_t * __pwcs, __const char *__s, size_t __n));
283 /* Convert a wide char string to multibyte string. */
284 extern size_t wcstombs __P ((char *__s, __const wchar_t * __pwcs, size_t __n));
287 __END_DECLS
289 #endif /* stdlib.h */