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