master
  1/*	$NetBSD: stdlib.h,v 1.125.2.2 2024/10/13 10:39:53 martin Exp $	*/
  2
  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 */
 33
 34#ifndef _STDLIB_H_
 35#define _STDLIB_H_
 36
 37#include <sys/cdefs.h>
 38#include <sys/featuretest.h>
 39
 40#if defined(_NETBSD_SOURCE)
 41#include <sys/types.h>		/* for quad_t, etc. */
 42#endif
 43
 44#include <machine/ansi.h>
 45
 46#ifdef	_BSD_SIZE_T_
 47typedef	_BSD_SIZE_T_	size_t;
 48#undef	_BSD_SIZE_T_
 49#endif
 50
 51#if defined(_BSD_WCHAR_T_) && !defined(__cplusplus)
 52typedef	_BSD_WCHAR_T_	wchar_t;
 53#undef	_BSD_WCHAR_T_
 54#endif
 55
 56typedef struct {
 57	int quot;		/* quotient */
 58	int rem;		/* remainder */
 59} div_t;
 60
 61typedef struct {
 62	long quot;		/* quotient */
 63	long rem;		/* remainder */
 64} ldiv_t;
 65
 66#if defined(_ISOC99_SOURCE) || (__STDC_VERSION__ - 0) >= 199901L || \
 67    defined(_NETBSD_SOURCE) || (__cplusplus - 0) >= 201103L
 68typedef struct {
 69	/* LONGLONG */
 70	long long int quot;	/* quotient */
 71	/* LONGLONG */
 72	long long int rem;	/* remainder */
 73} lldiv_t;
 74#endif
 75
 76#if defined(_NETBSD_SOURCE)
 77typedef struct {
 78	quad_t quot;		/* quotient */
 79	quad_t rem;		/* remainder */
 80} qdiv_t;
 81#endif
 82
 83
 84#include <sys/null.h>
 85
 86#define	EXIT_FAILURE	1
 87#define	EXIT_SUCCESS	0
 88
 89#define	RAND_MAX	0x7fffffff
 90
 91extern size_t __mb_cur_max;
 92#define	MB_CUR_MAX	__mb_cur_max
 93
 94__BEGIN_DECLS
 95__dead	 void _Exit(int);
 96__dead	 void abort(void);
 97__constfunc	int abs(int);
 98int	 atexit(void (*)(void));
 99double	 atof(const char *);
100int	 atoi(const char *);
101long	 atol(const char *);
102#ifndef __BSEARCH_DECLARED
103#define __BSEARCH_DECLARED
104/* also in search.h */
105void	*bsearch(const void *, const void *, size_t, size_t,
106    int (*)(const void *, const void *));
107#endif /* __BSEARCH_DECLARED */
108void	*calloc(size_t, size_t);
109div_t	 div(int, int);
110__dead	 void exit(int);
111void	 free(void *);
112__aconst char *getenv(const char *);
113__constfunc long
114	 labs(long);
115ldiv_t	 ldiv(long, long);
116void	*malloc(size_t);
117void	 qsort(void *, size_t, size_t, int (*)(const void *, const void *));
118int	 rand(void);
119void	*realloc(void *, size_t);
120void	 srand(unsigned);
121double	 strtod(const char * __restrict, char ** __restrict);
122long	 strtol(const char * __restrict, char ** __restrict, int);
123unsigned long
124	 strtoul(const char * __restrict, char ** __restrict, int);
125#ifdef _OPENBSD_SOURCE
126long long strtonum(const char *, long long, long long, const char **);
127#endif
128int	 system(const char *);
129
130/* These are currently just stubs. */
131int	 mblen(const char *, size_t);
132size_t	 mbstowcs(wchar_t * __restrict, const char * __restrict, size_t);
133int	 wctomb(char *, wchar_t);
134int	 mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
135size_t	 wcstombs(char * __restrict, const wchar_t * __restrict, size_t);
136
137#if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) || \
138    defined(_NETBSD_SOURCE)
139
140
141/*
142 * IEEE Std 1003.1c-95, also adopted by X/Open CAE Spec Issue 5 Version 2
143 */
144#if (_POSIX_C_SOURCE - 0) >= 199506L || (_XOPEN_SOURCE - 0) >= 500 || \
145    defined(_REENTRANT) || defined(_NETBSD_SOURCE)
146int	 rand_r(unsigned int *);
147#endif
148
149
150/*
151 * X/Open Portability Guide >= Issue 4
152 */
153#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
154double	 drand48(void);
155double	 erand48(unsigned short[3]);
156long	 jrand48(unsigned short[3]);
157void	 lcong48(unsigned short[7]);
158long	 lrand48(void);
159long	 mrand48(void);
160long	 nrand48(unsigned short[3]);
161unsigned short *
162	 seed48(unsigned short[3]);
163void	 srand48(long);
164
165#ifndef __LIBC12_SOURCE__
166int	 putenv(char *) __RENAME(__putenv50);
167#endif
168#endif
169
170
171/*
172 * X/Open Portability Guide >= Issue 4 Version 2
173 */
174#if (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
175    (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
176long	 a64l(const char *);
177char	*l64a(long);
178
179long	 random(void);
180char	*setstate(char *);
181#ifndef __LIBC12_SOURCE__
182char	*initstate(unsigned int, char *, size_t) __RENAME(__initstate60);
183void	 srandom(unsigned int) __RENAME(__srandom60);
184#endif
185#ifdef _NETBSD_SOURCE
186#define	RANDOM_MAX	0x7fffffff	/* (((long)1 << 31) - 1) */
187int	 mkostemp(char *, int);
188int	 mkostemps(char *, int, int);
189#endif
190
191char	*mktemp(char *)
192#ifdef __MKTEMP_OK__
193	__RENAME(_mktemp)
194#endif
195	;
196
197int	 setkey(const char *);
198
199char	*realpath(const char * __restrict, char * __restrict);
200
201int	 ttyslot(void);
202
203void	*valloc(size_t);		/* obsoleted by malloc() */
204
205int	 grantpt(int);
206int	 unlockpt(int);
207char	*ptsname(int);
208#endif
209
210/*
211 * ISO C99
212 */
213#if defined(_ISOC99_SOURCE) || (__STDC_VERSION__ - 0) >= 199901L || \
214    defined(_NETBSD_SOURCE) || (__cplusplus - 0) >= 201103L
215
216/* LONGLONG */
217long long int	atoll(const char *);
218/* LONGLONG */
219long long int	llabs(long long int);
220/* LONGLONG */
221lldiv_t		lldiv(long long int, long long int);
222/* LONGLONG */
223long long int	strtoll(const char * __restrict, char ** __restrict, int);
224/* LONGLONG */
225unsigned long long int
226		strtoull(const char * __restrict, char ** __restrict, int);
227float		strtof(const char * __restrict, char ** __restrict);
228long double	strtold(const char * __restrict, char ** __restrict);
229#endif
230
231#if defined(_ISOC11_SOURCE) || (__STDC_VERSION__ - 0) >= 201101L || \
232    defined(_NETBSD_SOURCE) || (__cplusplus - 0) >= 201103L
233void	*aligned_alloc(size_t, size_t);
234int	at_quick_exit(void (*)(void));
235__dead void quick_exit(int);
236#endif
237
238/*
239 * The Open Group Base Specifications, Issue 6; IEEE Std 1003.1-2001 (POSIX)
240 */
241#if (_POSIX_C_SOURCE - 0) >= 200112L || (_XOPEN_SOURCE - 0) >= 600 || \
242    defined(_NETBSD_SOURCE)
243int	 setenv(const char *, const char *, int);
244#ifndef __LIBC12_SOURCE__
245int	 unsetenv(const char *) __RENAME(__unsetenv13);
246#endif
247
248int	 posix_openpt(int);
249int	 posix_memalign(void **, size_t, size_t);
250#endif
251
252/*
253 * The Open Group Base Specifications, Issue 7; IEEE Std 1003.1-2008 (POSIX)
254 *   or
255 * X/Open Portability Guide >= Issue 4 Version 2
256 */
257#if (_POSIX_C_SOURCE - 0) >= 200809L || \
258    (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
259    (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
260char	*mkdtemp(char *);
261int	 mkstemp(char *);
262
263int	 getsubopt(char **, char * const *, char **);
264#endif
265
266/*
267 * Implementation-defined extensions
268 */
269#if defined(_NETBSD_SOURCE)
270#if defined(__PCC__) && !defined(__GNUC__)
271#define alloca(size) __builtin_alloca(size)
272#else
273void	*alloca(size_t);
274#endif /* __GNUC__ */
275
276uint32_t arc4random(void);
277void	 arc4random_stir(void);
278void	 arc4random_buf(void *, size_t);
279uint32_t arc4random_uniform(uint32_t);
280void	 arc4random_addrandom(unsigned char *, int);
281char	*getbsize(int *, long *);
282char	*cgetcap(char *, const char *, int);
283int	 cgetclose(void);
284int	 cgetent(char **, const char * const *, const char *);
285int	 cgetfirst(char **, const char * const *);
286int	 cgetmatch(const char *, const char *);
287int	 cgetnext(char **, const char * const *);
288int	 cgetnum(char *, const char *, long *);
289int	 cgetset(const char *);
290int	 cgetstr(char *, const char *, char **);
291int	 cgetustr(char *, const char *, char **);
292void	 csetexpandtc(int);
293
294int	 daemon(int, int);
295int	 devname_r(dev_t, mode_t, char *, size_t);
296#ifndef __LIBC12_SOURCE__
297__aconst char *devname(dev_t, mode_t) __RENAME(__devname50);
298#endif
299
300#define	HN_DECIMAL		0x01
301#define	HN_NOSPACE		0x02
302#define	HN_B			0x04
303#define	HN_DIVISOR_1000		0x08
304
305#define	HN_GETSCALE		0x10
306#define	HN_AUTOSCALE		0x20
307
308int	 humanize_number(char *, size_t, int64_t, const char *, int, int);
309int	 dehumanize_number(const char *, int64_t *);
310ssize_t	 hmac(const char *, const void *, size_t, const void *, size_t, void *,
311   size_t);
312
313devmajor_t getdevmajor(const char *, mode_t);
314int	 getloadavg(double [], int);
315
316int	 getenv_r(const char *, char *, size_t);
317
318void	 cfree(void *);
319
320int	 heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
321int	 mergesort(void *, size_t, size_t,
322	    int (*)(const void *, const void *));
323int	 ptsname_r(int, char *, size_t);
324int	 radixsort(const unsigned char **, int, const unsigned char *,
325	    unsigned);
326int	 sradixsort(const unsigned char **, int, const unsigned char *,
327	    unsigned);
328
329void	 mi_vector_hash(const void * __restrict, size_t, uint32_t,
330	    uint32_t[3]);
331
332void	 setproctitle(const char *, ...)
333	    __printflike(1, 2);
334const char *getprogname(void) __constfunc;
335void	setprogname(const char *);
336
337quad_t	 qabs(quad_t);
338quad_t	 strtoq(const char * __restrict, char ** __restrict, int);
339u_quad_t strtouq(const char * __restrict, char ** __restrict, int);
340
341	/* LONGLONG */
342long long strsuftoll(const char *, const char *, long long, long long);
343	/* LONGLONG */
344long long strsuftollx(const char *, const char *, long long, long long,
345	    		char *, size_t);
346
347int	 l64a_r(long, char *, int);
348
349size_t	shquote(const char *, char *, size_t);
350size_t	shquotev(int, char * const *, char *, size_t);
351
352int	reallocarr(void *, size_t, size_t);
353#endif /* _NETBSD_SOURCE */
354#endif /* _POSIX_C_SOURCE || _XOPEN_SOURCE || _NETBSD_SOURCE */
355
356#if defined(_NETBSD_SOURCE)
357qdiv_t	 qdiv(quad_t, quad_t);
358#endif
359
360#if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE)
361#  ifndef __LOCALE_T_DECLARED
362typedef struct _locale		*locale_t;
363#  define __LOCALE_T_DECLARED
364#  endif
365double		strtod_l(const char * __restrict, char ** __restrict, locale_t);
366float		strtof_l(const char * __restrict, char ** __restrict, locale_t);
367long double	strtold_l(const char * __restrict, char ** __restrict,
368			  locale_t);
369long	 strtol_l(const char * __restrict, char ** __restrict, int, locale_t);
370unsigned long
371	 strtoul_l(const char * __restrict, char ** __restrict, int, locale_t);
372/* LONGLONG */
373long long int
374	strtoll_l(const char * __restrict, char ** __restrict, int, locale_t);
375/* LONGLONG */
376unsigned long long int
377	strtoull_l(const char * __restrict, char ** __restrict, int, locale_t);
378
379#  if defined(_NETBSD_SOURCE)
380quad_t	 strtoq_l(const char * __restrict, char ** __restrict, int, locale_t);
381u_quad_t strtouq_l(const char * __restrict, char ** __restrict, int, locale_t);
382
383size_t	_mb_cur_max_l(locale_t);
384#define	MB_CUR_MAX_L(loc)	_mb_cur_max_l(loc)
385int	 mblen_l(const char *, size_t, locale_t);
386size_t	 mbstowcs_l(wchar_t * __restrict, const char * __restrict, size_t,
387		    locale_t);
388int	 wctomb_l(char *, wchar_t, locale_t);
389int	 mbtowc_l(wchar_t * __restrict, const char * __restrict, size_t,
390	          locale_t);
391size_t	 wcstombs_l(char * __restrict, const wchar_t * __restrict, size_t,
392		    locale_t);
393
394#  endif /* _NETBSD_SOURCE */
395#endif /* _POSIX_C_SOURCE >= 200809 || _NETBSD_SOURCE */
396
397#if (_POSIX_C_SOURCE - 0) >= 202405L || \
398    defined(_NETBSD_SOURCE) || defined(_OPENBSD_SOURCE)
399void	*reallocarray(void *, size_t, size_t);
400#endif	/* _POSIX_C_SOURCE >= 202405L || _NETBSD_SOURCE || _OPENBSD_SOURCE */
401
402__END_DECLS
403
404#endif /* !_STDLIB_H_ */