OSDN Git Service

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