]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/stdlib.h
Start of uchar.h work
[thirdparty/glibc.git] / stdlib / stdlib.h
CommitLineData
4c1423ed 1/* Copyright (C) 1991-2007, 2009, 2010, 2011 Free Software Foundation, Inc.
ba1ffaa1 2 This file is part of the GNU C Library.
28f540f4 3
ba1ffaa1 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
28f540f4 8
ba1ffaa1
UD
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
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 13
41bdb6e2
AJ
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
28f540f4
RM
18
19/*
d1646309 20 * ISO C99 Standard: 7.20 General utilities <stdlib.h>
28f540f4
RM
21 */
22
23#ifndef _STDLIB_H
5107cf1d 24
28f540f4
RM
25#include <features.h>
26
27/* Get size_t, wchar_t and NULL from <stddef.h>. */
7ef90c15
UD
28#define __need_size_t
29#ifndef __need_malloc_and_calloc
30# define __need_wchar_t
31# define __need_NULL
32#endif
28f540f4
RM
33#include <stddef.h>
34
50497a16
UD
35__BEGIN_DECLS
36
37#ifndef __need_malloc_and_calloc
38#define _STDLIB_H 1
39
f095bb72 40#if (defined __USE_XOPEN || defined __USE_XOPEN2K8) && !defined _SYS_WAIT_H
9323b58f
UD
41/* XPG requires a few symbols from <sys/wait.h> being defined. */
42# include <bits/waitflags.h>
43# include <bits/waitstatus.h>
44
c3fb74d3 45# ifdef __USE_BSD
9323b58f
UD
46
47/* Lots of hair to allow traditional BSD use of `union wait'
48 as well as POSIX.1 use of `int' for the status word. */
49
c3fb74d3 50# if defined __GNUC__ && !defined __cplusplus
a64039b0
UD
51# define __WAIT_INT(status) \
52 (__extension__ (((union { __typeof(status) __in; int __i; }) \
7f3146e7 53 { .__in = (status) }).__i))
c3fb74d3
UD
54# else
55# define __WAIT_INT(status) (*(int *) &(status))
56# endif
9323b58f
UD
57
58/* This is the type of the argument to `wait'. The funky union
8f0d3f49 59 causes redeclarations with either `int *' or `union wait *' to be
9323b58f
UD
60 allowed without complaint. __WAIT_STATUS_DEFN is the type used in
61 the actual function definitions. */
62
c3fb74d3
UD
63# if !defined __GNUC__ || __GNUC__ < 2 || defined __cplusplus
64# define __WAIT_STATUS void *
65# define __WAIT_STATUS_DEFN void *
66# else
9323b58f
UD
67/* This works in GCC 2.6.1 and later. */
68typedef union
69 {
70 union wait *__uptr;
71 int *__iptr;
72 } __WAIT_STATUS __attribute__ ((__transparent_union__));
c3fb74d3
UD
73# define __WAIT_STATUS_DEFN int *
74# endif
9323b58f 75
c3fb74d3 76# else /* Don't use BSD. */
9323b58f 77
c3fb74d3 78# define __WAIT_INT(status) (status)
145b8413 79# define __WAIT_STATUS int *
c3fb74d3 80# define __WAIT_STATUS_DEFN int *
9323b58f 81
c3fb74d3 82# endif /* Use BSD. */
9323b58f
UD
83
84/* Define the macros <sys/wait.h> also would define this way. */
8f0d3f49
UD
85# define WEXITSTATUS(status) __WEXITSTATUS (__WAIT_INT (status))
86# define WTERMSIG(status) __WTERMSIG (__WAIT_INT (status))
87# define WSTOPSIG(status) __WSTOPSIG (__WAIT_INT (status))
88# define WIFEXITED(status) __WIFEXITED (__WAIT_INT (status))
89# define WIFSIGNALED(status) __WIFSIGNALED (__WAIT_INT (status))
90# define WIFSTOPPED(status) __WIFSTOPPED (__WAIT_INT (status))
a044c713 91# ifdef __WIFCONTINUED
8f0d3f49 92# define WIFCONTINUED(status) __WIFCONTINUED (__WAIT_INT (status))
a044c713 93# endif
f095bb72 94#endif /* X/Open or XPG7 and <sys/wait.h> not included. */
9323b58f 95
7a5affeb 96__BEGIN_NAMESPACE_STD
28f540f4
RM
97/* Returned by `div'. */
98typedef struct
99 {
100 int quot; /* Quotient. */
101 int rem; /* Remainder. */
102 } div_t;
103
104/* Returned by `ldiv'. */
e518937a 105#ifndef __ldiv_t_defined
28f540f4
RM
106typedef struct
107 {
108 long int quot; /* Quotient. */
109 long int rem; /* Remainder. */
110 } ldiv_t;
e518937a
UD
111# define __ldiv_t_defined 1
112#endif
7a5affeb 113__END_NAMESPACE_STD
28f540f4 114
ec751a23 115#if defined __USE_ISOC99 && !defined __lldiv_t_defined
7a5affeb 116__BEGIN_NAMESPACE_C99
59dd8641 117/* Returned by `lldiv'. */
7782d0bf 118__extension__ typedef struct
59dd8641
RM
119 {
120 long long int quot; /* Quotient. */
121 long long int rem; /* Remainder. */
122 } lldiv_t;
e518937a 123# define __lldiv_t_defined 1
7a5affeb 124__END_NAMESPACE_C99
59dd8641
RM
125#endif
126
28f540f4
RM
127
128/* The largest number rand will return (same as INT_MAX). */
129#define RAND_MAX 2147483647
130
131
132/* We define these the same for all machines.
133 Changes from this to the outside world should be done in `_exit'. */
134#define EXIT_FAILURE 1 /* Failing exit status. */
135#define EXIT_SUCCESS 0 /* Successful exit status. */
136
137
0200214b
RM
138/* Maximum length of a multibyte character in the current locale. */
139#define MB_CUR_MAX (__ctype_get_mb_cur_max ())
0476597b 140extern size_t __ctype_get_mb_cur_max (void) __THROW __wur;
28f540f4
RM
141
142
7a5affeb 143__BEGIN_NAMESPACE_STD
28f540f4 144/* Convert a string to a floating-point number. */
be27d08c 145extern double atof (__const char *__nptr)
0476597b 146 __THROW __attribute_pure__ __nonnull ((1)) __wur;
28f540f4 147/* Convert a string to an integer. */
be27d08c 148extern int atoi (__const char *__nptr)
0476597b 149 __THROW __attribute_pure__ __nonnull ((1)) __wur;
28f540f4 150/* Convert a string to a long integer. */
be27d08c 151extern long int atol (__const char *__nptr)
0476597b 152 __THROW __attribute_pure__ __nonnull ((1)) __wur;
7a5affeb 153__END_NAMESPACE_STD
28f540f4 154
5224e27c 155#if defined __USE_ISOC99 || (defined __GLIBC_HAVE_LONG_LONG && defined __USE_MISC)
7a5affeb 156__BEGIN_NAMESPACE_C99
fb4dfa0c
AJ
157/* Convert a string to a long long integer. */
158__extension__ extern long long int atoll (__const char *__nptr)
0476597b 159 __THROW __attribute_pure__ __nonnull ((1)) __wur;
7a5affeb 160__END_NAMESPACE_C99
7cc27f44
UD
161#endif
162
7a5affeb 163__BEGIN_NAMESPACE_STD
28f540f4 164/* Convert a string to a floating-point number. */
c1422e5b 165extern double strtod (__const char *__restrict __nptr,
0476597b
UD
166 char **__restrict __endptr)
167 __THROW __nonnull ((1)) __wur;
7a5affeb 168__END_NAMESPACE_STD
28f540f4 169
ec751a23 170#ifdef __USE_ISOC99
7a5affeb 171__BEGIN_NAMESPACE_C99
28f540f4 172/* Likewise for `float' and `long double' sizes of floating-point numbers. */
c1422e5b 173extern float strtof (__const char *__restrict __nptr,
0476597b 174 char **__restrict __endptr) __THROW __nonnull ((1)) __wur;
2f6d1f1b 175
c1422e5b 176extern long double strtold (__const char *__restrict __nptr,
be27d08c 177 char **__restrict __endptr)
0476597b 178 __THROW __nonnull ((1)) __wur;
7a5affeb 179__END_NAMESPACE_C99
28f540f4
RM
180#endif
181
7a5affeb 182__BEGIN_NAMESPACE_STD
28f540f4 183/* Convert a string to a long integer. */
c1422e5b 184extern long int strtol (__const char *__restrict __nptr,
be27d08c 185 char **__restrict __endptr, int __base)
0476597b 186 __THROW __nonnull ((1)) __wur;
28f540f4 187/* Convert a string to an unsigned long integer. */
c1422e5b
UD
188extern unsigned long int strtoul (__const char *__restrict __nptr,
189 char **__restrict __endptr, int __base)
0476597b 190 __THROW __nonnull ((1)) __wur;
4708f2b1 191__END_NAMESPACE_STD
28f540f4 192
5224e27c 193#if defined __GLIBC_HAVE_LONG_LONG && defined __USE_BSD
28f540f4 194/* Convert a string to a quadword integer. */
7782d0bf 195__extension__
c1422e5b 196extern long long int strtoq (__const char *__restrict __nptr,
be27d08c 197 char **__restrict __endptr, int __base)
0476597b 198 __THROW __nonnull ((1)) __wur;
28f540f4 199/* Convert a string to an unsigned quadword integer. */
7782d0bf 200__extension__
c1422e5b
UD
201extern unsigned long long int strtouq (__const char *__restrict __nptr,
202 char **__restrict __endptr, int __base)
0476597b 203 __THROW __nonnull ((1)) __wur;
28f540f4
RM
204#endif /* GCC and use BSD. */
205
5224e27c 206#if defined __USE_ISOC99 || (defined __GLIBC_HAVE_LONG_LONG && defined __USE_MISC)
7a5affeb 207__BEGIN_NAMESPACE_C99
76060ec0 208/* Convert a string to a quadword integer. */
7782d0bf 209__extension__
c1422e5b 210extern long long int strtoll (__const char *__restrict __nptr,
be27d08c 211 char **__restrict __endptr, int __base)
0476597b 212 __THROW __nonnull ((1)) __wur;
76060ec0 213/* Convert a string to an unsigned quadword integer. */
7782d0bf 214__extension__
c1422e5b
UD
215extern unsigned long long int strtoull (__const char *__restrict __nptr,
216 char **__restrict __endptr, int __base)
0476597b 217 __THROW __nonnull ((1)) __wur;
7a5affeb 218__END_NAMESPACE_C99
ec751a23 219#endif /* ISO C99 or GCC and use MISC. */
76060ec0
RM
220
221
0501d603
UD
222#ifdef __USE_GNU
223/* The concept of one static locale per category is not very well
224 thought out. Many applications will need to process its data using
8f0d3f49 225 information from several different locales. Another problem is
0501d603 226 the implementation of the internationalization handling in the
8f0d3f49
UD
227 ISO C++ standard library. To support this another set of
228 the functions using locale data exist which take an additional
0501d603
UD
229 argument.
230
8f0d3f49
UD
231 Attention: even though several *_l interfaces are part of POSIX:2008,
232 these are not. */
0501d603
UD
233
234/* Structure for reentrant locale using functions. This is an
235 (almost) opaque type for the user level programs. */
236# include <xlocale.h>
237
238/* Special versions of the functions above which take the locale to
239 use as an additional parameter. */
1ab62b32
RM
240extern long int strtol_l (__const char *__restrict __nptr,
241 char **__restrict __endptr, int __base,
0476597b 242 __locale_t __loc) __THROW __nonnull ((1, 4)) __wur;
0501d603 243
1ab62b32
RM
244extern unsigned long int strtoul_l (__const char *__restrict __nptr,
245 char **__restrict __endptr,
be27d08c 246 int __base, __locale_t __loc)
0476597b 247 __THROW __nonnull ((1, 4)) __wur;
0501d603 248
7782d0bf 249__extension__
1ab62b32
RM
250extern long long int strtoll_l (__const char *__restrict __nptr,
251 char **__restrict __endptr, int __base,
be27d08c 252 __locale_t __loc)
0476597b 253 __THROW __nonnull ((1, 4)) __wur;
0501d603 254
7782d0bf 255__extension__
1ab62b32
RM
256extern unsigned long long int strtoull_l (__const char *__restrict __nptr,
257 char **__restrict __endptr,
258 int __base, __locale_t __loc)
0476597b 259 __THROW __nonnull ((1, 4)) __wur;
0501d603 260
1ab62b32
RM
261extern double strtod_l (__const char *__restrict __nptr,
262 char **__restrict __endptr, __locale_t __loc)
0476597b 263 __THROW __nonnull ((1, 3)) __wur;
0501d603 264
1ab62b32 265extern float strtof_l (__const char *__restrict __nptr,
be27d08c 266 char **__restrict __endptr, __locale_t __loc)
0476597b 267 __THROW __nonnull ((1, 3)) __wur;
0501d603 268
1ab62b32
RM
269extern long double strtold_l (__const char *__restrict __nptr,
270 char **__restrict __endptr,
0476597b
UD
271 __locale_t __loc)
272 __THROW __nonnull ((1, 3)) __wur;
0501d603
UD
273#endif /* GNU */
274
f0bf9cb9 275
07c416ed 276#ifdef __USE_EXTERN_INLINES
7a5affeb 277__BEGIN_NAMESPACE_STD
b037a293 278__extern_inline double
f377d022 279__NTH (atof (__const char *__nptr))
2604afb1
UD
280{
281 return strtod (__nptr, (char **) NULL);
282}
b037a293 283__extern_inline int
f377d022 284__NTH (atoi (__const char *__nptr))
2604afb1
UD
285{
286 return (int) strtol (__nptr, (char **) NULL, 10);
287}
b037a293 288__extern_inline long int
f377d022 289__NTH (atol (__const char *__nptr))
2604afb1
UD
290{
291 return strtol (__nptr, (char **) NULL, 10);
292}
7a5affeb 293__END_NAMESPACE_STD
2604afb1 294
ec751a23 295# if defined __USE_MISC || defined __USE_ISOC99
7a5affeb 296__BEGIN_NAMESPACE_C99
b037a293 297__extension__ __extern_inline long long int
f377d022 298__NTH (atoll (__const char *__nptr))
2604afb1
UD
299{
300 return strtoll (__nptr, (char **) NULL, 10);
301}
7a5affeb 302__END_NAMESPACE_C99
2604afb1 303# endif
0c6cee5d 304#endif /* Optimizing and Inlining. */
28f540f4
RM
305
306
377a515b 307#if defined __USE_SVID || defined __USE_XOPEN_EXTENDED
bbed653c
RM
308/* Convert N to base 64 using the digits "./0-9A-Za-z", least-significant
309 digit first. Returns a pointer to static storage overwritten by the
310 next call. */
0476597b 311extern char *l64a (long int __n) __THROW __wur;
bbed653c 312
036cc82f 313/* Read a number from a string S in base 64 as above. */
be27d08c 314extern long int a64l (__const char *__s)
0476597b 315 __THROW __attribute_pure__ __nonnull ((1)) __wur;
28f540f4 316
7ed33cba 317#endif /* Use SVID || extended X/Open. */
b20e47cb 318
7ed33cba 319#if defined __USE_SVID || defined __USE_XOPEN_EXTENDED || defined __USE_BSD
dfd2257a 320# include <sys/types.h> /* we need int32_t... */
b20e47cb 321
28f540f4
RM
322/* These are the functions that actually do things. The `random', `srandom',
323 `initstate' and `setstate' functions are those from BSD Unices.
324 The `rand' and `srand' functions are required by the ANSI standard.
325 We provide both interfaces to the same random number generator. */
326/* Return a random long integer between 0 and RAND_MAX inclusive. */
61f9d0a3 327extern long int random (void) __THROW;
9ebb936d 328
28f540f4 329/* Seed the random number generator with the given number. */
c1422e5b 330extern void srandom (unsigned int __seed) __THROW;
28f540f4
RM
331
332/* Initialize the random number generator to use state buffer STATEBUF,
333 of length STATELEN, and seed it with SEED. Optimal lengths are 8, 16,
334 32, 64, 128 and 256, the bigger the better; values less than 8 will
335 cause an error and values greater than 256 will be rounded down. */
9323b58f 336extern char *initstate (unsigned int __seed, char *__statebuf,
be27d08c 337 size_t __statelen) __THROW __nonnull ((2));
9ebb936d 338
28f540f4
RM
339/* Switch the random number generator to state buffer STATEBUF,
340 which should have been previously initialized by `initstate'. */
be27d08c 341extern char *setstate (char *__statebuf) __THROW __nonnull ((1));
28f540f4 342
60478656 343
2604afb1 344# ifdef __USE_MISC
60478656
RM
345/* Reentrant versions of the `random' family of functions.
346 These functions all use the following data structure to contain
347 state, rather than global state variables. */
348
349struct random_data
350 {
b20e47cb
RM
351 int32_t *fptr; /* Front pointer. */
352 int32_t *rptr; /* Rear pointer. */
353 int32_t *state; /* Array of state values. */
60478656
RM
354 int rand_type; /* Type of random number generator. */
355 int rand_deg; /* Degree of random number generator. */
356 int rand_sep; /* Distance between front and rear. */
b20e47cb 357 int32_t *end_ptr; /* Pointer behind state table. */
60478656
RM
358 };
359
c1422e5b 360extern int random_r (struct random_data *__restrict __buf,
be27d08c 361 int32_t *__restrict __result) __THROW __nonnull ((1, 2));
8a523922 362
be27d08c
UD
363extern int srandom_r (unsigned int __seed, struct random_data *__buf)
364 __THROW __nonnull ((2));
8a523922 365
9323b58f 366extern int initstate_r (unsigned int __seed, char *__restrict __statebuf,
c1422e5b 367 size_t __statelen,
be27d08c
UD
368 struct random_data *__restrict __buf)
369 __THROW __nonnull ((2, 4));
8a523922 370
9323b58f 371extern int setstate_r (char *__restrict __statebuf,
be27d08c
UD
372 struct random_data *__restrict __buf)
373 __THROW __nonnull ((1, 2));
2604afb1 374# endif /* Use misc. */
7ed33cba 375#endif /* Use SVID || extended X/Open || BSD. */
60478656
RM
376
377
7a5affeb 378__BEGIN_NAMESPACE_STD
2c6fe0bd 379/* Return a random integer between 0 and RAND_MAX inclusive. */
c1422e5b 380extern int rand (void) __THROW;
2c6fe0bd 381/* Seed the random number generator with the given number. */
c1422e5b 382extern void srand (unsigned int __seed) __THROW;
7a5affeb 383__END_NAMESPACE_STD
2c6fe0bd 384
4bae5567 385#ifdef __USE_POSIX
47707456 386/* Reentrant interface according to POSIX.1. */
c1422e5b 387extern int rand_r (unsigned int *__seed) __THROW;
47707456
UD
388#endif
389
2c6fe0bd 390
377a515b 391#if defined __USE_SVID || defined __USE_XOPEN
60478656
RM
392/* System V style 48-bit random number generator functions. */
393
2c6fe0bd 394/* Return non-negative, double-precision floating-point value in [0.0,1.0). */
c1422e5b 395extern double drand48 (void) __THROW;
be27d08c 396extern double erand48 (unsigned short int __xsubi[3]) __THROW __nonnull ((1));
2c6fe0bd
UD
397
398/* Return non-negative, long integer in [0,2^31). */
c1422e5b 399extern long int lrand48 (void) __THROW;
be27d08c
UD
400extern long int nrand48 (unsigned short int __xsubi[3])
401 __THROW __nonnull ((1));
2c6fe0bd
UD
402
403/* Return signed, long integers in [-2^31,2^31). */
c1422e5b 404extern long int mrand48 (void) __THROW;
be27d08c
UD
405extern long int jrand48 (unsigned short int __xsubi[3])
406 __THROW __nonnull ((1));
2c6fe0bd
UD
407
408/* Seed random number generator. */
c1422e5b 409extern void srand48 (long int __seedval) __THROW;
be27d08c
UD
410extern unsigned short int *seed48 (unsigned short int __seed16v[3])
411 __THROW __nonnull ((1));
412extern void lcong48 (unsigned short int __param[7]) __THROW __nonnull ((1));
2c6fe0bd 413
d17c01f9
UD
414# ifdef __USE_MISC
415/* Data structure for communication with thread safe versions. This
416 type is to be regarded as opaque. It's only exported because users
417 have to allocate objects of this type. */
60478656
RM
418struct drand48_data
419 {
d17c01f9
UD
420 unsigned short int __x[3]; /* Current state. */
421 unsigned short int __old_x[3]; /* Old state. */
422 unsigned short int __c; /* Additive const. in congruential formula. */
423 unsigned short int __init; /* Flag for initializing. */
424 unsigned long long int __a; /* Factor in congruential formula. */
60478656
RM
425 };
426
427/* Return non-negative, double-precision floating-point value in [0.0,1.0). */
c1422e5b 428extern int drand48_r (struct drand48_data *__restrict __buffer,
be27d08c 429 double *__restrict __result) __THROW __nonnull ((1, 2));
c1422e5b
UD
430extern int erand48_r (unsigned short int __xsubi[3],
431 struct drand48_data *__restrict __buffer,
be27d08c 432 double *__restrict __result) __THROW __nonnull ((1, 2));
2c6fe0bd 433
60478656 434/* Return non-negative, long integer in [0,2^31). */
c1422e5b 435extern int lrand48_r (struct drand48_data *__restrict __buffer,
be27d08c
UD
436 long int *__restrict __result)
437 __THROW __nonnull ((1, 2));
c1422e5b
UD
438extern int nrand48_r (unsigned short int __xsubi[3],
439 struct drand48_data *__restrict __buffer,
be27d08c
UD
440 long int *__restrict __result)
441 __THROW __nonnull ((1, 2));
2c6fe0bd 442
60478656 443/* Return signed, long integers in [-2^31,2^31). */
c1422e5b 444extern int mrand48_r (struct drand48_data *__restrict __buffer,
be27d08c
UD
445 long int *__restrict __result)
446 __THROW __nonnull ((1, 2));
c1422e5b
UD
447extern int jrand48_r (unsigned short int __xsubi[3],
448 struct drand48_data *__restrict __buffer,
be27d08c
UD
449 long int *__restrict __result)
450 __THROW __nonnull ((1, 2));
2c6fe0bd 451
60478656 452/* Seed random number generator. */
c1422e5b 453extern int srand48_r (long int __seedval, struct drand48_data *__buffer)
be27d08c 454 __THROW __nonnull ((2));
47f13fd4 455
c1422e5b 456extern int seed48_r (unsigned short int __seed16v[3],
be27d08c 457 struct drand48_data *__buffer) __THROW __nonnull ((1, 2));
47f13fd4 458
c1422e5b 459extern int lcong48_r (unsigned short int __param[7],
be27d08c
UD
460 struct drand48_data *__buffer)
461 __THROW __nonnull ((1, 2));
dfd2257a 462# endif /* Use misc. */
2c6fe0bd 463#endif /* Use SVID or X/Open. */
28f540f4 464
7ef90c15 465#endif /* don't just need malloc and calloc */
28f540f4 466
7ef90c15 467#ifndef __malloc_and_calloc_defined
7a5affeb
UD
468# define __malloc_and_calloc_defined
469__BEGIN_NAMESPACE_STD
28f540f4 470/* Allocate SIZE bytes of memory. */
0476597b 471extern void *malloc (size_t __size) __THROW __attribute_malloc__ __wur;
7ef90c15 472/* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
e9e9b245 473extern void *calloc (size_t __nmemb, size_t __size)
0476597b 474 __THROW __attribute_malloc__ __wur;
7a5affeb 475__END_NAMESPACE_STD
7ef90c15
UD
476#endif
477
478#ifndef __need_malloc_and_calloc
7a5affeb 479__BEGIN_NAMESPACE_STD
28f540f4 480/* Re-allocate the previously allocated block
c1422e5b 481 in PTR, making the new block SIZE bytes long. */
1c3e748e
UD
482/* __attribute_malloc__ is not used, because if realloc returns
483 the same pointer that was passed to it, aliasing needs to be allowed
484 between objects pointed by the old and new pointers. */
0476597b 485extern void *realloc (void *__ptr, size_t __size)
1c3e748e 486 __THROW __attribute_warn_unused_result__;
28f540f4 487/* Free a block allocated by `malloc', `realloc' or `calloc'. */
c1422e5b 488extern void free (void *__ptr) __THROW;
7a5affeb 489__END_NAMESPACE_STD
28f540f4
RM
490
491#ifdef __USE_MISC
492/* Free a block. An alias for `free'. (Sun Unices). */
c1422e5b 493extern void cfree (void *__ptr) __THROW;
28f540f4
RM
494#endif /* Use misc. */
495
377a515b 496#if defined __USE_GNU || defined __USE_BSD || defined __USE_MISC
2604afb1 497# include <alloca.h>
28f540f4
RM
498#endif /* Use GNU, BSD, or misc. */
499
c589e093
UD
500#if (defined __USE_XOPEN_EXTENDED && !defined __USE_XOPEN2K) \
501 || defined __USE_BSD
28f540f4 502/* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
0476597b 503extern void *valloc (size_t __size) __THROW __attribute_malloc__ __wur;
28f540f4
RM
504#endif
505
0758ea0c 506#ifdef __USE_XOPEN2K
c8c73ac3 507/* Allocate memory of SIZE bytes with an alignment of ALIGNMENT. */
61f9d0a3 508extern int posix_memalign (void **__memptr, size_t __alignment, size_t __size)
5bcbe69f 509 __THROW __nonnull ((1)) __wur;
0758ea0c 510#endif
28f540f4 511
380d7e87
UD
512#ifdef __USE_ISOC11
513/* ISO C variant of aligned allocation. */
514extern int aligned_alloc (size_t __alignment, size_t __size) __THROW __wur;
515#endif
516
7a5affeb 517__BEGIN_NAMESPACE_STD
28f540f4 518/* Abort execution and generate a core-dump. */
c1422e5b 519extern void abort (void) __THROW __attribute__ ((__noreturn__));
28f540f4
RM
520
521
522/* Register a function to be called when `exit' is called. */
be27d08c 523extern int atexit (void (*__func) (void)) __THROW __nonnull ((1));
610e67ed 524
4e9e7a35 525#if defined __USE_ISOC11 || defined __USE_ISOCXX11
610e67ed
UD
526/* Register a function to be called when `quick_exit' is called. */
527# ifdef __cplusplus
528extern "C++" int at_quick_exit (void (*__func) (void))
529 __THROW __asm ("at_quick_exit") __nonnull ((1));
530# else
531extern int at_quick_exit (void (*__func) (void)) __THROW __nonnull ((1));
532# endif
533#endif
7a5affeb 534__END_NAMESPACE_STD
28f540f4
RM
535
536#ifdef __USE_MISC
537/* Register a function to be called with the status
538 given to `exit' and the given argument. */
c1422e5b 539extern int on_exit (void (*__func) (int __status, void *__arg), void *__arg)
be27d08c 540 __THROW __nonnull ((1));
28f540f4
RM
541#endif
542
7a5affeb 543__BEGIN_NAMESPACE_STD
28f540f4 544/* Call all functions registered with `atexit' and `on_exit',
610e67ed 545 in the reverse of the order in which they were registered,
28f540f4 546 perform stdio cleanup, and terminate program execution with STATUS. */
c1422e5b 547extern void exit (int __status) __THROW __attribute__ ((__noreturn__));
610e67ed 548
4e9e7a35 549#if defined __USE_ISOC11 || defined __USE_ISOCXX11
610e67ed
UD
550/* Call all functions registered with `at_quick_exit' in the reverse
551 of the order in which they were registered and terminate program
552 execution with STATUS. */
553extern void quick_exit (int __status) __THROW __attribute__ ((__noreturn__));
554#endif
7a5affeb 555__END_NAMESPACE_STD
28f540f4 556
ec751a23 557#ifdef __USE_ISOC99
7a5affeb 558__BEGIN_NAMESPACE_C99
e518937a
UD
559/* Terminate the program with STATUS without calling any of the
560 functions registered with `atexit' or `on_exit'. */
c1422e5b 561extern void _Exit (int __status) __THROW __attribute__ ((__noreturn__));
7a5affeb 562__END_NAMESPACE_C99
e518937a
UD
563#endif
564
28f540f4 565
7a5affeb 566__BEGIN_NAMESPACE_STD
28f540f4 567/* Return the value of envariable NAME, or NULL if it doesn't exist. */
0476597b 568extern char *getenv (__const char *__name) __THROW __nonnull ((1)) __wur;
7a5affeb 569__END_NAMESPACE_STD
28f540f4 570
d68171ed
UD
571/* This function is similar to the above but returns NULL if the
572 programs is running with SUID or SGID enabled. */
0476597b
UD
573extern char *__secure_getenv (__const char *__name)
574 __THROW __nonnull ((1)) __wur;
d68171ed 575
377a515b 576#if defined __USE_SVID || defined __USE_XOPEN
28f540f4
RM
577/* The SVID says this is in <stdio.h>, but this seems a better place. */
578/* Put STRING, which is of the form "NAME=VALUE", in the environment.
579 If there is no `=', remove NAME from the environment. */
be27d08c 580extern int putenv (char *__string) __THROW __nonnull ((1));
28f540f4
RM
581#endif
582
61f9d0a3 583#if defined __USE_BSD || defined __USE_XOPEN2K
28f540f4
RM
584/* Set NAME to VALUE in the environment.
585 If REPLACE is nonzero, overwrite an existing value. */
c1422e5b 586extern int setenv (__const char *__name, __const char *__value, int __replace)
90692538 587 __THROW __nonnull ((2));
196980f5
RM
588
589/* Remove the variable NAME from the environment. */
e26dfa69 590extern int unsetenv (__const char *__name) __THROW __nonnull ((1));
28f540f4
RM
591#endif
592
f0e44959
UD
593#ifdef __USE_MISC
594/* The `clearenv' was planned to be added to POSIX.1 but probably
595 never made it. Nevertheless the POSIX.9 standard (POSIX bindings
596 for Fortran 77) requires this function. */
c1422e5b 597extern int clearenv (void) __THROW;
f0e44959
UD
598#endif
599
2c6fe0bd 600
52e96a80
UD
601#if defined __USE_MISC \
602 || (defined __USE_XOPEN_EXTENDED && !defined __USE_XOPEN2K)
2c6fe0bd
UD
603/* Generate a unique temporary file name from TEMPLATE.
604 The last six characters of TEMPLATE must be "XXXXXX";
605 they are replaced with a string that makes the file name unique.
606 Returns TEMPLATE, or a null pointer if it cannot get a unique file name. */
0476597b 607extern char *mktemp (char *__template) __THROW __nonnull ((1)) __wur;
f095bb72 608#endif
2c6fe0bd 609
f095bb72
UD
610#if defined __USE_MISC || defined __USE_XOPEN_EXTENDED \
611 || defined __USE_XOPEN2K8
2c6fe0bd
UD
612/* Generate a unique temporary file name from TEMPLATE.
613 The last six characters of TEMPLATE must be "XXXXXX";
614 they are replaced with a string that makes the filename unique.
615 Returns a file descriptor open on the file for reading and writing,
2c008571
UD
616 or -1 if it cannot create a uniquely-named file.
617
7f3146e7 618 This function is a possible cancellation point and therefore not
2c008571 619 marked with __THROW. */
85adf316 620# ifndef __USE_FILE_OFFSET64
0476597b 621extern int mkstemp (char *__template) __nonnull ((1)) __wur;
85adf316
UD
622# else
623# ifdef __REDIRECT
0476597b
UD
624extern int __REDIRECT (mkstemp, (char *__template), mkstemp64)
625 __nonnull ((1)) __wur;
85adf316
UD
626# else
627# define mkstemp mkstemp64
628# endif
629# endif
630# ifdef __USE_LARGEFILE64
0476597b 631extern int mkstemp64 (char *__template) __nonnull ((1)) __wur;
85adf316 632# endif
2c6fe0bd
UD
633#endif
634
7f3146e7
UD
635#ifdef __USE_MISC
636/* Similar to mkstemp, but the template can have a suffix after the
637 XXXXXX. The length of the suffix is specified in the second
638 parameter.
639
640 This function is a possible cancellation point and therefore not
641 marked with __THROW. */
642# ifndef __USE_FILE_OFFSET64
643extern int mkstemps (char *__template, int __suffixlen) __nonnull ((1)) __wur;
644# else
645# ifdef __REDIRECT
646extern int __REDIRECT (mkstemps, (char *__template, int __suffixlen),
647 mkstemps64) __nonnull ((1)) __wur;
648# else
649# define mkstemps mkstemps64
650# endif
651# endif
652# ifdef __USE_LARGEFILE64
653extern int mkstemps64 (char *__template, int __suffixlen)
654 __nonnull ((1)) __wur;
655# endif
656#endif
657
6cbe890a 658#if defined __USE_BSD || defined __USE_XOPEN2K8
2e65ca2b
UD
659/* Create a unique temporary directory from TEMPLATE.
660 The last six characters of TEMPLATE must be "XXXXXX";
661 they are replaced with a string that makes the directory name unique.
662 Returns TEMPLATE, or a null pointer if it cannot get a unique name.
663 The directory is created mode 700. */
0476597b 664extern char *mkdtemp (char *__template) __THROW __nonnull ((1)) __wur;
2e65ca2b
UD
665#endif
666
d7e23b02
UD
667#ifdef __USE_GNU
668/* Generate a unique temporary file name from TEMPLATE similar to
669 mkstemp. But allow the caller to pass additional flags which are
670 used in the open call to create the file..
671
4c1423ed 672 This function is a possible cancellation point and therefore not
d7e23b02
UD
673 marked with __THROW. */
674# ifndef __USE_FILE_OFFSET64
675extern int mkostemp (char *__template, int __flags) __nonnull ((1)) __wur;
676# else
677# ifdef __REDIRECT
678extern int __REDIRECT (mkostemp, (char *__template, int __flags), mkostemp64)
679 __nonnull ((1)) __wur;
680# else
681# define mkostemp mkostemp64
682# endif
683# endif
684# ifdef __USE_LARGEFILE64
685extern int mkostemp64 (char *__template, int __flags) __nonnull ((1)) __wur;
686# endif
3a83202d
UD
687
688/* Similar to mkostemp, but the template can have a suffix after the
689 XXXXXX. The length of the suffix is specified in the second
690 parameter.
691
692 This function is a possible cancellation point and therefore not
693 marked with __THROW. */
694# ifndef __USE_FILE_OFFSET64
695extern int mkostemps (char *__template, int __suffixlen, int __flags)
696 __nonnull ((1)) __wur;
697# else
698# ifdef __REDIRECT
699extern int __REDIRECT (mkostemps, (char *__template, int __suffixlen,
700 int __flags), mkostemps64)
701 __nonnull ((1)) __wur;
702# else
703# define mkostemps mkostemps64
704# endif
705# endif
706# ifdef __USE_LARGEFILE64
707extern int mkostemps64 (char *__template, int __suffixlen, int __flags)
708 __nonnull ((1)) __wur;
709# endif
d7e23b02
UD
710#endif
711
2c6fe0bd 712
7a5affeb 713__BEGIN_NAMESPACE_STD
2c008571
UD
714/* Execute the given line as a shell command.
715
716 This function is a cancellation point and therefore not marked with
717 __THROW. */
0476597b 718extern int system (__const char *__command) __wur;
7a5affeb 719__END_NAMESPACE_STD
28f540f4
RM
720
721
fa0bc87c
RM
722#ifdef __USE_GNU
723/* Return a malloc'd string containing the canonical absolute name of the
11bf311e 724 existing named file. */
be27d08c 725extern char *canonicalize_file_name (__const char *__name)
0476597b 726 __THROW __nonnull ((1)) __wur;
fa0bc87c
RM
727#endif
728
377a515b 729#if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
b7674b11
UD
730/* Return the canonical absolute name of file NAME. If RESOLVED is
731 null, the result is malloc'd; otherwise, if the canonical name is
732 PATH_MAX chars or more, returns null with `errno' set to
733 ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
734 returns the name in RESOLVED. */
c1422e5b 735extern char *realpath (__const char *__restrict __name,
0476597b 736 char *__restrict __resolved) __THROW __wur;
fa0bc87c
RM
737#endif
738
739
28f540f4 740/* Shorthand for type of comparison functions. */
60478656 741#ifndef __COMPAR_FN_T
2604afb1 742# define __COMPAR_FN_T
c1422e5b 743typedef int (*__compar_fn_t) (__const void *, __const void *);
28f540f4 744
2604afb1 745# ifdef __USE_GNU
28f540f4 746typedef __compar_fn_t comparison_fn_t;
2604afb1 747# endif
28f540f4 748#endif
e458144c
UD
749#ifdef __USE_GNU
750typedef int (*__compar_d_fn_t) (__const void *, __const void *, void *);
751#endif
28f540f4 752
7a5affeb 753__BEGIN_NAMESPACE_STD
28f540f4
RM
754/* Do a binary search for KEY in BASE, which consists of NMEMB elements
755 of SIZE bytes each, using COMPAR to perform the comparisons. */
c1422e5b 756extern void *bsearch (__const void *__key, __const void *__base,
be27d08c 757 size_t __nmemb, size_t __size, __compar_fn_t __compar)
0476597b 758 __nonnull ((1, 2, 5)) __wur;
28f540f4
RM
759
760/* Sort NMEMB elements of BASE, of SIZE bytes each,
761 using COMPAR to perform the comparisons. */
c1422e5b 762extern void qsort (void *__base, size_t __nmemb, size_t __size,
be27d08c 763 __compar_fn_t __compar) __nonnull ((1, 4));
e458144c
UD
764#ifdef __USE_GNU
765extern void qsort_r (void *__base, size_t __nmemb, size_t __size,
766 __compar_d_fn_t __compar, void *__arg)
767 __nonnull ((1, 4));
768#endif
28f540f4
RM
769
770
28f540f4 771/* Return the absolute value of X. */
0476597b
UD
772extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
773extern long int labs (long int __x) __THROW __attribute__ ((__const__)) __wur;
7a5affeb
UD
774__END_NAMESPACE_STD
775
ec751a23 776#ifdef __USE_ISOC99
c1422e5b 777__extension__ extern long long int llabs (long long int __x)
0476597b 778 __THROW __attribute__ ((__const__)) __wur;
59dd8641 779#endif
28f540f4
RM
780
781
7a5affeb 782__BEGIN_NAMESPACE_STD
59dd8641 783/* Return the `div_t', `ldiv_t' or `lldiv_t' representation
28f540f4
RM
784 of the value of NUMER over DENOM. */
785/* GCC may have built-ins for these someday. */
c1422e5b 786extern div_t div (int __numer, int __denom)
0476597b 787 __THROW __attribute__ ((__const__)) __wur;
c1422e5b 788extern ldiv_t ldiv (long int __numer, long int __denom)
0476597b 789 __THROW __attribute__ ((__const__)) __wur;
7a5affeb
UD
790__END_NAMESPACE_STD
791
ec751a23 792#ifdef __USE_ISOC99
7a5affeb 793__BEGIN_NAMESPACE_C99
c1422e5b
UD
794__extension__ extern lldiv_t lldiv (long long int __numer,
795 long long int __denom)
0476597b 796 __THROW __attribute__ ((__const__)) __wur;
7a5affeb 797__END_NAMESPACE_C99
59dd8641 798#endif
28f540f4
RM
799
800
c589e093
UD
801#if (defined __USE_XOPEN_EXTENDED && !defined __USE_XOPEN2K) \
802 || defined __USE_SVID
60478656
RM
803/* Convert floating point numbers to strings. The returned values are
804 valid only until another call to the same function. */
805
806/* Convert VALUE to a string with NDIGIT digits and return a pointer to
807 this. Set *DECPT with the position of the decimal character and *SIGN
808 with the sign of the number. */
c1422e5b 809extern char *ecvt (double __value, int __ndigit, int *__restrict __decpt,
0476597b 810 int *__restrict __sign) __THROW __nonnull ((3, 4)) __wur;
60478656
RM
811
812/* Convert VALUE to a string rounded to NDIGIT decimal digits. Set *DECPT
813 with the position of the decimal character and *SIGN with the sign of
814 the number. */
c1422e5b 815extern char *fcvt (double __value, int __ndigit, int *__restrict __decpt,
0476597b 816 int *__restrict __sign) __THROW __nonnull ((3, 4)) __wur;
60478656
RM
817
818/* If possible convert VALUE to a string with NDIGIT significant digits.
819 Otherwise use exponential representation. The resulting string will
820 be written to BUF. */
be27d08c 821extern char *gcvt (double __value, int __ndigit, char *__buf)
0476597b 822 __THROW __nonnull ((3)) __wur;
60478656 823
61f9d0a3
UD
824
825# ifdef __USE_MISC
2064087b 826/* Long double versions of above functions. */
c1422e5b 827extern char *qecvt (long double __value, int __ndigit,
be27d08c 828 int *__restrict __decpt, int *__restrict __sign)
0476597b 829 __THROW __nonnull ((3, 4)) __wur;
c1422e5b 830extern char *qfcvt (long double __value, int __ndigit,
be27d08c 831 int *__restrict __decpt, int *__restrict __sign)
0476597b 832 __THROW __nonnull ((3, 4)) __wur;
be27d08c 833extern char *qgcvt (long double __value, int __ndigit, char *__buf)
0476597b 834 __THROW __nonnull ((3)) __wur;
2064087b
RM
835
836
60478656
RM
837/* Reentrant version of the functions above which provide their own
838 buffers. */
c1422e5b
UD
839extern int ecvt_r (double __value, int __ndigit, int *__restrict __decpt,
840 int *__restrict __sign, char *__restrict __buf,
be27d08c 841 size_t __len) __THROW __nonnull ((3, 4, 5));
c1422e5b
UD
842extern int fcvt_r (double __value, int __ndigit, int *__restrict __decpt,
843 int *__restrict __sign, char *__restrict __buf,
be27d08c 844 size_t __len) __THROW __nonnull ((3, 4, 5));
c1422e5b
UD
845
846extern int qecvt_r (long double __value, int __ndigit,
847 int *__restrict __decpt, int *__restrict __sign,
be27d08c
UD
848 char *__restrict __buf, size_t __len)
849 __THROW __nonnull ((3, 4, 5));
c1422e5b
UD
850extern int qfcvt_r (long double __value, int __ndigit,
851 int *__restrict __decpt, int *__restrict __sign,
be27d08c
UD
852 char *__restrict __buf, size_t __len)
853 __THROW __nonnull ((3, 4, 5));
dfd2257a 854# endif /* misc */
2c6fe0bd 855#endif /* use MISC || use X/Open Unix */
60478656
RM
856
857
7a5affeb 858__BEGIN_NAMESPACE_STD
28f540f4
RM
859/* Return the length of the multibyte character
860 in S, which is no longer than N. */
0476597b 861extern int mblen (__const char *__s, size_t __n) __THROW __wur;
28f540f4
RM
862/* Return the length of the given multibyte character,
863 putting its `wchar_t' representation in *PWC. */
c1422e5b 864extern int mbtowc (wchar_t *__restrict __pwc,
0476597b 865 __const char *__restrict __s, size_t __n) __THROW __wur;
28f540f4
RM
866/* Put the multibyte character represented
867 by WCHAR in S, returning its length. */
0476597b 868extern int wctomb (char *__s, wchar_t __wchar) __THROW __wur;
28f540f4 869
28f540f4
RM
870
871/* Convert a multibyte string to a wide char string. */
c1422e5b
UD
872extern size_t mbstowcs (wchar_t *__restrict __pwcs,
873 __const char *__restrict __s, size_t __n) __THROW;
28f540f4 874/* Convert a wide char string to multibyte string. */
c1422e5b
UD
875extern size_t wcstombs (char *__restrict __s,
876 __const wchar_t *__restrict __pwcs, size_t __n)
877 __THROW;
7a5affeb 878__END_NAMESPACE_STD
28f540f4
RM
879
880
857fa1b8
RM
881#ifdef __USE_SVID
882/* Determine whether the string value of RESPONSE matches the affirmation
883 or negative response expression as specified by the LC_MESSAGES category
884 in the program's current locale. Returns 1 if affirmative, 0 if
885 negative, and -1 if not matching. */
0476597b 886extern int rpmatch (__const char *__response) __THROW __nonnull ((1)) __wur;
857fa1b8
RM
887#endif
888
889
f095bb72 890#if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8
2064087b
RM
891/* Parse comma separated suboption from *OPTIONP and match against
892 strings in TOKENS. If found return index and set *VALUEP to
893 optional value introduced by an equal sign. If the suboption is
894 not part of TOKENS return in *VALUEP beginning of unknown
895 suboption. On exit *OPTIONP is set to the beginning of the next
2604afb1 896 token or at the terminating NUL character. */
c1422e5b
UD
897extern int getsubopt (char **__restrict __optionp,
898 char *__const *__restrict __tokens,
be27d08c 899 char **__restrict __valuep)
0476597b 900 __THROW __nonnull ((1, 2, 3)) __wur;
2064087b
RM
901#endif
902
903
2c6fe0bd 904#ifdef __USE_XOPEN
2c6fe0bd 905/* Setup DES tables according KEY. */
be27d08c 906extern void setkey (__const char *__key) __THROW __nonnull ((1));
0101a56f
UD
907#endif
908
6591c335
UD
909
910/* X/Open pseudo terminal handling. */
911
f095bb72 912#ifdef __USE_XOPEN2KXSI
0101a56f 913/* Return a master pseudo-terminal handle. */
0476597b 914extern int posix_openpt (int __oflag) __wur;
0101a56f
UD
915#endif
916
917#ifdef __USE_XOPEN
6591c335
UD
918/* The next four functions all take a master pseudo-tty fd and
919 perform an operation on the associated slave: */
920
921/* Chown the slave to the calling user. */
c1422e5b 922extern int grantpt (int __fd) __THROW;
6591c335
UD
923
924/* Release an internal lock so the slave can be opened.
925 Call after grantpt(). */
c1422e5b 926extern int unlockpt (int __fd) __THROW;
6591c335 927
4bca4c17
UD
928/* Return the pathname of the pseudo terminal slave assoicated with
929 the master FD is open on, or NULL on errors.
930 The returned storage is good until the next call to this function. */
0476597b 931extern char *ptsname (int __fd) __THROW __wur;
2c6fe0bd
UD
932#endif
933
6591c335 934#ifdef __USE_GNU
4bca4c17
UD
935/* Store at most BUFLEN characters of the pathname of the slave pseudo
936 terminal associated with the master FD is open on in BUF.
937 Return 0 on success, otherwise an error number. */
be27d08c
UD
938extern int ptsname_r (int __fd, char *__buf, size_t __buflen)
939 __THROW __nonnull ((2));
6591c335 940
4bca4c17 941/* Open a master pseudo terminal and return its file descriptor. */
f1c30c98 942extern int getpt (void);
6591c335 943#endif
2c6fe0bd 944
b2277c15
RM
945#ifdef __USE_BSD
946/* Put the 1 minute, 5 minute and 15 minute load averages into the first
947 NELEM elements of LOADAVG. Return the number written (never more than
948 three, but may be less than NELEM), or -1 if an error occurred. */
be27d08c
UD
949extern int getloadavg (double __loadavg[], int __nelem)
950 __THROW __nonnull ((1));
b2277c15
RM
951#endif
952
b799f91d
UD
953
954/* Define some macros helping to catch buffer overflows. */
de1c3ebb 955#if __USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline
b799f91d
UD
956# include <bits/stdlib.h>
957#endif
c6251f03
RM
958#ifdef __LDBL_COMPAT
959# include <bits/stdlib-ldbl.h>
960#endif
b799f91d 961
7ef90c15
UD
962#endif /* don't just need malloc and calloc */
963#undef __need_malloc_and_calloc
964
28f540f4
RM
965__END_DECLS
966
967#endif /* stdlib.h */