Home | History | Annotate | Line # | Download | only in include
stdlib.h revision 1.110
      1 /*	$NetBSD: stdlib.h,v 1.110 2014/06/18 17:48:22 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
     32  */
     33 
     34 #ifndef _STDLIB_H_
     35 #define _STDLIB_H_
     36 
     37 #include <sys/cdefs.h>
     38 #include <sys/featuretest.h>
     39 
     40 #if defined(_NETBSD_SOURCE)
     41 #include <sys/types.h>		/* for quad_t, etc. */
     42 #endif
     43 
     44 #include <machine/ansi.h>
     45 
     46 #ifdef	_BSD_SIZE_T_
     47 typedef	_BSD_SIZE_T_	size_t;
     48 #undef	_BSD_SIZE_T_
     49 #endif
     50 
     51 #if defined(_BSD_WCHAR_T_) && !defined(__cplusplus)
     52 typedef	_BSD_WCHAR_T_	wchar_t;
     53 #undef	_BSD_WCHAR_T_
     54 #endif
     55 
     56 typedef struct {
     57 	int quot;		/* quotient */
     58 	int rem;		/* remainder */
     59 } div_t;
     60 
     61 typedef struct {
     62 	long quot;		/* quotient */
     63 	long rem;		/* remainder */
     64 } ldiv_t;
     65 
     66 #if !defined(_ANSI_SOURCE) && \
     67     (defined(_ISOC99_SOURCE) || (__STDC_VERSION__ - 0) >= 199901L || \
     68      (__cplusplus - 0) >= 201103L || defined(_NETBSD_SOURCE))
     69 typedef struct {
     70 	/* LONGLONG */
     71 	long long int quot;	/* quotient */
     72 	/* LONGLONG */
     73 	long long int rem;	/* remainder */
     74 } lldiv_t;
     75 #endif
     76 
     77 #if defined(_NETBSD_SOURCE)
     78 typedef struct {
     79 	quad_t quot;		/* quotient */
     80 	quad_t rem;		/* remainder */
     81 } qdiv_t;
     82 #endif
     83 
     84 
     85 #include <sys/null.h>
     86 
     87 #define	EXIT_FAILURE	1
     88 #define	EXIT_SUCCESS	0
     89 
     90 #define	RAND_MAX	0x7fffffff
     91 
     92 extern size_t __mb_cur_max;
     93 #define	MB_CUR_MAX	__mb_cur_max
     94 
     95 __BEGIN_DECLS
     96 __dead	 void _Exit(int);
     97 __dead	 void abort(void);
     98 __pure	 int abs(int);
     99 int	 atexit(void (*)(void));
    100 double	 atof(const char *);
    101 int	 atoi(const char *);
    102 long	 atol(const char *);
    103 #ifndef __BSEARCH_DECLARED
    104 #define __BSEARCH_DECLARED
    105 /* also in search.h */
    106 void	*bsearch(const void *, const void *, size_t, size_t,
    107     int (*)(const void *, const void *));
    108 #endif /* __BSEARCH_DECLARED */
    109 void	*calloc(size_t, size_t);
    110 div_t	 div(int, int);
    111 __dead	 void exit(int);
    112 void	 free(void *);
    113 __aconst char *getenv(const char *);
    114 __pure long
    115 	 labs(long);
    116 ldiv_t	 ldiv(long, long);
    117 void	*malloc(size_t);
    118 void	 qsort(void *, size_t, size_t, int (*)(const void *, const void *));
    119 int	 rand(void);
    120 void	*realloc(void *, size_t);
    121 void	 srand(unsigned);
    122 double	 strtod(const char * __restrict, char ** __restrict);
    123 long	 strtol(const char * __restrict, char ** __restrict, int);
    124 unsigned long
    125 	 strtoul(const char * __restrict, char ** __restrict, int);
    126 int	 system(const char *);
    127 
    128 /* These are currently just stubs. */
    129 int	 mblen(const char *, size_t);
    130 size_t	 mbstowcs(wchar_t * __restrict, const char * __restrict, size_t);
    131 int	 wctomb(char *, wchar_t);
    132 int	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
    133 size_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
    134 
    135 #if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) || \
    136     defined(_NETBSD_SOURCE)
    137 
    138 
    139 /*
    140  * IEEE Std 1003.1c-95, also adopted by X/Open CAE Spec Issue 5 Version 2
    141  */
    142 #if (_POSIX_C_SOURCE - 0) >= 199506L || (_XOPEN_SOURCE - 0) >= 500 || \
    143     defined(_REENTRANT) || defined(_NETBSD_SOURCE)
    144 int	 rand_r(unsigned int *);
    145 #endif
    146 
    147 
    148 /*
    149  * X/Open Portability Guide >= Issue 4
    150  */
    151 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
    152 double	 drand48(void);
    153 double	 erand48(unsigned short[3]);
    154 long	 jrand48(unsigned short[3]);
    155 void	 lcong48(unsigned short[7]);
    156 long	 lrand48(void);
    157 long	 mrand48(void);
    158 long	 nrand48(unsigned short[3]);
    159 unsigned short *
    160 	 seed48(unsigned short[3]);
    161 void	 srand48(long);
    162 
    163 #ifndef __LIBC12_SOURCE__
    164 int	 putenv(char *) __RENAME(__putenv50);
    165 #endif
    166 #endif
    167 
    168 
    169 /*
    170  * X/Open Portability Guide >= Issue 4 Version 2
    171  */
    172 #if (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
    173     (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
    174 long	 a64l(const char *);
    175 char	*l64a(long);
    176 
    177 long	 random(void);
    178 char	*setstate(char *);
    179 #ifndef __LIBC12_SOURCE__
    180 char	*initstate(unsigned int, char *, size_t) __RENAME(__initstate60);
    181 void	 srandom(unsigned int) __RENAME(__srandom60);
    182 #endif
    183 #ifdef _NETBSD_SOURCE
    184 #define	RANDOM_MAX	0x7fffffff	/* (((long)1 << 31) - 1) */
    185 int	 mkostemp(char *, int);
    186 int	 mkostemps(char *, int, int);
    187 #endif
    188 
    189 char	*mkdtemp(char *);
    190 int	 mkstemp(char *);
    191 char	*mktemp(char *)
    192 #ifdef __MKTEMP_OK__
    193 	__RENAME(_mktemp)
    194 #endif
    195 	;
    196 
    197 int	 setkey(const char *);
    198 
    199 char	*realpath(const char * __restrict, char * __restrict);
    200 
    201 int	 ttyslot(void);
    202 
    203 void	*valloc(size_t);		/* obsoleted by malloc() */
    204 
    205 int	 grantpt(int);
    206 int	 unlockpt(int);
    207 char	*ptsname(int);
    208 #endif
    209 
    210 /*
    211  * ISO C99
    212  */
    213 #if defined(_ISOC99_SOURCE) || (__STDC_VERSION__ - 0) >= 199901L || \
    214     defined(_NETBSD_SOURCE) || (__cplusplus - 0) >= 201103L
    215 
    216 /* LONGLONG */
    217 long long int	atoll(const char *);
    218 /* LONGLONG */
    219 long long int	llabs(long long int);
    220 /* LONGLONG */
    221 lldiv_t		lldiv(long long int, long long int);
    222 /* LONGLONG */
    223 long long int	strtoll(const char * __restrict, char ** __restrict, int);
    224 /* LONGLONG */
    225 unsigned long long int
    226 		strtoull(const char * __restrict, char ** __restrict, int);
    227 float		strtof(const char * __restrict, char ** __restrict);
    228 long double	strtold(const char * __restrict, char ** __restrict);
    229 #endif
    230 
    231 #if defined(_ISOC11_SOURCE) || (__STDC_VERSION__ - 0) >= 201101L || \
    232     defined(_NETBSD_SOURCE) || (__cplusplus - 0) >= 201103L
    233 int	at_quick_exit(void (*)(void));
    234 __dead void quick_exit(int);
    235 #endif
    236 
    237 /*
    238  * The Open Group Base Specifications, Issue 6; IEEE Std 1003.1-2001 (POSIX)
    239  */
    240 #if (_POSIX_C_SOURCE - 0) >= 200112L || (_XOPEN_SOURCE - 0) >= 600 || \
    241     defined(_NETBSD_SOURCE)
    242 int	 setenv(const char *, const char *, int);
    243 #ifndef __LIBC12_SOURCE__
    244 int	 unsetenv(const char *) __RENAME(__unsetenv13);
    245 #endif
    246 
    247 int	 posix_openpt(int);
    248 int	 posix_memalign(void **, size_t, size_t);
    249 #endif
    250 
    251 /*
    252  * Implementation-defined extensions
    253  */
    254 #if defined(_NETBSD_SOURCE)
    255 #if defined(alloca) && (alloca == __builtin_alloca) && \
    256 	defined(__GNUC__) && (__GNUC__ < 2)
    257 void	*alloca(int);     /* built-in for gcc */
    258 #elif defined(__PCC__) && !defined(__GNUC__)
    259 #define alloca(size) __builtin_alloca(size)
    260 #else
    261 void	*alloca(size_t);
    262 #endif /* __GNUC__ */
    263 
    264 uint32_t arc4random(void);
    265 void	 arc4random_stir(void);
    266 void	 arc4random_buf(void *, size_t);
    267 uint32_t arc4random_uniform(uint32_t);
    268 void	 arc4random_addrandom(u_char *, int);
    269 char	*getbsize(int *, long *);
    270 char	*cgetcap(char *, const char *, int);
    271 int	 cgetclose(void);
    272 int	 cgetent(char **, const char * const *, const char *);
    273 int	 cgetfirst(char **, const char * const *);
    274 int	 cgetmatch(const char *, const char *);
    275 int	 cgetnext(char **, const char * const *);
    276 int	 cgetnum(char *, const char *, long *);
    277 int	 cgetset(const char *);
    278 int	 cgetstr(char *, const char *, char **);
    279 int	 cgetustr(char *, const char *, char **);
    280 void	 csetexpandtc(int);
    281 
    282 int	 daemon(int, int);
    283 int	 devname_r(dev_t, mode_t, char *, size_t);
    284 #ifndef __LIBC12_SOURCE__
    285 __aconst char *devname(dev_t, mode_t) __RENAME(__devname50);
    286 #endif
    287 
    288 #define	HN_DECIMAL		0x01
    289 #define	HN_NOSPACE		0x02
    290 #define	HN_B			0x04
    291 #define	HN_DIVISOR_1000		0x08
    292 
    293 #define	HN_GETSCALE		0x10
    294 #define	HN_AUTOSCALE		0x20
    295 
    296 int	 humanize_number(char *, size_t, int64_t, const char *, int, int);
    297 int	 dehumanize_number(const char *, int64_t *);
    298 
    299 devmajor_t getdevmajor(const char *, mode_t);
    300 int	 getloadavg(double [], int);
    301 
    302 int	 getenv_r(const char *, char *, size_t);
    303 
    304 void	 cfree(void *);
    305 
    306 int	 heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
    307 int	 mergesort(void *, size_t, size_t,
    308 	    int (*)(const void *, const void *));
    309 int	 ptsname_r(int, char *, size_t);
    310 int	 radixsort(const unsigned char **, int, const unsigned char *,
    311 	    unsigned);
    312 int	 sradixsort(const unsigned char **, int, const unsigned char *,
    313 	    unsigned);
    314 
    315 void	 mi_vector_hash(const void * __restrict, size_t, uint32_t,
    316 	    uint32_t[3]);
    317 
    318 void	 setproctitle(const char *, ...)
    319 	    __printflike(1, 2);
    320 const char *getprogname(void) __constfunc;
    321 void	setprogname(const char *);
    322 
    323 quad_t	 qabs(quad_t);
    324 quad_t	 strtoq(const char * __restrict, char ** __restrict, int);
    325 u_quad_t strtouq(const char * __restrict, char ** __restrict, int);
    326 
    327 	/* LONGLONG */
    328 long long strsuftoll(const char *, const char *, long long, long long);
    329 	/* LONGLONG */
    330 long long strsuftollx(const char *, const char *, long long, long long,
    331 	    		char *, size_t);
    332 
    333 int	 l64a_r(long, char *, int);
    334 
    335 size_t	shquote(const char *, char *, size_t);
    336 size_t	shquotev(int, char * const *, char *, size_t);
    337 #endif /* _NETBSD_SOURCE */
    338 #endif /* _POSIX_C_SOURCE || _XOPEN_SOURCE || _NETBSD_SOURCE */
    339 
    340 #if defined(_NETBSD_SOURCE)
    341 qdiv_t	 qdiv(quad_t, quad_t);
    342 #endif
    343 
    344 #if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE)
    345 #  ifndef __LOCALE_T_DECLARED
    346 typedef struct _locale		*locale_t;
    347 #  define __LOCALE_T_DECLARED
    348 #  endif
    349 double		strtod_l(const char * __restrict, char ** __restrict, locale_t);
    350 float		strtof_l(const char * __restrict, char ** __restrict, locale_t);
    351 long double	strtold_l(const char * __restrict, char ** __restrict,
    352 			  locale_t);
    353 long	 strtol_l(const char * __restrict, char ** __restrict, int, locale_t);
    354 unsigned long
    355 	 strtoul_l(const char * __restrict, char ** __restrict, int, locale_t);
    356 /* LONGLONG */
    357 long long int
    358 	strtoll_l(const char * __restrict, char ** __restrict, int, locale_t);
    359 /* LONGLONG */
    360 unsigned long long int
    361 	strtoull_l(const char * __restrict, char ** __restrict, int, locale_t);
    362 
    363 #  if defined(_NETBSD_SOURCE)
    364 quad_t	 strtoq_l(const char * __restrict, char ** __restrict, int, locale_t);
    365 u_quad_t strtouq_l(const char * __restrict, char ** __restrict, int, locale_t);
    366 
    367 size_t	_mb_cur_max_l(locale_t);
    368 #define	MB_CUR_MAX_L(loc)	_mb_cur_max_l(loc)
    369 int	 mblen_l(const char *, size_t, locale_t);
    370 size_t	 mbstowcs_l(wchar_t * __restrict, const char * __restrict, size_t,
    371 		    locale_t);
    372 int	 wctomb_l(char *, wchar_t, locale_t);
    373 int	 mbtowc_l(wchar_t * __restrict, const char * __restrict, size_t,
    374 	          locale_t);
    375 size_t	 wcstombs_l(char * __restrict, const wchar_t * __restrict, size_t,
    376 		    locale_t);
    377 
    378 #  endif /* _NETBSD_SOURCE */
    379 #endif /* _POSIX_C_SOURCE >= 200809 || _NETBSD_SOURCE */
    380 
    381 __END_DECLS
    382 
    383 #endif /* !_STDLIB_H_ */
    384