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 <sys/cdefs.h>
44 #include <machine/ieeefp.h>
45 #define __need_size_t
46 #define __need_wchar_t
47 #define __need_NULL
48 #include <stddef.h>
49 
50 #include <machine/stdlib.h>
51 #ifndef __STRICT_ANSI__
52 #include <alloca.h>
53 #endif
54 
55 
56 #if __GNU_VISIBLE
57 #include <sys/_locale.h>
58 #endif
59 
60 _BEGIN_STD_C
61 
62 #include <sys/_wait.h>
63 
64 typedef struct
65 {
66   int quot; /* quotient */
67   int rem; /* remainder */
68 } div_t;
69 
70 typedef struct
71 {
72   long quot; /* quotient */
73   long rem; /* remainder */
74 } ldiv_t;
75 
76 #if __ISO_C_VISIBLE >= 1999
77 typedef struct
78 {
79   long long int quot; /* quotient */
80   long long int rem; /* remainder */
81 } lldiv_t;
82 #endif
83 
84 #ifndef __compar_fn_t_defined
85 #define __compar_fn_t_defined
86 typedef int (*__compar_fn_t) (const void *, const void *);
87 #endif
88 
89 #define EXIT_FAILURE 1
90 #define EXIT_SUCCESS 0
91 
92 #define RAND_MAX __RAND_MAX
93 
94 size_t	__locale_mb_cur_max (void);
95 
96 #define MB_CUR_MAX __locale_mb_cur_max()
97 
98 /* Declare free up here so it can be used with __malloc_like */
99 void	free (void *) _NOTHROW;
100 
101 #if __ISO_C_VISIBLE >= 1999
102 _Noreturn void	_Exit (int __status);
103 #endif
104 #if __SVID_VISIBLE || __XSI_VISIBLE >= 4
105 long    a64l (const char *__input);
106 #endif
107 _Noreturn void	abort (void);
108 int	abs (int);
109 #if __ISO_C_VISIBLE >= 2011
110 void   *aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
111                          __alloc_size(2) __result_use_check _NOTHROW;
112 #endif
113 #if __BSD_VISIBLE
114 __uint32_t arc4random (void);
115 __uint32_t arc4random_uniform (__uint32_t);
116 void    arc4random_buf (void *, size_t);
117 #endif
118 #if __ISO_C_VISIBLE >= 2011
119 int	at_quick_exit(void (*)(void));
120 #endif
121 int	atexit (void (*__func)(void));
122 double	atof (const char *__nptr);
123 #if __MISC_VISIBLE
124 float	atoff (const char *__nptr);
125 #endif
126 int	atoi (const char *__nptr);
127 long	atol (const char *__nptr);
128 #if __ISO_C_VISIBLE >= 1999
129 long long atoll (const char *__nptr);
130 #endif
131 void *	bsearch (const void *__key,
132                  const void *__base,
133                  size_t __nmemb,
134                  size_t __size,
135                  __compar_fn_t _compar);
136 void	*calloc(size_t, size_t) __malloc_like __result_use_check
137 	     __alloc_size2(1, 2) _NOTHROW;
138 div_t	div (int __numer, int __denom);
139 #if __SVID_VISIBLE || __XSI_VISIBLE
140 struct _rand48;
141 double drand48 (void);
142 double _drand48_r (struct _rand48 *);
143 double erand48 (unsigned short [3]);
144 double _erand48_r (struct _rand48 *, unsigned short [3]);
145 #endif
146 _Noreturn void	exit (int __status);
147 void	free (void *) _NOTHROW;
148 char *  getenv (const char *__string);
149 #if __GNU_VISIBLE
150 char *  secure_getenv (const char *__string);
151 #endif
152 #if __MISC_VISIBLE
153 char *	_findenv (const char *, int *);
154 #endif
155 #if __POSIX_VISIBLE >= 200809
156 extern char *suboptarg;			/* getsubopt(3) external variable */
157 int	getsubopt (char **, char * const *, char **);
158 #endif
159 #if __XSI_VISIBLE >= 500
160 int grantpt(int fd);
161 #endif
162 #if __SVID_VISIBLE || __XSI_VISIBLE >= 4 || __BSD_VISIBLE
163 char *	initstate (unsigned, char *, size_t);
164 #endif
165 #if __SVID_VISIBLE || __XSI_VISIBLE
166 long   jrand48 (unsigned short [3]);
167 long   _jrand48_r (struct _rand48 *, unsigned short [3]);
168 #endif
169 long	labs (long);
170 #if __SVID_VISIBLE || __XSI_VISIBLE
171 void  lcong48 (unsigned short [7]);
172 void  _lcong48_r (struct _rand48 *, unsigned short [7]);
173 #endif
174 ldiv_t	ldiv (long __numer, long __denom);
175 #if __ISO_C_VISIBLE >= 1999
176 long long llabs (long long);
177 lldiv_t	lldiv (long long __numer, long long __denom);
178 #endif
179 #if __SVID_VISIBLE || __XSI_VISIBLE
180 long   lrand48 (void);
181 long   _lrand48_r (struct _rand48 *);
182 #endif
183 void	*malloc(size_t) __malloc_like __result_use_check __alloc_size(1) _NOTHROW;
184 int	mblen (const char *, size_t);
185 size_t	mbstowcs (wchar_t *__restrict, const char *__restrict, size_t);
186 int	mbtowc (wchar_t *__restrict, const char *__restrict, size_t);
187 #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200809
188 char *	mkdtemp (char *);
189 #endif
190 #if __GNU_VISIBLE
191 int	mkostemp (char *, int);
192 int	mkostemps (char *, int, int);
193 #endif
194 #if __MISC_VISIBLE || __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE >= 4
195 int	mkstemp (char *);
196 #endif
197 #if __MISC_VISIBLE
198 int	mkstemps (char *, int);
199 #endif
200 #if __BSD_VISIBLE || (__XSI_VISIBLE >= 4 && __POSIX_VISIBLE < 200112)
201 char *	mktemp (char *) __picolibc_deprecated("the use of `mktemp' is dangerous; use `mkstemp' instead");
202 #endif
203 #if __SVID_VISIBLE || __XSI_VISIBLE
204 long   mrand48 (void);
205 long   _mrand48_r (struct _rand48 *);
206 long   nrand48 (unsigned short [3]);
207 long   _nrand48_r (struct _rand48 *, unsigned short [3]);
208 #endif
209 #if __POSIX_VISIBLE >= 200112
210 int	posix_memalign (void **, size_t, size_t) __nonnull((1))
211 	    __result_use_check;
212 #endif
213 #if __SVID_VISIBLE || __XSI_VISIBLE
214 int	putenv (char *__string);
215 #endif
216 #if __ISO_C_VISIBLE >= 2011
217 _Noreturn void
218 	quick_exit(int);
219 #endif /* __ISO_C_VISIBLE >= 2011 */
220 void	qsort (void *__base, size_t __nmemb, size_t __size, __compar_fn_t _compar);
221 /* There are two common qsort_r variants.  If you request
222    _BSD_SOURCE, you get the BSD version; otherwise you get the GNU
223    version.  We want that #undef qsort_r will still let you
224    invoke the underlying function, but that requires gcc support. */
225 #if __GNU_VISIBLE
226 void	qsort_r (void *__base, size_t __nmemb, size_t __size, int (*_compar)(const void *, const void *, void *), void *__thunk);
227 #elif __BSD_VISIBLE
228 # ifdef __GNUC__
229 void	qsort_r (void *__base, size_t __nmemb, size_t __size, void *__thunk, int (*_compar)(void *, const void *, const void *))
230              __asm__ (__ASMNAME ("__bsd_qsort_r"));
231 # else
232 void	__bsd_qsort_r (void *__base, size_t __nmemb, size_t __size, void *__thunk, int (*_compar)(void *, const void *, const void *));
233 #  define qsort_r __bsd_qsort_r
234 # endif
235 #endif
236 int	rand (void);
237 #if __POSIX_VISIBLE
238 int	rand_r (unsigned *__seed);
239 #endif
240 #if __SVID_VISIBLE || __XSI_VISIBLE >= 4 || __BSD_VISIBLE
241 long	random (void);
242 #endif
243 void	*realloc(void *, size_t) __result_use_check __alloc_size(2) _NOTHROW;
244 #if __BSD_VISIBLE
245 void	*reallocarray(void *, size_t, size_t) __result_use_check __alloc_size2(2, 3);
246 void	*reallocf(void *, size_t) __result_use_check __alloc_size(2);
247 #endif
248 #if __BSD_VISIBLE || __XSI_VISIBLE >= 4
249 char *	realpath (const char *__restrict path, char *__restrict resolved_path);
250 #endif
251 #if __BSD_VISIBLE
252 int	rpmatch (const char *response);
253 #endif
254 #if __SVID_VISIBLE || __XSI_VISIBLE
255 unsigned short *
256        seed48 (unsigned short [3]);
257 unsigned short *
258        _seed48_r (struct _rand48 *, unsigned short [3]);
259 #endif /* __SVID_VISIBLE || __XSI_VISIBLE */
260 #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112
261 int	setenv (const char *__string, const char *__value, int __overwrite);
262 #endif
263 #if __XSI_VISIBLE
264 void	setkey (const char *__key);
265 #endif
266 #if __SVID_VISIBLE || __XSI_VISIBLE >= 4 || __BSD_VISIBLE
267 char *	setstate (char *);
268 #endif
269 void	srand (unsigned __seed);
270 #if __SVID_VISIBLE || __XSI_VISIBLE
271 void    srand48 (long);
272 void    _srand48_r (struct _rand48 *, long);
273 #endif /* __SVID_VISIBLE || __XSI_VISIBLE */
274 #if __SVID_VISIBLE || __XSI_VISIBLE >= 4 || __BSD_VISIBLE
275 void	srandom (unsigned);
276 #endif
277 double	strtod (const char *__restrict __n, char **__restrict __end_PTR);
278 #ifdef TINY_STDIO
279 int     strfromd(char *__restrict str, size_t n,
280                  const char *__restrict format, double fp);
281 #endif
282 #if __ISO_C_VISIBLE >= 1999
283 float	strtof (const char *__restrict __n, char **__restrict __end_PTR);
284 #ifdef _HAVE_LONG_DOUBLE
285 long double
286         strtold (const char *__restrict __n, char **__restrict __end_PTR);
287 #endif
288 #ifdef TINY_STDIO
289 int     strfromf(char *__restrict str, size_t n,
290                  const char *__restrict format, float fp);
291 #ifdef _HAVE_LONG_DOUBLE
292 int     strfroml (char *__restrict str, size_t n,
293                   const char *__restrict format, long double fp);
294 #endif
295 #endif
296 #endif
297 #if __MISC_VISIBLE
298 /* the following strtodf interface is deprecated...use strtof instead */
299 # ifndef strtodf
300 #  define strtodf strtof
301 # endif
302 #endif
303 long	strtol (const char *__restrict __n, char **__restrict __end_PTR, int __base);
304 #ifdef _HAVE_LONG_DOUBLE
305 #if __ISO_C_VISIBLE >= 1999
306 extern long double
307         strtold (const char *__restrict, char **__restrict);
308 #endif
309 #endif /* _HAVE_LONG_DOUBLE */
310 #if __ISO_C_VISIBLE >= 1999
311 long long
312         strtoll (const char *__restrict __n, char **__restrict __end_PTR, int __base);
313 #endif
314 unsigned long
315         strtoul (const char *__restrict __n, char **__restrict __end_PTR, int __base);
316 #if __ISO_C_VISIBLE >= 1999
317 unsigned long long
318         strtoull (const char *__restrict __n, char **__restrict __end_PTR, int __base);
319 #endif
320 
321 #if __GNU_VISIBLE
322 double	strtod_l (const char *__restrict, char **__restrict, locale_t);
323 float	strtof_l (const char *__restrict, char **__restrict, locale_t);
324 #ifdef _HAVE_LONG_DOUBLE
325 extern long double
326         strtold_l (const char *__restrict, char **__restrict,
327                    locale_t);
328 #endif /* _HAVE_LONG_DOUBLE */
329 long	strtol_l (const char *__restrict, char **__restrict, int, locale_t);
330 unsigned long
331         strtoul_l (const char *__restrict, char **__restrict, int,
332 			 locale_t __loc);
333 long long
334         strtoll_l (const char *__restrict, char **__restrict, int, locale_t);
335 unsigned long long
336         strtoull_l (const char *__restrict, char **__restrict, int,
337                     locale_t __loc);
338 #endif
339 int	system (const char *__string);
340 #if __BSD_VISIBLE || __POSIX_VISIBLE >= 200112
341 int	unsetenv (const char *__string);
342 #endif
343 size_t	wcstombs (char *__restrict, const wchar_t *__restrict, size_t);
344 int	wctomb (char *, wchar_t);
345 void   *valloc (size_t) __malloc_like __result_use_check __alloc_size(1) _NOTHROW;
346 #if __SVID_VISIBLE || __XSI_VISIBLE >= 4
347 char   *l64a (long __input);
348 #endif
349 #if __MISC_VISIBLE
350 int	on_exit (void (*__func)(int, void *),void *__arg);
351 #endif
352 
353 /* XSI Legacy option group */
354 #if __XSI_VISIBLE >= 4
355 char   *ecvt (double,int,int *,int *);
356 #ifdef TINY_STDIO
357 int	ecvt_r (double,int,int *,int *, char*, size_t);
358 #else
359 char   *ecvtbuf (double, int, int*, int*, char *);
360 #endif
361 
362 char   *ecvtf (float,int,int *,int *);
363 #ifdef TINY_STDIO
364 int	ecvtf_r (float,int,int *,int *, char*, size_t);
365 #else
366 char   *ecvtfbuf (float, int, int*, int*, char *);
367 #endif
368 
369 char   *fcvt (double,int,int *,int *);
370 #ifdef TINY_STDIO
371 int	fcvt_r (double, int, int*, int*, char *, size_t);
372 #else
373 char   *fcvtbuf (double, int, int*, int*, char *);
374 #endif
375 
376 char   *fcvtf (float,int,int *,int *);
377 #ifdef TINY_STDIO
378 int	fcvtf_r (float, int, int*, int*, char *, size_t);
379 #else
380 char   *fcvtfbuf (float, int, int*, int*, char *);
381 #endif
382 
383 #if defined(_HAVE_LONG_DOUBLE) && defined(TINY_STDIO)
384 char   *ecvtl(long double, int, int*, int*);
385 int     ecvtl_r(long double, int, int*, int*, char *, size_t);
386 char   *fcvtl(long double, int, int*, int*);
387 int     fcvtl_r(long double, int, int*, int*, char *, size_t);
388 #endif
389 
390 char   *gcvt (double,int,char *);
391 char   *gcvtf (float,int,char *);
392 
393 #if defined(_HAVE_LONG_DOUBLE) && defined(TINY_STDIO)
394 char   *gcvtl(long double, int, char *);
395 #endif
396 
397 #endif /* XSI_VISIBLE >= 4 */
398 
399 /* Random newlib APIs */
400 # if __MISC_VISIBLE
401 char   *__itoa (int, char *, int);
402 char   *__utoa (unsigned, char *, int);
403 char   *itoa (int, char *, int);
404 char   *utoa (unsigned, char *, int);
405 void	cfree (void *);
406 char   *__dtoa (double, int, int, int *, int*, char**);
407 char   *__ldtoa (long double, int, int, int *, int *, char **);
408 void	__eprintf (const char *, const char *, unsigned int, const char *);
409 #endif
410 
411 #if __STDC_WANT_LIB_EXT1__ == 1
412 #include <sys/_types.h>
413 
414 #ifndef _ERRNO_T_DEFINED
415 typedef __errno_t errno_t;
416 #define _ERRNO_T_DEFINED
417 #endif
418 
419 #ifndef _RSIZE_T_DEFINED
420 typedef __rsize_t rsize_t;
421 #define _RSIZE_T_DEFINED
422 #endif
423 
424 typedef void (*constraint_handler_t)(const char *restrict msg,
425                                      void *restrict ptr, __errno_t error);
426 
427 constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
428 void abort_handler_s(const char *restrict msg, void *restrict ptr,
429                      __errno_t error);
430 void ignore_handler_s(const char *restrict msg, void *restrict ptr,
431                      __errno_t error);
432 #endif
433 
434 _END_STD_C
435 
436 #if __SSP_FORTIFY_LEVEL > 0
437 #include <ssp/stdlib.h>
438 #endif
439 
440 #endif /* _STDLIB_H_ */
441