1 // Workarounds for horrible build environment idiosyncrasies.
2
3 // Instead of polluting the code with strange #ifdefs to work around bugs
4 // in specific compiler, library, or OS versions, localize all that here
5 // and in portability.c
6
7 // Always use long file support.
8 // This must come before we #include any system header file to take effect!
9 #define _FILE_OFFSET_BITS 64
10
11 // For musl
12 #define _ALL_SOURCE
13 #include <regex.h>
14 #ifndef REG_STARTEND
15 #define REG_STARTEND 0
16 #endif
17
18 #ifdef __APPLE__
19 // macOS 10.13 doesn't have the POSIX 2008 direct access to timespec in
20 // struct stat, but we can ask it to give us something equivalent...
21 // (This must come before any #include!)
22 #define _DARWIN_C_SOURCE
23 // ...and then use macros to paper over the difference.
24 #define st_atim st_atimespec
25 #define st_ctim st_ctimespec
26 #define st_mtim st_mtimespec
27 #endif
28
29 // Test for gcc (using compiler builtin #define)
30
31 #ifdef __GNUC__
32 #define printf_format __attribute__((format(printf, 1, 2)))
33 #else
34 #define printf_format
35 #endif
36
37 // This isn't in the spec, but it's how we determine what libc we're using.
38
39 // Types various replacement prototypes need.
40 // This also lets us determine what libc we're using. Systems that
41 // have <features.h> will transitively include it, and ones that don't --
42 // macOS -- won't break.
43 #include <sys/types.h>
44
45 // Various constants old build environments might not have even if kernel does
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 // Introduced in Linux 3.1
64 #ifndef SEEK_DATA
65 #define SEEK_DATA 3
66 #endif
67 #ifndef SEEK_HOLE
68 #define SEEK_HOLE 4
69 #endif
70
71 // We don't define GNU_dammit because we're not part of the gnu project, and
72 // don't want to get any FSF on us. Unfortunately glibc (gnu libc)
73 // won't give us Linux syscall wrappers without claiming to be part of the
74 // gnu project (because Stallman's "GNU owns Linux" revisionist history
75 // crusade includes the kernel, even though Linux was inspired by Minix).
76
77 // We use most non-posix Linux syscalls directly through the syscall() wrapper,
78 // but even many posix-2008 functions aren't provided by glibc unless you
79 // claim it's in the name of Gnu.
80
81 #if defined(__GLIBC__)
82 // "Function prototypes shall be provided." but aren't.
83 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
84 char *crypt(const char *key, const char *salt);
85
86 // According to posix, #include header, get a function definition. But glibc...
87 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
88 #include <wchar.h>
89 int wcwidth(wchar_t wc);
90
91 // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
92 #include <time.h>
93 char *strptime(const char *buf, const char *format, struct tm *tm);
94
95 // They didn't like posix basename so they defined another function with the
96 // same name and if you include libgen.h it #defines basename to something
97 // else (where they implemented the real basename), and that define breaks
98 // the table entry for the basename command. They didn't make a new function
99 // with a different name for their new behavior because gnu.
100 //
101 // Solution: don't use their broken header, provide an inline to redirect the
102 // correct name to the broken name.
103
104 char *dirname(char *path);
105 char *__xpg_basename(char *path);
basename(char * path)106 static inline char *basename(char *path) { return __xpg_basename(path); }
107 char *strcasestr(const char *haystack, const char *needle);
108 void *memmem(const void *haystack, size_t haystack_length,
109 const void *needle, size_t needle_length);
110 #endif // defined(glibc)
111
112 // getopt_long(), getopt_long_only(), and struct option.
113 #include <getopt.h>
114
115 #if !defined(__GLIBC__)
116 // POSIX basename.
117 #include <libgen.h>
118 #endif
119
120 // Work out how to do endianness
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 // Linux headers not listed by POSIX or LSB
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 // Linux and macOS has both have getxattr and friends in <sys/xattr.h>, but
198 // they aren't compatible.
199 #include <sys/xattr.h>
200 ssize_t xattr_get(const char *, const char *, void *, size_t);
201 ssize_t xattr_lget(const char *, const char *, void *, size_t);
202 ssize_t xattr_fget(int fd, const char *, void *, size_t);
203 ssize_t xattr_list(const char *, char *, size_t);
204 ssize_t xattr_llist(const char *, char *, size_t);
205 ssize_t xattr_flist(int, char *, size_t);
206 ssize_t xattr_set(const char*, const char*, const void*, size_t, int);
207 ssize_t xattr_lset(const char*, const char*, const void*, size_t, int);
208 ssize_t xattr_fset(int, const char*, const void*, size_t, int);
209 #endif
210
211 #if defined(__APPLE__)
212 // macOS doesn't have these functions, but we can fake them.
213 int mknodat(int, const char*, mode_t, dev_t);
214 int posix_fallocate(int, off_t, off_t);
215
216 // macOS keeps newlocale(3) in the non-POSIX <xlocale.h> rather than <locale.h>.
217 #include <xlocale.h>
218 #endif
219
220 #if defined(__APPLE__) || defined(__OpenBSD__)
statfs_bsize(struct statfs * sf)221 static inline long statfs_bsize(struct statfs *sf) { return sf->f_iosize; }
statfs_frsize(struct statfs * sf)222 static inline long statfs_frsize(struct statfs *sf) { return sf->f_bsize; }
223 #else
statfs_bsize(struct statfs * sf)224 static inline long statfs_bsize(struct statfs *sf) { return sf->f_bsize; }
statfs_frsize(struct statfs * sf)225 static inline long statfs_frsize(struct statfs *sf) { return sf->f_frsize; }
226 #endif
227
228
229 // Android is missing some headers and functions
230 // "generated/config.h" is included first
231 #if CFG_TOYBOX_SHADOW
232 #include <shadow.h>
233 #endif
234 #if CFG_TOYBOX_UTMPX
235 #include <utmpx.h>
236 #else
237 struct utmpx {int ut_type;};
238 #define USER_PROCESS 0
getutxent(void)239 static inline struct utmpx *getutxent(void) {return 0;}
setutxent(void)240 static inline void setutxent(void) {;}
endutxent(void)241 static inline void endutxent(void) {;}
242 #endif
243
244 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
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 // Glibc won't give you linux-kernel constants unless you say "no, a BUD lite"
263 // even though linux has nothing to do with the FSF and never has.
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__
274 typedef double FLOAT;
275 #else
276 typedef float FLOAT;
277 #endif
278
279 #ifndef __uClinux__
280 pid_t xfork(void);
281 #endif
282
283 //#define strncpy(...) @@strncpyisbadmmkay@@
284 //#define strncat(...) @@strncatisbadmmkay@@
285
286 // Support building the Android tools on glibc, so hermetic AOSP builds can
287 // use toybox before they're ready to switch to host bionic.
288 #ifdef __BIONIC__
289 #include <android/log.h>
290 #else
291 typedef 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;
__android_log_write(int pri,const char * tag,const char * msg)302 static inline int __android_log_write(int pri, const char *tag, const char *msg)
303 {
304 return -1;
305 }
306 #endif
307
308 // libprocessgroup is an Android platform library not included in the NDK.
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
get_sched_policy(int tid,void * policy)318 static inline int get_sched_policy(int tid, void *policy) {return 0;}
get_sched_policy_name(int policy)319 static inline char *get_sched_policy_name(int policy) {return "unknown";}
320 #endif
321
322 // Android NDKv18 has liblog.so but not liblog.c for static builds,
323 // stub it out for now.
324 #ifdef __ANDROID_NDK__
325 #define __android_log_write(a, b, c) (0)
326 #endif
327
328 #ifndef SYSLOG_NAMES
329 typedef struct {char *c_name; int c_val;} CODE;
330 extern CODE prioritynames[], facilitynames[];
331 #endif
332
333 #if CFG_TOYBOX_GETRANDOM
334 #include <sys/random.h>
335 #endif
336 int xgetrandom(void *buf, unsigned len, unsigned flags);
337
338 // Android's bionic libc doesn't have confstr.
339 #ifdef __BIONIC__
340 #define _CS_PATH 0
341 #define _CS_V7_ENV 1
342 #include <string.h>
confstr(int a,char * b,int c)343 static inline void confstr(int a, char *b, int c) {strcpy(b, a ? "POSIXLY_CORRECT=1" : "/bin:/usr/bin");}
344 #endif
345
346 // Paper over the differences between BSD kqueue and Linux inotify for tail.
347
348 struct xnotify {
349 char **paths;
350 int max, *fds, count, kq;
351 };
352
353 struct xnotify *xnotify_init(int max);
354 int xnotify_add(struct xnotify *not, int fd, char *path);
355 int xnotify_wait(struct xnotify *not, char **path);
356
357 int sig_to_num(char *s);
358 char *num_to_sig(int sig);
359
360 struct signame {
361 int num;
362 char *name;
363 };
364 void xsignal_all_killers(void *handler);
365
366 // Different OSes encode major/minor device numbers differently.
367 int dev_minor(int dev);
368 int dev_major(int dev);
369 int dev_makedev(int major, int minor);
370
371 char *fs_type_name(struct statfs *statfs);
372
373 int get_block_device_size(int fd, unsigned long long *size);
374