1 /* Copyright (C) 1991-2024 Free Software Foundation, Inc.
2 Copyright The GNU Toolchain Authors.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
20 * ISO C99 Standard: 7.20 General utilities <stdlib.h>
25 #define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
26 #include <bits/libc-header-start.h>
28 /* Get size_t, wchar_t and NULL from <stddef.h>. */
30 #define __need_wchar_t
38 #if (defined __USE_XOPEN || defined __USE_XOPEN2K8) && !defined _SYS_WAIT_H
39 /* XPG requires a few symbols from <sys/wait.h> being defined. */
40 # include <bits/waitflags.h>
41 # include <bits/waitstatus.h>
43 /* Define the macros <sys/wait.h> also would define this way. */
44 # define WEXITSTATUS(status) __WEXITSTATUS (status)
45 # define WTERMSIG(status) __WTERMSIG (status)
46 # define WSTOPSIG(status) __WSTOPSIG (status)
47 # define WIFEXITED(status) __WIFEXITED (status)
48 # define WIFSIGNALED(status) __WIFSIGNALED (status)
49 # define WIFSTOPPED(status) __WIFSTOPPED (status)
50 # ifdef __WIFCONTINUED
51 # define WIFCONTINUED(status) __WIFCONTINUED (status)
53 #endif /* X/Open or XPG7 and <sys/wait.h> not included. */
55 /* _FloatN API tests for enablement. */
56 #include <bits/floatn.h>
58 /* Returned by `div'. */
61 int quot
; /* Quotient. */
62 int rem
; /* Remainder. */
65 /* Returned by `ldiv'. */
66 #ifndef __ldiv_t_defined
69 long int quot
; /* Quotient. */
70 long int rem
; /* Remainder. */
72 # define __ldiv_t_defined 1
75 #if defined __USE_ISOC99 && !defined __lldiv_t_defined
76 /* Returned by `lldiv'. */
77 __extension__
typedef struct
79 long long int quot
; /* Quotient. */
80 long long int rem
; /* Remainder. */
82 # define __lldiv_t_defined 1
86 /* The largest number rand will return (same as INT_MAX). */
87 #define RAND_MAX 2147483647
90 /* We define these the same for all machines.
91 Changes from this to the outside world should be done in `_exit'. */
92 #define EXIT_FAILURE 1 /* Failing exit status. */
93 #define EXIT_SUCCESS 0 /* Successful exit status. */
96 /* Maximum length of a multibyte character in the current locale. */
97 #define MB_CUR_MAX (__ctype_get_mb_cur_max ())
98 extern size_t __ctype_get_mb_cur_max (void) __THROW __wur
;
101 /* Convert a string to a floating-point number. */
102 extern double atof (const char *__nptr
)
103 __THROW __attribute_pure__
__nonnull ((1)) __wur
;
104 /* Convert a string to an integer. */
105 extern int atoi (const char *__nptr
)
106 __THROW __attribute_pure__
__nonnull ((1)) __wur
;
107 /* Convert a string to a long integer. */
108 extern long int atol (const char *__nptr
)
109 __THROW __attribute_pure__
__nonnull ((1)) __wur
;
112 /* Convert a string to a long long integer. */
113 __extension__
extern long long int atoll (const char *__nptr
)
114 __THROW __attribute_pure__
__nonnull ((1)) __wur
;
117 /* Convert a string to a floating-point number. */
118 extern double strtod (const char *__restrict __nptr
,
119 char **__restrict __endptr
)
120 __THROW
__nonnull ((1));
123 /* Likewise for `float' and `long double' sizes of floating-point numbers. */
124 extern float strtof (const char *__restrict __nptr
,
125 char **__restrict __endptr
) __THROW
__nonnull ((1));
127 extern long double strtold (const char *__restrict __nptr
,
128 char **__restrict __endptr
)
129 __THROW
__nonnull ((1));
132 /* Likewise for '_FloatN' and '_FloatNx'. */
134 #if __HAVE_FLOAT16 && __GLIBC_USE (IEC_60559_TYPES_EXT)
135 extern _Float16
strtof16 (const char *__restrict __nptr
,
136 char **__restrict __endptr
)
137 __THROW
__nonnull ((1));
140 #if __HAVE_FLOAT32 && __GLIBC_USE (IEC_60559_TYPES_EXT)
141 extern _Float32
strtof32 (const char *__restrict __nptr
,
142 char **__restrict __endptr
)
143 __THROW
__nonnull ((1));
146 #if __HAVE_FLOAT64 && __GLIBC_USE (IEC_60559_TYPES_EXT)
147 extern _Float64
strtof64 (const char *__restrict __nptr
,
148 char **__restrict __endptr
)
149 __THROW
__nonnull ((1));
152 #if __HAVE_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)
153 extern _Float128
strtof128 (const char *__restrict __nptr
,
154 char **__restrict __endptr
)
155 __THROW
__nonnull ((1));
158 #if __HAVE_FLOAT32X && __GLIBC_USE (IEC_60559_TYPES_EXT)
159 extern _Float32x
strtof32x (const char *__restrict __nptr
,
160 char **__restrict __endptr
)
161 __THROW
__nonnull ((1));
164 #if __HAVE_FLOAT64X && __GLIBC_USE (IEC_60559_TYPES_EXT)
165 extern _Float64x
strtof64x (const char *__restrict __nptr
,
166 char **__restrict __endptr
)
167 __THROW
__nonnull ((1));
170 #if __HAVE_FLOAT128X && __GLIBC_USE (IEC_60559_TYPES_EXT)
171 extern _Float128x
strtof128x (const char *__restrict __nptr
,
172 char **__restrict __endptr
)
173 __THROW
__nonnull ((1));
176 /* Convert a string to a long integer. */
177 extern long int strtol (const char *__restrict __nptr
,
178 char **__restrict __endptr
, int __base
)
179 __THROW
__nonnull ((1));
180 /* Convert a string to an unsigned long integer. */
181 extern unsigned long int strtoul (const char *__restrict __nptr
,
182 char **__restrict __endptr
, int __base
)
183 __THROW
__nonnull ((1));
186 /* Convert a string to a quadword integer. */
188 extern long long int strtoq (const char *__restrict __nptr
,
189 char **__restrict __endptr
, int __base
)
190 __THROW
__nonnull ((1));
191 /* Convert a string to an unsigned quadword integer. */
193 extern unsigned long long int strtouq (const char *__restrict __nptr
,
194 char **__restrict __endptr
, int __base
)
195 __THROW
__nonnull ((1));
196 #endif /* Use misc. */
199 /* Convert a string to a quadword integer. */
201 extern long long int strtoll (const char *__restrict __nptr
,
202 char **__restrict __endptr
, int __base
)
203 __THROW
__nonnull ((1));
204 /* Convert a string to an unsigned quadword integer. */
206 extern unsigned long long int strtoull (const char *__restrict __nptr
,
207 char **__restrict __endptr
, int __base
)
208 __THROW
__nonnull ((1));
209 #endif /* ISO C99 or use MISC. */
211 /* Versions of the above functions that handle '0b' and '0B' prefixes
213 #if __GLIBC_USE (C2X_STRTOL)
215 extern long int __REDIRECT_NTH (strtol
, (const char *__restrict __nptr
,
216 char **__restrict __endptr
,
217 int __base
), __isoc23_strtol
)
219 extern unsigned long int __REDIRECT_NTH (strtoul
,
220 (const char *__restrict __nptr
,
221 char **__restrict __endptr
,
222 int __base
), __isoc23_strtoul
)
226 extern long long int __REDIRECT_NTH (strtoq
, (const char *__restrict __nptr
,
227 char **__restrict __endptr
,
228 int __base
), __isoc23_strtoll
)
231 extern unsigned long long int __REDIRECT_NTH (strtouq
,
232 (const char *__restrict __nptr
,
233 char **__restrict __endptr
,
234 int __base
), __isoc23_strtoull
)
238 extern long long int __REDIRECT_NTH (strtoll
, (const char *__restrict __nptr
,
239 char **__restrict __endptr
,
240 int __base
), __isoc23_strtoll
)
243 extern unsigned long long int __REDIRECT_NTH (strtoull
,
244 (const char *__restrict __nptr
,
245 char **__restrict __endptr
,
246 int __base
), __isoc23_strtoull
)
249 extern long int __isoc23_strtol (const char *__restrict __nptr
,
250 char **__restrict __endptr
, int __base
)
251 __THROW
__nonnull ((1));
252 extern unsigned long int __isoc23_strtoul (const char *__restrict __nptr
,
253 char **__restrict __endptr
,
255 __THROW
__nonnull ((1));
257 extern long long int __isoc23_strtoll (const char *__restrict __nptr
,
258 char **__restrict __endptr
, int __base
)
259 __THROW
__nonnull ((1));
261 extern unsigned long long int __isoc23_strtoull (const char *__restrict __nptr
,
262 char **__restrict __endptr
,
264 __THROW
__nonnull ((1));
265 # define strtol __isoc23_strtol
266 # define strtoul __isoc23_strtoul
268 # define strtoq __isoc23_strtoll
269 # define strtouq __isoc23_strtoull
271 # define strtoll __isoc23_strtoll
272 # define strtoull __isoc23_strtoull
276 /* Convert a floating-point number to a string. */
277 #if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
278 extern int strfromd (char *__dest
, size_t __size
, const char *__format
,
280 __THROW
__nonnull ((3));
282 extern int strfromf (char *__dest
, size_t __size
, const char *__format
,
284 __THROW
__nonnull ((3));
286 extern int strfroml (char *__dest
, size_t __size
, const char *__format
,
288 __THROW
__nonnull ((3));
291 #if __HAVE_FLOAT16 && __GLIBC_USE (IEC_60559_TYPES_EXT)
292 extern int strfromf16 (char *__dest
, size_t __size
, const char * __format
,
294 __THROW
__nonnull ((3));
297 #if __HAVE_FLOAT32 && __GLIBC_USE (IEC_60559_TYPES_EXT)
298 extern int strfromf32 (char *__dest
, size_t __size
, const char * __format
,
300 __THROW
__nonnull ((3));
303 #if __HAVE_FLOAT64 && __GLIBC_USE (IEC_60559_TYPES_EXT)
304 extern int strfromf64 (char *__dest
, size_t __size
, const char * __format
,
306 __THROW
__nonnull ((3));
309 #if __HAVE_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)
310 extern int strfromf128 (char *__dest
, size_t __size
, const char * __format
,
312 __THROW
__nonnull ((3));
315 #if __HAVE_FLOAT32X && __GLIBC_USE (IEC_60559_TYPES_EXT)
316 extern int strfromf32x (char *__dest
, size_t __size
, const char * __format
,
318 __THROW
__nonnull ((3));
321 #if __HAVE_FLOAT64X && __GLIBC_USE (IEC_60559_TYPES_EXT)
322 extern int strfromf64x (char *__dest
, size_t __size
, const char * __format
,
324 __THROW
__nonnull ((3));
327 #if __HAVE_FLOAT128X && __GLIBC_USE (IEC_60559_TYPES_EXT)
328 extern int strfromf128x (char *__dest
, size_t __size
, const char * __format
,
330 __THROW
__nonnull ((3));
335 /* Parallel versions of the functions above which take the locale to
336 use as an additional parameter. These are GNU extensions inspired
337 by the POSIX.1-2008 extended locale API. */
338 # include <bits/types/locale_t.h>
340 extern long int strtol_l (const char *__restrict __nptr
,
341 char **__restrict __endptr
, int __base
,
342 locale_t __loc
) __THROW
__nonnull ((1, 4));
344 extern unsigned long int strtoul_l (const char *__restrict __nptr
,
345 char **__restrict __endptr
,
346 int __base
, locale_t __loc
)
347 __THROW
__nonnull ((1, 4));
350 extern long long int strtoll_l (const char *__restrict __nptr
,
351 char **__restrict __endptr
, int __base
,
353 __THROW
__nonnull ((1, 4));
356 extern unsigned long long int strtoull_l (const char *__restrict __nptr
,
357 char **__restrict __endptr
,
358 int __base
, locale_t __loc
)
359 __THROW
__nonnull ((1, 4));
361 /* Versions of the above functions that handle '0b' and '0B' prefixes
363 # if __GLIBC_USE (C2X_STRTOL)
365 extern long int __REDIRECT_NTH (strtol_l
, (const char *__restrict __nptr
,
366 char **__restrict __endptr
,
367 int __base
, locale_t __loc
),
370 extern unsigned long int __REDIRECT_NTH (strtoul_l
,
371 (const char *__restrict __nptr
,
372 char **__restrict __endptr
,
373 int __base
, locale_t __loc
),
377 extern long long int __REDIRECT_NTH (strtoll_l
, (const char *__restrict __nptr
,
378 char **__restrict __endptr
,
384 extern unsigned long long int __REDIRECT_NTH (strtoull_l
,
385 (const char *__restrict __nptr
,
386 char **__restrict __endptr
,
387 int __base
, locale_t __loc
),
391 extern long int __isoc23_strtol_l (const char *__restrict __nptr
,
392 char **__restrict __endptr
, int __base
,
393 locale_t __loc
) __THROW
__nonnull ((1, 4));
394 extern unsigned long int __isoc23_strtoul_l (const char *__restrict __nptr
,
395 char **__restrict __endptr
,
396 int __base
, locale_t __loc
)
397 __THROW
__nonnull ((1, 4));
399 extern long long int __isoc23_strtoll_l (const char *__restrict __nptr
,
400 char **__restrict __endptr
,
401 int __base
, locale_t __loc
)
402 __THROW
__nonnull ((1, 4));
404 extern unsigned long long int __isoc23_strtoull_l (const char *__restrict __nptr
,
405 char **__restrict __endptr
,
406 int __base
, locale_t __loc
)
407 __THROW
__nonnull ((1, 4));
408 # define strtol_l __isoc23_strtol_l
409 # define strtoul_l __isoc23_strtoul_l
410 # define strtoll_l __isoc23_strtoll_l
411 # define strtoull_l __isoc23_strtoull_l
415 extern double strtod_l (const char *__restrict __nptr
,
416 char **__restrict __endptr
, locale_t __loc
)
417 __THROW
__nonnull ((1, 3));
419 extern float strtof_l (const char *__restrict __nptr
,
420 char **__restrict __endptr
, locale_t __loc
)
421 __THROW
__nonnull ((1, 3));
423 extern long double strtold_l (const char *__restrict __nptr
,
424 char **__restrict __endptr
,
426 __THROW
__nonnull ((1, 3));
429 extern _Float16
strtof16_l (const char *__restrict __nptr
,
430 char **__restrict __endptr
,
432 __THROW
__nonnull ((1, 3));
436 extern _Float32
strtof32_l (const char *__restrict __nptr
,
437 char **__restrict __endptr
,
439 __THROW
__nonnull ((1, 3));
443 extern _Float64
strtof64_l (const char *__restrict __nptr
,
444 char **__restrict __endptr
,
446 __THROW
__nonnull ((1, 3));
450 extern _Float128
strtof128_l (const char *__restrict __nptr
,
451 char **__restrict __endptr
,
453 __THROW
__nonnull ((1, 3));
457 extern _Float32x
strtof32x_l (const char *__restrict __nptr
,
458 char **__restrict __endptr
,
460 __THROW
__nonnull ((1, 3));
464 extern _Float64x
strtof64x_l (const char *__restrict __nptr
,
465 char **__restrict __endptr
,
467 __THROW
__nonnull ((1, 3));
470 # if __HAVE_FLOAT128X
471 extern _Float128x
strtof128x_l (const char *__restrict __nptr
,
472 char **__restrict __endptr
,
474 __THROW
__nonnull ((1, 3));
479 #ifdef __USE_EXTERN_INLINES
481 __NTH (atoi (const char *__nptr
))
483 return (int) strtol (__nptr
, (char **) NULL
, 10);
485 __extern_inline
long int
486 __NTH (atol (const char *__nptr
))
488 return strtol (__nptr
, (char **) NULL
, 10);
492 __extension__ __extern_inline
long long int
493 __NTH (atoll (const char *__nptr
))
495 return strtoll (__nptr
, (char **) NULL
, 10);
498 #endif /* Optimizing and Inlining. */
501 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
502 /* Convert N to base 64 using the digits "./0-9A-Za-z", least-significant
503 digit first. Returns a pointer to static storage overwritten by the
505 extern char *l64a (long int __n
) __THROW __wur
;
507 /* Read a number from a string S in base 64 as above. */
508 extern long int a64l (const char *__s
)
509 __THROW __attribute_pure__
__nonnull ((1)) __wur
;
511 #endif /* Use misc || extended X/Open. */
513 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
514 # include <sys/types.h> /* we need int32_t... */
516 /* These are the functions that actually do things. The `random', `srandom',
517 `initstate' and `setstate' functions are those from BSD Unices.
518 The `rand' and `srand' functions are required by the ANSI standard.
519 We provide both interfaces to the same random number generator. */
520 /* Return a random long integer between 0 and 2^31-1 inclusive. */
521 extern long int random (void) __THROW
;
523 /* Seed the random number generator with the given number. */
524 extern void srandom (unsigned int __seed
) __THROW
;
526 /* Initialize the random number generator to use state buffer STATEBUF,
527 of length STATELEN, and seed it with SEED. Optimal lengths are 8, 16,
528 32, 64, 128 and 256, the bigger the better; values less than 8 will
529 cause an error and values greater than 256 will be rounded down. */
530 extern char *initstate (unsigned int __seed
, char *__statebuf
,
531 size_t __statelen
) __THROW
__nonnull ((2));
533 /* Switch the random number generator to state buffer STATEBUF,
534 which should have been previously initialized by `initstate'. */
535 extern char *setstate (char *__statebuf
) __THROW
__nonnull ((1));
539 /* Reentrant versions of the `random' family of functions.
540 These functions all use the following data structure to contain
541 state, rather than global state variables. */
545 int32_t *fptr
; /* Front pointer. */
546 int32_t *rptr
; /* Rear pointer. */
547 int32_t *state
; /* Array of state values. */
548 int rand_type
; /* Type of random number generator. */
549 int rand_deg
; /* Degree of random number generator. */
550 int rand_sep
; /* Distance between front and rear. */
551 int32_t *end_ptr
; /* Pointer behind state table. */
554 extern int random_r (struct random_data
*__restrict __buf
,
555 int32_t *__restrict __result
) __THROW
__nonnull ((1, 2));
557 extern int srandom_r (unsigned int __seed
, struct random_data
*__buf
)
558 __THROW
__nonnull ((2));
560 extern int initstate_r (unsigned int __seed
, char *__restrict __statebuf
,
562 struct random_data
*__restrict __buf
)
563 __THROW
__nonnull ((2, 4));
565 extern int setstate_r (char *__restrict __statebuf
,
566 struct random_data
*__restrict __buf
)
567 __THROW
__nonnull ((1, 2));
568 # endif /* Use misc. */
569 #endif /* Use extended X/Open || misc. */
572 /* Return a random integer between 0 and RAND_MAX inclusive. */
573 extern int rand (void) __THROW
;
574 /* Seed the random number generator with the given number. */
575 extern void srand (unsigned int __seed
) __THROW
;
577 #ifdef __USE_POSIX199506
578 /* Reentrant interface according to POSIX.1. */
579 extern int rand_r (unsigned int *__seed
) __THROW
;
583 #if defined __USE_MISC || defined __USE_XOPEN
584 /* System V style 48-bit random number generator functions. */
586 /* Return non-negative, double-precision floating-point value in [0.0,1.0). */
587 extern double drand48 (void) __THROW
;
588 extern double erand48 (unsigned short int __xsubi
[3]) __THROW
__nonnull ((1));
590 /* Return non-negative, long integer in [0,2^31). */
591 extern long int lrand48 (void) __THROW
;
592 extern long int nrand48 (unsigned short int __xsubi
[3])
593 __THROW
__nonnull ((1));
595 /* Return signed, long integers in [-2^31,2^31). */
596 extern long int mrand48 (void) __THROW
;
597 extern long int jrand48 (unsigned short int __xsubi
[3])
598 __THROW
__nonnull ((1));
600 /* Seed random number generator. */
601 extern void srand48 (long int __seedval
) __THROW
;
602 extern unsigned short int *seed48 (unsigned short int __seed16v
[3])
603 __THROW
__nonnull ((1));
604 extern void lcong48 (unsigned short int __param
[7]) __THROW
__nonnull ((1));
607 /* Data structure for communication with thread safe versions. This
608 type is to be regarded as opaque. It's only exported because users
609 have to allocate objects of this type. */
612 unsigned short int __x
[3]; /* Current state. */
613 unsigned short int __old_x
[3]; /* Old state. */
614 unsigned short int __c
; /* Additive const. in congruential formula. */
615 unsigned short int __init
; /* Flag for initializing. */
616 __extension__
unsigned long long int __a
; /* Factor in congruential
620 /* Return non-negative, double-precision floating-point value in [0.0,1.0). */
621 extern int drand48_r (struct drand48_data
*__restrict __buffer
,
622 double *__restrict __result
) __THROW
__nonnull ((1, 2));
623 extern int erand48_r (unsigned short int __xsubi
[3],
624 struct drand48_data
*__restrict __buffer
,
625 double *__restrict __result
) __THROW
__nonnull ((1, 2));
627 /* Return non-negative, long integer in [0,2^31). */
628 extern int lrand48_r (struct drand48_data
*__restrict __buffer
,
629 long int *__restrict __result
)
630 __THROW
__nonnull ((1, 2));
631 extern int nrand48_r (unsigned short int __xsubi
[3],
632 struct drand48_data
*__restrict __buffer
,
633 long int *__restrict __result
)
634 __THROW
__nonnull ((1, 2));
636 /* Return signed, long integers in [-2^31,2^31). */
637 extern int mrand48_r (struct drand48_data
*__restrict __buffer
,
638 long int *__restrict __result
)
639 __THROW
__nonnull ((1, 2));
640 extern int jrand48_r (unsigned short int __xsubi
[3],
641 struct drand48_data
*__restrict __buffer
,
642 long int *__restrict __result
)
643 __THROW
__nonnull ((1, 2));
645 /* Seed random number generator. */
646 extern int srand48_r (long int __seedval
, struct drand48_data
*__buffer
)
647 __THROW
__nonnull ((2));
649 extern int seed48_r (unsigned short int __seed16v
[3],
650 struct drand48_data
*__buffer
) __THROW
__nonnull ((1, 2));
652 extern int lcong48_r (unsigned short int __param
[7],
653 struct drand48_data
*__buffer
)
654 __THROW
__nonnull ((1, 2));
656 /* Return a random integer between zero and 2**32-1 (inclusive). */
657 extern __uint32_t
arc4random (void)
660 /* Fill the buffer with random data. */
661 extern void arc4random_buf (void *__buf
, size_t __size
)
662 __THROW
__nonnull ((1));
664 /* Return a random number between zero (inclusive) and the specified
665 limit (exclusive). */
666 extern __uint32_t
arc4random_uniform (__uint32_t __upper_bound
)
668 # endif /* Use misc. */
669 #endif /* Use misc or X/Open. */
671 /* Allocate SIZE bytes of memory. */
672 extern void *malloc (size_t __size
) __THROW __attribute_malloc__
673 __attribute_alloc_size__ ((1)) __wur
;
674 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
675 extern void *calloc (size_t __nmemb
, size_t __size
)
676 __THROW __attribute_malloc__
__attribute_alloc_size__ ((1, 2)) __wur
;
678 /* Re-allocate the previously allocated block
679 in PTR, making the new block SIZE bytes long. */
680 /* __attribute_malloc__ is not used, because if realloc returns
681 the same pointer that was passed to it, aliasing needs to be allowed
682 between objects pointed by the old and new pointers. */
683 extern void *realloc (void *__ptr
, size_t __size
)
684 __THROW __attribute_warn_unused_result__
__attribute_alloc_size__ ((2));
686 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
687 extern void free (void *__ptr
) __THROW
;
690 /* Re-allocate the previously allocated block in PTR, making the new
691 block large enough for NMEMB elements of SIZE bytes each. */
692 /* __attribute_malloc__ is not used, because if reallocarray returns
693 the same pointer that was passed to it, aliasing needs to be allowed
694 between objects pointed by the old and new pointers. */
695 extern void *reallocarray (void *__ptr
, size_t __nmemb
, size_t __size
)
696 __THROW __attribute_warn_unused_result__
697 __attribute_alloc_size__ ((2, 3))
700 /* Add reallocarray as its own deallocator. */
701 extern void *reallocarray (void *__ptr
, size_t __nmemb
, size_t __size
)
702 __THROW
__attr_dealloc (reallocarray
, 1);
707 #endif /* Use misc. */
709 #if (defined __USE_XOPEN_EXTENDED && !defined __USE_XOPEN2K) \
710 || defined __USE_MISC
711 /* Allocate SIZE bytes on a page boundary. The storage cannot be freed. */
712 extern void *valloc (size_t __size
) __THROW __attribute_malloc__
713 __attribute_alloc_size__ ((1)) __wur
;
717 /* Allocate memory of SIZE bytes with an alignment of ALIGNMENT. */
718 extern int posix_memalign (void **__memptr
, size_t __alignment
, size_t __size
)
719 __THROW
__nonnull ((1)) __wur
;
723 /* ISO C variant of aligned allocation. */
724 extern void *aligned_alloc (size_t __alignment
, size_t __size
)
725 __THROW __attribute_malloc__
__attribute_alloc_align__ ((1))
726 __attribute_alloc_size__ ((2)) __wur
;
729 /* Abort execution and generate a core-dump. */
730 extern void abort (void) __THROW
__attribute__ ((__noreturn__
));
733 /* Register a function to be called when `exit' is called. */
734 extern int atexit (void (*__func
) (void)) __THROW
__nonnull ((1));
736 #if defined __USE_ISOC11 || defined __USE_ISOCXX11
737 /* Register a function to be called when `quick_exit' is called. */
739 extern "C++" int at_quick_exit (void (*__func
) (void))
740 __THROW
__asm ("at_quick_exit") __nonnull ((1));
742 extern int at_quick_exit (void (*__func
) (void)) __THROW
__nonnull ((1));
747 /* Register a function to be called with the status
748 given to `exit' and the given argument. */
749 extern int on_exit (void (*__func
) (int __status
, void *__arg
), void *__arg
)
750 __THROW
__nonnull ((1));
753 /* Call all functions registered with `atexit' and `on_exit',
754 in the reverse of the order in which they were registered,
755 perform stdio cleanup, and terminate program execution with STATUS. */
756 extern void exit (int __status
) __THROW
__attribute__ ((__noreturn__
));
758 #if defined __USE_ISOC11 || defined __USE_ISOCXX11
759 /* Call all functions registered with `at_quick_exit' in the reverse
760 of the order in which they were registered and terminate program
761 execution with STATUS. */
762 extern void quick_exit (int __status
) __THROW
__attribute__ ((__noreturn__
));
766 /* Terminate the program with STATUS without calling any of the
767 functions registered with `atexit' or `on_exit'. */
768 extern void _Exit (int __status
) __THROW
__attribute__ ((__noreturn__
));
772 /* Return the value of envariable NAME, or NULL if it doesn't exist. */
773 extern char *getenv (const char *__name
) __THROW
__nonnull ((1)) __wur
;
776 /* This function is similar to the above but returns NULL if the
777 programs is running with SUID or SGID enabled. */
778 extern char *secure_getenv (const char *__name
)
779 __THROW
__nonnull ((1)) __wur
;
782 #if defined __USE_MISC || defined __USE_XOPEN
783 /* The SVID says this is in <stdio.h>, but this seems a better place. */
784 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
785 If there is no `=', remove NAME from the environment. */
786 extern int putenv (char *__string
) __THROW
__nonnull ((1));
790 /* Set NAME to VALUE in the environment.
791 If REPLACE is nonzero, overwrite an existing value. */
792 extern int setenv (const char *__name
, const char *__value
, int __replace
)
793 __THROW
__nonnull ((2));
795 /* Remove the variable NAME from the environment. */
796 extern int unsetenv (const char *__name
) __THROW
__nonnull ((1));
800 /* The `clearenv' was planned to be added to POSIX.1 but probably
801 never made it. Nevertheless the POSIX.9 standard (POSIX bindings
802 for Fortran 77) requires this function. */
803 extern int clearenv (void) __THROW
;
807 #if defined __USE_MISC \
808 || (defined __USE_XOPEN_EXTENDED && !defined __USE_XOPEN2K8)
809 /* Generate a unique temporary file name from TEMPLATE.
810 The last six characters of TEMPLATE must be "XXXXXX";
811 they are replaced with a string that makes the file name unique.
812 Always returns TEMPLATE, it's either a temporary file name or a null
813 string if it cannot get a unique file name. */
814 extern char *mktemp (char *__template
) __THROW
__nonnull ((1));
817 #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8
818 /* Generate a unique temporary file name from TEMPLATE.
819 The last six characters of TEMPLATE must be "XXXXXX";
820 they are replaced with a string that makes the filename unique.
821 Returns a file descriptor open on the file for reading and writing,
822 or -1 if it cannot create a uniquely-named file.
824 This function is a possible cancellation point and therefore not
825 marked with __THROW. */
826 # ifndef __USE_FILE_OFFSET64
827 extern int mkstemp (char *__template
) __nonnull ((1)) __wur
;
830 extern int __REDIRECT (mkstemp
, (char *__template
), mkstemp64
)
831 __nonnull ((1)) __wur
;
833 # define mkstemp mkstemp64
836 # ifdef __USE_LARGEFILE64
837 extern int mkstemp64 (char *__template
) __nonnull ((1)) __wur
;
842 /* Similar to mkstemp, but the template can have a suffix after the
843 XXXXXX. The length of the suffix is specified in the second
846 This function is a possible cancellation point and therefore not
847 marked with __THROW. */
848 # ifndef __USE_FILE_OFFSET64
849 extern int mkstemps (char *__template
, int __suffixlen
) __nonnull ((1)) __wur
;
852 extern int __REDIRECT (mkstemps
, (char *__template
, int __suffixlen
),
853 mkstemps64
) __nonnull ((1)) __wur
;
855 # define mkstemps mkstemps64
858 # ifdef __USE_LARGEFILE64
859 extern int mkstemps64 (char *__template
, int __suffixlen
)
860 __nonnull ((1)) __wur
;
864 #ifdef __USE_XOPEN2K8
865 /* Create a unique temporary directory from TEMPLATE.
866 The last six characters of TEMPLATE must be "XXXXXX";
867 they are replaced with a string that makes the directory name unique.
868 Returns TEMPLATE, or a null pointer if it cannot get a unique name.
869 The directory is created mode 700. */
870 extern char *mkdtemp (char *__template
) __THROW
__nonnull ((1)) __wur
;
874 /* Generate a unique temporary file name from TEMPLATE similar to
875 mkstemp. But allow the caller to pass additional flags which are
876 used in the open call to create the file..
878 This function is a possible cancellation point and therefore not
879 marked with __THROW. */
880 # ifndef __USE_FILE_OFFSET64
881 extern int mkostemp (char *__template
, int __flags
) __nonnull ((1)) __wur
;
884 extern int __REDIRECT (mkostemp
, (char *__template
, int __flags
), mkostemp64
)
885 __nonnull ((1)) __wur
;
887 # define mkostemp mkostemp64
890 # ifdef __USE_LARGEFILE64
891 extern int mkostemp64 (char *__template
, int __flags
) __nonnull ((1)) __wur
;
894 /* Similar to mkostemp, but the template can have a suffix after the
895 XXXXXX. The length of the suffix is specified in the second
898 This function is a possible cancellation point and therefore not
899 marked with __THROW. */
900 # ifndef __USE_FILE_OFFSET64
901 extern int mkostemps (char *__template
, int __suffixlen
, int __flags
)
902 __nonnull ((1)) __wur
;
905 extern int __REDIRECT (mkostemps
, (char *__template
, int __suffixlen
,
906 int __flags
), mkostemps64
)
907 __nonnull ((1)) __wur
;
909 # define mkostemps mkostemps64
912 # ifdef __USE_LARGEFILE64
913 extern int mkostemps64 (char *__template
, int __suffixlen
, int __flags
)
914 __nonnull ((1)) __wur
;
919 /* Execute the given line as a shell command.
921 This function is a cancellation point and therefore not marked with
923 extern int system (const char *__command
) __wur
;
927 /* Return a malloc'd string containing the canonical absolute name of the
928 existing named file. */
929 extern char *canonicalize_file_name (const char *__name
)
930 __THROW
__nonnull ((1)) __attribute_malloc__
931 __attr_dealloc_free __wur
;
934 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
935 /* Return the canonical absolute name of file NAME. If RESOLVED is
936 null, the result is malloc'd; otherwise, if the canonical name is
937 PATH_MAX chars or more, returns null with `errno' set to
938 ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
939 returns the name in RESOLVED. */
940 extern char *realpath (const char *__restrict __name
,
941 char *__restrict __resolved
) __THROW __wur
;
945 /* Shorthand for type of comparison functions. */
946 #ifndef __COMPAR_FN_T
947 # define __COMPAR_FN_T
948 typedef int (*__compar_fn_t
) (const void *, const void *);
951 typedef __compar_fn_t comparison_fn_t
;
955 typedef int (*__compar_d_fn_t
) (const void *, const void *, void *);
958 /* Do a binary search for KEY in BASE, which consists of NMEMB elements
959 of SIZE bytes each, using COMPAR to perform the comparisons. */
960 extern void *bsearch (const void *__key
, const void *__base
,
961 size_t __nmemb
, size_t __size
, __compar_fn_t __compar
)
962 __nonnull ((1, 2, 5)) __wur
;
964 #ifdef __USE_EXTERN_INLINES
965 # include <bits/stdlib-bsearch.h>
968 /* Sort NMEMB elements of BASE, of SIZE bytes each,
969 using COMPAR to perform the comparisons. */
970 extern void qsort (void *__base
, size_t __nmemb
, size_t __size
,
971 __compar_fn_t __compar
) __nonnull ((1, 4));
973 extern void qsort_r (void *__base
, size_t __nmemb
, size_t __size
,
974 __compar_d_fn_t __compar
, void *__arg
)
979 /* Return the absolute value of X. */
980 extern int abs (int __x
) __THROW
__attribute__ ((__const__
)) __wur
;
981 extern long int labs (long int __x
) __THROW
__attribute__ ((__const__
)) __wur
;
984 __extension__
extern long long int llabs (long long int __x
)
985 __THROW
__attribute__ ((__const__
)) __wur
;
989 /* Return the `div_t', `ldiv_t' or `lldiv_t' representation
990 of the value of NUMER over DENOM. */
991 /* GCC may have built-ins for these someday. */
992 extern div_t div (int __numer
, int __denom
)
993 __THROW
__attribute__ ((__const__
)) __wur
;
994 extern ldiv_t ldiv (long int __numer
, long int __denom
)
995 __THROW
__attribute__ ((__const__
)) __wur
;
998 __extension__
extern lldiv_t
lldiv (long long int __numer
,
999 long long int __denom
)
1000 __THROW
__attribute__ ((__const__
)) __wur
;
1004 #if (defined __USE_XOPEN_EXTENDED && !defined __USE_XOPEN2K8) \
1005 || defined __USE_MISC
1006 /* Convert floating point numbers to strings. The returned values are
1007 valid only until another call to the same function. */
1009 /* Convert VALUE to a string with NDIGIT digits and return a pointer to
1010 this. Set *DECPT with the position of the decimal character and *SIGN
1011 with the sign of the number. */
1012 extern char *ecvt (double __value
, int __ndigit
, int *__restrict __decpt
,
1013 int *__restrict __sign
) __THROW
__nonnull ((3, 4)) __wur
;
1015 /* Convert VALUE to a string rounded to NDIGIT decimal digits. Set *DECPT
1016 with the position of the decimal character and *SIGN with the sign of
1018 extern char *fcvt (double __value
, int __ndigit
, int *__restrict __decpt
,
1019 int *__restrict __sign
) __THROW
__nonnull ((3, 4)) __wur
;
1021 /* If possible convert VALUE to a string with NDIGIT significant digits.
1022 Otherwise use exponential representation. The resulting string will
1023 be written to BUF. */
1024 extern char *gcvt (double __value
, int __ndigit
, char *__buf
)
1025 __THROW
__nonnull ((3)) __wur
;
1029 /* Long double versions of above functions. */
1030 extern char *qecvt (long double __value
, int __ndigit
,
1031 int *__restrict __decpt
, int *__restrict __sign
)
1032 __THROW
__nonnull ((3, 4)) __wur
;
1033 extern char *qfcvt (long double __value
, int __ndigit
,
1034 int *__restrict __decpt
, int *__restrict __sign
)
1035 __THROW
__nonnull ((3, 4)) __wur
;
1036 extern char *qgcvt (long double __value
, int __ndigit
, char *__buf
)
1037 __THROW
__nonnull ((3)) __wur
;
1040 /* Reentrant version of the functions above which provide their own
1042 extern int ecvt_r (double __value
, int __ndigit
, int *__restrict __decpt
,
1043 int *__restrict __sign
, char *__restrict __buf
,
1044 size_t __len
) __THROW
__nonnull ((3, 4, 5));
1045 extern int fcvt_r (double __value
, int __ndigit
, int *__restrict __decpt
,
1046 int *__restrict __sign
, char *__restrict __buf
,
1047 size_t __len
) __THROW
__nonnull ((3, 4, 5));
1049 extern int qecvt_r (long double __value
, int __ndigit
,
1050 int *__restrict __decpt
, int *__restrict __sign
,
1051 char *__restrict __buf
, size_t __len
)
1052 __THROW
__nonnull ((3, 4, 5));
1053 extern int qfcvt_r (long double __value
, int __ndigit
,
1054 int *__restrict __decpt
, int *__restrict __sign
,
1055 char *__restrict __buf
, size_t __len
)
1056 __THROW
__nonnull ((3, 4, 5));
1060 /* Return the length of the multibyte character
1061 in S, which is no longer than N. */
1062 extern int mblen (const char *__s
, size_t __n
) __THROW
;
1063 /* Return the length of the given multibyte character,
1064 putting its `wchar_t' representation in *PWC. */
1065 extern int mbtowc (wchar_t *__restrict __pwc
,
1066 const char *__restrict __s
, size_t __n
) __THROW
;
1067 /* Put the multibyte character represented
1068 by WCHAR in S, returning its length. */
1069 extern int wctomb (char *__s
, wchar_t __wchar
) __THROW
;
1072 /* Convert a multibyte string to a wide char string. */
1073 extern size_t mbstowcs (wchar_t *__restrict __pwcs
,
1074 const char *__restrict __s
, size_t __n
) __THROW
1075 __attr_access ((__read_only__
, 2));
1076 /* Convert a wide char string to multibyte string. */
1077 extern size_t wcstombs (char *__restrict __s
,
1078 const wchar_t *__restrict __pwcs
, size_t __n
)
1080 __fortified_attr_access (__write_only__
, 1, 3)
1081 __attr_access ((__read_only__
, 2));
1084 /* Determine whether the string value of RESPONSE matches the affirmation
1085 or negative response expression as specified by the LC_MESSAGES category
1086 in the program's current locale. Returns 1 if affirmative, 0 if
1087 negative, and -1 if not matching. */
1088 extern int rpmatch (const char *__response
) __THROW
__nonnull ((1)) __wur
;
1092 #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8
1093 /* Parse comma separated suboption from *OPTIONP and match against
1094 strings in TOKENS. If found return index and set *VALUEP to
1095 optional value introduced by an equal sign. If the suboption is
1096 not part of TOKENS return in *VALUEP beginning of unknown
1097 suboption. On exit *OPTIONP is set to the beginning of the next
1098 token or at the terminating NUL character. */
1099 extern int getsubopt (char **__restrict __optionp
,
1100 char *const *__restrict __tokens
,
1101 char **__restrict __valuep
)
1102 __THROW
__nonnull ((1, 2, 3)) __wur
;
1106 /* X/Open pseudo terminal handling. */
1108 #ifdef __USE_XOPEN2KXSI
1109 /* Return a master pseudo-terminal handle. */
1110 extern int posix_openpt (int __oflag
) __wur
;
1113 #ifdef __USE_XOPEN_EXTENDED
1114 /* The next four functions all take a master pseudo-tty fd and
1115 perform an operation on the associated slave: */
1117 /* Chown the slave to the calling user. */
1118 extern int grantpt (int __fd
) __THROW
;
1120 /* Release an internal lock so the slave can be opened.
1121 Call after grantpt(). */
1122 extern int unlockpt (int __fd
) __THROW
;
1124 /* Return the pathname of the pseudo terminal slave associated with
1125 the master FD is open on, or NULL on errors.
1126 The returned storage is good until the next call to this function. */
1127 extern char *ptsname (int __fd
) __THROW __wur
;
1131 /* Store at most BUFLEN characters of the pathname of the slave pseudo
1132 terminal associated with the master FD is open on in BUF.
1133 Return 0 on success, otherwise an error number. */
1134 extern int ptsname_r (int __fd
, char *__buf
, size_t __buflen
)
1135 __THROW
__nonnull ((2)) __fortified_attr_access (__write_only__
, 2, 3);
1137 /* Open a master pseudo terminal and return its file descriptor. */
1138 extern int getpt (void);
1142 /* Put the 1 minute, 5 minute and 15 minute load averages into the first
1143 NELEM elements of LOADAVG. Return the number written (never more than
1144 three, but may be less than NELEM), or -1 if an error occurred. */
1145 extern int getloadavg (double __loadavg
[], int __nelem
)
1146 __THROW
__nonnull ((1));
1149 #if defined __USE_XOPEN_EXTENDED && !defined __USE_XOPEN2K
1150 /* Return the index into the active-logins file (utmp) for
1151 the controlling terminal. */
1152 extern int ttyslot (void) __THROW
;
1155 #include <bits/stdlib-float.h>
1157 /* Define some macros helping to catch buffer overflows. */
1158 #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
1159 # include <bits/stdlib.h>
1162 #include <bits/floatn.h>
1163 #if defined __LDBL_COMPAT || __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
1164 # include <bits/stdlib-ldbl.h>
1169 #endif /* stdlib.h */