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