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 */
33
34/*	Copyright (c) 1988 AT&T	*/
35/*	  All Rights Reserved	*/
36
37#ifndef _STDLIB_H
38#define	_STDLIB_H
39
40#include <iso/stdlib_iso.h>
41#include <iso/stdlib_c99.h>
42#include <iso/stdlib_c11.h>
43
44#if defined(__EXTENSIONS__) || defined(_XPG4)
45#include <sys/wait.h>
46#endif
47
48/*
49 * Allow global visibility for symbols defined in
50 * C++ "std" namespace in <iso/stdlib_iso.h>.
51 */
52#if __cplusplus >= 199711L
53using std::div_t;
54using std::ldiv_t;
55using std::size_t;
56using std::abort;
57using std::abs;
58using std::atexit;
59using std::atof;
60using std::atoi;
61using std::atol;
62using std::bsearch;
63using std::calloc;
64using std::div;
65using std::exit;
66using std::free;
67using std::getenv;
68using std::labs;
69using std::ldiv;
70using std::malloc;
71using std::mblen;
72using std::mbstowcs;
73using std::mbtowc;
74using std::qsort;
75using std::rand;
76using std::realloc;
77using std::srand;
78using std::strtod;
79using std::strtol;
80using std::strtoul;
81using std::system;
82using std::wcstombs;
83using std::wctomb;
84#endif
85
86/*
87 * Allow global visibility for symbols defined in
88 * C++ "std" namespace in <iso/stdlib_c11.h>.
89 */
90#if __cplusplus >= 201103L
91using std::at_quick_exit;
92using std::quick_exit;
93#endif
94#if __cplusplus >= 201703L
95using std::aligned_alloc;
96#endif
97
98#ifdef	__cplusplus
99extern "C" {
100#endif
101
102#ifndef _UID_T
103#define	_UID_T
104typedef	unsigned int	uid_t;		/* UID type		*/
105#endif	/* !_UID_T */
106
107/* large file compilation environment setup */
108#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
109
110#ifdef	__PRAGMA_REDEFINE_EXTNAME
111#pragma redefine_extname	mkstemp		mkstemp64
112#pragma redefine_extname	mkstemps	mkstemps64
113#pragma	redefine_extname	mkostemp	mkostemp64
114#pragma	redefine_extname	mkostemps	mkostemps64
115#else	/* __PRAGMA_REDEFINE_EXTNAME */
116#define	mkstemp			mkstemp64
117#define	mkstemps		mkstemps64
118#define	mkostemp		mkostemp64
119#define	mkostemps		mkostemps64
120#endif	/* __PRAGMA_REDEFINE_EXTNAME */
121
122#endif	/* _FILE_OFFSET_BITS == 64 */
123
124/* In the LP64 compilation environment, all APIs are already large file */
125#if defined(_LP64) && defined(_LARGEFILE64_SOURCE)
126
127#ifdef	__PRAGMA_REDEFINE_EXTNAME
128#pragma redefine_extname	mkstemp64	mkstemp
129#pragma redefine_extname	mkstemps64	mkstemps
130#pragma	redefine_extname	mkostemp64	mkostemp
131#pragma	redefine_extname	mkostemps64	mkostemps
132#else	/* __PRAGMA_REDEFINE_EXTNAME */
133#define	mkstemp64		mkstemp
134#define	mkstemps64		mkstemps
135#define	mkostemp64		mkostemp
136#define	mkostemps64		mkostemps
137#endif	/* __PRAGMA_REDEFINE_EXTNAME */
138
139#endif	/* _LP64 && _LARGEFILE64_SOURCE */
140
141#if defined(__EXTENSIONS__) || \
142	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
143	(_POSIX_C_SOURCE - 0 >= 199506L) || defined(_REENTRANT)
144extern int rand_r(unsigned int *);
145#endif
146
147extern void _exithandle(void);
148
149#if defined(__EXTENSIONS__) || \
150	(!defined(_STRICT_STDC) && !defined(_POSIX_C_SOURCE)) || \
151	defined(_XPG4)
152extern double drand48(void);
153extern double erand48(unsigned short *);
154extern long jrand48(unsigned short *);
155extern void lcong48(unsigned short *);
156extern long lrand48(void);
157extern long mrand48(void);
158extern long nrand48(unsigned short *);
159extern unsigned short *seed48(unsigned short *);
160extern void srand48(long);
161extern int putenv(char *);
162extern void setkey(const char *);
163#endif /* defined(__EXTENSIONS__) || !defined(_STRICT_STDC) ... */
164
165/*
166 * swab() has historically been in <stdlib.h> as delivered from AT&T
167 * and continues to be visible in the default compilation environment.
168 * As of Issue 4 of the X/Open Portability Guides, swab() was declared
169 * in <unistd.h>. As a result, with respect to X/Open namespace the
170 * swab() declaration in this header is only visible for the XPG3
171 * environment.
172 */
173#if (defined(__EXTENSIONS__) || \
174	(!defined(_STRICT_STDC__) && !defined(_POSIX_C_SOURCE))) && \
175	(!defined(_XOPEN_SOURCE) || (defined(_XPG3) && !defined(_XPG4)))
176#ifndef	_SSIZE_T
177#define	_SSIZE_T
178#if defined(_LP64) || defined(_I32LPx)
179typedef long	ssize_t;	/* size of something in bytes or -1 */
180#else
181typedef int	ssize_t;	/* (historical version) */
182#endif
183#endif	/* !_SSIZE_T */
184
185extern void swab(const char *, char *, ssize_t);
186#endif /* defined(__EXTENSIONS__) || !defined(_STRICT_STDC) ... */
187
188#if defined(__EXTENSIONS__) || \
189	!defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
190	(defined(_LARGEFILE_SOURCE) && _FILE_OFFSET_BITS == 64)
191extern int	mkstemp(char *);
192#if !defined(_XPG4_2) || defined(__EXTENSIONS__)
193extern int	mkstemps(char *, int);
194#endif
195#endif /* defined(__EXTENSIONS__) ... */
196
197#if	defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
198	    !defined(__PRAGMA_REDEFINE_EXTNAME))
199extern int	mkstemp64(char *);
200#if !defined(_XPG4_2) || defined(__EXTENSIONS__)
201extern int	mkstemps64(char *, int);
202#endif
203#endif	/* _LARGEFILE64_SOURCE... */
204
205#if !defined(_STRICT_SYMBOLS) || defined(_XPG7)
206extern char	*mkdtemp(char *);
207#endif	/* !defined(_STRICT_SYMBOLS) || defined(_XPG7) */
208
209#if !defined(_STRICT_SYMBOLS)
210extern int		mkostemp(char *, int);
211extern int		mkostemps(char *, int, int);
212#if defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
213		!defined(__PRAGMA_REDEFINE_EXTNAME))
214extern int		mkostemp64(char *, int);
215extern int		mkostemps64(char *, int, int);
216#endif	/* defined(_LARGEFILE64_SOURCE) || !((_FILE_OFFSET_BITS == 64) ... */
217#endif	/* !defined(_STRICT_SYMBOLS) */
218
219#if defined(__EXTENSIONS__) || \
220	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
221	defined(_XPG4_2)
222extern long a64l(const char *);
223extern char *ecvt(double, int, int *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
224extern char *fcvt(double, int, int *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
225extern char *gcvt(double, int, char *);
226extern int getsubopt(char **, char *const *, char **);
227extern int  grantpt(int);
228extern char *initstate(unsigned, char *, size_t);
229extern char *l64a(long);
230extern char *mktemp(char *);
231extern char *ptsname(int);
232extern long random(void);
233extern char *realpath(const char *_RESTRICT_KYWD, char *_RESTRICT_KYWD);
234extern char *setstate(const char *);
235extern void srandom(unsigned);
236extern int  unlockpt(int);
237/* Marked LEGACY in SUSv2 and removed in SUSv3 */
238#if !defined(_XPG6) || defined(__EXTENSIONS__)
239extern int ttyslot(void);
240extern void *valloc(size_t);
241#endif /* !defined(_XPG6) || defined(__EXTENSIONS__) */
242#endif /* defined(__EXTENSIONS__) || ... || defined(_XPG4_2) */
243
244#if defined(__EXTENSIONS__) || \
245	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
246	defined(_XPG6)
247extern int posix_memalign(void **, size_t, size_t);
248extern int posix_openpt(int);
249extern int setenv(const char *, const char *, int);
250extern int unsetenv(const char *);
251#endif
252
253/*
254 * In strict XPG4v2 mode, slave pseudo terminal devices behave differently.
255 * See the block comment in usr/src/lib/libc/port/gen/pt.c
256 */
257#if defined(_STRICT_SYMBOLS) && defined(_XPG4_2)
258#ifdef	__PRAGMA_REDEFINE_EXTNAME
259#pragma redefine_extname unlockpt __unlockpt_xpg4
260#else
261extern int __unlockpt_xpg4(int);
262#define	unlockpt __unlockpt_xpg4
263#endif
264#endif	/* defined(_STRICT_SYMBOLS) && defined(_XPG4_2) */
265
266#if defined(__EXTENSIONS__) || \
267	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX))
268extern char *canonicalize_file_name(const char *);
269extern int clearenv(void);
270extern void closefrom(int);
271extern int daemon(int, int);
272extern int dup2(int, int);
273extern int dup3(int, int, int);
274extern int fdwalk(int (*)(void *, int), void *);
275extern char *qecvt(long double, int, int *, int *);
276extern char *qfcvt(long double, int, int *, int *);
277extern char *qgcvt(long double, int, char *);
278extern char *getcwd(char *, size_t);
279extern const char *getexecname(void);
280
281#ifndef	__GETLOGIN_DEFINED	/* Avoid duplicate in unistd.h */
282#define	__GETLOGIN_DEFINED
283#ifndef	__USE_LEGACY_LOGNAME__
284#ifdef	__PRAGMA_REDEFINE_EXTNAME
285#pragma	redefine_extname getlogin getloginx
286#else	/* __PRAGMA_REDEFINE_EXTNAME */
287extern char *getloginx(void);
288#define	getlogin	getloginx
289#endif	/* __PRAGMA_REDEFINE_EXTNAME */
290#endif	/* __USE_LEGACY_LOGNAME__ */
291extern char *getlogin(void);
292#endif	/* __GETLOGIN_DEFINED */
293
294extern int getopt(int, char *const *, const char *);
295extern char *optarg;
296extern int optind, opterr, optopt;
297extern char *getpass(const char *);
298extern char *getpassphrase(const char *);
299extern int getpw(uid_t, char *);
300extern int isatty(int);
301extern void *memalign(size_t, size_t);
302extern char *ttyname(int);
303extern char *mkdtemp(char *);
304extern const char *getprogname(void);
305extern void setprogname(const char *);
306
307#if !defined(_STRICT_STDC) && defined(_LONGLONG_TYPE)
308extern char *lltostr(long long, char *);
309extern char *ulltostr(unsigned long long, char *);
310#endif	/* !defined(_STRICT_STDC) && defined(_LONGLONG_TYPE) */
311
312#endif /* defined(__EXTENSIONS__) || !defined(_STRICT_STDC) ... */
313
314/* OpenBSD and misc. compatibility functions */
315#if !defined(_STRICT_SYMBOLS)
316
317#include <inttypes.h>
318extern uint32_t arc4random(void);
319extern void arc4random_buf(void *, size_t);
320extern uint32_t arc4random_uniform(uint32_t);
321extern void freezero(void *, size_t);
322extern void *reallocarray(void *, size_t, size_t);
323extern void *recallocarray(void *, size_t, size_t, size_t);
324extern long long strtonum(const char *, long long, long long, const char **);
325extern void *reallocf(void *, size_t);
326
327extern void qsort_r(void *, size_t, size_t,
328    int (*)(const void *, const void *, void *), void *);
329#endif	/* !_STRICT_SYBMOLS */
330
331
332#ifdef	__cplusplus
333}
334#endif
335
336#endif	/* _STDLIB_H */
337