1 /*
2 Copyright (c) 1991, 1993
3 The Regents of the University of California.  All rights reserved.
4 c) UNIX System Laboratories, Inc.
5 All or some portions of this file are derived from material licensed
6 to the University of California by American Telephone and Telegraph
7 Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 the permission of UNIX System Laboratories, Inc.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13 1. Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15 2. Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
18 3. Neither the name of the University nor the names of its contributors
19 may be used to endorse or promote products derived from this software
20 without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 SUCH DAMAGE.
33  */
34 /*
35  * stdlib.h
36  *
37  * Definitions for common types, variables, and functions.
38  */
39 
40 #ifndef _STDLIB_H_
41 #define _STDLIB_H_
42 
43 #include <machine/ieeefp.h>
44 #include "_ansi.h"
45 
46 #define __need_size_t
47 #define __need_wchar_t
48 #define __need_NULL
49 #include <stddef.h>
50 
51 #include <sys/cdefs.h>
52 #include <machine/stdlib.h>
53 #ifndef __STRICT_ANSI__
54 #include <alloca.h>
55 #endif
56 
57 #ifdef __CYGWIN__
58 #include <cygwin/stdlib.h>
59 #endif
60 
61 #if __GNU_VISIBLE
62 #include <sys/_locale.h>
63 #endif
64 
65 _BEGIN_STD_C
66 
67 typedef struct
68 {
69   int quot; /* quotient */
70   int rem; /* remainder */
71 } div_t;
72 
73 typedef struct
74 {
75   long quot; /* quotient */
76   long rem; /* remainder */
77 } ldiv_t;
78 
79 #if __ISO_C_VISIBLE >= 1999
80 typedef struct
81 {
82   long long int quot; /* quotient */
83   long long int rem; /* remainder */
84 } lldiv_t;
85 #endif
86 
87 #ifndef __compar_fn_t_defined
88 #define __compar_fn_t_defined
89 typedef int (*__compar_fn_t) (const void *, const void *);
90 #endif
91 
92 #define EXIT_FAILURE 1
93 #define EXIT_SUCCESS 0
94 
95 #define RAND_MAX __RAND_MAX
96 
97 size_t	__locale_mb_cur_max (void);
98 
99 #define MB_CUR_MAX __locale_mb_cur_max()
100 
101 void	abort (void) _ATTRIBUTE ((__noreturn__));
102 int	abs (int);
103 #if __BSD_VISIBLE
104 __uint32_t arc4random (void);
105 __uint32_t arc4random_uniform (__uint32_t);
106 void    arc4random_buf (void *, size_t);
107 #endif
108 #define ATEXIT_MAX	32
109 int	atexit (void (*__func)(void));
110 double	atof (const char *__nptr);
111 #if __MISC_VISIBLE
112 float	atoff (const char *__nptr);
113 #endif
114 int	atoi (const char *__nptr);
115 long	atol (const char *__nptr);
116 void *	bsearch (const void *__key,
117 		       const void *__base,
118 		       size_t __nmemb,
119 		       size_t __size,
120 		       __compar_fn_t _compar);
121 void	free (void *) _NOTHROW;
122 void	*calloc(size_t, size_t) __malloc_like __result_use_check
123 	     __alloc_size2(1, 2) _NOTHROW;
124 div_t	div (int __numer, int __denom);
125 void	exit (int __status) _ATTRIBUTE ((__noreturn__));
126 char *  getenv (const char *__string);
127 #if __GNU_VISIBLE
128 char *  secure_getenv (const char *__string);
129 #endif
130 char *	_findenv (const char *, int *);
131 #if __POSIX_VISIBLE >= 200809
132 extern char *suboptarg;			/* getsubopt(3) external variable */
133 int	getsubopt (char **, char * const *, char **);
134 #endif
135 long	labs (long);
136 ldiv_t	ldiv (long __numer, long __denom);
137 void	*malloc(size_t) __malloc_like __result_use_check __alloc_size(1) _NOTHROW;
138 void    *valloc (size_t) __malloc_like __result_use_check __alloc_size(1) _NOTHROW;
139 int	mblen (const char *, size_t);
140 int	mbtowc (wchar_t *__restrict, const char *__restrict, size_t);
141 int	wctomb (char *, wchar_t);
142 size_t	mbstowcs (wchar_t *__restrict, const char *__restrict, size_t);
143 size_t	wcstombs (char *__restrict, const wchar_t *__restrict, size_t);
144 #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
145 char *	mkdtemp (char *);
146 #endif
147 #if __GNU_VISIBLE
148 int	mkostemp (char *, int);
149 int	mkostemps (char *, int, int);
150 #endif
151 #if __MISC_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE >= 4
152 int	mkstemp (char *);
153 #endif
154 #if __MISC_VISIBLE
155 int	mkstemps (char *, int);
156 #endif
157 #if __BSD_VISIBLE || (__XSI_VISIBLE >= 4 && __POSIX_VISIBLE < 200112)
158 char *	mktemp (char *) __picolibc_deprecated("the use of `mktemp' is dangerous; use `mkstemp' instead");
159 #endif
160 void	qsort (void *__base, size_t __nmemb, size_t __size, __compar_fn_t _compar);
161 int	rand (void);
162 void	*realloc(void *, size_t) __result_use_check __alloc_size(2) _NOTHROW;
163 #if __BSD_VISIBLE
164 void	*reallocarray(void *, size_t, size_t) __result_use_check __alloc_size2(2, 3);
165 void	*reallocf(void *, size_t) __result_use_check __alloc_size(2);
166 #endif
167 #if __BSD_VISIBLE || __XSI_VISIBLE >= 4
168 char *	realpath (const char *__restrict path, char *__restrict resolved_path);
169 #endif
170 #if __BSD_VISIBLE
171 int	rpmatch (const char *response);
172 #endif
173 #if __XSI_VISIBLE
174 void	setkey (const char *__key);
175 #endif
176 void	srand (unsigned __seed);
177 double	strtod (const char *__restrict __n, char **__restrict __end_PTR);
178 #ifdef TINY_STDIO
179 int strfromd(char *__restrict str, size_t n,
180 	     const char *__restrict format, double fp);
181 #endif
182 #if __ISO_C_VISIBLE >= 1999
183 float	strtof (const char *__restrict __n, char **__restrict __end_PTR);
184 #ifdef _HAVE_LONG_DOUBLE
185 long double strtold (const char *__restrict __n, char **__restrict __end_PTR);
186 #endif
187 #ifdef TINY_STDIO
188 int strfromf(char *__restrict str, size_t n,
189 	     const char *__restrict format, float fp);
190 #ifdef _HAVE_LONG_DOUBLE
191 int strfroml (char *__restrict str, size_t n,
192                const char *__restrict format, long double fp);
193 #endif
194 #endif
195 #endif
196 #if __MISC_VISIBLE
197 /* the following strtodf interface is deprecated...use strtof instead */
198 # ifndef strtodf
199 #  define strtodf strtof
200 # endif
201 #endif
202 long	strtol (const char *__restrict __n, char **__restrict __end_PTR, int __base);
203 unsigned long strtoul (const char *__restrict __n, char **__restrict __end_PTR, int __base);
204 
205 #if __GNU_VISIBLE
206 double	strtod_l (const char *__restrict, char **__restrict, locale_t);
207 float	strtof_l (const char *__restrict, char **__restrict, locale_t);
208 #ifdef _HAVE_LONG_DOUBLE
209 extern long double strtold_l (const char *__restrict, char **__restrict,
210 			      locale_t);
211 #endif /* _HAVE_LONG_DOUBLE */
212 long	strtol_l (const char *__restrict, char **__restrict, int, locale_t);
213 unsigned long strtoul_l (const char *__restrict, char **__restrict, int,
214 			 locale_t __loc);
215 long long strtoll_l (const char *__restrict, char **__restrict, int, locale_t);
216 unsigned long long strtoull_l (const char *__restrict, char **__restrict, int,
217 			       locale_t __loc);
218 #endif
219 
220 int	system (const char *__string);
221 
222 #if __SVID_VISIBLE || __XSI_VISIBLE >= 4
223 long    a64l (const char *__input);
224 char *  l64a (long __input);
225 #endif
226 #if __MISC_VISIBLE
227 int	on_exit (void (*__func)(int, void *),void *__arg);
228 #endif
229 #if __ISO_C_VISIBLE >= 1999
230 void	_Exit (int __status) _ATTRIBUTE ((__noreturn__));
231 #endif
232 #if __SVID_VISIBLE || __XSI_VISIBLE
233 int	putenv (char *__string);
234 #endif
235 #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112
236 int	setenv (const char *__string, const char *__value, int __overwrite);
237 #endif
238 
239 #if __XSI_VISIBLE >= 4
240 char *	gcvt (double,int,char *);
241 char *	gcvtf (float,int,char *);
242 
243 char *	ecvt (double,int,int *,int *);
244 #ifdef TINY_STDIO
245 int	ecvt_r (double,int,int *,int *, char*, size_t);
246 #else
247 char *	ecvtbuf (double, int, int*, int*, char *);
248 #endif
249 
250 char *	ecvtf (float,int,int *,int *);
251 #ifdef TINY_STDIO
252 int	ecvtf_r (float,int,int *,int *, char*, size_t);
253 #else
254 char *	ecvtfbuf (float, int, int*, int*, char *);
255 #endif
256 
257 char *	fcvt (double,int,int *,int *);
258 #ifdef TINY_STDIO
259 int	fcvt_r (double, int, int*, int*, char *, size_t);
260 #else
261 char *	fcvtbuf (double, int, int*, int*, char *);
262 #endif
263 
264 char *	fcvtf (float,int,int *,int *);
265 #ifdef TINY_STDIO
266 int	fcvtf_r (float, int, int*, int*, char *, size_t);
267 #else
268 char *	fcvtfbuf (float, int, int*, int*, char *);
269 #endif
270 
271 #if defined(_HAVE_LONG_DOUBLE) && defined(TINY_STDIO)
272 char *  ecvtl(long double, int, int*, int*);
273 int     ecvtl_r(long double, int, int*, int*, char *, size_t);
274 char *  fcvtl(long double, int, int*, int*);
275 int     fcvtl_r(long double, int, int*, int*, char *, size_t);
276 char *	gcvtl(long double, int, char *);
277 #endif
278 
279 #endif
280 char *	__itoa (int, char *, int);
281 char *	__utoa (unsigned, char *, int);
282 #if __MISC_VISIBLE
283 char *	itoa (int, char *, int);
284 char *	utoa (unsigned, char *, int);
285 #endif
286 #if __POSIX_VISIBLE
287 int	rand_r (unsigned *__seed);
288 #endif
289 
290 #if __SVID_VISIBLE || __XSI_VISIBLE
291 struct _rand48;
292 double drand48 (void);
293 double _drand48_r (struct _rand48 *);
294 double erand48 (unsigned short [3]);
295 double _erand48_r (struct _rand48 *, unsigned short [3]);
296 long   jrand48 (unsigned short [3]);
297 long   _jrand48_r (struct _rand48 *, unsigned short [3]);
298 void  lcong48 (unsigned short [7]);
299 void  _lcong48_r (struct _rand48 *, unsigned short [7]);
300 long   lrand48 (void);
301 long   _lrand48_r (struct _rand48 *);
302 long   mrand48 (void);
303 long   _mrand48_r (struct _rand48 *);
304 long   nrand48 (unsigned short [3]);
305 long   _nrand48_r (struct _rand48 *, unsigned short [3]);
306 unsigned short *
307        seed48 (unsigned short [3]);
308 unsigned short *
309        _seed48_r (struct _rand48 *, unsigned short [3]);
310 void  srand48 (long);
311 void  _srand48_r (struct _rand48 *, long);
312 #endif /* __SVID_VISIBLE || __XSI_VISIBLE */
313 #if __SVID_VISIBLE || __XSI_VISIBLE >= 4 || __BSD_VISIBLE
314 char *	initstate (unsigned, char *, size_t);
315 long	random (void);
316 char *	setstate (char *);
317 void	srandom (unsigned);
318 #endif
319 #if __ISO_C_VISIBLE >= 1999
320 long long atoll (const char *__nptr);
321 #endif
322 #if __ISO_C_VISIBLE >= 1999
323 long long llabs (long long);
324 lldiv_t	lldiv (long long __numer, long long __denom);
325 long long strtoll (const char *__restrict __n, char **__restrict __end_PTR, int __base);
326 #endif
327 #if __ISO_C_VISIBLE >= 1999
328 unsigned long long strtoull (const char *__restrict __n, char **__restrict __end_PTR, int __base);
329 #endif
330 
331 #ifndef __CYGWIN__
332 #if __MISC_VISIBLE
333 void	cfree (void *);
334 #endif
335 #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112
336 int	unsetenv (const char *__string);
337 #endif
338 #endif /* !__CYGWIN__ */
339 
340 #if __POSIX_VISIBLE >= 200112
341 int	posix_memalign (void **, size_t, size_t) __nonnull((1))
342 	    __result_use_check;
343 #endif
344 
345 char *	__dtoa (double, int, int, int *, int*, char**);
346 char *  __ldtoa (long double, int, int, int *, int *, char **);
347 
348 void	__eprintf (const char *, const char *, unsigned int, const char *);
349 
350 /* There are two common qsort_r variants.  If you request
351    _BSD_SOURCE, you get the BSD version; otherwise you get the GNU
352    version.  We want that #undef qsort_r will still let you
353    invoke the underlying function, but that requires gcc support. */
354 #if __GNU_VISIBLE
355 void	qsort_r (void *__base, size_t __nmemb, size_t __size, int (*_compar)(const void *, const void *, void *), void *__thunk);
356 #elif __BSD_VISIBLE
357 # ifdef __GNUC__
358 void	qsort_r (void *__base, size_t __nmemb, size_t __size, void *__thunk, int (*_compar)(void *, const void *, const void *))
359              __asm__ (__ASMNAME ("__bsd_qsort_r"));
360 # else
361 void	__bsd_qsort_r (void *__base, size_t __nmemb, size_t __size, void *__thunk, int (*_compar)(void *, const void *, const void *));
362 #  define qsort_r __bsd_qsort_r
363 # endif
364 #endif
365 
366 /* On platforms where long double equals double.  */
367 #ifdef _HAVE_LONG_DOUBLE
368 #if __ISO_C_VISIBLE >= 1999
369 extern long double strtold (const char *__restrict, char **__restrict);
370 #endif
371 #endif /* _HAVE_LONG_DOUBLE */
372 
373 /*
374  * If we're in a mode greater than C99, expose C11 functions.
375  */
376 #if __ISO_C_VISIBLE >= 2011
377 void *	aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
378 	    __alloc_size(2) __result_use_check _NOTHROW;
379 int	at_quick_exit(void (*)(void));
380 _Noreturn void
381 	quick_exit(int);
382 #endif /* __ISO_C_VISIBLE >= 2011 */
383 
384 _END_STD_C
385 
386 #if __SSP_FORTIFY_LEVEL > 0
387 #include <ssp/stdlib.h>
388 #endif
389 
390 #endif /* _STDLIB_H_ */
391