xref: /illumos-gate/usr/src/head/stdlib.h (revision f44e1126d9eae71c48c5d1de51e24750c6ec20a4)
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 2023 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	/* !defined(_STRICT_SYMBOLS) || defined(_XPG7) */
209 
210 #if !defined(_STRICT_SYMBOLS)
211 extern int		mkostemp(char *, int);
212 extern int		mkostemps(char *, int, int);
213 #if defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
214 		!defined(__PRAGMA_REDEFINE_EXTNAME))
215 extern int		mkostemp64(char *, int);
216 extern int		mkostemps64(char *, int, int);
217 #endif	/* defined(_LARGEFILE64_SOURCE) || !((_FILE_OFFSET_BITS == 64) ... */
218 #endif	/* !defined(_STRICT_SYMBOLS) */
219 
220 #if defined(__EXTENSIONS__) || \
221 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
222 	defined(_XPG4_2)
223 extern long a64l(const char *);
224 extern char *ecvt(double, int, int *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
225 extern char *fcvt(double, int, int *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
226 extern char *gcvt(double, int, char *);
227 extern int getsubopt(char **, char *const *, char **);
228 extern int  grantpt(int);
229 extern char *initstate(unsigned, char *, size_t);
230 extern char *l64a(long);
231 extern char *mktemp(char *);
232 extern char *ptsname(int);
233 extern long random(void);
234 extern char *realpath(const char *_RESTRICT_KYWD, char *_RESTRICT_KYWD);
235 extern char *setstate(const char *);
236 extern void srandom(unsigned);
237 extern int  unlockpt(int);
238 /* Marked LEGACY in SUSv2 and removed in SUSv3 */
239 #if !defined(_XPG6) || defined(__EXTENSIONS__)
240 extern int ttyslot(void);
241 extern void *valloc(size_t);
242 #endif /* !defined(_XPG6) || defined(__EXTENSIONS__) */
243 #endif /* defined(__EXTENSIONS__) || ... || defined(_XPG4_2) */
244 
245 #if defined(__EXTENSIONS__) || \
246 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
247 	defined(_XPG6)
248 extern int posix_memalign(void **, size_t, size_t);
249 extern int posix_openpt(int);
250 extern int setenv(const char *, const char *, int);
251 extern int unsetenv(const char *);
252 #endif
253 
254 /*
255  * In strict XPG4v2 mode, slave pseudo terminal devices behave differently.
256  * See the block comment in usr/src/lib/libc/port/gen/pt.c
257  */
258 #if defined(_STRICT_SYMBOLS) && defined(_XPG4_2)
259 #ifdef	__PRAGMA_REDEFINE_EXTNAME
260 #pragma redefine_extname unlockpt __unlockpt_xpg4
261 #else
262 extern int __unlockpt_xpg4(int);
263 #define	unlockpt __unlockpt_xpg4
264 #endif
265 #endif	/* defined(_STRICT_SYMBOLS) && defined(_XPG4_2) */
266 
267 #if defined(__EXTENSIONS__) || \
268 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX))
269 extern char *canonicalize_file_name(const char *);
270 extern int clearenv(void);
271 extern void closefrom(int);
272 extern int daemon(int, int);
273 extern int dup2(int, int);
274 extern int dup3(int, int, int);
275 extern int fdwalk(int (*)(void *, int), void *);
276 extern char *qecvt(long double, int, int *, int *);
277 extern char *qfcvt(long double, int, int *, int *);
278 extern char *qgcvt(long double, int, char *);
279 extern char *getcwd(char *, size_t);
280 extern const char *getexecname(void);
281 
282 #ifndef	__GETLOGIN_DEFINED	/* Avoid duplicate in unistd.h */
283 #define	__GETLOGIN_DEFINED
284 #ifndef	__USE_LEGACY_LOGNAME__
285 #ifdef	__PRAGMA_REDEFINE_EXTNAME
286 #pragma	redefine_extname getlogin getloginx
287 #else	/* __PRAGMA_REDEFINE_EXTNAME */
288 extern char *getloginx(void);
289 #define	getlogin	getloginx
290 #endif	/* __PRAGMA_REDEFINE_EXTNAME */
291 #endif	/* __USE_LEGACY_LOGNAME__ */
292 extern char *getlogin(void);
293 #endif	/* __GETLOGIN_DEFINED */
294 
295 extern int getopt(int, char *const *, const char *);
296 extern char *optarg;
297 extern int optind, opterr, optopt;
298 extern char *getpass(const char *);
299 extern char *getpassphrase(const char *);
300 extern int getpw(uid_t, char *);
301 extern int isatty(int);
302 extern void *memalign(size_t, size_t);
303 extern char *ttyname(int);
304 extern char *mkdtemp(char *);
305 extern const char *getprogname(void);
306 extern void setprogname(const char *);
307 
308 #if !defined(_STRICT_STDC) && defined(_LONGLONG_TYPE)
309 extern char *lltostr(long long, char *);
310 extern char *ulltostr(unsigned long long, char *);
311 #endif	/* !defined(_STRICT_STDC) && defined(_LONGLONG_TYPE) */
312 
313 #endif /* defined(__EXTENSIONS__) || !defined(_STRICT_STDC) ... */
314 
315 /* OpenBSD and misc. compatibility functions */
316 #if !defined(_STRICT_SYMBOLS)
317 
318 #include <inttypes.h>
319 extern uint32_t arc4random(void);
320 extern void arc4random_buf(void *, size_t);
321 extern uint32_t arc4random_uniform(uint32_t);
322 extern void freezero(void *, size_t);
323 extern void *reallocarray(void *, size_t, size_t);
324 extern void *recallocarray(void *, size_t, size_t, size_t);
325 extern long long strtonum(const char *, long long, long long, const char **);
326 extern long long strtonumx(const char *, long long, long long, const char **,
327     int);
328 extern void *reallocf(void *, size_t);
329 
330 extern void qsort_r(void *, size_t, size_t,
331     int (*)(const void *, const void *, void *), void *);
332 #endif	/* !_STRICT_SYBMOLS */
333 
334 
335 #ifdef	__cplusplus
336 }
337 #endif
338 
339 #endif	/* _STDLIB_H */
340