xref: /illumos-gate/usr/src/head/stdlib.h (revision 7216809591be497087c98047f2a145b1dcc5873d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
24  * Copyright (c) 2013 Gary Mills
25  *
26  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27  */
28 
29 /*
30  * Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
31  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
32  * Copyright 2025 Oxide Computer Company
33  */
34 
35 /*	Copyright (c) 1988 AT&T	*/
36 /*	  All Rights Reserved	*/
37 
38 #ifndef _STDLIB_H
39 #define	_STDLIB_H
40 
41 #include <iso/stdlib_iso.h>
42 #include <iso/stdlib_c99.h>
43 #include <iso/stdlib_c11.h>
44 
45 #if defined(__EXTENSIONS__) || defined(_XPG4)
46 #include <sys/wait.h>
47 #endif
48 
49 /*
50  * Allow global visibility for symbols defined in
51  * C++ "std" namespace in <iso/stdlib_iso.h>.
52  */
53 #if __cplusplus >= 199711L
54 using std::div_t;
55 using std::ldiv_t;
56 using std::size_t;
57 using std::abort;
58 using std::abs;
59 using std::atexit;
60 using std::atof;
61 using std::atoi;
62 using std::atol;
63 using std::bsearch;
64 using std::calloc;
65 using std::div;
66 using std::exit;
67 using std::free;
68 using std::getenv;
69 using std::labs;
70 using std::ldiv;
71 using std::malloc;
72 using std::mblen;
73 using std::mbstowcs;
74 using std::mbtowc;
75 using std::qsort;
76 using std::rand;
77 using std::realloc;
78 using std::srand;
79 using std::strtod;
80 using std::strtol;
81 using std::strtoul;
82 using std::system;
83 using std::wcstombs;
84 using std::wctomb;
85 #endif
86 
87 /*
88  * Allow global visibility for symbols defined in
89  * C++ "std" namespace in <iso/stdlib_c11.h>.
90  */
91 #if __cplusplus >= 201103L
92 using std::at_quick_exit;
93 using std::quick_exit;
94 #endif
95 #if __cplusplus >= 201703L
96 using std::aligned_alloc;
97 #endif
98 
99 #ifdef	__cplusplus
100 extern "C" {
101 #endif
102 
103 #ifndef _UID_T
104 #define	_UID_T
105 typedef	unsigned int	uid_t;		/* UID type		*/
106 #endif	/* !_UID_T */
107 
108 /* large file compilation environment setup */
109 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
110 
111 #ifdef	__PRAGMA_REDEFINE_EXTNAME
112 #pragma redefine_extname	mkstemp		mkstemp64
113 #pragma redefine_extname	mkstemps	mkstemps64
114 #pragma	redefine_extname	mkostemp	mkostemp64
115 #pragma	redefine_extname	mkostemps	mkostemps64
116 #else	/* __PRAGMA_REDEFINE_EXTNAME */
117 #define	mkstemp			mkstemp64
118 #define	mkstemps		mkstemps64
119 #define	mkostemp		mkostemp64
120 #define	mkostemps		mkostemps64
121 #endif	/* __PRAGMA_REDEFINE_EXTNAME */
122 
123 #endif	/* _FILE_OFFSET_BITS == 64 */
124 
125 /* In the LP64 compilation environment, all APIs are already large file */
126 #if defined(_LP64) && defined(_LARGEFILE64_SOURCE)
127 
128 #ifdef	__PRAGMA_REDEFINE_EXTNAME
129 #pragma redefine_extname	mkstemp64	mkstemp
130 #pragma redefine_extname	mkstemps64	mkstemps
131 #pragma	redefine_extname	mkostemp64	mkostemp
132 #pragma	redefine_extname	mkostemps64	mkostemps
133 #else	/* __PRAGMA_REDEFINE_EXTNAME */
134 #define	mkstemp64		mkstemp
135 #define	mkstemps64		mkstemps
136 #define	mkostemp64		mkostemp
137 #define	mkostemps64		mkostemps
138 #endif	/* __PRAGMA_REDEFINE_EXTNAME */
139 
140 #endif	/* _LP64 && _LARGEFILE64_SOURCE */
141 
142 #if defined(__EXTENSIONS__) || \
143 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
144 	(_POSIX_C_SOURCE - 0 >= 199506L) || defined(_REENTRANT)
145 extern int rand_r(unsigned int *);
146 #endif
147 
148 extern void _exithandle(void);
149 
150 #if defined(__EXTENSIONS__) || \
151 	(!defined(_STRICT_STDC) && !defined(_POSIX_C_SOURCE)) || \
152 	defined(_XPG4)
153 extern double drand48(void);
154 extern double erand48(unsigned short *);
155 extern long jrand48(unsigned short *);
156 extern void lcong48(unsigned short *);
157 extern long lrand48(void);
158 extern long mrand48(void);
159 extern long nrand48(unsigned short *);
160 extern unsigned short *seed48(unsigned short *);
161 extern void srand48(long);
162 extern int putenv(char *);
163 extern void setkey(const char *);
164 #endif /* defined(__EXTENSIONS__) || !defined(_STRICT_STDC) ... */
165 
166 /*
167  * swab() has historically been in <stdlib.h> as delivered from AT&T
168  * and continues to be visible in the default compilation environment.
169  * As of Issue 4 of the X/Open Portability Guides, swab() was declared
170  * in <unistd.h>. As a result, with respect to X/Open namespace the
171  * swab() declaration in this header is only visible for the XPG3
172  * environment.
173  */
174 #if (defined(__EXTENSIONS__) || \
175 	(!defined(_STRICT_STDC__) && !defined(_POSIX_C_SOURCE))) && \
176 	(!defined(_XOPEN_SOURCE) || (defined(_XPG3) && !defined(_XPG4)))
177 #ifndef	_SSIZE_T
178 #define	_SSIZE_T
179 #if defined(_LP64) || defined(_I32LPx)
180 typedef long	ssize_t;	/* size of something in bytes or -1 */
181 #else
182 typedef int	ssize_t;	/* (historical version) */
183 #endif
184 #endif	/* !_SSIZE_T */
185 
186 extern void swab(const char *, char *, ssize_t);
187 #endif /* defined(__EXTENSIONS__) || !defined(_STRICT_STDC) ... */
188 
189 #if defined(__EXTENSIONS__) || \
190 	!defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
191 	(defined(_LARGEFILE_SOURCE) && _FILE_OFFSET_BITS == 64)
192 extern int	mkstemp(char *);
193 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
194 extern int	mkstemps(char *, int);
195 #endif
196 #endif /* defined(__EXTENSIONS__) ... */
197 
198 #if	defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
199 	    !defined(__PRAGMA_REDEFINE_EXTNAME))
200 extern int	mkstemp64(char *);
201 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
202 extern int	mkstemps64(char *, int);
203 #endif
204 #endif	/* _LARGEFILE64_SOURCE... */
205 
206 #if !defined(_STRICT_SYMBOLS) || defined(_XPG7)
207 extern char	*mkdtemp(char *);
208 #endif	/* !_STRICT_SYMBOLS || _XPG7 */
209 
210 #if !defined(_STRICT_SYMBOLS) || defined(_XPG8)
211 extern int		mkostemp(char *, int);
212 #if defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
213 		!defined(__PRAGMA_REDEFINE_EXTNAME))
214 extern int		mkostemp64(char *, int);
215 #endif	/* _LARGEFILE64_SOURCE || !((_FILE_OFFSET_BITS == 64) ... */
216 #endif	/* !_STRICT_SYMBOLS || _XPG8  */
217 
218 #if !defined(_STRICT_SYMBOLS)
219 extern int		mkostemps(char *, int, int);
220 #if defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
221 		!defined(__PRAGMA_REDEFINE_EXTNAME))
222 extern int		mkostemps64(char *, int, int);
223 #endif	/* _LARGEFILE64_SOURCE || !((_FILE_OFFSET_BITS == 64) ... */
224 #endif	/* !_STRICT_SYMBOLS */
225 
226 
227 #if defined(__EXTENSIONS__) || \
228 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
229 	defined(_XPG4_2)
230 extern long a64l(const char *);
231 extern char *ecvt(double, int, int *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
232 extern char *fcvt(double, int, int *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
233 extern char *gcvt(double, int, char *);
234 extern int getsubopt(char **, char *const *, char **);
235 extern int  grantpt(int);
236 extern char *initstate(unsigned, char *, size_t);
237 extern char *l64a(long);
238 extern char *mktemp(char *);
239 extern char *ptsname(int);
240 extern long random(void);
241 extern char *realpath(const char *_RESTRICT_KYWD, char *_RESTRICT_KYWD);
242 extern char *setstate(const char *);
243 extern void srandom(unsigned);
244 extern int  unlockpt(int);
245 /* Marked LEGACY in SUSv2 and removed in SUSv3 */
246 #if !defined(_XPG6) || defined(__EXTENSIONS__)
247 extern int ttyslot(void);
248 extern void *valloc(size_t);
249 #endif /* !defined(_XPG6) || defined(__EXTENSIONS__) */
250 #endif /* defined(__EXTENSIONS__) || ... || defined(_XPG4_2) */
251 
252 #if defined(__EXTENSIONS__) || \
253 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
254 	defined(_XPG6)
255 extern int posix_memalign(void **, size_t, size_t);
256 extern int posix_openpt(int);
257 extern int setenv(const char *, const char *, int);
258 extern int unsetenv(const char *);
259 #endif
260 
261 /*
262  * In strict XPG4v2 mode, slave pseudo terminal devices behave differently.
263  * See the block comment in usr/src/lib/libc/port/gen/pt.c
264  */
265 #if defined(_STRICT_SYMBOLS) && defined(_XPG4_2)
266 #ifdef	__PRAGMA_REDEFINE_EXTNAME
267 #pragma redefine_extname unlockpt __unlockpt_xpg4
268 #else
269 extern int __unlockpt_xpg4(int);
270 #define	unlockpt __unlockpt_xpg4
271 #endif
272 #endif	/* defined(_STRICT_SYMBOLS) && defined(_XPG4_2) */
273 
274 #if defined(__EXTENSIONS__) || \
275 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX))
276 extern char *canonicalize_file_name(const char *);
277 extern int clearenv(void);
278 extern void closefrom(int);
279 extern int daemon(int, int);
280 extern int dup2(int, int);
281 extern int dup3(int, int, int);
282 extern int fdwalk(int (*)(void *, int), void *);
283 extern char *qecvt(long double, int, int *, int *);
284 extern char *qfcvt(long double, int, int *, int *);
285 extern char *qgcvt(long double, int, char *);
286 extern char *getcwd(char *, size_t);
287 extern const char *getexecname(void);
288 
289 #ifndef	__GETLOGIN_DEFINED	/* Avoid duplicate in unistd.h */
290 #define	__GETLOGIN_DEFINED
291 #ifndef	__USE_LEGACY_LOGNAME__
292 #ifdef	__PRAGMA_REDEFINE_EXTNAME
293 #pragma	redefine_extname getlogin getloginx
294 #else	/* __PRAGMA_REDEFINE_EXTNAME */
295 extern char *getloginx(void);
296 #define	getlogin	getloginx
297 #endif	/* __PRAGMA_REDEFINE_EXTNAME */
298 #endif	/* __USE_LEGACY_LOGNAME__ */
299 extern char *getlogin(void);
300 #endif	/* __GETLOGIN_DEFINED */
301 
302 extern int getopt(int, char *const *, const char *);
303 extern char *optarg;
304 extern int optind, opterr, optopt;
305 extern char *getpass(const char *);
306 extern char *getpassphrase(const char *);
307 extern int getpw(uid_t, char *);
308 extern int isatty(int);
309 extern void *memalign(size_t, size_t);
310 extern char *ttyname(int);
311 extern char *mkdtemp(char *);
312 extern const char *getprogname(void);
313 extern void setprogname(const char *);
314 
315 #if !defined(_STRICT_STDC) && defined(_LONGLONG_TYPE)
316 extern char *lltostr(long long, char *);
317 extern char *ulltostr(unsigned long long, char *);
318 #endif	/* !defined(_STRICT_STDC) && defined(_LONGLONG_TYPE) */
319 
320 #endif /* defined(__EXTENSIONS__) || !defined(_STRICT_STDC) ... */
321 
322 /* OpenBSD and misc. compatibility functions */
323 #if !defined(_STRICT_SYMBOLS)
324 
325 #include <inttypes.h>
326 extern uint32_t arc4random(void);
327 extern void arc4random_buf(void *, size_t);
328 extern uint32_t arc4random_uniform(uint32_t);
329 extern void freezero(void *, size_t);
330 extern void *recallocarray(void *, size_t, size_t, size_t);
331 extern long long strtonum(const char *, long long, long long, const char **);
332 extern long long strtonumx(const char *, long long, long long, const char **,
333     int);
334 extern void *reallocf(void *, size_t);
335 #endif	/* !_STRICT_SYBMOLS */
336 
337 /*
338  * POSIX 2024 functions.
339  */
340 #if !defined(_STRICT_SYMBOLS) || defined(_XPG8)
341 extern int ptsname_r(int, char *, size_t);
342 extern void qsort_r(void *, size_t, size_t,
343     int (*)(const void *, const void *, void *), void *);
344 extern void *reallocarray(void *, size_t, size_t);
345 extern char *secure_getenv(const char *);
346 #endif	/* !_STRICT_SYMBOLS || _XPG8 */
347 
348 #ifdef	__cplusplus
349 }
350 #endif
351 
352 #endif	/* _STDLIB_H */
353