1/* A GNU-like <stdlib.h>.
2
3   Copyright (C) 1995, 2001-2004, 2006-2010 Free Software Foundation, Inc.
4
5   This program is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 3 of the License, or
8   (at your option) any later version.
9
10   This program 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
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18#if __GNUC__ >= 3
19@PRAGMA_SYSTEM_HEADER@
20#endif
21
22#if defined __need_malloc_and_calloc
23/* Special invocation convention inside glibc header files.  */
24
25#@INCLUDE_NEXT@ @NEXT_STDLIB_H@
26
27#else
28/* Normal invocation convention.  */
29
30#ifndef _GL_STDLIB_H
31
32/* The include_next requires a split double-inclusion guard.  */
33#@INCLUDE_NEXT@ @NEXT_STDLIB_H@
34
35#ifndef _GL_STDLIB_H
36#define _GL_STDLIB_H
37
38/* NetBSD 5.0 mis-defines NULL.  */
39#include <stddef.h>
40
41/* Solaris declares getloadavg() in <sys/loadavg.h>.  */
42#if (@GNULIB_GETLOADAVG@ || defined GNULIB_POSIXCHECK) && @HAVE_SYS_LOADAVG_H@
43# include <sys/loadavg.h>
44#endif
45
46/* OSF/1 5.1 declares 'struct random_data' in <random.h>, which is included
47   from <stdlib.h> if _REENTRANT is defined.  Include it always.  */
48#if @HAVE_RANDOM_H@
49# include <random.h>
50#endif
51
52#if !@HAVE_STRUCT_RANDOM_DATA@ || (@GNULIB_RANDOM_R@ && !@HAVE_RANDOM_R@) \
53    || defined GNULIB_POSIXCHECK
54# include <stdint.h>
55#endif
56
57#if !@HAVE_STRUCT_RANDOM_DATA@
58struct random_data
59{
60  int32_t *fptr;                /* Front pointer.  */
61  int32_t *rptr;                /* Rear pointer.  */
62  int32_t *state;               /* Array of state values.  */
63  int rand_type;                /* Type of random number generator.  */
64  int rand_deg;                 /* Degree of random number generator.  */
65  int rand_sep;                 /* Distance between front and rear.  */
66  int32_t *end_ptr;             /* Pointer behind state table.  */
67};
68#endif
69
70#if (@GNULIB_MKSTEMP@ || @GNULIB_GETSUBOPT@ || defined GNULIB_POSIXCHECK) && ! defined __GLIBC__
71/* On MacOS X 10.3, only <unistd.h> declares mkstemp.  */
72/* On Cygwin 1.7.1, only <unistd.h> declares getsubopt.  */
73/* But avoid namespace pollution on glibc systems.  */
74# include <unistd.h>
75#endif
76
77/* The definition of GL_LINK_WARNING is copied here.  */
78
79/* The definition of _GL_ARG_NONNULL is copied here.  */
80
81
82/* Some systems do not define EXIT_*, despite otherwise supporting C89.  */
83#ifndef EXIT_SUCCESS
84# define EXIT_SUCCESS 0
85#endif
86/* Tandem/NSK and other platforms that define EXIT_FAILURE as -1 interfere
87   with proper operation of xargs.  */
88#ifndef EXIT_FAILURE
89# define EXIT_FAILURE 1
90#elif EXIT_FAILURE != 1
91# undef EXIT_FAILURE
92# define EXIT_FAILURE 1
93#endif
94
95
96#ifdef __cplusplus
97extern "C" {
98#endif
99
100#if @GNULIB_ATOLL@
101# if !@HAVE_ATOLL@
102/* Parse a signed decimal integer.
103   Returns the value of the integer.  Errors are not detected.  */
104extern long long atoll (const char *string) _GL_ARG_NONNULL ((1));
105# endif
106#elif defined GNULIB_POSIXCHECK
107# undef atoll
108# define atoll(s) \
109    (GL_LINK_WARNING ("atoll is unportable - " \
110                      "use gnulib module atoll for portability"), \
111     atoll (s))
112#endif
113
114#if @GNULIB_CALLOC_POSIX@
115# if !@HAVE_CALLOC_POSIX@
116#  undef calloc
117#  define calloc rpl_calloc
118extern void * calloc (size_t nmemb, size_t size);
119# endif
120#elif defined GNULIB_POSIXCHECK
121# undef calloc
122# define calloc(n,s) \
123    (GL_LINK_WARNING ("calloc is not POSIX compliant everywhere - " \
124                      "use gnulib module calloc-posix for portability"), \
125     calloc (n, s))
126#endif
127
128#if @GNULIB_CANONICALIZE_FILE_NAME@
129# if @REPLACE_CANONICALIZE_FILE_NAME@
130#  define canonicalize_file_name rpl_canonicalize_file_name
131# endif
132# if !@HAVE_CANONICALIZE_FILE_NAME@ || @REPLACE_CANONICALIZE_FILE_NAME@
133extern char *canonicalize_file_name (const char *name) _GL_ARG_NONNULL ((1));
134# endif
135#elif defined GNULIB_POSIXCHECK
136# undef canonicalize_file_name
137# define canonicalize_file_name(n)                        \
138    (GL_LINK_WARNING ("canonicalize_file_name is unportable - " \
139                      "use gnulib module canonicalize-lgpl for portability"), \
140     canonicalize_file_name (n))
141#endif
142
143#if @GNULIB_GETLOADAVG@
144# if !@HAVE_DECL_GETLOADAVG@
145/* Store max(NELEM,3) load average numbers in LOADAVG[].
146   The three numbers are the load average of the last 1 minute, the last 5
147   minutes, and the last 15 minutes, respectively.
148   LOADAVG is an array of NELEM numbers.  */
149extern int getloadavg (double loadavg[], int nelem) _GL_ARG_NONNULL ((1));
150# endif
151#elif defined GNULIB_POSIXCHECK
152# undef getloadavg
153# define getloadavg(l,n) \
154    (GL_LINK_WARNING ("getloadavg is not portable - " \
155                      "use gnulib module getloadavg for portability"), \
156     getloadavg (l, n))
157#endif
158
159#if @GNULIB_GETSUBOPT@
160/* Assuming *OPTIONP is a comma separated list of elements of the form
161   "token" or "token=value", getsubopt parses the first of these elements.
162   If the first element refers to a "token" that is member of the given
163   NULL-terminated array of tokens:
164     - It replaces the comma with a NUL byte, updates *OPTIONP to point past
165       the first option and the comma, sets *VALUEP to the value of the
166       element (or NULL if it doesn't contain an "=" sign),
167     - It returns the index of the "token" in the given array of tokens.
168   Otherwise it returns -1, and *OPTIONP and *VALUEP are undefined.
169   For more details see the POSIX:2001 specification.
170   http://www.opengroup.org/susv3xsh/getsubopt.html */
171# if !@HAVE_GETSUBOPT@
172extern int getsubopt (char **optionp, char *const *tokens, char **valuep)
173     _GL_ARG_NONNULL ((1, 2, 3));
174# endif
175#elif defined GNULIB_POSIXCHECK
176# undef getsubopt
177# define getsubopt(o,t,v) \
178    (GL_LINK_WARNING ("getsubopt is unportable - " \
179                      "use gnulib module getsubopt for portability"), \
180     getsubopt (o, t, v))
181#endif
182
183#if @GNULIB_MALLOC_POSIX@
184# if !@HAVE_MALLOC_POSIX@
185#  undef malloc
186#  define malloc rpl_malloc
187extern void * malloc (size_t size);
188# endif
189#elif defined GNULIB_POSIXCHECK
190# undef malloc
191# define malloc(s) \
192    (GL_LINK_WARNING ("malloc is not POSIX compliant everywhere - " \
193                      "use gnulib module malloc-posix for portability"), \
194     malloc (s))
195#endif
196
197#if @GNULIB_MKDTEMP@
198# if !@HAVE_MKDTEMP@
199/* Create a unique temporary directory from TEMPLATE.
200   The last six characters of TEMPLATE must be "XXXXXX";
201   they are replaced with a string that makes the directory name unique.
202   Returns TEMPLATE, or a null pointer if it cannot get a unique name.
203   The directory is created mode 700.  */
204extern char * mkdtemp (char * /*template*/) _GL_ARG_NONNULL ((1));
205# endif
206#elif defined GNULIB_POSIXCHECK
207# undef mkdtemp
208# define mkdtemp(t) \
209    (GL_LINK_WARNING ("mkdtemp is unportable - " \
210                      "use gnulib module mkdtemp for portability"), \
211     mkdtemp (t))
212#endif
213
214#if @GNULIB_MKOSTEMP@
215# if !@HAVE_MKOSTEMP@
216/* Create a unique temporary file from TEMPLATE.
217   The last six characters of TEMPLATE must be "XXXXXX";
218   they are replaced with a string that makes the file name unique.
219   The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
220   and O_TEXT, O_BINARY (defined in "binary-io.h").
221   The file is then created, with the specified flags, ensuring it didn't exist
222   before.
223   The file is created read-write (mask at least 0600 & ~umask), but it may be
224   world-readable and world-writable (mask 0666 & ~umask), depending on the
225   implementation.
226   Returns the open file descriptor if successful, otherwise -1 and errno
227   set.  */
228extern int mkostemp (char * /*template*/, int /*flags*/) _GL_ARG_NONNULL ((1));
229# endif
230#elif defined GNULIB_POSIXCHECK
231# undef mkostemp
232# define mkostemp(t,f) \
233    (GL_LINK_WARNING ("mkostemp is unportable - " \
234                      "use gnulib module mkostemp for portability"), \
235     mkostemp (t, f))
236#endif
237
238#if @GNULIB_MKOSTEMPS@
239# if !@HAVE_MKOSTEMPS@
240/* Create a unique temporary file from TEMPLATE.
241   The last six characters of TEMPLATE before a suffix of length
242   SUFFIXLEN must be "XXXXXX";
243   they are replaced with a string that makes the file name unique.
244   The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
245   and O_TEXT, O_BINARY (defined in "binary-io.h").
246   The file is then created, with the specified flags, ensuring it didn't exist
247   before.
248   The file is created read-write (mask at least 0600 & ~umask), but it may be
249   world-readable and world-writable (mask 0666 & ~umask), depending on the
250   implementation.
251   Returns the open file descriptor if successful, otherwise -1 and errno
252   set.  */
253extern int mkostemps (char * /*template*/, int /*suffixlen*/, int /*flags*/)
254     _GL_ARG_NONNULL ((1));
255# endif
256#elif defined GNULIB_POSIXCHECK
257# undef mkostemps
258# define mkostemps(t,s,f)                          \
259    (GL_LINK_WARNING ("mkostemps is unportable - " \
260                      "use gnulib module mkostemps for portability"), \
261     mkostemps (t, s, f))
262#endif
263
264#if @GNULIB_MKSTEMP@
265# if @REPLACE_MKSTEMP@
266/* Create a unique temporary file from TEMPLATE.
267   The last six characters of TEMPLATE must be "XXXXXX";
268   they are replaced with a string that makes the file name unique.
269   The file is then created, ensuring it didn't exist before.
270   The file is created read-write (mask at least 0600 & ~umask), but it may be
271   world-readable and world-writable (mask 0666 & ~umask), depending on the
272   implementation.
273   Returns the open file descriptor if successful, otherwise -1 and errno
274   set.  */
275#  define mkstemp rpl_mkstemp
276extern int mkstemp (char * /*template*/) _GL_ARG_NONNULL ((1));
277# endif
278#elif defined GNULIB_POSIXCHECK
279# undef mkstemp
280# define mkstemp(t) \
281    (GL_LINK_WARNING ("mkstemp is unportable - " \
282                      "use gnulib module mkstemp for portability"), \
283     mkstemp (t))
284#endif
285
286#if @GNULIB_MKSTEMPS@
287# if !@HAVE_MKSTEMPS@
288/* Create a unique temporary file from TEMPLATE.
289   The last six characters of TEMPLATE prior to a suffix of length
290   SUFFIXLEN must be "XXXXXX";
291   they are replaced with a string that makes the file name unique.
292   The file is then created, ensuring it didn't exist before.
293   The file is created read-write (mask at least 0600 & ~umask), but it may be
294   world-readable and world-writable (mask 0666 & ~umask), depending on the
295   implementation.
296   Returns the open file descriptor if successful, otherwise -1 and errno
297   set.  */
298extern int mkstemps (char * /*template*/, int /*suffixlen*/)
299     _GL_ARG_NONNULL ((1));
300# endif
301#elif defined GNULIB_POSIXCHECK
302# undef mkstemps
303# define mkstemps(t,s)                             \
304    (GL_LINK_WARNING ("mkstemps is unportable - " \
305                      "use gnulib module mkstemps for portability"), \
306     mkstemps (t, s))
307#endif
308
309#if @GNULIB_PUTENV@
310# if @REPLACE_PUTENV@
311#  undef putenv
312#  define putenv rpl_putenv
313extern int putenv (char *string) _GL_ARG_NONNULL ((1));
314# endif
315#endif
316
317#if @GNULIB_RANDOM_R@
318# if !@HAVE_RANDOM_R@
319
320#  ifndef RAND_MAX
321#   define RAND_MAX 2147483647
322#  endif
323
324int srandom_r (unsigned int seed, struct random_data *rand_state)
325     _GL_ARG_NONNULL ((2));
326int initstate_r (unsigned int seed, char *buf, size_t buf_size,
327                 struct random_data *rand_state)
328     _GL_ARG_NONNULL ((2, 4));
329int setstate_r (char *arg_state, struct random_data *rand_state)
330     _GL_ARG_NONNULL ((1, 2));
331int random_r (struct random_data *buf, int32_t *result)
332     _GL_ARG_NONNULL ((1, 2));
333# endif
334#elif defined GNULIB_POSIXCHECK
335# undef random_r
336# define random_r(b,r)                            \
337    (GL_LINK_WARNING ("random_r is unportable - " \
338                      "use gnulib module random_r for portability"), \
339     random_r (b,r))
340# undef initstate_r
341# define initstate_r(s,b,sz,r)                       \
342    (GL_LINK_WARNING ("initstate_r is unportable - " \
343                      "use gnulib module random_r for portability"), \
344     initstate_r (s,b,sz,r))
345# undef srandom_r
346# define srandom_r(s,r)                            \
347    (GL_LINK_WARNING ("srandom_r is unportable - " \
348                      "use gnulib module random_r for portability"), \
349     srandom_r (s,r))
350# undef setstate_r
351# define setstate_r(a,r)                                    \
352    (GL_LINK_WARNING ("setstate_r is unportable - " \
353                      "use gnulib module random_r for portability"), \
354     setstate_r (a,r))
355#endif
356
357#if @GNULIB_REALLOC_POSIX@
358# if !@HAVE_REALLOC_POSIX@
359#  undef realloc
360#  define realloc rpl_realloc
361extern void * realloc (void *ptr, size_t size);
362# endif
363#elif defined GNULIB_POSIXCHECK
364# undef realloc
365# define realloc(p,s) \
366    (GL_LINK_WARNING ("realloc is not POSIX compliant everywhere - " \
367                      "use gnulib module realloc-posix for portability"), \
368     realloc (p, s))
369#endif
370
371#if @GNULIB_REALPATH@
372# if @REPLACE_REALPATH@
373#  define realpath rpl_realpath
374# endif
375# if !@HAVE_REALPATH@ || @REPLACE_REALPATH@
376extern char *realpath (const char *name, char *resolved) _GL_ARG_NONNULL ((1));
377# endif
378#elif defined GNULIB_POSIXCHECK
379# undef realpath
380# define realpath(n,r)                        \
381    (GL_LINK_WARNING ("realpath is unportable - use gnulib module " \
382                      "canonicalize or canonicalize-lgpl for portability"), \
383     realpath (n, r))
384#endif
385
386#if @GNULIB_RPMATCH@
387# if !@HAVE_RPMATCH@
388/* Test a user response to a question.
389   Return 1 if it is affirmative, 0 if it is negative, or -1 if not clear.  */
390extern int rpmatch (const char *response) _GL_ARG_NONNULL ((1));
391# endif
392#elif defined GNULIB_POSIXCHECK
393# undef rpmatch
394# define rpmatch(r) \
395    (GL_LINK_WARNING ("rpmatch is unportable - " \
396                      "use gnulib module rpmatch for portability"), \
397     rpmatch (r))
398#endif
399
400#if @GNULIB_SETENV@
401# if @REPLACE_SETENV@
402#  undef setenv
403#  define setenv rpl_setenv
404# endif
405# if !@HAVE_SETENV@ || @REPLACE_SETENV@
406/* Set NAME to VALUE in the environment.
407   If REPLACE is nonzero, overwrite an existing value.  */
408extern int setenv (const char *name, const char *value, int replace)
409     _GL_ARG_NONNULL ((1));
410# endif
411#elif defined GNULIB_POSIXCHECK
412# undef setenv
413# define setenv(n,v,o)                                                  \
414    (GL_LINK_WARNING ("setenv is unportable - "                         \
415                      "use gnulib module setenv for portability"),      \
416     setenv (n, v, o))
417#endif
418
419#if @GNULIB_STRTOD@
420# if @REPLACE_STRTOD@
421#  define strtod rpl_strtod
422# endif
423# if !@HAVE_STRTOD@ || @REPLACE_STRTOD@
424 /* Parse a double from STRING, updating ENDP if appropriate.  */
425extern double strtod (const char *str, char **endp) _GL_ARG_NONNULL ((1));
426# endif
427#elif defined GNULIB_POSIXCHECK
428# undef strtod
429# define strtod(s, e)                           \
430    (GL_LINK_WARNING ("strtod is unportable - " \
431                      "use gnulib module strtod for portability"), \
432     strtod (s, e))
433#endif
434
435#if @GNULIB_STRTOLL@
436# if !@HAVE_STRTOLL@
437/* Parse a signed integer whose textual representation starts at STRING.
438   The integer is expected to be in base BASE (2 <= BASE <= 36); if BASE == 0,
439   it may be decimal or octal (with prefix "0") or hexadecimal (with prefix
440   "0x").
441   If ENDPTR is not NULL, the address of the first byte after the integer is
442   stored in *ENDPTR.
443   Upon overflow, the return value is LLONG_MAX or LLONG_MIN, and errno is set
444   to ERANGE.  */
445extern long long strtoll (const char *string, char **endptr, int base)
446     _GL_ARG_NONNULL ((1));
447# endif
448#elif defined GNULIB_POSIXCHECK
449# undef strtoll
450# define strtoll(s,e,b) \
451    (GL_LINK_WARNING ("strtoll is unportable - " \
452                      "use gnulib module strtoll for portability"), \
453     strtoll (s, e, b))
454#endif
455
456#if @GNULIB_STRTOULL@
457# if !@HAVE_STRTOULL@
458/* Parse an unsigned integer whose textual representation starts at STRING.
459   The integer is expected to be in base BASE (2 <= BASE <= 36); if BASE == 0,
460   it may be decimal or octal (with prefix "0") or hexadecimal (with prefix
461   "0x").
462   If ENDPTR is not NULL, the address of the first byte after the integer is
463   stored in *ENDPTR.
464   Upon overflow, the return value is ULLONG_MAX, and errno is set to
465   ERANGE.  */
466extern unsigned long long strtoull (const char *string, char **endptr, int base)
467     _GL_ARG_NONNULL ((1));
468# endif
469#elif defined GNULIB_POSIXCHECK
470# undef strtoull
471# define strtoull(s,e,b) \
472    (GL_LINK_WARNING ("strtoull is unportable - " \
473                      "use gnulib module strtoull for portability"), \
474     strtoull (s, e, b))
475#endif
476
477#if @GNULIB_UNSETENV@
478# if @REPLACE_UNSETENV@
479#  undef unsetenv
480#  define unsetenv rpl_unsetenv
481# endif
482# if !@HAVE_UNSETENV@ || @REPLACE_UNSETENV@
483/* Remove the variable NAME from the environment.  */
484extern int unsetenv (const char *name) _GL_ARG_NONNULL ((1));
485# endif
486#elif defined GNULIB_POSIXCHECK
487# undef unsetenv
488# define unsetenv(n)                                                    \
489    (GL_LINK_WARNING ("unsetenv is unportable - "                       \
490                      "use gnulib module unsetenv for portability"),    \
491     unsetenv (n))
492#endif
493
494#ifdef __cplusplus
495}
496#endif
497
498#endif /* _GL_STDLIB_H */
499#endif /* _GL_STDLIB_H */
500#endif
501