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 #ifdef __APPLE__
12 // macOS 10.13 doesn't have the POSIX 2008 direct access to timespec in
13 // struct stat, but we can ask it to give us something equivalent...
14 // (This must come before any #include!)
15 #define _DARWIN_C_SOURCE
16 // ...and then use macros to paper over the difference.
17 #define st_atim st_atimespec
18 #define st_ctim st_ctimespec
19 #define st_mtim st_mtimespec
20 #endif
21
22 // For musl
23 #define _ALL_SOURCE
24 #include <regex.h>
25 #ifndef REG_STARTEND
26 #define REG_STARTEND 0
27 #endif
28
29 // for some reason gnu/libc only sets these if you #define ia_ia_stallman_ftaghn
30 // despite FreeBSD and MacOS having both with the same value, and bionic's
31 // "upstream-openbsd" directory documenting them as "BSD extensions".
32 // (The flexible extension would have been an fnmatch() that returns length
33 // matched at location so we could check trailing data ourselves, but no.
34 // And it's ANSI only case matching instead of UTF8...)
35 #include <fnmatch.h>
36 #ifndef FNM_LEADING_DIR
37 #define FNM_LEADING_DIR 8
38 #endif
39 #ifndef FNM_CASEFOLD
40 #define FNM_CASEFOLD 16
41 #endif
42
43 // Test for gcc (using compiler builtin #define)
44
45 #ifdef __GNUC__
46 #define QUIET = 0 // shut up false positive "may be used uninitialized" warning
47 #define printf_format __attribute__((format(printf, 1, 2)))
48 #else
49 #define QUIET
50 #define printf_format
51 #endif
52
53 // This lets us determine what libc we're using: systems that have <features.h>
54 // will transitively include it, and ones that don't (macOS) won't break.
55 #include <sys/types.h>
56
57 // Various constants old build environments might not have even if kernel does
58
59 #ifndef AT_FDCWD // Kernel commit 5590ff0d5528 2006
60 #define AT_FDCWD -100
61 #endif
62
63 #ifndef AT_SYMLINK_NOFOLLOW
64 #define AT_SYMLINK_NOFOLLOW 0x100
65 #endif
66
67 #ifndef AT_REMOVEDIR
68 #define AT_REMOVEDIR 0x200
69 #endif
70
71 #ifndef RLIMIT_RTTIME // Commit 78f2c7db6068f 2008
72 #define RLIMIT_RTTIME 15
73 #endif
74
75 // Introduced in Linux 3.1 (Commit 982d816581eee 2011)
76 #ifndef SEEK_DATA
77 #define SEEK_DATA 3
78 #endif
79 #ifndef SEEK_HOLE
80 #define SEEK_HOLE 4
81 #endif
82
83 // We don't define GNU_dammit because we're not part of the gnu project, and
84 // don't want to get any FSF on us. Unfortunately glibc (gnu libc)
85 // won't give us Linux syscall wrappers without claiming to be part of the
86 // gnu project (because Stallman's "GNU owns Linux" revisionist history
87 // crusade includes the kernel, even though Linux was inspired by Minix).
88
89 // We use most non-posix Linux syscalls directly through the syscall() wrapper,
90 // but even many posix-2008 functions aren't provided by glibc unless you
91 // claim it's in the name of Gnu.
92
93 #if defined(__GLIBC__)
94 // Glibc violates posix: "Function prototypes shall be provided." but aren't.
95 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
96 char *crypt(const char *key, const char *salt);
97
98 // According to posix, #include header, get a function definition. But glibc...
99 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
100 #include <wchar.h>
101 int wcwidth(wchar_t wc);
102
103 // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
104 #include <time.h>
105 char *strptime(const char *buf, const char *format, struct tm *tm);
106
107 // Gnu didn't like posix basename so they defined another function with the
108 // same name and if you include libgen.h it #defines basename to something
109 // else (where they implemented the real basename), and that define breaks
110 // the table entry for the basename command. They didn't make a new function
111 // with a different name for their new behavior because gnu.
112 //
113 // Solution: don't use their broken header and provide an inline to redirect
114 // the standard name to the renamed function with the standard behavior.
115
116 char *dirname(char *path);
117 char *__xpg_basename(char *path);
basename(char * path)118 static inline char *basename(char *path) { return __xpg_basename(path); }
119 char *strcasestr(const char *haystack, const char *needle);
120 void *memmem(const void *haystack, size_t haystack_length,
121 const void *needle, size_t needle_length);
122 #endif // defined(glibc)
123
124 #if !defined(__GLIBC__)
125 // POSIX basename.
126 #include <libgen.h>
127 #endif
128
129 // Work out how to do endianness
130
131 #define IS_LITTLE_ENDIAN (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
132 #define IS_BIG_ENDIAN (!IS_LITTLE_ENDIAN)
133
134 #ifdef __APPLE__
135
136 #include <libkern/OSByteOrder.h>
137 #define bswap_16(x) OSSwapInt16(x)
138 #define bswap_32(x) OSSwapInt32(x)
139 #define bswap_64(x) OSSwapInt64(x)
140 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
141 #include <sys/endian.h>
142 #define bswap_16(x) bswap16(x)
143 #define bswap_32(x) bswap32(x)
144 #define bswap_64(x) bswap64(x)
145 #else
146 #include <byteswap.h>
147 #endif
148
149 #if IS_BIG_ENDIAN
150 #define SWAP_BE16(x) (x)
151 #define SWAP_BE32(x) (x)
152 #define SWAP_BE64(x) (x)
153 #define SWAP_LE16(x) bswap_16(x)
154 #define SWAP_LE32(x) bswap_32(x)
155 #define SWAP_LE64(x) bswap_64(x)
156 #else
157 #define SWAP_BE16(x) bswap_16(x)
158 #define SWAP_BE32(x) bswap_32(x)
159 #define SWAP_BE64(x) bswap_64(x)
160 #define SWAP_LE16(x) (x)
161 #define SWAP_LE32(x) (x)
162 #define SWAP_LE64(x) (x)
163 #endif
164
165 // Linux headers not listed by POSIX or LSB
166 #include <sys/mount.h>
167 #ifdef __linux__
168 #include <sys/statfs.h>
169 #include <sys/swap.h>
170 #include <sys/sysinfo.h>
171 #endif
172
173 #ifdef __APPLE__
174 #include <util.h>
175 #elif !defined(__FreeBSD__) && !defined(__OpenBSD__)
176 #include <pty.h>
177 #else
178 #include <termios.h>
179 #ifndef IUTF8
180 #define IUTF8 0
181 #endif
182 #endif
183
184 #ifdef __linux__
185 #include <sys/personality.h>
186 #else
187 #define PER_LINUX32 0
188 int personality(int);
189 #endif
190
191 #if defined(__APPLE__) || defined(__linux__)
192 // Linux and macOS has both have getxattr and friends in <sys/xattr.h>, but
193 // they aren't compatible.
194 #include <sys/xattr.h>
195 ssize_t xattr_get(const char *, const char *, void *, size_t);
196 ssize_t xattr_lget(const char *, const char *, void *, size_t);
197 ssize_t xattr_fget(int fd, const char *, void *, size_t);
198 ssize_t xattr_list(const char *, char *, size_t);
199 ssize_t xattr_llist(const char *, char *, size_t);
200 ssize_t xattr_flist(int, char *, size_t);
201 ssize_t xattr_set(const char*, const char*, const void*, size_t, int);
202 ssize_t xattr_lset(const char*, const char*, const void*, size_t, int);
203 ssize_t xattr_fset(int, const char*, const void*, size_t, int);
204 #endif
205
206 #if defined(__APPLE__)
207 // macOS doesn't have these functions, but we can fake them.
208 int mknodat(int, const char*, mode_t, dev_t);
209 int posix_fallocate(int, off_t, off_t);
210
211 // macOS keeps newlocale(3) in the non-POSIX <xlocale.h> rather than <locale.h>.
212 #include <xlocale.h>
213 #endif
214
215 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
statfs_bsize(struct statfs * sf)216 static inline long statfs_bsize(struct statfs *sf) { return sf->f_iosize; }
statfs_frsize(struct statfs * sf)217 static inline long statfs_frsize(struct statfs *sf) { return sf->f_bsize; }
218 #else
statfs_bsize(struct statfs * sf)219 static inline long statfs_bsize(struct statfs *sf) { return sf->f_bsize; }
statfs_frsize(struct statfs * sf)220 static inline long statfs_frsize(struct statfs *sf) { return sf->f_frsize; }
221 #endif
222
223
224 // Android is missing some headers and functions
225 // "generated/config.h" is included first
226 #if __has_include(<shadow.h>)
227 #include <shadow.h>
228 #endif
229 #if __has_include(<utmpx.h>)
230 #include <utmpx.h>
231 #else
232 struct utmpx {int ut_type;};
233 #define USER_PROCESS 0
getutxent(void)234 static inline struct utmpx *getutxent(void) {return 0;}
setutxent(void)235 static inline void setutxent(void) {;}
endutxent(void)236 static inline void endutxent(void) {;}
237 #endif
238
239 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
240 #include <fcntl.h>
241 #if defined(__APPLE__)
242 #define O_PATH 0
243 #else
244 #ifndef O_NOFOLLOW
245 #define O_NOFOLLOW 0
246 #endif
247 #ifndef O_NOATIME
248 #define O_NOATIME 01000000
249 #endif
250 #ifndef O_CLOEXEC
251 #define O_CLOEXEC 02000000
252 #endif
253 #ifndef O_PATH
254 #define O_PATH 010000000
255 #endif
256 #ifndef SCHED_RESET_ON_FORK
257 #define SCHED_RESET_ON_FORK (1<<30)
258 #endif
259 #endif
260
261 // Glibc won't give you linux-kernel constants unless you say "no, a BUD lite"
262 // even though linux has nothing to do with the FSF and never has.
263 #ifndef F_SETPIPE_SZ
264 #define F_SETPIPE_SZ 1031
265 #endif
266
267 #ifndef F_GETPIPE_SZ
268 #define F_GETPIPE_SZ 1032
269 #endif
270
271 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
272 && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
273 typedef double FLOAT;
274 #else
275 typedef float FLOAT;
276 #endif
277
278 #ifndef __uClinux__
279 pid_t xfork(void);
280 #endif
281
282 // gratuitously memsets ALL the extra space with zeroes (not just a terminator)
283 // but to make up for it truncating doesn't null terminate the output at all.
284 // There are occasions to use it, but it is NOT A GENERAL PURPOSE FUNCTION.
285 // #define strncpy(...) @@strncpyisbadmmkay@@
286 // strncat writes a null terminator one byte PAST the buffer size it's given.
287 #define strncat(...) strncatisbadmmkay(__VA_ARGS__)
288
289 // Support building the Android tools on glibc, so hermetic AOSP builds can
290 // use toybox before they're ready to switch to host bionic.
291 #ifdef __BIONIC__
292 #include <android/log.h>
293 #else
294 typedef enum android_LogPriority {
295 ANDROID_LOG_UNKNOWN = 0,
296 ANDROID_LOG_DEFAULT,
297 ANDROID_LOG_VERBOSE,
298 ANDROID_LOG_DEBUG,
299 ANDROID_LOG_INFO,
300 ANDROID_LOG_WARN,
301 ANDROID_LOG_ERROR,
302 ANDROID_LOG_FATAL,
303 ANDROID_LOG_SILENT,
304 } android_LogPriority;
305 #endif
306 #if !defined(__BIONIC__) || defined(__ANDROID_NDK__)
307 // Android NDKv18 has liblog.so but not liblog.a for static builds.
stub_out_log_write(int pri,const char * tag,const char * msg)308 static inline int stub_out_log_write(int pri, const char *tag, const char *msg)
309 {
310 return -1;
311 }
312 #ifdef __ANDROID_NDK__
313 #define __android_log_write(a, b, c) stub_out_log_write(a, b, c)
314 #endif
315
316 #endif
317
318 #ifndef SYSLOG_NAMES
319 typedef struct {char *c_name; int c_val;} CODE;
320 extern CODE prioritynames[], facilitynames[];
321 #endif
322
323 #if __has_include (<sys/random.h>)
324 #include <sys/random.h>
325 #endif
326 void xgetrandom(void *buf, unsigned len);
327
328 // Android's bionic libc doesn't have confstr.
329 #ifdef __BIONIC__
330 #define _CS_PATH 0
331 #define _CS_V7_ENV 1
332 #include <string.h>
confstr(int a,char * b,int c)333 static inline void confstr(int a, char *b, int c) {strcpy(b, a ? "POSIXLY_CORRECT=1" : "/bin:/usr/bin");}
334 #endif
335
336 // Paper over the differences between BSD kqueue and Linux inotify for tail.
337
338 struct xnotify {
339 char **paths;
340 int max, *fds, count, kq;
341 };
342
343 struct xnotify *xnotify_init(int max);
344 int xnotify_add(struct xnotify *not, int fd, char *path);
345 int xnotify_wait(struct xnotify *not, char **path);
346
347 int sig_to_num(char *s);
348 char *num_to_sig(int sig);
349
350 struct signame {
351 int num;
352 char *name;
353 };
354 void xsignal_all_killers(void *handler);
355
356 // Different OSes encode major/minor device numbers differently.
357 int dev_minor(int dev);
358 int dev_major(int dev);
359 int dev_makedev(int major, int minor);
360
361 char *fs_type_name(struct statfs *statfs);
362
363 int get_block_device_size(int fd, unsigned long long *size);
364 int rename_exchange(char *file1, char *file2);
365
366 #ifdef __APPLE__
367 // Apple doesn't have POSIX timers; this is "just enough" for timeout(1).
368 typedef int timer_t;
369 struct itimerspec {
370 struct timespec it_value;
371 };
372 int timer_create(clock_t c, struct sigevent *se, timer_t *t);
373 int timer_settime(timer_t t, int flags, struct itimerspec *new, void *old);
374 #elif defined(__GLIBC__)
375 // Work around a glibc bug that interacts badly with a gcc bug.
376 #include <syscall.h>
377 #include <signal.h>
378 #include <time.h>
379 int timer_create_wrap(clockid_t c, struct sigevent *se, timer_t *t);
380 #define timer_create(...) timer_create_wrap(__VA_ARGS__)
381 int timer_settime_wrap(timer_t t, int flags, struct itimerspec *val,
382 struct itimerspec *old);
383 #define timer_settime(...) timer_settime_wrap(__VA_ARGS__)
384 #endif
385