/ include / stdlib.h
stdlib.h
  1  /*
  2   * Copyright (c) 2000, 2002 - 2008 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  #include <Availability.h>
 62  #include <sys/cdefs.h>
 63  
 64  #include <_types.h>
 65  #if !defined(_ANSI_SOURCE)
 66  #ifndef UNIFDEF_DRIVERKIT
 67  #include <sys/wait.h>
 68  #endif /* UNIFDEF_DRIVERKIT */
 69  #if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
 70  #include <alloca.h>
 71  #endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
 72  #endif /* !_ANSI_SOURCE */
 73  
 74  /* DO NOT REMOVE THIS COMMENT: fixincludes needs to see:
 75   * _GCC_SIZE_T */
 76  #include <sys/_types/_size_t.h>
 77  
 78  #if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
 79  #include <sys/_types/_ct_rune_t.h>
 80  #include <sys/_types/_rune_t.h>
 81  #endif	/* !_ANSI_SOURCE && (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
 82  
 83  #include <sys/_types/_wchar_t.h>
 84  
 85  typedef struct {
 86  	int quot;		/* quotient */
 87  	int rem;		/* remainder */
 88  } div_t;
 89  
 90  typedef struct {
 91  	long quot;		/* quotient */
 92  	long rem;		/* remainder */
 93  } ldiv_t;
 94  
 95  #if !__DARWIN_NO_LONG_LONG
 96  typedef struct {
 97  	long long quot;
 98  	long long rem;
 99  } lldiv_t;
100  #endif /* !__DARWIN_NO_LONG_LONG */
101  
102  #include <sys/_types/_null.h>
103  
104  #ifndef UNIFDEF_DRIVERKIT
105  #define	EXIT_FAILURE	1
106  #define	EXIT_SUCCESS	0
107  #endif /* UNIFDEF_DRIVERKIT */
108  
109  #define	RAND_MAX	0x7fffffff
110  
111  #ifdef _USE_EXTENDED_LOCALES_
112  #include <_xlocale.h>
113  #endif /* _USE_EXTENDED_LOCALES_ */
114  
115  #ifndef MB_CUR_MAX
116  #ifdef _USE_EXTENDED_LOCALES_
117  #define	MB_CUR_MAX	(___mb_cur_max())
118  #ifndef MB_CUR_MAX_L
119  #define	MB_CUR_MAX_L(x)	(___mb_cur_max_l(x))
120  #endif /* !MB_CUR_MAX_L */
121  #else /* !_USE_EXTENDED_LOCALES_ */
122  extern int __mb_cur_max;
123  #define	MB_CUR_MAX	__mb_cur_max
124  #endif /* _USE_EXTENDED_LOCALES_ */
125  #endif /* MB_CUR_MAX */
126  
127  #if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)) \
128      && defined(_USE_EXTENDED_LOCALES_) && !defined(MB_CUR_MAX_L)
129  #define	MB_CUR_MAX_L(x)	(___mb_cur_max_l(x))
130  #endif
131  
132  #ifndef UNIFDEF_DRIVERKIT
133  #include <malloc/_malloc.h>
134  #endif /* UNIFDEF_DRIVERKIT */
135  
136  __BEGIN_DECLS
137  void	 abort(void) __cold __dead2;
138  int	 abs(int) __pure2;
139  #ifndef UNIFDEF_DRIVERKIT
140  int	 atexit(void (* _Nonnull)(void));
141  #endif /* UNIFDEF_DRIVERKIT */
142  double	 atof(const char *);
143  int	 atoi(const char *);
144  long	 atol(const char *);
145  #if !__DARWIN_NO_LONG_LONG
146  long long
147  	 atoll(const char *);
148  #endif /* !__DARWIN_NO_LONG_LONG */
149  void	*bsearch(const void *__key, const void *__base, size_t __nel,
150  	    size_t __width, int (* _Nonnull __compar)(const void *, const void *));
151  #ifndef UNIFDEF_DRIVERKIT
152  /* calloc is now declared in _malloc.h */
153  #endif /* UNIFDEF_DRIVERKIT */
154  div_t	 div(int, int) __pure2;
155  #ifndef UNIFDEF_DRIVERKIT
156  void	 exit(int) __dead2;
157  /* free is now declared in _malloc.h */
158  char	*getenv(const char *);
159  #endif /* UNIFDEF_DRIVERKIT */
160  long	 labs(long) __pure2;
161  ldiv_t	 ldiv(long, long) __pure2;
162  #if !__DARWIN_NO_LONG_LONG
163  long long
164  	 llabs(long long);
165  lldiv_t	 lldiv(long long, long long);
166  #endif /* !__DARWIN_NO_LONG_LONG */
167  #ifndef UNIFDEF_DRIVERKIT
168  /* malloc is now declared in _malloc.h */
169  #endif /* UNIFDEF_DRIVERKIT */
170  int	 mblen(const char *__s, size_t __n);
171  size_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
172  int	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
173  #ifndef UNIFDEF_DRIVERKIT
174  /* posix_memalign is now declared in _malloc.h */
175  #endif /* UNIFDEF_DRIVERKIT */
176  void	 qsort(void *__base, size_t __nel, size_t __width,
177  	    int (* _Nonnull __compar)(const void *, const void *));
178  #ifndef UNIFDEF_DRIVERKIT
179  int	 rand(void) __swift_unavailable("Use arc4random instead.");
180  /* realloc is now declared in _malloc.h */
181  void	 srand(unsigned) __swift_unavailable("Use arc4random instead.");
182  #endif /* UNIFDEF_DRIVERKIT */
183  double	 strtod(const char *, char **) __DARWIN_ALIAS(strtod);
184  float	 strtof(const char *, char **) __DARWIN_ALIAS(strtof);
185  long	 strtol(const char *__str, char **__endptr, int __base);
186  long double
187  	 strtold(const char *, char **);
188  #if !__DARWIN_NO_LONG_LONG
189  long long 
190  	 strtoll(const char *__str, char **__endptr, int __base);
191  #endif /* !__DARWIN_NO_LONG_LONG */
192  unsigned long
193  	 strtoul(const char *__str, char **__endptr, int __base);
194  #if !__DARWIN_NO_LONG_LONG
195  unsigned long long
196  	 strtoull(const char *__str, char **__endptr, int __base);
197  #endif /* !__DARWIN_NO_LONG_LONG */
198  #ifndef UNIFDEF_DRIVERKIT
199  
200  #if TARGET_OS_IPHONE
201  #define __swift_unavailable_on(osx_msg, ios_msg) __swift_unavailable(ios_msg)
202  #else
203  #define __swift_unavailable_on(osx_msg, ios_msg) __swift_unavailable(osx_msg)
204  #endif
205  
206  __swift_unavailable_on("Use posix_spawn APIs or NSTask instead.", "Process spawning is unavailable")
207  __API_AVAILABLE(macos(10.0)) __IOS_PROHIBITED
208  __WATCHOS_PROHIBITED __TVOS_PROHIBITED
209  int	 system(const char *) __DARWIN_ALIAS_C(system);
210  
211  #undef __swift_unavailable_on
212  #endif /* UNIFDEF_DRIVERKIT */
213  
214  size_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
215  int	 wctomb(char *, wchar_t);
216  
217  #ifndef UNIFDEF_DRIVERKIT
218  #ifndef _ANSI_SOURCE
219  void	_Exit(int) __dead2;
220  long	 a64l(const char *);
221  double	 drand48(void);
222  char	*ecvt(double, int, int *__restrict, int *__restrict); /* LEGACY */
223  double	 erand48(unsigned short[3]);
224  char	*fcvt(double, int, int *__restrict, int *__restrict); /* LEGACY */
225  char	*gcvt(double, int, char *); /* LEGACY */
226  int	 getsubopt(char **, char * const *, char **);
227  int	 grantpt(int);
228  #if __DARWIN_UNIX03
229  char	*initstate(unsigned, char *, size_t); /* no  __DARWIN_ALIAS needed */
230  #else /* !__DARWIN_UNIX03 */
231  char	*initstate(unsigned long, char *, long);
232  #endif /* __DARWIN_UNIX03 */
233  long	 jrand48(unsigned short[3]) __swift_unavailable("Use arc4random instead.");
234  char	*l64a(long);
235  void	 lcong48(unsigned short[7]);
236  long	 lrand48(void) __swift_unavailable("Use arc4random instead.");
237  char	*mktemp(char *);
238  int	 mkstemp(char *);
239  long	 mrand48(void) __swift_unavailable("Use arc4random instead.");
240  long	 nrand48(unsigned short[3]) __swift_unavailable("Use arc4random instead.");
241  int	 posix_openpt(int);
242  char	*ptsname(int);
243  
244  #if (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
245  int ptsname_r(int fildes, char *buffer, size_t buflen) __API_AVAILABLE(macos(10.13.4), ios(11.3), tvos(11.3), watchos(4.3));
246  #endif
247  
248  int	 putenv(char *) __DARWIN_ALIAS(putenv);
249  long	 random(void) __swift_unavailable("Use arc4random instead.");
250  int	 rand_r(unsigned *) __swift_unavailable("Use arc4random instead.");
251  #if (__DARWIN_UNIX03 && !defined(_POSIX_C_SOURCE)) || defined(_DARWIN_C_SOURCE) || defined(_DARWIN_BETTER_REALPATH)
252  char	*realpath(const char * __restrict, char * __restrict) __DARWIN_EXTSN(realpath);
253  #else /* (!__DARWIN_UNIX03 || _POSIX_C_SOURCE) && !_DARWIN_C_SOURCE && !_DARWIN_BETTER_REALPATH */
254  char	*realpath(const char * __restrict, char * __restrict) __DARWIN_ALIAS(realpath);
255  #endif /* (__DARWIN_UNIX03 && _POSIX_C_SOURCE) || _DARWIN_C_SOURCE || _DARWIN_BETTER_REALPATH */
256  unsigned short
257  	*seed48(unsigned short[3]);
258  int	 setenv(const char * __name, const char * __value, int __overwrite) __DARWIN_ALIAS(setenv);
259  #if __DARWIN_UNIX03
260  void	 setkey(const char *) __DARWIN_ALIAS(setkey);
261  #else /* !__DARWIN_UNIX03 */
262  int	 setkey(const char *);
263  #endif /* __DARWIN_UNIX03 */
264  char	*setstate(const char *);
265  void	 srand48(long);
266  #if __DARWIN_UNIX03
267  void	 srandom(unsigned);
268  #else /* !__DARWIN_UNIX03 */
269  void	 srandom(unsigned long);
270  #endif /* __DARWIN_UNIX03 */
271  int	 unlockpt(int);
272  #if __DARWIN_UNIX03
273  int	 unsetenv(const char *) __DARWIN_ALIAS(unsetenv);
274  #else /* !__DARWIN_UNIX03 */
275  void	 unsetenv(const char *);
276  #endif /* __DARWIN_UNIX03 */
277  #endif	/* !_ANSI_SOURCE */
278  #endif /* UNIFDEF_DRIVERKIT */
279  
280  #if !defined(_ANSI_SOURCE) && (!defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE))
281  #include <machine/types.h>
282  #ifndef UNIFDEF_DRIVERKIT
283  #include <sys/_types/_dev_t.h>
284  #include <sys/_types/_mode_t.h>
285  #endif /* UNIFDEF_DRIVERKIT */
286  #include <_types/_uint32_t.h>
287  
288  uint32_t arc4random(void);
289  #ifndef UNIFDEF_DRIVERKIT
290  void	 arc4random_addrandom(unsigned char * /*dat*/, int /*datlen*/)
291      __OSX_DEPRECATED(10.0, 10.12, "use arc4random_stir")
292      __IOS_DEPRECATED(2.0, 10.0, "use arc4random_stir")
293      __TVOS_DEPRECATED(2.0, 10.0, "use arc4random_stir")
294      __WATCHOS_DEPRECATED(1.0, 3.0, "use arc4random_stir");
295  #endif /* UNIFDEF_DRIVERKIT */
296  void	 arc4random_buf(void * __buf, size_t __nbytes) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
297  void	 arc4random_stir(void);
298  uint32_t
299  	 arc4random_uniform(uint32_t __upper_bound) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
300  #ifdef __BLOCKS__
301  #ifndef UNIFDEF_DRIVERKIT
302  int	 atexit_b(void (^ _Nonnull)(void)) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
303  #endif /* UNIFDEF_DRIVERKIT */
304  void	*bsearch_b(const void *__key, const void *__base, size_t __nel,
305  	    size_t __width, int (^ _Nonnull __compar)(const void *, const void *)) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
306  #endif /* __BLOCKS__ */
307  
308  #ifndef UNIFDEF_DRIVERKIT
309  	 /* getcap(3) functions */
310  char	*cgetcap(char *, const char *, int);
311  int	 cgetclose(void);
312  int	 cgetent(char **, char **, const char *);
313  int	 cgetfirst(char **, char **);
314  int	 cgetmatch(const char *, const char *);
315  int	 cgetnext(char **, char **);
316  int	 cgetnum(char *, const char *, long *);
317  int	 cgetset(const char *);
318  int	 cgetstr(char *, const char *, char **);
319  int	 cgetustr(char *, const char *, char **);
320  
321  int	 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;
322  char	*devname(dev_t, mode_t);
323  char	*devname_r(dev_t, mode_t, char *buf, int len);
324  char	*getbsize(int *, long *);
325  int	 getloadavg(double [], int);
326  const char
327  	*getprogname(void);
328  void	 setprogname(const char *);
329  #endif /* UNIFDEF_DRIVERKIT */
330  
331  #ifdef __BLOCKS__
332  #if __has_attribute(noescape)
333  #define __sort_noescape __attribute__((__noescape__))
334  #else
335  #define __sort_noescape
336  #endif
337  #endif /* __BLOCKS__ */
338  
339  int	 heapsort(void *__base, size_t __nel, size_t __width,
340  	    int (* _Nonnull __compar)(const void *, const void *));
341  #ifdef __BLOCKS__
342  int	 heapsort_b(void *__base, size_t __nel, size_t __width,
343  	    int (^ _Nonnull __compar)(const void *, const void *) __sort_noescape)
344  	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
345  #endif /* __BLOCKS__ */
346  int	 mergesort(void *__base, size_t __nel, size_t __width,
347  	    int (* _Nonnull __compar)(const void *, const void *));
348  #ifdef __BLOCKS__
349  int	 mergesort_b(void *__base, size_t __nel, size_t __width,
350  	    int (^ _Nonnull __compar)(const void *, const void *) __sort_noescape)
351  	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
352  #endif /* __BLOCKS__ */
353  #ifndef UNIFDEF_DRIVERKIT
354  void	 psort(void *__base, size_t __nel, size_t __width,
355  	    int (* _Nonnull __compar)(const void *, const void *))
356  	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
357  #ifdef __BLOCKS__
358  void	 psort_b(void *__base, size_t __nel, size_t __width,
359  	    int (^ _Nonnull __compar)(const void *, const void *) __sort_noescape)
360  	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
361  #endif /* __BLOCKS__ */
362  void	 psort_r(void *__base, size_t __nel, size_t __width, void *,
363  	    int (* _Nonnull __compar)(void *, const void *, const void *))
364  	    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
365  #endif /* UNIFDEF_DRIVERKIT */
366  #ifdef __BLOCKS__
367  void	 qsort_b(void *__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__ */
371  void	 qsort_r(void *__base, size_t __nel, size_t __width, void *,
372  	    int (* _Nonnull __compar)(void *, const void *, const void *));
373  int	 radixsort(const unsigned char **__base, int __nel, const unsigned char *__table,
374  	    unsigned __endbyte);
375  int	rpmatch(const char *)
376  	__API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), watchos(6.0));
377  int	 sradixsort(const unsigned char **__base, int __nel, const unsigned char *__table,
378  	    unsigned __endbyte);
379  #ifndef UNIFDEF_DRIVERKIT
380  void	 sranddev(void);
381  void	 srandomdev(void);
382  void	*reallocf(void *__ptr, size_t __size) __alloc_size(2);
383  long long
384  	strtonum(const char *__numstr, long long __minval, long long __maxval, const char **__errstrp)
385  	__API_AVAILABLE(macos(10.16), ios(14.0), tvos(14.0), watchos(7.0));
386  #endif /* UNIFDEF_DRIVERKIT */
387  #if !__DARWIN_NO_LONG_LONG
388  long long
389  	 strtoq(const char *__str, char **__endptr, int __base);
390  unsigned long long
391  	 strtouq(const char *__str, char **__endptr, int __base);
392  #endif /* !__DARWIN_NO_LONG_LONG */
393  #ifndef UNIFDEF_DRIVERKIT
394  extern char *suboptarg;		/* getsubopt(3) external variable */
395  /* valloc is now declared in _malloc.h */
396  #endif /* UNIFDEF_DRIVERKIT */
397  #endif	/* !_ANSI_SOURCE && !_POSIX_SOURCE */
398  
399  /* Poison the following routines if -fshort-wchar is set */
400  #if !defined(__cplusplus) && defined(__WCHAR_MAX__) && __WCHAR_MAX__ <= 0xffffU
401  #pragma GCC poison mbstowcs mbtowc wcstombs wctomb
402  #endif
403  __END_DECLS
404  
405  #ifdef _USE_EXTENDED_LOCALES_
406  #include <xlocale/_stdlib.h>
407  #endif /* _USE_EXTENDED_LOCALES_ */
408  
409  #endif /* _STDLIB_H_ */