1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
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 * $FreeBSD$
33 */
34
35#ifndef _STDLIB_H_
36#define	_STDLIB_H_
37
38#include <sys/cdefs.h>
39#include <sys/_null.h>
40#include <sys/_types.h>
41
42__NULLABILITY_PRAGMA_PUSH
43
44#if __BSD_VISIBLE
45#ifndef _RUNE_T_DECLARED
46typedef	__rune_t	rune_t;
47#define	_RUNE_T_DECLARED
48#endif
49#endif
50
51#ifndef _SIZE_T_DECLARED
52typedef	__size_t	size_t;
53#define	_SIZE_T_DECLARED
54#endif
55
56#ifndef	__cplusplus
57#ifndef _WCHAR_T_DECLARED
58typedef	___wchar_t	wchar_t;
59#define	_WCHAR_T_DECLARED
60#endif
61#endif
62
63typedef struct {
64	int	quot;		/* quotient */
65	int	rem;		/* remainder */
66} div_t;
67
68typedef struct {
69	long	quot;
70	long	rem;
71} ldiv_t;
72
73#define	EXIT_FAILURE	1
74#define	EXIT_SUCCESS	0
75
76/*
77 * I.e., INT_MAX; rand(3) returns a signed integer but must produce output in
78 * the range [0, RAND_MAX], so half of the possible output range is unused.
79 */
80#define	RAND_MAX	0x7fffffff
81
82__BEGIN_DECLS
83#ifdef _XLOCALE_H_
84#include <xlocale/_stdlib.h>
85#endif
86extern int __mb_cur_max;
87extern int ___mb_cur_max(void);
88#define	MB_CUR_MAX	((size_t)___mb_cur_max())
89
90_Noreturn void	 abort(void);
91int	 abs(int) __pure2;
92int	 atexit(void (* _Nonnull)(void));
93double	 atof(const char *);
94int	 atoi(const char *);
95long	 atol(const char *);
96void	*bsearch(const void *, const void *, size_t,
97	    size_t, int (*)(const void * _Nonnull, const void *));
98void	*calloc(size_t, size_t) __malloc_like __result_use_check
99	     __alloc_size2(1, 2);
100div_t	 div(int, int) __pure2;
101_Noreturn void	 exit(int);
102void	 free(void *);
103char	*getenv(const char *);
104long	 labs(long) __pure2;
105ldiv_t	 ldiv(long, long) __pure2;
106void	*malloc(size_t) __malloc_like __result_use_check __alloc_size(1);
107int	 mblen(const char *, size_t);
108size_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
109int	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
110void	 qsort(void *, size_t, size_t,
111	    int (* _Nonnull)(const void *, const void *));
112int	 rand(void);
113void	*realloc(void *, size_t) __result_use_check __alloc_size(2);
114void	 srand(unsigned);
115double	 strtod(const char * __restrict, char ** __restrict);
116float	 strtof(const char * __restrict, char ** __restrict);
117long	 strtol(const char * __restrict, char ** __restrict, int);
118long double
119	 strtold(const char * __restrict, char ** __restrict);
120unsigned long
121	 strtoul(const char * __restrict, char ** __restrict, int);
122int	 system(const char *);
123int	 wctomb(char *, wchar_t);
124size_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
125
126/*
127 * Functions added in C99 which we make conditionally available in the
128 * BSD^C89 namespace if the compiler supports `long long'.
129 * The #if test is more complicated than it ought to be because
130 * __BSD_VISIBLE implies __ISO_C_VISIBLE == 1999 *even if* `long long'
131 * is not supported in the compilation environment (which therefore means
132 * that it can't really be ISO C99).
133 *
134 * (The only other extension made by C99 in thie header is _Exit().)
135 */
136#if __ISO_C_VISIBLE >= 1999 || defined(__cplusplus)
137#ifdef __LONG_LONG_SUPPORTED
138/* LONGLONG */
139typedef struct {
140	long long quot;
141	long long rem;
142} lldiv_t;
143
144/* LONGLONG */
145long long
146	 atoll(const char *);
147/* LONGLONG */
148long long
149	 llabs(long long) __pure2;
150/* LONGLONG */
151lldiv_t	 lldiv(long long, long long) __pure2;
152/* LONGLONG */
153long long
154	 strtoll(const char * __restrict, char ** __restrict, int);
155/* LONGLONG */
156unsigned long long
157	 strtoull(const char * __restrict, char ** __restrict, int);
158#endif /* __LONG_LONG_SUPPORTED */
159
160_Noreturn void	 _Exit(int);
161#endif /* __ISO_C_VISIBLE >= 1999 */
162
163/*
164 * If we're in a mode greater than C99, expose C11 functions.
165 */
166#if __ISO_C_VISIBLE >= 2011 || __cplusplus >= 201103L
167void *	aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
168	    __alloc_size(2);
169int	at_quick_exit(void (*)(void));
170_Noreturn void
171	quick_exit(int);
172#endif /* __ISO_C_VISIBLE >= 2011 */
173/*
174 * Extensions made by POSIX relative to C.
175 */
176#if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE
177char	*realpath(const char * __restrict, char * __restrict);
178#endif
179#if __POSIX_VISIBLE >= 199506
180int	 rand_r(unsigned *);			/* (TSF) */
181#endif
182#if __POSIX_VISIBLE >= 200112
183int	 posix_memalign(void **, size_t, size_t); /* (ADV) */
184int	 setenv(const char *, const char *, int);
185int	 unsetenv(const char *);
186#endif
187
188#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
189int	 getsubopt(char **, char *const *, char **);
190#ifndef _MKDTEMP_DECLARED
191char	*mkdtemp(char *);
192#define	_MKDTEMP_DECLARED
193#endif
194#ifndef _MKSTEMP_DECLARED
195int	 mkstemp(char *);
196#define	_MKSTEMP_DECLARED
197#endif
198#endif /* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
199
200/*
201 * The only changes to the XSI namespace in revision 6 were the deletion
202 * of the ttyslot() and valloc() functions, which FreeBSD never declared
203 * in this header.  For revision 7, ecvt(), fcvt(), and gcvt(), which
204 * FreeBSD also does not have, and mktemp(), are to be deleted.
205 */
206#if __XSI_VISIBLE
207/* XXX XSI requires pollution from <sys/wait.h> here.  We'd rather not. */
208long	 a64l(const char *);
209double	 drand48(void);
210/* char	*ecvt(double, int, int * __restrict, int * __restrict); */
211double	 erand48(unsigned short[3]);
212/* char	*fcvt(double, int, int * __restrict, int * __restrict); */
213/* char	*gcvt(double, int, int * __restrict, int * __restrict); */
214char	*initstate(unsigned int, char *, size_t);
215long	 jrand48(unsigned short[3]);
216char	*l64a(long);
217void	 lcong48(unsigned short[7]);
218long	 lrand48(void);
219#if !defined(_MKTEMP_DECLARED) && (__BSD_VISIBLE || __XSI_VISIBLE <= 600)
220char	*mktemp(char *);
221#define	_MKTEMP_DECLARED
222#endif
223long	 mrand48(void);
224long	 nrand48(unsigned short[3]);
225int	 putenv(char *);
226long	 random(void);
227unsigned short
228	*seed48(unsigned short[3]);
229char	*setstate(/* const */ char *);
230void	 srand48(long);
231void	 srandom(unsigned int);
232#endif /* __XSI_VISIBLE */
233
234#if __XSI_VISIBLE
235int	 grantpt(int);
236int	 posix_openpt(int);
237char	*ptsname(int);
238int	 unlockpt(int);
239#endif /* __XSI_VISIBLE */
240#if __BSD_VISIBLE
241/* ptsname_r will be included in POSIX issue 8 */
242int	 ptsname_r(int, char *, size_t);
243#endif
244
245#if __BSD_VISIBLE
246extern const char *malloc_conf;
247extern void (*malloc_message)(void *, const char *);
248
249/*
250 * The alloca() function can't be implemented in C, and on some
251 * platforms it can't be implemented at all as a callable function.
252 * The GNU C compiler provides a built-in alloca() which we can use.
253 * On platforms where alloca() is not in libc, programs which use it
254 * will fail to link when compiled with non-GNU compilers.
255 */
256#if __GNUC__ >= 2
257#undef  alloca	/* some GNU bits try to get cute and define this on their own */
258#define alloca(sz) __builtin_alloca(sz)
259#endif
260
261void	 abort2(const char *, int, void **) __dead2;
262__uint32_t
263	 arc4random(void);
264void	 arc4random_buf(void *, size_t);
265__uint32_t
266	 arc4random_uniform(__uint32_t);
267
268#ifdef __BLOCKS__
269int	 atexit_b(void (^ _Nonnull)(void));
270void	*bsearch_b(const void *, const void *, size_t,
271	    size_t, int (^ _Nonnull)(const void *, const void *));
272#endif
273char	*getbsize(int *, long *);
274					/* getcap(3) functions */
275char	*cgetcap(char *, const char *, int);
276int	 cgetclose(void);
277int	 cgetent(char **, char **, const char *);
278int	 cgetfirst(char **, char **);
279int	 cgetmatch(const char *, const char *);
280int	 cgetnext(char **, char **);
281int	 cgetnum(char *, const char *, long *);
282int	 cgetset(const char *);
283int	 cgetstr(char *, const char *, char **);
284int	 cgetustr(char *, const char *, char **);
285
286int	 daemon(int, int);
287int	 daemonfd(int, int);
288char	*devname(__dev_t, __mode_t);
289char	*devname_r(__dev_t, __mode_t, char *, int);
290char	*fdevname(int);
291char	*fdevname_r(int, char *, int);
292int	 getloadavg(double [], int);
293const char *
294	 getprogname(void);
295
296int	 heapsort(void *, size_t, size_t,
297	    int (* _Nonnull)(const void *, const void *));
298#ifdef __BLOCKS__
299int	 heapsort_b(void *, size_t, size_t,
300	    int (^ _Nonnull)(const void *, const void *));
301void	 qsort_b(void *, size_t, size_t,
302	    int (^ _Nonnull)(const void *, const void *));
303#endif
304int	 l64a_r(long, char *, int);
305int	 mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
306#ifdef __BLOCKS__
307int	 mergesort_b(void *, size_t, size_t, int (^)(const void *, const void *));
308#endif
309int	 mkostemp(char *, int);
310int	 mkostemps(char *, int, int);
311int	 mkostempsat(int, char *, int, int);
312void	 qsort_r(void *, size_t, size_t, void *,
313	    int (*)(void *, const void *, const void *));
314int	 radixsort(const unsigned char **, int, const unsigned char *,
315	    unsigned);
316void	*reallocarray(void *, size_t, size_t) __result_use_check
317	    __alloc_size2(2, 3);
318void	*reallocf(void *, size_t) __result_use_check __alloc_size(2);
319int	 rpmatch(const char *);
320void	 setprogname(const char *);
321int	 sradixsort(const unsigned char **, int, const unsigned char *,
322	    unsigned);
323void	 srandomdev(void);
324long long
325	strtonum(const char *, long long, long long, const char **);
326
327/* Deprecated interfaces, to be removed. */
328__int64_t
329	 strtoq(const char *, char **, int);
330__uint64_t
331	 strtouq(const char *, char **, int);
332
333extern char *suboptarg;			/* getsubopt(3) external variable */
334#endif /* __BSD_VISIBLE */
335
336#if __EXT1_VISIBLE
337
338#ifndef _RSIZE_T_DEFINED
339#define _RSIZE_T_DEFINED
340typedef size_t rsize_t;
341#endif
342
343#ifndef _ERRNO_T_DEFINED
344#define _ERRNO_T_DEFINED
345typedef int errno_t;
346#endif
347
348/* K.3.6 */
349typedef void (*constraint_handler_t)(const char * __restrict,
350    void * __restrict, errno_t);
351/* K.3.6.1.1 */
352constraint_handler_t set_constraint_handler_s(constraint_handler_t handler);
353/* K.3.6.1.2 */
354_Noreturn void abort_handler_s(const char * __restrict, void * __restrict,
355    errno_t);
356/* K3.6.1.3 */
357void ignore_handler_s(const char * __restrict, void * __restrict, errno_t);
358/* K.3.6.3.2 */
359errno_t	 qsort_s(void *, rsize_t, rsize_t,
360    int (*)(const void *, const void *, void *), void *);
361#endif /* __EXT1_VISIBLE */
362
363__END_DECLS
364__NULLABILITY_PRAGMA_POP
365
366#endif /* !_STDLIB_H_ */
367