Home | History | Annotate | Download | only in lib
      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 
     39 /* Solaris declares getloadavg() in <sys/loadavg.h>.  */
     40 #if @GNULIB_GETLOADAVG@ && @HAVE_SYS_LOADAVG_H@
     41 # include <sys/loadavg.h>
     42 #endif
     43 
     44 /* OSF/1 5.1 declares 'struct random_data' in <random.h>, which is included
     45    from <stdlib.h> if _REENTRANT is defined.  Include it always.  */
     46 #if @HAVE_RANDOM_H@
     47 # include <random.h>
     48 #endif
     49 
     50 #if @GNULIB_RANDOM_R@ || !@HAVE_STRUCT_RANDOM_DATA@
     51 # include <stdint.h>
     52 #endif
     53 
     54 #if !@HAVE_STRUCT_RANDOM_DATA@
     55 struct random_data
     56 {
     57   int32_t *fptr;		/* Front pointer.  */
     58   int32_t *rptr;		/* Rear pointer.  */
     59   int32_t *state;		/* Array of state values.  */
     60   int rand_type;		/* Type of random number generator.  */
     61   int rand_deg;			/* Degree of random number generator.  */
     62   int rand_sep;			/* Distance between front and rear.  */
     63   int32_t *end_ptr;		/* Pointer behind state table.  */
     64 };
     65 #endif
     66 
     67 /* The definition of GL_LINK_WARNING is copied here.  */
     68 
     69 
     70 /* Some systems do not define EXIT_*, despite otherwise supporting C89.  */
     71 #ifndef EXIT_SUCCESS
     72 # define EXIT_SUCCESS 0
     73 #endif
     74 /* Tandem/NSK and other platforms that define EXIT_FAILURE as -1 interfere
     75    with proper operation of xargs.  */
     76 #ifndef EXIT_FAILURE
     77 # define EXIT_FAILURE 1
     78 #elif EXIT_FAILURE != 1
     79 # undef EXIT_FAILURE
     80 # define EXIT_FAILURE 1
     81 #endif
     82 
     83 
     84 #ifdef __cplusplus
     85 extern "C" {
     86 #endif
     87 
     88 
     89 #if @GNULIB_MALLOC_POSIX@
     90 # if !@HAVE_MALLOC_POSIX@
     91 #  undef malloc
     92 #  define malloc rpl_malloc
     93 extern void * malloc (size_t size);
     94 # endif
     95 #elif defined GNULIB_POSIXCHECK
     96 # undef malloc
     97 # define malloc(s) \
     98     (GL_LINK_WARNING ("malloc is not POSIX compliant everywhere - " \
     99                       "use gnulib module malloc-posix for portability"), \
    100      malloc (s))
    101 #endif
    102 
    103 
    104 #if @GNULIB_REALLOC_POSIX@
    105 # if !@HAVE_REALLOC_POSIX@
    106 #  undef realloc
    107 #  define realloc rpl_realloc
    108 extern void * realloc (void *ptr, size_t size);
    109 # endif
    110 #elif defined GNULIB_POSIXCHECK
    111 # undef realloc
    112 # define realloc(p,s) \
    113     (GL_LINK_WARNING ("realloc is not POSIX compliant everywhere - " \
    114                       "use gnulib module realloc-posix for portability"), \
    115      realloc (p, s))
    116 #endif
    117 
    118 
    119 #if @GNULIB_CALLOC_POSIX@
    120 # if !@HAVE_CALLOC_POSIX@
    121 #  undef calloc
    122 #  define calloc rpl_calloc
    123 extern void * calloc (size_t nmemb, size_t size);
    124 # endif
    125 #elif defined GNULIB_POSIXCHECK
    126 # undef calloc
    127 # define calloc(n,s) \
    128     (GL_LINK_WARNING ("calloc is not POSIX compliant everywhere - " \
    129                       "use gnulib module calloc-posix for portability"), \
    130      calloc (n, s))
    131 #endif
    132 
    133 
    134 #if @GNULIB_ATOLL@
    135 # if !@HAVE_ATOLL@
    136 /* Parse a signed decimal integer.
    137    Returns the value of the integer.  Errors are not detected.  */
    138 extern long long atoll (const char *string);
    139 # endif
    140 #elif defined GNULIB_POSIXCHECK
    141 # undef atoll
    142 # define atoll(s) \
    143     (GL_LINK_WARNING ("atoll is unportable - " \
    144                       "use gnulib module atoll for portability"), \
    145      atoll (s))
    146 #endif
    147 
    148 
    149 #if @GNULIB_GETLOADAVG@
    150 # if !@HAVE_DECL_GETLOADAVG@
    151 /* Store max(NELEM,3) load average numbers in LOADAVG[].
    152    The three numbers are the load average of the last 1 minute, the last 5
    153    minutes, and the last 15 minutes, respectively.
    154    LOADAVG is an array of NELEM numbers.  */
    155 extern int getloadavg (double loadavg[], int nelem);
    156 # endif
    157 #elif defined GNULIB_POSIXCHECK
    158 # undef getloadavg
    159 # define getloadavg(l,n) \
    160     (GL_LINK_WARNING ("getloadavg is not portable - " \
    161                       "use gnulib module getloadavg for portability"), \
    162      getloadavg (l, n))
    163 #endif
    164 
    165 
    166 #if @GNULIB_GETSUBOPT@
    167 /* Assuming *OPTIONP is a comma separated list of elements of the form
    168    "token" or "token=value", getsubopt parses the first of these elements.
    169    If the first element refers to a "token" that is member of the given
    170    NULL-terminated array of tokens:
    171      - It replaces the comma with a NUL byte, updates *OPTIONP to point past
    172        the first option and the comma, sets *VALUEP to the value of the
    173        element (or NULL if it doesn't contain an "=" sign),
    174      - It returns the index of the "token" in the given array of tokens.
    175    Otherwise it returns -1, and *OPTIONP and *VALUEP are undefined.
    176    For more details see the POSIX:2001 specification.
    177    http://www.opengroup.org/susv3xsh/getsubopt.html */
    178 # if !@HAVE_GETSUBOPT@
    179 extern int getsubopt (char **optionp, char *const *tokens, char **valuep);
    180 # endif
    181 #elif defined GNULIB_POSIXCHECK
    182 # undef getsubopt
    183 # define getsubopt(o,t,v) \
    184     (GL_LINK_WARNING ("getsubopt is unportable - " \
    185                       "use gnulib module getsubopt for portability"), \
    186      getsubopt (o, t, v))
    187 #endif
    188 
    189 
    190 #if @GNULIB_MKDTEMP@
    191 # if !@HAVE_MKDTEMP@
    192 /* Create a unique temporary directory from TEMPLATE.
    193    The last six characters of TEMPLATE must be "XXXXXX";
    194    they are replaced with a string that makes the directory name unique.
    195    Returns TEMPLATE, or a null pointer if it cannot get a unique name.
    196    The directory is created mode 700.  */
    197 extern char * mkdtemp (char * /*template*/);
    198 # endif
    199 #elif defined GNULIB_POSIXCHECK
    200 # undef mkdtemp
    201 # define mkdtemp(t) \
    202     (GL_LINK_WARNING ("mkdtemp is unportable - " \
    203                       "use gnulib module mkdtemp for portability"), \
    204      mkdtemp (t))
    205 #endif
    206 
    207 
    208 #if @GNULIB_MKSTEMP@
    209 # if @REPLACE_MKSTEMP@
    210 /* Create a unique temporary file from TEMPLATE.
    211    The last six characters of TEMPLATE must be "XXXXXX";
    212    they are replaced with a string that makes the file name unique.
    213    The file is then created, ensuring it didn't exist before.
    214    The file is created read-write (mask at least 0600 & ~umask), but it may be
    215    world-readable and world-writable (mask 0666 & ~umask), depending on the
    216    implementation.
    217    Returns the open file descriptor if successful, otherwise -1 and errno
    218    set.  */
    219 #  define mkstemp rpl_mkstemp
    220 extern int mkstemp (char * /*template*/);
    221 # else
    222 /* On MacOS X 10.3, only <unistd.h> declares mkstemp.  */
    223 #  include <unistd.h>
    224 # endif
    225 #elif defined GNULIB_POSIXCHECK
    226 # undef mkstemp
    227 # define mkstemp(t) \
    228     (GL_LINK_WARNING ("mkstemp is unportable - " \
    229                       "use gnulib module mkstemp for portability"), \
    230      mkstemp (t))
    231 #endif
    232 
    233 
    234 #if @GNULIB_PUTENV@
    235 # if @REPLACE_PUTENV@
    236 #  undef putenv
    237 #  define putenv rpl_putenv
    238 extern int putenv (char *string);
    239 # endif
    240 #endif
    241 
    242 
    243 #if @GNULIB_RANDOM_R@
    244 # if !@HAVE_RANDOM_R@
    245 
    246 #  ifndef RAND_MAX
    247 #   define RAND_MAX 2147483647
    248 #  endif
    249 
    250 int srandom_r (unsigned int seed, struct random_data *rand_state);
    251 int initstate_r (unsigned int seed, char *buf, size_t buf_size,
    252 		 struct random_data *rand_state);
    253 int setstate_r (char *arg_state, struct random_data *rand_state);
    254 int random_r (struct random_data *buf, int32_t *result);
    255 # endif
    256 #elif defined GNULIB_POSIXCHECK
    257 # undef random_r
    258 # define random_r(b,r)				  \
    259     (GL_LINK_WARNING ("random_r is unportable - " \
    260                       "use gnulib module random_r for portability"), \
    261      random_r (b,r))
    262 # undef initstate_r
    263 # define initstate_r(s,b,sz,r)			     \
    264     (GL_LINK_WARNING ("initstate_r is unportable - " \
    265                       "use gnulib module random_r for portability"), \
    266      initstate_r (s,b,sz,r))
    267 # undef srandom_r
    268 # define srandom_r(s,r)				   \
    269     (GL_LINK_WARNING ("srandom_r is unportable - " \
    270                       "use gnulib module random_r for portability"), \
    271      srandom_r (s,r))
    272 # undef setstate_r
    273 # define setstate_r(a,r)				    \
    274     (GL_LINK_WARNING ("setstate_r is unportable - " \
    275                       "use gnulib module random_r for portability"), \
    276      setstate_r (a,r))
    277 #endif
    278 
    279 
    280 #if @GNULIB_RPMATCH@
    281 # if !@HAVE_RPMATCH@
    282 /* Test a user response to a question.
    283    Return 1 if it is affirmative, 0 if it is negative, or -1 if not clear.  */
    284 extern int rpmatch (const char *response);
    285 # endif
    286 #elif defined GNULIB_POSIXCHECK
    287 # undef rpmatch
    288 # define rpmatch(r) \
    289     (GL_LINK_WARNING ("rpmatch is unportable - " \
    290                       "use gnulib module rpmatch for portability"), \
    291      rpmatch (r))
    292 #endif
    293 
    294 
    295 #if @GNULIB_SETENV@
    296 # if !@HAVE_SETENV@
    297 /* Set NAME to VALUE in the environment.
    298    If REPLACE is nonzero, overwrite an existing value.  */
    299 extern int setenv (const char *name, const char *value, int replace);
    300 # endif
    301 #endif
    302 
    303 
    304 #if @GNULIB_UNSETENV@
    305 # if @HAVE_UNSETENV@
    306 #  if @VOID_UNSETENV@
    307 /* On some systems, unsetenv() returns void.
    308    This is the case for MacOS X 10.3, FreeBSD 4.8, NetBSD 1.6, OpenBSD 3.4.  */
    309 #   define unsetenv(name) ((unsetenv)(name), 0)
    310 #  endif
    311 # else
    312 /* Remove the variable NAME from the environment.  */
    313 extern int unsetenv (const char *name);
    314 # endif
    315 #endif
    316 
    317 
    318 #if @GNULIB_STRTOD@
    319 # if @REPLACE_STRTOD@
    320 #  define strtod rpl_strtod
    321 # endif
    322 # if !@HAVE_STRTOD@ || @REPLACE_STRTOD@
    323  /* Parse a double from STRING, updating ENDP if appropriate.  */
    324 extern double strtod (const char *str, char **endp);
    325 # endif
    326 #elif defined GNULIB_POSIXCHECK
    327 # undef strtod
    328 # define strtod(s, e)                           \
    329     (GL_LINK_WARNING ("strtod is unportable - " \
    330                       "use gnulib module strtod for portability"), \
    331      strtod (s, e))
    332 #endif
    333 
    334 
    335 #if @GNULIB_STRTOLL@
    336 # if !@HAVE_STRTOLL@
    337 /* Parse a signed integer whose textual representation starts at STRING.
    338    The integer is expected to be in base BASE (2 <= BASE <= 36); if BASE == 0,
    339    it may be decimal or octal (with prefix "0") or hexadecimal (with prefix
    340    "0x").
    341    If ENDPTR is not NULL, the address of the first byte after the integer is
    342    stored in *ENDPTR.
    343    Upon overflow, the return value is LLONG_MAX or LLONG_MIN, and errno is set
    344    to ERANGE.  */
    345 extern long long strtoll (const char *string, char **endptr, int base);
    346 # endif
    347 #elif defined GNULIB_POSIXCHECK
    348 # undef strtoll
    349 # define strtoll(s,e,b) \
    350     (GL_LINK_WARNING ("strtoll is unportable - " \
    351                       "use gnulib module strtoll for portability"), \
    352      strtoll (s, e, b))
    353 #endif
    354 
    355 
    356 #if @GNULIB_STRTOULL@
    357 # if !@HAVE_STRTOULL@
    358 /* Parse an unsigned integer whose textual representation starts at STRING.
    359    The integer is expected to be in base BASE (2 <= BASE <= 36); if BASE == 0,
    360    it may be decimal or octal (with prefix "0") or hexadecimal (with prefix
    361    "0x").
    362    If ENDPTR is not NULL, the address of the first byte after the integer is
    363    stored in *ENDPTR.
    364    Upon overflow, the return value is ULLONG_MAX, and errno is set to
    365    ERANGE.  */
    366 extern unsigned long long strtoull (const char *string, char **endptr, int base);
    367 # endif
    368 #elif defined GNULIB_POSIXCHECK
    369 # undef strtoull
    370 # define strtoull(s,e,b) \
    371     (GL_LINK_WARNING ("strtoull is unportable - " \
    372                       "use gnulib module strtoull for portability"), \
    373      strtoull (s, e, b))
    374 #endif
    375 
    376 
    377 #ifdef __cplusplus
    378 }
    379 #endif
    380 
    381 #endif /* _GL_STDLIB_H */
    382 #endif /* _GL_STDLIB_H */
    383 #endif
    384