1 /**	$MirOS: src/include/stdlib.h,v 1.34 2014/03/05 14:07:26 tg Exp $ */
2 /*	$OpenBSD: stdlib.h,v 1.34 2005/05/27 17:45:56 millert Exp $	*/
3 /*	$NetBSD: stdlib.h,v 1.25 1995/12/27 21:19:08 jtc Exp $	*/
4 
5 /*-
6  * Copyright © 2013, 2014
7  *	Thorsten “mirabilos” Glaser <tg@mirbsd.org>
8  * Copyright (c) 1990 The Regents of the University of California.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)stdlib.h	5.13 (Berkeley) 6/4/91
36  */
37 
38 #ifndef _STDLIB_H_
39 #define _STDLIB_H_
40 
41 #include <machine/ansi.h>
42 
43 #if !defined(_ANSI_SOURCE)	/* for quad_t, etc. */
44 #include <sys/types.h>
45 #include <sys/wait.h>
46 #endif
47 
48 #if !defined(_GCC_SIZE_T)
49 #define	_GCC_SIZE_T
50 typedef	__SIZE_TYPE__	size_t;
51 #endif
52 
53 /* C11 optional */
54 #if !defined(__STDC_WANT_LIB_EXT1__) || (__STDC_WANT_LIB_EXT1__)
55 #ifndef rsize_t
56 #define rsize_t		rsize_t
57 typedef size_t		rsize_t;
58 #endif
59 #endif /* __STDC_WANT_LIB_EXT1__ */
60 
61 #if !defined(_GCC_WCHAR_T) && !defined(__cplusplus)
62 #define	_GCC_WCHAR_T
63 typedef	__WCHAR_TYPE__	wchar_t;
64 #endif
65 
66 typedef struct {
67 	int quot;		/* quotient */
68 	int rem;		/* remainder */
69 } div_t;
70 
71 typedef struct {
72 	long quot;		/* quotient */
73 	long rem;		/* remainder */
74 } ldiv_t;
75 
76 #if !defined(_ANSI_SOURCE)
77 typedef struct {
78 	quad_t quot;		/* quotient */
79 	quad_t rem;		/* remainder */
80 } qdiv_t;
81 #endif
82 
83 
84 #ifndef NULL
85 #ifdef 	__GNUG__
86 #define	NULL	__null
87 #elif defined(lint)
88 #define	NULL	0
89 #else
90 #define	NULL	((void *)((__PTRDIFF_TYPE__)0UL))
91 #endif
92 #endif
93 
94 #define	EXIT_FAILURE	1
95 #define	EXIT_SUCCESS	0
96 
97 #define	RAND_MAX	0x7fffffff
98 
99 /* maximum length of a multibyte character sequence (all locales) */
100 /*
101  * Note: we internally use UCS-2 which yields a maximum of 3, but
102  * due to the brain-dead specification of e.g. wcrtomb(3), 2 more
103  * bytes (MAX - 1) can be stored due to old mbstate_t processing.
104  */
105 #undef MB_LEN_MAX
106 #define MB_LEN_MAX	5	/* 3 (UCS-2) * 2 - 1 */
107 
108 /* maximum length of a multibyte character sequence (current locale) */
109 #undef MB_CUR_MAX
110 #define MB_CUR_MAX	5
111 
112 #include <sys/cdefs.h>
113 
114 #ifndef _STDLIB_H_abs_DEFINED
115 /*
116  * Some header files may define an abs macro.
117  * If defined, undef it to prevent a syntax error and issue a warning.
118  */
119 #ifdef abs
120 #undef abs
121 #warning abs macro collides with abs() prototype, undefining
122 #endif
123 __BEGIN_DECLS
124 int abs(int);
125 __END_DECLS
126 #define _STDLIB_H_abs_DEFINED
127 #define abs(j) __extension__({				\
128 	int abs_j = (j);				\
129 	(abs_j < 0 ? -abs_j : abs_j);			\
130 })
131 #endif
132 
133 __BEGIN_DECLS
134 __dead void	 abort(void);
135 int	 atexit(void (*)(void));
136 double	 atof(const char *);
137 int	 atoi(const char *);
138 long	 atol(const char *);
139 /* LONGLONG */
140 long long atoll(const char *);
141 void	*bsearch(const void *, const void *, size_t, size_t,
142 	    int (*)(const void *, const void *));
143 void	*calloc(size_t, size_t);
144 div_t	 div(int, int);
145 char	*ecvt(double, int, int *, int *);
146 __dead void	 exit(int);
147 __dead void	 _Exit(int);
148 char	*fcvt(double, int, int *, int *);
149 void	 free(void *);
150 char	*gcvt(double, int, char *);
151 char	*getenv(const char *);
152 long	 labs(long);
153 ldiv_t	 ldiv(long, long);
154 /* LONGLONG */
155 long long
156 	 llabs(long long);
157 void	*malloc(size_t);
158 int	 posix_memalign(void **, size_t, size_t);
159 char	*mkdtemp(char *);
160 int	 mkstemp(char *);
161 int	 mkstemps(char *, int);
162 char	*mktemp(char *);
163 void	 qsort(void *, size_t, size_t, int (*)(const void *, const void *));
164 int	 rand(void);
165 int	 rand_r(unsigned int *);
166 void	*realloc(void *, size_t);
167 void	 srand(unsigned);
168 double	 strtod(const char *, char **);
169 float	 strtof(const char *, char **);
170 long	 strtol(const char *, char **, int);
171 /* LONGLONG */
172 long long
173 	 strtoll(const char *, char **, int);
174 /* LONGLONG */
175 long long
176 	 strtonum(const char *, long long, long long, const char **);
177 unsigned long
178 	 strtoul(const char *, char **, int);
179 /* LONGLONG */
180 unsigned long long
181 	 strtoull(const char *, char **, int);
182 int	 system(const char *);
183 
184 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE)
185 #if defined(alloca) && (alloca == __builtin_alloca) && (__GNUC__ < 2)
186 void  *alloca(int);     /* built-in for gcc */
187 #elif defined(__PCC__)
188 #define alloca(size)	__builtin_alloca(size)
189 #else
190 void  *alloca(size_t);
191 #endif /* __GNUC__ */
192 
193 char	*getbsize(int *, long *);
194 char	*cgetcap(char *, const char *, int);
195 int	 cgetclose(void);
196 int	 cgetent(char **, char **, const char *);
197 int	 cgetfirst(char **, char **);
198 int	 cgetmatch(char *, const char *);
199 int	 cgetnext(char **, char **);
200 int	 cgetnum(char *, const char *, long *);
201 int	 cgetset(const char *);
202 int	 cgetusedb(int);
203 int	 cgetstr(char *, const char *, char **);
204 int	 cgetustr(char *, const char *, char **);
205 
206 int	 daemon(int, int);
207 char	*devname(int, int);
208 int	 getloadavg(double *, int);
209 
210 long	 a64l(const char *);
211 char	*l64a(long);
212 
213 void	 cfree(void *);
214 
215 #ifndef _GETOPT_DEFINED_
216 #define _GETOPT_DEFINED_
217 int	 getopt(int, char * const *, const char *);
218 extern	 char *optarg;			/* getopt(3) external variables */
219 extern	 int opterr;
220 extern	 int optind;
221 extern	 int optopt;
222 extern	 int optreset;
223 int	 getsubopt(char **, char * const *, char **);
224 extern	 char *suboptarg;		/* getsubopt(3) external variable */
225 #endif /* _GETOPT_DEFINED_ */
226 
227 int	 heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
228 int	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
229 int	 radixsort(const unsigned char **, int, const unsigned char *,
230 	    unsigned);
231 int	 sradixsort(const unsigned char **, int, const unsigned char *,
232 	    unsigned);
233 
234 char	*initstate(unsigned int, char *, size_t)
235 		__attribute__((__bounded__(__string__, 2, 3)));
236 long	 random(void);
237 char	*realpath(const char *, char *)
238 		__attribute__((__bounded__(__minbytes__, 2, 1024)));
239 char	*setstate(const char *);
240 void	 srandom(unsigned int);
241 void	 srandomdev(void);
242 
243 int	 putenv(const char *);
244 int	 setenv(const char *, const char *, int);
245 int	 unsetenv(const char *);
246 void	 setproctitle(const char *, ...)
247 	__attribute__((__format__(__printf__, 1, 2)));
248 
249 quad_t	 qabs(quad_t);
250 qdiv_t	 qdiv(quad_t, quad_t);
251 quad_t	 strtoq(const char *, char **, int);
252 u_quad_t strtouq(const char *, char **, int);
253 
254 double	 drand48(void);
255 double	 erand48(unsigned short *);
256 long	 jrand48(unsigned short *);
257 void	 lcong48(unsigned short *);
258 long	 lrand48(void);
259 long	 mrand48(void);
260 long	 nrand48(unsigned short *);
261 unsigned short *seed48(unsigned short *);
262 void	 srand48(long);
263 
264 /* starting with MirOS 0AAD define the entire arc4random API as cpp macros */
265 
266 /* core API */
267 #undef arc4random
268 #undef arc4random_stir
269 /* core API, deprecated in favour of arc4random_pushb_fast */
270 #undef arc4random_addrandom
271 /* useful OpenBSD extension */
272 #undef arc4random_buf
273 #undef arc4random_uniform
274 /* current MirBSD extension */
275 #undef arc4random_pushb_fast
276 /* deprecated MirBSD extension */
277 #undef arc4random_push
278 #undef arc4random_pushb
279 #undef arc4random_pushk
280 
281 u_int32_t arc4random(void);
282 void	arc4random_stir(void);
283 void arc4random_buf(void *, size_t)
284 	__attribute__((__bounded__(__string__, 1, 2)));
285 u_int32_t arc4random_uniform(u_int32_t);
286 void arc4random_pushb_fast(const void *, size_t)
287 	__attribute__((__bounded__(__string__, 1, 2)));
288 
289 #define arc4random		arc4random
290 #define arc4random_stir		arc4random_stir
291 #define arc4random_buf		arc4random_buf
292 #define arc4random_uniform	arc4random_uniform
293 #define arc4random_pushb_fast	arc4random_pushb_fast
294 
295 void	arc4random_addrandom(unsigned char *, int)
296 	__attribute__((__bounded__(__string__, 1, 2)));
297 #define arc4random_addrandom	arc4random_addrandom
298 void	arc4random_push(int);
299 #define arc4random_push(n)	do {			\
300 	int arc4random_push_n = (n);			\
301 	arc4random_pushb_fast(&arc4random_push_n,	\
302 	    sizeof(arc4random_push_n));			\
303 } while (/* CONSTCOND */ 0)
304 uint32_t arc4random_pushb(const void *, size_t)
305 	__attribute__((__bounded__(__string__, 1, 2)));
306 #define arc4random_pushb(buf,n)	\
307 	(arc4random_pushb_fast((buf),(n)), arc4random())
308 #define arc4random_pushk(buf,n)	\
309 	(arc4random_pushb_fast((buf),(n)), arc4random())
310 
311 
312 void	setprogname(const char *);
313 const char *getprogname(void);
314 #endif /* !_ANSI_SOURCE && !_POSIX_SOURCE */
315 
316 int	mblen(const char *, size_t);
317 size_t	mbstowcs(wchar_t *, const char *, size_t);
318 int	mbtowc(wchar_t *, const char *, size_t);
319 size_t	wcstombs(char *, const wchar_t *, size_t);
320 int	wctomb(char *, const wchar_t);
321 __END_DECLS
322 
323 #if defined(_SVID_SOURCE) && !defined(__STRICT_ANSI__)
324 __BEGIN_DECLS
325 int rpmatch(const char *);
326 __END_DECLS
327 #endif
328 
329 #endif /* _STDLIB_H_ */
330