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);
106void *memmem(const void *haystack, size_t haystack_length,
107 const void *needle, size_t needle_length);
108#endif
109
110
111#include <getopt.h>
112
113#if !defined(__GLIBC__)
114
115#include <libgen.h>
116#endif
117
118
119
120#ifdef __APPLE__
121
122#include <libkern/OSByteOrder.h>
123
124#ifdef __BIG_ENDIAN__
125#define IS_BIG_ENDIAN 1
126#else
127#define IS_BIG_ENDIAN 0
128#endif
129
130#define bswap_16(x) OSSwapInt16(x)
131#define bswap_32(x) OSSwapInt32(x)
132#define bswap_64(x) OSSwapInt64(x)
133
134#elif defined(__FreeBSD__)
135
136#include <sys/endian.h>
137
138#if _BYTE_ORDER == _BIG_ENDIAN
139#define IS_BIG_ENDIAN 1
140#else
141#define IS_BIG_ENDIAN 0
142#endif
143
144#else
145
146#include <byteswap.h>
147#include <endian.h>
148
149#if __BYTE_ORDER == __BIG_ENDIAN
150#define IS_BIG_ENDIAN 1
151#else
152#define IS_BIG_ENDIAN 0
153#endif
154
155#endif
156
157#if IS_BIG_ENDIAN
158#define IS_LITTLE_ENDIAN 0
159#define SWAP_BE16(x) (x)
160#define SWAP_BE32(x) (x)
161#define SWAP_BE64(x) (x)
162#define SWAP_LE16(x) bswap_16(x)
163#define SWAP_LE32(x) bswap_32(x)
164#define SWAP_LE64(x) bswap_64(x)
165#else
166#define IS_LITTLE_ENDIAN 1
167#define SWAP_BE16(x) bswap_16(x)
168#define SWAP_BE32(x) bswap_32(x)
169#define SWAP_BE64(x) bswap_64(x)
170#define SWAP_LE16(x) (x)
171#define SWAP_LE32(x) (x)
172#define SWAP_LE64(x) (x)
173#endif
174
175
176#include <sys/mount.h>
177#ifdef __linux__
178#include <sys/statfs.h>
179#include <sys/swap.h>
180#include <sys/sysinfo.h>
181#endif
182
183#ifdef __APPLE__
184#include <util.h>
185#elif !defined(__FreeBSD__)
186#include <pty.h>
187#else
188#include <termios.h>
189#ifndef IUTF8
190#define IUTF8 0
191#endif
192#endif
193
194#if defined(__APPLE__) || defined(__linux__)
195
196
197#include <sys/xattr.h>
198ssize_t xattr_get(const char *, const char *, void *, size_t);
199ssize_t xattr_lget(const char *, const char *, void *, size_t);
200ssize_t xattr_fget(int fd, const char *, void *, size_t);
201ssize_t xattr_list(const char *, char *, size_t);
202ssize_t xattr_llist(const char *, char *, size_t);
203ssize_t xattr_flist(int, char *, size_t);
204ssize_t xattr_set(const char*, const char*, const void*, size_t, int);
205ssize_t xattr_lset(const char*, const char*, const void*, size_t, int);
206ssize_t xattr_fset(int, const char*, const void*, size_t, int);
207#endif
208
209
210#ifdef __APPLE__
211int mknodat(int, const char*, mode_t, dev_t);
212int posix_fallocate(int, off_t, off_t);
213#endif
214
215
216
217#if CFG_TOYBOX_SHADOW
218#include <shadow.h>
219#endif
220#if CFG_TOYBOX_UTMPX
221#include <utmpx.h>
222#else
223struct utmpx {int ut_type;};
224#define USER_PROCESS 0
225static inline struct utmpx *getutxent(void) {return 0;}
226static inline void setutxent(void) {;}
227static inline void endutxent(void) {;}
228#endif
229
230
231#include <fcntl.h>
232#ifndef O_NOFOLLOW
233#define O_NOFOLLOW 0
234#endif
235#ifndef O_NOATIME
236#define O_NOATIME 01000000
237#endif
238#ifndef O_CLOEXEC
239#define O_CLOEXEC 02000000
240#endif
241#ifndef O_PATH
242#define O_PATH 010000000
243#endif
244#ifndef SCHED_RESET_ON_FORK
245#define SCHED_RESET_ON_FORK (1<<30)
246#endif
247
248
249
250#ifndef F_SETPIPE_SZ
251#define F_SETPIPE_SZ 1031
252#endif
253
254#ifndef F_GETPIPE_SZ
255#define F_GETPIPE_SZ 1032
256#endif
257
258#if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
259 && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
260typedef double FLOAT;
261#else
262typedef float FLOAT;
263#endif
264
265#ifndef __uClinux__
266pid_t xfork(void);
267#endif
268
269
270
271
272
273
274#ifdef __BIONIC__
275#include <android/log.h>
276#else
277typedef enum android_LogPriority {
278 ANDROID_LOG_UNKNOWN = 0,
279 ANDROID_LOG_DEFAULT,
280 ANDROID_LOG_VERBOSE,
281 ANDROID_LOG_DEBUG,
282 ANDROID_LOG_INFO,
283 ANDROID_LOG_WARN,
284 ANDROID_LOG_ERROR,
285 ANDROID_LOG_FATAL,
286 ANDROID_LOG_SILENT,
287} android_LogPriority;
288static inline int __android_log_write(int pri, const char *tag, const char *msg)
289{
290 return -1;
291}
292#endif
293
294
295#if defined(__BIONIC__)
296#if __has_include(<processgroup/sched_policy.h>)
297#include <processgroup/sched_policy.h>
298#define GOT_IT
299#endif
300#endif
301#ifdef GOT_IT
302#undef GOT_IT
303#else
304static inline int get_sched_policy(int tid, void *policy) {return 0;}
305static inline char *get_sched_policy_name(int policy) {return "unknown";}
306#endif
307
308
309
310#ifdef __ANDROID_NDK__
311#define __android_log_write(a, b, c) (0)
312#endif
313
314#ifndef SYSLOG_NAMES
315typedef struct {char *c_name; int c_val;} CODE;
316extern CODE prioritynames[], facilitynames[];
317#endif
318
319#if CFG_TOYBOX_GETRANDOM
320#include <sys/random.h>
321#endif
322int xgetrandom(void *buf, unsigned len, unsigned flags);
323
324
325#ifdef __BIONIC__
326#define _CS_PATH 0
327#define _CS_V7_ENV 1
328#include <string.h>
329static inline void confstr(int a, char *b, int c) {strcpy(b, a ? "POSIXLY_CORRECT=1" : "/bin:/usr/bin");}
330#endif
331
332
333
334struct xnotify {
335 char **paths;
336 int max, *fds, count, kq;
337};
338
339struct xnotify *xnotify_init(int max);
340int xnotify_add(struct xnotify *not, int fd, char *path);
341int xnotify_wait(struct xnotify *not, char **path);
342
343#ifdef __APPLE__
344#define f_frsize f_iosize
345#endif
346
347int sig_to_num(char *s);
348char *num_to_sig(int sig);
349
350struct signame {
351 int num;
352 char *name;
353};
354void xsignal_all_killers(void *handler);
355
356
357int dev_minor(int dev);
358int dev_major(int dev);
359int dev_makedev(int major, int minor);
360
361char *fs_type_name(struct statfs *statfs);
362
363int get_block_device_size(int fd, unsigned long long *size);
364