1/*-
2 * Copyright (c) 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
30 * $FreeBSD: stable/11/include/stdlib.h 367512 2020-11-09 01:56:06Z delphij $
31 */
32
33#ifndef _STDLIB_H_
34#define	_STDLIB_H_
35
36#include <sys/cdefs.h>
37#include <sys/_null.h>
38#include <sys/_types.h>
39
40__NULLABILITY_PRAGMA_PUSH
41
42#if __BSD_VISIBLE
43#ifndef _RUNE_T_DECLARED
44typedef	__rune_t	rune_t;
45#define	_RUNE_T_DECLARED
46#endif
47#endif
48
49#ifndef _SIZE_T_DECLARED
50typedef	__size_t	size_t;
51#define	_SIZE_T_DECLARED
52#endif
53
54#ifndef	__cplusplus
55#ifndef _WCHAR_T_DECLARED
56typedef	___wchar_t	wchar_t;
57#define	_WCHAR_T_DECLARED
58#endif
59#endif
60
61typedef struct {
62	int	quot;		/* quotient */
63	int	rem;		/* remainder */
64} div_t;
65
66typedef struct {
67	long	quot;
68	long	rem;
69} ldiv_t;
70
71#define	EXIT_FAILURE	1
72#define	EXIT_SUCCESS	0
73
74#define	RAND_MAX	0x7ffffffd
75
76__BEGIN_DECLS
77#ifdef _XLOCALE_H_
78#include <xlocale/_stdlib.h>
79#endif
80extern int __mb_cur_max;
81extern int ___mb_cur_max(void);
82#define	MB_CUR_MAX	(___mb_cur_max())
83
84_Noreturn void	 abort(void);
85int	 abs(int) __pure2;
86int	 atexit(void (* _Nonnull)(void));
87double	 atof(const char *);
88int	 atoi(const char *);
89long	 atol(const char *);
90void	*bsearch(const void *, const void *, size_t,
91	    size_t, int (*)(const void * _Nonnull, const void *));
92void	*calloc(size_t, size_t) __malloc_like __result_use_check
93	     __alloc_size2(1, 2);
94div_t	 div(int, int) __pure2;
95_Noreturn void	 exit(int);
96void	 free(void *);
97char	*getenv(const char *);
98long	 labs(long) __pure2;
99ldiv_t	 ldiv(long, long) __pure2;
100void	*malloc(size_t) __malloc_like __result_use_check __alloc_size(1);
101int	 mblen(const char *, size_t);
102size_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
103int	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
104void	 qsort(void *, size_t, size_t,
105	    int (* _Nonnull)(const void *, const void *));
106int	 rand(void);
107void	*realloc(void *, size_t) __result_use_check __alloc_size(2);
108void	 srand(unsigned);
109double	 strtod(const char * __restrict, char ** __restrict);
110float	 strtof(const char * __restrict, char ** __restrict);
111long	 strtol(const char * __restrict, char ** __restrict, int);
112long double
113	 strtold(const char * __restrict, char ** __restrict);
114unsigned long
115	 strtoul(const char * __restrict, char ** __restrict, int);
116int	 system(const char *);
117int	 wctomb(char *, wchar_t);
118size_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
119
120/*
121 * Functions added in C99 which we make conditionally available in the
122 * BSD^C89 namespace if the compiler supports `long long'.
123 * The #if test is more complicated than it ought to be because
124 * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
125 * is not supported in the compilation environment (which therefore means
126 * that it can't really be ISO C99).
127 *
128 * (The only other extension made by C99 in thie header is _Exit().)
129 */
130#if __ISO_C_VISIBLE >= 1999 || defined(__cplusplus)
131#ifdef __LONG_LONG_SUPPORTED
132/* LONGLONG */
133typedef struct {
134	long long quot;
135	long long rem;
136} lldiv_t;
137
138/* LONGLONG */
139long long
140	 atoll(const char *);
141/* LONGLONG */
142long long
143	 llabs(long long) __pure2;
144/* LONGLONG */
145lldiv_t	 lldiv(long long, long long) __pure2;
146/* LONGLONG */
147long long
148	 strtoll(const char * __restrict, char ** __restrict, int);
149/* LONGLONG */
150unsigned long long
151	 strtoull(const char * __restrict, char ** __restrict, int);
152#endif /* __LONG_LONG_SUPPORTED */
153
154_Noreturn void	 _Exit(int);
155#endif /* __ISO_C_VISIBLE >= 1999 */
156
157/*
158 * If we're in a mode greater than C99, expose C11 functions.
159 */
160#if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L
161void *	aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
162	    __alloc_size(2);
163int	at_quick_exit(void (*)(void));
164_Noreturn void
165	quick_exit(int);
166#endif /* __ISO_C_VISIBLE >= 2011 */
167/*
168 * Extensions made by POSIX relative to C.
169 */
170#if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE
171char	*realpath(const char * __restrict, char * __restrict);
172#endif
173#if __POSIX_VISIBLE >= 199506
174int	 rand_r(unsigned *);			/* (TSF) */
175#endif
176#if __POSIX_VISIBLE >= 200112
177int	 posix_memalign(void **, size_t, size_t); /* (ADV) */
178int	 setenv(const char *, const char *, int);
179int	 unsetenv(const char *);
180#endif
181
182#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
183int	 getsubopt(char **, char *const *, char **);
184#ifndef _MKDTEMP_DECLARED
185char	*mkdtemp(char *);
186#define	_MKDTEMP_DECLARED
187#endif
188#ifndef _MKSTEMP_DECLARED
189int	 mkstemp(char *);
190#define	_MKSTEMP_DECLARED
191#endif
192#endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
193
194/*
195 * The only changes to the XSI namespace in revision 6 were the deletion
196 * of the ttyslot() and valloc() functions, which FreeBSD never declared
197 * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
198 * FreeBSD also does not have, and mktemp(), are to be deleted.
199 */
200#if __XSI_VISIBLE
201/* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
202long	 a64l(const char *);
203double	 drand48(void);
204/* char	*ecvt(double, int, int * __restrict, int * __restrict); */
205double	 erand48(unsigned short[3]);
206/* char	*fcvt(double, int, int * __restrict, int * __restrict); */
207/* char	*gcvt(double, int, int * __restrict, int * __restrict); */
208char	*initstate(unsigned long /* XSI requires u_int */, char *, long);
209long	 jrand48(unsigned short[3]);
210char	*l64a(long);
211void	 lcong48(unsigned short[7]);
212long	 lrand48(void);
213#if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600)
214char	*mktemp(char *);
215#define	_MKTEMP_DECLARED
216#endif
217long	 mrand48(void);
218long	 nrand48(unsigned short[3]);
219int	 putenv(char *);
220long	 random(void);
221unsigned short
222	*seed48(unsigned short[3]);
223#ifndef _SETKEY_DECLARED
224int	 setkey(const char *);
225#define	_SETKEY_DECLARED
226#endif
227char	*setstate(/* const */ char *);
228void	 srand48(long);
229void	 srandom(unsigned long);
230#endif /* __XSI_VISIBLE */
231
232#if __XSI_VISIBLE
233int	 grantpt(int);
234int	 posix_openpt(int);
235char	*ptsname(int);
236int	 unlockpt(int);
237#endif /* __XSI_VISIBLE */
238#if __BSD_VISIBLE
239/* ptsname_r will be included in POSIX issue 8 */
240int	 ptsname_r(int, char *, size_t);
241#endif
242
243#if __BSD_VISIBLE
244extern const char *malloc_conf;
245extern void (*malloc_message)(void *, const char *);
246
247/*
248 * The alloca() function can't be implemented in C, and on some
249 * platforms it can't be implemented at all as a callable function.
250 * The GNU C compiler provides a built-in alloca() which we can use;
251 * in all other cases, provide a prototype, mainly to pacify various
252 * incarnations of lint.  On platforms where alloca() is not in libc,
253 * programs which use it will fail to link when compiled with non-GNU
254 * compilers.
255 */
256#if __GNUC__ >= 2 || defined(__INTEL_COMPILER)
257#undef  alloca	/* some GNU bits try to get cute and define this on their own */
258#define alloca(sz) __builtin_alloca(sz)
259#elif defined(lint)
260void	*alloca(size_t);
261#endif
262
263void	 abort2(const char *, int, void **) __dead2;
264__uint32_t
265	 arc4random(void);
266void	 arc4random_addrandom(unsigned char *, int);
267void	 arc4random_buf(void *, size_t);
268void	 arc4random_stir(void);
269__uint32_t
270	 arc4random_uniform(__uint32_t);
271#ifdef __BLOCKS__
272int	 atexit_b(void (^ _Nonnull)(void));
273void	*bsearch_b(const void *, const void *, size_t,
274	    size_t, int (^ _Nonnull)(const void *, const void *));
275#endif
276char	*getbsize(int *, long *);
277					/* getcap(3) functions */
278char	*cgetcap(char *, const char *, int);
279int	 cgetclose(void);
280int	 cgetent(char **, char **, const char *);
281int	 cgetfirst(char **, char **);
282int	 cgetmatch(const char *, const char *);
283int	 cgetnext(char **, char **);
284int	 cgetnum(char *, const char *, long *);
285int	 cgetset(const char *);
286int	 cgetstr(char *, const char *, char **);
287int	 cgetustr(char *, const char *, char **);
288
289int	 daemon(int, int);
290char	*devname(__dev_t, __mode_t);
291char	*devname_r(__dev_t, __mode_t, char *, int);
292char	*fdevname(int);
293char	*fdevname_r(int, char *, int);
294int	 getloadavg(double [], int);
295const char *
296	 getprogname(void);
297
298int	 heapsort(void *, size_t, size_t,
299	    int (* _Nonnull)(const void *, const void *));
300#ifdef __BLOCKS__
301int	 heapsort_b(void *, size_t, size_t,
302	    int (^ _Nonnull)(const void *, const void *));
303void	 qsort_b(void *, size_t, size_t,
304	    int (^ _Nonnull)(const void *, const void *));
305#endif
306int	 l64a_r(long, char *, int);
307int	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
308#ifdef __BLOCKS__
309int	 mergesort_b(void *, size_t, size_t, int (^)(const void *, const void *));
310#endif
311int	 mkostemp(char *, int);
312int	 mkostemps(char *, int, int);
313void	 qsort_r(void *, size_t, size_t, void *,
314	    int (*)(void *, const void *, const void *));
315int	 radixsort(const unsigned char **, int, const unsigned char *,
316	    unsigned);
317void	*reallocarray(void *, size_t, size_t) __result_use_check
318	    __alloc_size2(2, 3);
319void	*reallocf(void *, size_t) __result_use_check __alloc_size(2);
320int	 rpmatch(const char *);
321void	 setprogname(const char *);
322int	 sradixsort(const unsigned char **, int, const unsigned char *,
323	    unsigned);
324void	 sranddev(void);
325void	 srandomdev(void);
326long long
327	strtonum(const char *, long long, long long, const char **);
328
329/* Deprecated interfaces, to be removed. */
330__int64_t
331	 strtoq(const char *, char **, int);
332__uint64_t
333	 strtouq(const char *, char **, int);
334
335extern char *suboptarg;			/* getsubopt(3) external variable */
336#endif /* __BSD_VISIBLE */
337
338#if __EXT1_VISIBLE
339
340#ifndef _ERRNO_T_DEFINED
341#define _ERRNO_T_DEFINED
342typedef int errno_t;
343#endif
344
345/* K.3.6 */
346typedef void (*constraint_handler_t)(const char * __restrict,
347    void * __restrict, errno_t);
348/* K.3.6.1.1 */
349constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
350/* K.3.6.1.2 */
351_Noreturn void abort_handler_s(const char * __restrict, void * __restrict,
352    errno_t);
353/* K3.6.1.3 */
354void ignore_handler_s(const char * __restrict, void * __restrict, errno_t);
355#endif /* __EXT1_VISIBLE */
356
357__END_DECLS
358__NULLABILITY_PRAGMA_POP
359
360#endif /* !_STDLIB_H_ */
361