portable.h
1/*
2 * Multi-platform support.
3 */
4
5#ifndef _OKSH_PORTABLE_H_
6#define _OKSH_PORTABLE_H_
7
8/*
9 * Includes
10 */
11
12#if defined(__linux__) || defined(__CYGWIN__) || defined(__midipix__)
13#include <sys/file.h>
14#include <sys/types.h>
15
16#include <grp.h>
17#include <stdint.h>
18#include <stdlib.h>
19#endif /* __linux__ || __CYGWIN__ || __midipix__ */
20
21#include <sys/param.h>
22#include <sys/time.h>
23
24#ifdef __APPLE__
25#include <mach/clock.h>
26#include <mach/mach.h>
27#endif /* __APPLE__ */
28
29#if defined(_AIX) || defined(__sun)
30#include <sys/file.h>
31#endif /* _AIX || __sun */
32
33#if defined(__HAIKU__)
34#include <stdint.h>
35#endif
36
37#include <time.h>
38
39#include "pconfig.h"
40
41/*
42 * Defines
43 */
44
45#ifndef CHILD_MAX
46#define CHILD_MAX 80
47#endif /* !CHILD_MAX */
48
49#ifndef O_EXLOCK
50#define O_EXLOCK 0
51#endif /* !O_EXLOCK */
52
53#ifndef _PATH_BSHELL
54#define _PATH_BSHELL "/bin/sh"
55#endif /* !_PATH_BSHELL */
56
57#ifndef _PW_NAME_LEN
58#if defined(__linux__) || defined(__CYGWIN__) || defined(_AIX) || defined(__midipix__) || defined(__HAIKU__)
59#define _PW_NAME_LEN LOGIN_NAME_MAX
60#elif defined(__NetBSD__)
61#define _PW_NAME_LEN MAXLOGNAME
62#elif defined(__sun)
63#define _PW_NAME_LEN LOGNAME_MAX
64#elif defined(__hpux)
65#define _PW_NAME_LEN 8
66#else
67#define _PW_NAME_LEN MAXLOGNAME - 1
68#endif /* __linux__ || __CYGWIN__ || _AIX || __NetBSD__ || __sun || __midipix__ || __HAIKU__ */
69#endif /* !_PW_NAME_LEN */
70
71#ifndef LOCK_EX
72#define LOCK_EX 0x02
73#endif /* !LOCK_EX */
74
75#ifndef LOCK_UN
76#define LOCK_UN 0x08
77#endif /* !LOCK_UN */
78
79#ifndef RLIMIT_RSS
80#define RLIMIT_RSS 5 /* resident set size */
81#endif /* !RLIMIT_RSS */
82
83#ifndef RLIMIT_MEMLOCK
84#define RLIMIT_MEMLOCK 6 /* locked-in-memory address space */
85#endif /* !RLIMIT_MEMLOCK */
86
87#ifndef RLIMIT_NPROC
88#define RLIMIT_NPROC 7 /* number of processes */
89#endif /* !RLIMIT_NPROC */
90
91/* Convert clock_gettime() to clock_get_time() on Max OS X < 10.12 */
92#if defined(__APPLE__) && defined(__MACH__) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
93#define clock_gettime(x, y) \
94 clock_serv_t cclock; \
95 mach_timespec_t mts; \
96 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); \
97 clock_get_time(cclock, &mts); \
98 mach_port_deallocate(mach_task_self(), cclock); \
99 (y)->tv_sec = mts.tv_sec; \
100 (y)->tv_nsec = mts.tv_nsec;
101#endif /* __APPLE__ && __MACH__ && < 10.12 */
102
103#ifdef _AIX
104#define VWERASE VWERSE
105#define VDISCARD VDISCRD
106#define _PATH_DEFPATH "/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin"
107#define WCOREFLAG 0200
108#define WCOREDUMP(x) ((x) & WCOREFLAG)
109#undef BAD
110#endif /* _AIX */
111
112#ifdef __HAIKU__
113#define _PATH_DEFPATH ".:/boot/home/config/non-packaged/bin:/boot/home/config/bin:/boot/system/non-packaged/bin:/bin:/boot/system/apps:/boot/system/preferences"
114#define WCOREFLAG 0200
115#define WCOREDUMP(x) ((x) & WCOREFLAG)
116#define nice(x) (int)0
117#endif /* __HAIKU__ */
118
119#ifndef HAVE_SETRESGID
120#define setresgid(x, y, z) setgid(x); setegid(y); setgid(z)
121#endif /* !HAVE_SETRESGID */
122
123#ifndef HAVE_SETRESUID
124#define setresuid(x, y, z) setuid(x); seteuid(y); setuid(z)
125#endif /* !HAVE_SETRESUID */
126
127#ifndef HAVE_SRAND_DETERMINISTIC
128#define srand_deterministic(x) srand(x)
129#endif /* !HAVE_SRAND_DETERMINISTIC */
130
131#ifndef HAVE_TIMERADD
132#define timeradd(tvp, uvp, vvp) \
133 do { \
134 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
135 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
136 if ((vvp)->tv_usec >= 1000000) { \
137 (vvp)->tv_sec++; \
138 (vvp)->tv_usec -= 1000000; \
139 } \
140 } while (0)
141#endif /* !HAVE_TIMERADD */
142
143#ifndef HAVE_TIMERCLEAR
144#define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
145#endif /* !HAVE_TIMERCLEAR */
146
147#ifndef HAVE_TIMERSUB
148#define timersub(tvp, uvp, vvp) \
149 do { \
150 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
151 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
152 if ((vvp)->tv_usec < 0) { \
153 (vvp)->tv_sec--; \
154 (vvp)->tv_usec += 1000000; \
155 } \
156 } while (0)
157#endif /* !HAVE_TIMERSUB */
158
159/* struct stat compatibility */
160#ifndef HAVE_ST_MTIM
161#ifndef HAVE_ST_MTIMESPEC
162#define st_mtim st_mtime
163#define timespeccmp(tsp, usp, cmp) (tsp) cmp (usp)
164#else
165#define st_mtim st_mtimespec
166#endif /* !HAVE_ST_TIMESPEC */
167#endif /* !HAVE_ST_MTIM */
168
169/* Cygwin already has a sys_signame but we want to use our own */
170#ifdef __CYGWIN__
171#undef sys_signame
172#endif /* __CYGWIN__ */
173
174/* Android is missing _CS_PATH */
175#if defined(__linux__) && defined(__ANDROID__)
176#ifndef _CS_PATH
177#define _CS_PATH 1
178#endif
179#endif
180
181/* From OpenBSD sys/time.h */
182#ifndef timespeccmp
183#define timespeccmp(tsp, usp, cmp) \
184 (((tsp)->tv_sec == (usp)->tv_sec) ? \
185 ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
186 ((tsp)->tv_sec cmp (usp)->tv_sec))
187#endif
188
189#ifndef timespecsub
190#define timespecsub(tsp, usp, vsp) \
191 do { \
192 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
193 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
194 if ((vsp)->tv_nsec < 0) { \
195 (vsp)->tv_sec--; \
196 (vsp)->tv_nsec += 1000000000L; \
197 } \
198 } while (0)
199#endif
200
201/*
202 * Prototypes
203 */
204
205#ifndef HAVE_ASPRINTF
206int asprintf(char **str, const char *fmt, ...);
207#endif /* !HAVE_ASPRINTF */
208
209#ifndef HAVE_CONFSTR
210size_t confstr(int, char *, size_t);
211#endif /* !HAVE_CONFSTR */
212
213#ifndef HAVE_REALLOCARRAY
214void *reallocarray(void *, size_t, size_t);
215#endif /* !HAVE_REALLOCARRAY */
216
217#ifndef HAVE_STRAVIS
218int stravis(char **, const char *, int);
219#endif /* !HAVE_STRAVIS */
220
221#ifndef HAVE_STRLCAT
222size_t strlcat(char *, const char *, size_t);
223#endif /* !HAVE_STRLCAT */
224
225#ifndef HAVE_STRLCPY
226size_t strlcpy(char *, const char *, size_t);
227#endif /* !HAVE_STRLCPY */
228
229#ifndef HAVE_STRTONUM
230long long strtonum(const char *numstr, long long minval, long long maxval,
231 const char **errstrp);
232#endif /* !HAVE_STRTONUM */
233
234#ifndef HAVE_STRUNVIS
235int strunvis(char *, const char *);
236#endif /* !HAVE_STRUNVIS */
237
238int oksh_issetugid(void);
239
240/*
241 * Externs
242 */
243
244#if !defined(HAVE_SIGLIST) || !defined(HAVE_SIGNAME)
245#ifdef NSIG
246#undef NSIG
247#endif /* NSIG */
248#define NSIG 33
249#ifndef HAVE_SIGLIST
250extern const char *const sys_siglist[NSIG];
251#endif /* !HAVE_SIGLIST */
252#ifndef HAVE_SIGNAME
253extern const char *const sys_signame[NSIG];
254#endif /* !HAVE_SIGNAME */
255#endif /* !HAVE_SIGLIST || !HAVE_SIGNAME */
256
257/*
258 * Types
259 */
260
261#ifndef HAVE_SIG_T
262typedef void (*sig_t) (int);
263#endif /* !HAVE_SIG_T */
264
265#endif /* !_OKSH_PORTABLE_H_ */