master
  1/*
  2 * Copyright (c) 2023 Apple Inc. All rights reserved.
  3 *
  4 * @APPLE_LICENSE_HEADER_START@
  5 * 
  6 * This file contains Original Code and/or Modifications of Original Code
  7 * as defined in and that are subject to the Apple Public Source License
  8 * Version 2.0 (the 'License'). You may not use this file except in
  9 * compliance with the License. Please obtain a copy of the License at
 10 * http://www.opensource.apple.com/apsl/ and read it before using this
 11 * file.
 12 * 
 13 * The Original Code and all software distributed under the License are
 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 18 * Please see the License for the specific language governing rights and
 19 * limitations under the License.
 20 * 
 21 * @APPLE_LICENSE_HEADER_END@
 22 */
 23/*-
 24 * Copyright (c) 1990, 1993
 25 *	The Regents of the University of California.  All rights reserved.
 26 *
 27 * Redistribution and use in source and binary forms, with or without
 28 * modification, are permitted provided that the following conditions
 29 * are met:
 30 * 1. Redistributions of source code must retain the above copyright
 31 *    notice, this list of conditions and the following disclaimer.
 32 * 2. Redistributions in binary form must reproduce the above copyright
 33 *    notice, this list of conditions and the following disclaimer in the
 34 *    documentation and/or other materials provided with the distribution.
 35 * 3. All advertising materials mentioning features or use of this software
 36 *    must display the following acknowledgement:
 37 *	This product includes software developed by the University of
 38 *	California, Berkeley and its contributors.
 39 * 4. Neither the name of the University nor the names of its contributors
 40 *    may be used to endorse or promote products derived from this software
 41 *    without specific prior written permission.
 42 *
 43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 46 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 53 * SUCH DAMAGE.
 54 *
 55 *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
 56 */
 57
 58#ifndef _STDLIB_H_
 59#define _STDLIB_H_
 60
 61#define _LIBC_COUNT__MB_LEN_MAX	_LIBC_UNSAFE_INDEXABLE
 62#define _LIBC_COUNT__PATH_MAX	_LIBC_UNSAFE_INDEXABLE
 63
 64#include <Availability.h>
 65#include <sys/cdefs.h>
 66
 67#include <_bounds.h>
 68#include <_types.h>
 69#if !defined(_ANSI_SOURCE)
 70#include <sys/wait.h>
 71#if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
 72#include <alloca.h>
 73#if defined(__LIBC_STAGED_BOUNDS_SAFETY_ATTRIBUTES) && __has_ptrcheck
 74
 75/* required for bounds annotations, but brings in unexpected names and may
 76 * need adoption work from clients */
 77#include <limits.h> /* for MB_LEN_MAX */
 78#include <sys/syslimits.h> /* for PATH_MAX */
 79
 80#undef _LIBC_COUNT__MB_LEN_MAX
 81#undef _LIBC_COUNT__PATH_MAX
 82#define _LIBC_COUNT__MB_LEN_MAX	_LIBC_COUNT(MB_LEN_MAX)
 83#define _LIBC_COUNT__PATH_MAX	_LIBC_COUNT_OR_NULL(PATH_MAX)
 84
 85#endif /* defined(__LIBC_STAGED_BOUNDS_SAFETY_ATTRIBUTES) && __has_ptrcheck */
 86#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
 87#endif /* !_ANSI_SOURCE */
 88
 89/* DO NOT REMOVE THIS COMMENT: fixincludes needs to see:
 90 * _GCC_SIZE_T */
 91#include <sys/_types/_size_t.h>
 92
 93#if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
 94#include <sys/_types/_ct_rune_t.h>
 95#include <sys/_types/_rune_t.h>
 96#endif	/* !_ANSI_SOURCE && (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
 97
 98#include <sys/_types/_wchar_t.h>
 99
100_LIBC_SINGLE_BY_DEFAULT()
101
102typedef struct {
103	int quot;		/* quotient */
104	int rem;		/* remainder */
105} div_t;
106
107typedef struct {
108	long quot;		/* quotient */
109	long rem;		/* remainder */
110} ldiv_t;
111
112#if !__DARWIN_NO_LONG_LONG
113typedef struct {
114	long long quot;
115	long long rem;
116} lldiv_t;
117#endif /* !__DARWIN_NO_LONG_LONG */
118
119#include <sys/_types/_null.h>
120
121#define	EXIT_FAILURE	1
122#define	EXIT_SUCCESS	0
123
124#define	RAND_MAX	0x7fffffff
125
126#if __has_feature(modules)
127// When clang modules are enabled, there can only be one definition of
128// MB_CUR_MAX, and that needs to be the same one used by xlocale.h.
129#include <_mb_cur_max.h>
130#else /* !__has_feature(modules) */
131#ifndef MB_CUR_MAX
132#ifdef _USE_EXTENDED_LOCALES_
133#define	MB_CUR_MAX	(___mb_cur_max())
134#ifndef MB_CUR_MAX_L
135#define	MB_CUR_MAX_L(x)	(___mb_cur_max_l(x))
136#endif /* !MB_CUR_MAX_L */
137#else /* !_USE_EXTENDED_LOCALES_ */
138extern int __mb_cur_max;
139#define	MB_CUR_MAX	__mb_cur_max
140#endif /* _USE_EXTENDED_LOCALES_ */
141#endif /* MB_CUR_MAX */
142#endif /* __has_feature(modules) */
143
144#include <malloc/_malloc.h>
145#include <_abort.h>
146
147__BEGIN_DECLS
148int	 abs(int) __pure2;
149int	 atexit(void (* _Nonnull)(void));
150#if (__DARWIN_C_LEVEL >= __DARWIN_C_FULL) || \
151	defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
152int	at_quick_exit(void (*)(void));
153#endif
154double	 atof(const char *);
155int	 atoi(const char *);
156long	 atol(const char *);
157#if !__DARWIN_NO_LONG_LONG
158long long
159	 atoll(const char *);
160#endif /* !__DARWIN_NO_LONG_LONG */
161void	*bsearch(const void *_LIBC_SIZE(__width) __key, const void *_LIBC_SIZE(__width * __nel) __base, size_t __nel,
162	    size_t __width, int (* _Nonnull __compar)(const void *, const void *));
163/* calloc is now declared in _malloc.h */
164div_t	 div(int, int) __pure2;
165void	 exit(int) __dead2;
166/* free is now declared in _malloc.h */
167char *_LIBC_CSTR	getenv(const char *);
168long	 labs(long) __pure2;
169ldiv_t	 ldiv(long, long) __pure2;
170#if !__DARWIN_NO_LONG_LONG
171long long
172	 llabs(long long);
173lldiv_t	 lldiv(long long, long long);
174#endif /* !__DARWIN_NO_LONG_LONG */
175/* malloc is now declared in _malloc.h */
176int	 mblen(const char *_LIBC_COUNT(__n) __s, size_t __n);
177size_t	 mbstowcs(wchar_t * __restrict _LIBC_COUNT(__n), const char * __restrict, size_t __n);
178int	 mbtowc(wchar_t * __restrict, const char * __restrict _LIBC_COUNT(__n), size_t __n);
179/* posix_memalign is now declared in _malloc.h */
180void	 qsort(void *_LIBC_SIZE(__nel * __width) __base, size_t __nel, size_t __width,
181	    int (* _Nonnull __compar)(const void *, const void *));
182#if (__DARWIN_C_LEVEL >= __DARWIN_C_FULL) || \
183	defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
184void	quick_exit(int) __dead2;
185#endif
186int	 rand(void) __swift_unavailable("Use arc4random instead.");
187/* realloc is now declared in _malloc.h */
188void	 srand(unsigned) __swift_unavailable("Use arc4random instead.");
189double	 strtod(const char *, char *_LIBC_CSTR *) __DARWIN_ALIAS(strtod);
190float	 strtof(const char *, char *_LIBC_CSTR *) __DARWIN_ALIAS(strtof);
191long	 strtol(const char *__str, char *_LIBC_CSTR *__endptr, int __base);
192long double
193	 strtold(const char *, char *_LIBC_CSTR *);
194#if !__DARWIN_NO_LONG_LONG
195long long 
196	 strtoll(const char *__str, char *_LIBC_CSTR *__endptr, int __base);
197#endif /* !__DARWIN_NO_LONG_LONG */
198unsigned long
199	 strtoul(const char *__str, char *_LIBC_CSTR *__endptr, int __base);
200#if !__DARWIN_NO_LONG_LONG
201unsigned long long
202	 strtoull(const char *__str, char *_LIBC_CSTR *__endptr, int __base);
203#endif /* !__DARWIN_NO_LONG_LONG */
204
205__swift_unavailable("Use posix_spawn APIs or NSTask instead. (On iOS, process spawning is unavailable.)")
206__API_AVAILABLE(macos(10.0)) __IOS_PROHIBITED
207__WATCHOS_PROHIBITED __TVOS_PROHIBITED
208int	 system(const char *) __DARWIN_ALIAS_C(system);
209
210
211size_t	 wcstombs(char * __restrict _LIBC_COUNT(__n), const wchar_t * __restrict, size_t __n);
212int	 wctomb(char *_LIBC_COUNT__MB_LEN_MAX, wchar_t);
213
214#ifndef _ANSI_SOURCE
215void	_Exit(int) __dead2;
216long	 a64l(const char *);
217double	 drand48(void);
218char *_LIBC_CSTR	ecvt(double, int, int *__restrict, int *__restrict); /* LEGACY */
219double	 erand48(unsigned short[3]);
220char *_LIBC_CSTR	fcvt(double, int, int *__restrict, int *__restrict); /* LEGACY */
221char *_LIBC_CSTR	gcvt(double, int, char *_LIBC_UNSAFE_INDEXABLE) _LIBC_PTRCHECK_REPLACED(snprintf); /* LEGACY */
222int	 getsubopt(char *_LIBC_CSTR *, char *_LIBC_CSTR const *_LIBC_NULL_TERMINATED, char *_LIBC_CSTR *);
223int	 grantpt(int);
224#if __DARWIN_UNIX03
225char *_LIBC_UNSAFE_INDEXABLE
226		initstate(unsigned, char *_LIBC_COUNT(__size), size_t __size); /* no  __DARWIN_ALIAS needed */
227#else /* !__DARWIN_UNIX03 */
228char *_LIBC_UNSAFE_INDEXABLE
229		initstate(unsigned long, char *_LIBC_COUNT(__size), long __size);
230#endif /* __DARWIN_UNIX03 */
231long	 jrand48(unsigned short[3]) __swift_unavailable("Use arc4random instead.");
232char	*l64a(long);
233void	 lcong48(unsigned short[7]);
234long	 lrand48(void) __swift_unavailable("Use arc4random instead.");
235#if !defined(_POSIX_C_SOURCE)
236__deprecated_msg("This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of mktemp(3), it is highly recommended that you use mkstemp(3) instead.")
237#endif
238char *_LIBC_CSTR	mktemp(char *_LIBC_CSTR);
239int	 mkstemp(char *_LIBC_CSTR);
240long	 mrand48(void) __swift_unavailable("Use arc4random instead.");
241long	 nrand48(unsigned short[3]) __swift_unavailable("Use arc4random instead.");
242int	 posix_openpt(int);
243char *_LIBC_CSTR	ptsname(int);
244
245#if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
246int ptsname_r(int fildes, char *_LIBC_COUNT(buflen) buffer, size_t buflen) __API_AVAILABLE(macos(10.13.4), ios(11.3), tvos(11.3), watchos(4.3));
247#endif
248
249int	 putenv(char *_LIBC_CSTR) __DARWIN_ALIAS(putenv);
250long	 random(void) __swift_unavailable("Use arc4random instead.");
251int	 rand_r(unsigned *) __swift_unavailable("Use arc4random instead.");
252#if (__DARWIN_UNIX03 && !defined(_POSIX_C_SOURCE)) || defined(_DARWIN_C_SOURCE) || defined(_DARWIN_BETTER_REALPATH)
253char *_LIBC_CSTR	realpath(const char * __restrict, char * __restrict _LIBC_COUNT__PATH_MAX) __DARWIN_EXTSN(realpath);
254#else /* (!__DARWIN_UNIX03 || _POSIX_C_SOURCE) && !_DARWIN_C_SOURCE && !_DARWIN_BETTER_REALPATH */
255char *_LIBC_CSTR	realpath(const char * __restrict, char * __restrict _LIBC_COUNT__PATH_MAX) __DARWIN_ALIAS(realpath);
256#endif /* (__DARWIN_UNIX03 && _POSIX_C_SOURCE) || _DARWIN_C_SOURCE || _DARWIN_BETTER_REALPATH */
257unsigned short *_LIBC_COUNT(3) seed48(unsigned short[3]);
258int	 setenv(const char * __name, const char * __value, int __overwrite) __DARWIN_ALIAS(setenv);
259#if __DARWIN_UNIX03
260void	 setkey(const char *) __DARWIN_ALIAS(setkey);
261#else /* !__DARWIN_UNIX03 */
262int	 setkey(const char *);
263#endif /* __DARWIN_UNIX03 */
264char *_LIBC_UNSAFE_INDEXABLE	setstate(const char *_LIBC_UNSAFE_INDEXABLE);
265void	 srand48(long);
266#if __DARWIN_UNIX03
267void	 srandom(unsigned);
268#else /* !__DARWIN_UNIX03 */
269void	 srandom(unsigned long);
270#endif /* __DARWIN_UNIX03 */
271int	 unlockpt(int);
272#if __DARWIN_UNIX03
273int	 unsetenv(const char *) __DARWIN_ALIAS(unsetenv);
274#else /* !__DARWIN_UNIX03 */
275void	 unsetenv(const char *);
276#endif /* __DARWIN_UNIX03 */
277#endif	/* !_ANSI_SOURCE */
278__END_DECLS
279
280#if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
281#include <machine/types.h>
282#include <sys/_types/_dev_t.h>
283#include <sys/_types/_mode_t.h>
284#include <_types/_uint32_t.h>
285
286__BEGIN_DECLS
287uint32_t arc4random(void);
288void	 arc4random_addrandom(unsigned char *_LIBC_COUNT(__datlen) /*dat*/, int __datlen)
289    __OSX_DEPRECATED(10.0, 10.12, "use arc4random_stir")
290    __IOS_DEPRECATED(2.0, 10.0, "use arc4random_stir")
291    __TVOS_DEPRECATED(2.0, 10.0, "use arc4random_stir")
292    __WATCHOS_DEPRECATED(1.0, 3.0, "use arc4random_stir");
293void	 arc4random_buf(void *_LIBC_SIZE(__nbytes) __buf, size_t __nbytes) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
294void	 arc4random_stir(void);
295uint32_t
296	 arc4random_uniform(uint32_t __upper_bound) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
297#ifdef __BLOCKS__
298int	 atexit_b(void (^ _Nonnull)(void)) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
299
300#ifdef __BLOCKS__
301#if __has_attribute(noescape)
302#define __bsearch_noescape __attribute__((__noescape__))
303#else
304#define __bsearch_noescape
305#endif
306#endif /* __BLOCKS__ */
307void	*bsearch_b(const void *_LIBC_SIZE(__width) __key, const void *_LIBC_SIZE(__width * __nel) __base, size_t __nel,
308	    size_t __width, int (^ _Nonnull __compar)(const void *, const void *) __bsearch_noescape)
309	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
310#endif /* __BLOCKS__ */
311
312	 /* getcap(3) functions */
313char *_LIBC_CSTR	cgetcap(char *_LIBC_CSTR, const char *, int);
314int	 cgetclose(void);
315int	 cgetent(char *_LIBC_CSTR *, char *_LIBC_CSTR *_LIBC_NULL_TERMINATED, const char *);
316int	 cgetfirst(char *_LIBC_CSTR *, char *_LIBC_CSTR *_LIBC_NULL_TERMINATED);
317int	 cgetmatch(const char *, const char *);
318int	 cgetnext(char *_LIBC_CSTR *, char *_LIBC_CSTR *_LIBC_NULL_TERMINATED);
319int	 cgetnum(char *_LIBC_CSTR, const char *, long *);
320int	 cgetset(const char *);
321int	 cgetstr(char *_LIBC_CSTR, const char *, char *_LIBC_CSTR *);
322int	 cgetustr(char *_LIBC_CSTR, const char *, char *_LIBC_CSTR *);
323
324int	 daemon(int, int) __DARWIN_1050(daemon) __OSX_AVAILABLE_BUT_DEPRECATED_MSG(__MAC_10_0, __MAC_10_5, __IPHONE_2_0, __IPHONE_2_0, "Use posix_spawn APIs instead.") __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
325char *_LIBC_CSTR	devname(dev_t, mode_t);
326char *_LIBC_CSTR	devname_r(dev_t, mode_t, char *_LIBC_COUNT(len) buf, int len);
327char *_LIBC_CSTR	getbsize(int *, long *);
328int	 getloadavg(double [_LIBC_COUNT(__nelem)], int __nelem);
329const char
330	*getprogname(void);
331void	 setprogname(const char *);
332
333#ifdef __BLOCKS__
334#if __has_attribute(noescape)
335#define __sort_noescape __attribute__((__noescape__))
336#else
337#define __sort_noescape
338#endif
339#endif /* __BLOCKS__ */
340
341int	 heapsort(void *_LIBC_SIZE(__nel * __width) __base, size_t __nel, size_t __width,
342	    int (* _Nonnull __compar)(const void *, const void *));
343#ifdef __BLOCKS__
344int	 heapsort_b(void *_LIBC_SIZE(__nel * __width) __base, size_t __nel, size_t __width,
345	    int (^ _Nonnull __compar)(const void *, const void *) __sort_noescape)
346	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
347#endif /* __BLOCKS__ */
348int	 mergesort(void *_LIBC_SIZE(__nel * __width) __base, size_t __nel, size_t __width,
349	    int (* _Nonnull __compar)(const void *, const void *));
350#ifdef __BLOCKS__
351int	 mergesort_b(void *_LIBC_SIZE(__nel * __width) __base, size_t __nel, size_t __width,
352	    int (^ _Nonnull __compar)(const void *, const void *) __sort_noescape)
353	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
354#endif /* __BLOCKS__ */
355void	 psort(void *_LIBC_SIZE(__nel * __width) __base, size_t __nel, size_t __width,
356	    int (* _Nonnull __compar)(const void *, const void *))
357	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
358#ifdef __BLOCKS__
359void	 psort_b(void *_LIBC_SIZE(__nel * __width) __base, size_t __nel, size_t __width,
360	    int (^ _Nonnull __compar)(const void *, const void *) __sort_noescape)
361	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
362#endif /* __BLOCKS__ */
363void	 psort_r(void *_LIBC_SIZE(__nel * __width) __base, size_t __nel, size_t __width, void *,
364	    int (* _Nonnull __compar)(void *, const void *, const void *))
365	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
366#ifdef __BLOCKS__
367void	 qsort_b(void *_LIBC_SIZE(__nel * __width) __base, size_t __nel, size_t __width,
368	    int (^ _Nonnull __compar)(const void *, const void *) __sort_noescape)
369	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
370#endif /* __BLOCKS__ */
371void	 qsort_r(void *_LIBC_SIZE(__nel * __width) __base, size_t __nel, size_t __width, void *,
372	    int (* _Nonnull __compar)(void *, const void *, const void *));
373int	 radixsort(const unsigned char *_LIBC_UNSAFE_INDEXABLE *_LIBC_COUNT(__nel) __base, int __nel, const unsigned char *_LIBC_COUNT_OR_NULL(256) __table,
374	    unsigned __endbyte);
375int	rpmatch(const char *)
376	__API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), watchos(6.0));
377int	 sradixsort(const unsigned char *_LIBC_UNSAFE_INDEXABLE *_LIBC_COUNT(__nel) __base, int __nel, const unsigned char *_LIBC_COUNT_OR_NULL(256) __table,
378	    unsigned __endbyte);
379void	 sranddev(void);
380void	 srandomdev(void);
381/* reallocf is now declared in _malloc.h */
382long long
383	strtonum(const char *__numstr, long long __minval, long long __maxval, const char *_LIBC_CSTR *__errstrp)
384	__API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0), watchos(7.0));
385#if !__DARWIN_NO_LONG_LONG
386long long
387	 strtoq(const char *__str, char *_LIBC_CSTR *__endptr, int __base);
388unsigned long long
389	 strtouq(const char *__str, char *_LIBC_CSTR *__endptr, int __base);
390#endif /* !__DARWIN_NO_LONG_LONG */
391extern char *_LIBC_CSTR suboptarg;		/* getsubopt(3) external variable */
392/* valloc is now declared in _malloc.h */
393__END_DECLS
394#endif	/* !_ANSI_SOURCE && !_POSIX_SOURCE */
395
396__BEGIN_DECLS
397/* Poison the following routines if -fshort-wchar is set */
398#if !defined(__cplusplus) && defined(__WCHAR_MAX__) && __WCHAR_MAX__ <= 0xffffU
399#pragma GCC poison mbstowcs mbtowc wcstombs wctomb
400#endif
401__END_DECLS
402
403#endif /* _STDLIB_H_ */