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