1
2
3
4
5
6
7
8#define _ALL_SOURCE
9#ifndef REG_STARTEND
10#define REG_STARTEND 0
11#endif
12
13#ifdef __APPLE__
14
15
16
17#define _DARWIN_C_SOURCE
18
19#define st_atim st_atimespec
20#define st_ctim st_ctimespec
21#define st_mtim st_mtimespec
22#endif
23
24
25
26#ifdef __GNUC__
27#define printf_format __attribute__((format(printf, 1, 2)))
28#else
29#define printf_format
30#endif
31
32
33#define _FILE_OFFSET_BITS 64
34
35
36
37
38
39
40
41#include <sys/types.h>
42
43
44
45#ifndef AT_FDCWD
46#define AT_FDCWD -100
47#endif
48
49#ifndef AT_SYMLINK_NOFOLLOW
50#define AT_SYMLINK_NOFOLLOW 0x100
51#endif
52
53#ifndef AT_REMOVEDIR
54#define AT_REMOVEDIR 0x200
55#endif
56
57#ifndef RLIMIT_RTTIME
58#define RLIMIT_RTTIME 15
59#endif
60
61
62#ifndef SEEK_DATA
63#define SEEK_DATA 3
64#endif
65#ifndef SEEK_HOLE
66#define SEEK_HOLE 4
67#endif
68
69
70
71
72
73
74
75
76
77
78
79#if defined(__GLIBC__)
80
81
82char *crypt(const char *key, const char *salt);
83
84
85
86#include <wchar.h>
87int wcwidth(wchar_t wc);
88
89
90#include <time.h>
91char *strptime(const char *buf, const char *format, struct tm *tm);
92
93
94
95
96
97
98
99
100
101
102char *dirname(char *path);
103char *__xpg_basename(char *path);
104static inline char *basename(char *path) { return __xpg_basename(path); }
105char *strcasestr(const char *haystack, const char *needle);
106#endif
107
108#if !defined(__GLIBC__)
109
110#include <libgen.h>
111#endif
112
113
114
115#ifdef __APPLE__
116
117#include <libkern/OSByteOrder.h>
118
119#ifdef __BIG_ENDIAN__
120#define IS_BIG_ENDIAN 1
121#else
122#define IS_BIG_ENDIAN 0
123#endif
124
125#define bswap_16(x) OSSwapInt16(x)
126#define bswap_32(x) OSSwapInt32(x)
127#define bswap_64(x) OSSwapInt64(x)
128
129#elif defined(__FreeBSD__)
130
131#include <sys/endian.h>
132
133#if _BYTE_ORDER == _BIG_ENDIAN
134#define IS_BIG_ENDIAN 1
135#else
136#define IS_BIG_ENDIAN 0
137#endif
138
139#else
140
141#include <byteswap.h>
142#include <endian.h>
143
144#if __BYTE_ORDER == __BIG_ENDIAN
145#define IS_BIG_ENDIAN 1
146#else
147#define IS_BIG_ENDIAN 0
148#endif
149
150#endif
151
152#if IS_BIG_ENDIAN
153#define IS_LITTLE_ENDIAN 0
154#define SWAP_BE16(x) (x)
155#define SWAP_BE32(x) (x)
156#define SWAP_BE64(x) (x)
157#define SWAP_LE16(x) bswap_16(x)
158#define SWAP_LE32(x) bswap_32(x)
159#define SWAP_LE64(x) bswap_64(x)
160#else
161#define IS_LITTLE_ENDIAN 1
162#define SWAP_BE16(x) bswap_16(x)
163#define SWAP_BE32(x) bswap_32(x)
164#define SWAP_BE64(x) bswap_64(x)
165#define SWAP_LE16(x) (x)
166#define SWAP_LE32(x) (x)
167#define SWAP_LE64(x) (x)
168#endif
169
170
171#include <sys/mount.h>
172#ifdef __linux__
173#include <sys/statfs.h>
174#include <sys/swap.h>
175#include <sys/sysinfo.h>
176#endif
177
178#ifdef __APPLE__
179#include <util.h>
180#elif !defined(__FreeBSD__)
181#include <pty.h>
182#else
183#include <termios.h>
184#ifndef IUTF8
185#define IUTF8 0
186#endif
187#endif
188
189#if defined(__APPLE__) || defined(__linux__)
190
191
192#include <sys/xattr.h>
193ssize_t xattr_get(const char *, const char *, void *, size_t);
194ssize_t xattr_lget(const char *, const char *, void *, size_t);
195ssize_t xattr_fget(int fd, const char *, void *, size_t);
196ssize_t xattr_list(const char *, char *, size_t);
197ssize_t xattr_llist(const char *, char *, size_t);
198ssize_t xattr_flist(int, char *, size_t);
199ssize_t xattr_set(const char*, const char*, const void*, size_t, int);
200ssize_t xattr_lset(const char*, const char*, const void*, size_t, int);
201ssize_t xattr_fset(int, const char*, const void*, size_t, int);
202#endif
203
204
205#ifdef __APPLE__
206int mknodat(int, const char*, mode_t, dev_t);
207#endif
208
209
210
211#if CFG_TOYBOX_SHADOW
212#include <shadow.h>
213#endif
214#if CFG_TOYBOX_UTMPX
215#include <utmpx.h>
216#else
217struct utmpx {int ut_type;};
218#define USER_PROCESS 0
219static inline struct utmpx *getutxent(void) {return 0;}
220static inline void setutxent(void) {;}
221static inline void endutxent(void) {;}
222#endif
223
224
225#include <fcntl.h>
226#ifndef O_NOFOLLOW
227#define O_NOFOLLOW 0
228#endif
229#ifndef O_NOATIME
230#define O_NOATIME 01000000
231#endif
232#ifndef O_CLOEXEC
233#define O_CLOEXEC 02000000
234#endif
235#ifndef O_PATH
236#define O_PATH 010000000
237#endif
238#ifndef SCHED_RESET_ON_FORK
239#define SCHED_RESET_ON_FORK (1<<30)
240#endif
241
242
243
244#ifndef F_SETPIPE_SZ
245#define F_SETPIPE_SZ 1031
246#endif
247
248#ifndef F_GETPIPE_SZ
249#define F_GETPIPE_SZ 1032
250#endif
251
252#if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
253 && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
254typedef double FLOAT;
255#else
256typedef float FLOAT;
257#endif
258
259#ifndef __uClinux__
260pid_t xfork(void);
261#endif
262
263
264
265
266
267
268#ifdef __BIONIC__
269#include <android/log.h>
270#else
271typedef enum android_LogPriority {
272 ANDROID_LOG_UNKNOWN = 0,
273 ANDROID_LOG_DEFAULT,
274 ANDROID_LOG_VERBOSE,
275 ANDROID_LOG_DEBUG,
276 ANDROID_LOG_INFO,
277 ANDROID_LOG_WARN,
278 ANDROID_LOG_ERROR,
279 ANDROID_LOG_FATAL,
280 ANDROID_LOG_SILENT,
281} android_LogPriority;
282static inline int __android_log_write(int pri, const char *tag, const char *msg)
283{
284 return -1;
285}
286#endif
287
288
289#if defined(__BIONIC__) && __has_include(<processgroup/sched_policy.h>)
290#include <processgroup/sched_policy.h>
291#else
292static inline int get_sched_policy(int tid, void *policy) {return 0;}
293static inline char *get_sched_policy_name(int policy) {return "unknown";}
294#endif
295
296
297
298#ifdef __ANDROID_NDK__
299#define __android_log_write(a, b, c) (0)
300#define adjtime(x, y) (0)
301#endif
302
303#ifndef SYSLOG_NAMES
304typedef struct {char *c_name; int c_val;} CODE;
305extern CODE prioritynames[], facilitynames[];
306#endif
307
308#if CFG_TOYBOX_GETRANDOM
309#include <sys/random.h>
310#endif
311int xgetrandom(void *buf, unsigned len, unsigned flags);
312
313
314#ifdef __BIONIC__
315#define _CS_PATH 0
316#define _CS_V7_ENV 1
317#include <string.h>
318static inline void confstr(int a, char *b, int c) {strcpy(b, a ? "POSIXLY_CORRECT=1" : "/bin:/usr/bin");}
319#endif
320
321
322
323struct xnotify {
324 char **paths;
325 int max, *fds, count, kq;
326};
327
328struct xnotify *xnotify_init(int max);
329int xnotify_add(struct xnotify *not, int fd, char *path);
330int xnotify_wait(struct xnotify *not, char **path);
331
332#ifdef __APPLE__
333#define f_frsize f_iosize
334#endif
335
336int sig_to_num(char *s);
337char *num_to_sig(int sig);
338
339struct signame {
340 int num;
341 char *name;
342};
343void xsignal_all_killers(void *handler);
344