• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // For musl
8 #define _ALL_SOURCE
9 #ifndef REG_STARTEND
10 #define REG_STARTEND 0
11 #endif
12 
13 #ifdef __APPLE__
14 // macOS 10.13 doesn't have the POSIX 2008 direct access to timespec in
15 // struct stat, but we can ask it to give us something equivalent...
16 // (This must come before any #include!)
17 #define _DARWIN_C_SOURCE
18 // ...and then use macros to paper over the difference.
19 #define st_atim st_atimespec
20 #define st_ctim st_ctimespec
21 #define st_mtim st_mtimespec
22 #endif
23 
24 // Test for gcc (using compiler builtin #define)
25 
26 #ifdef __GNUC__
27 #define printf_format	__attribute__((format(printf, 1, 2)))
28 #else
29 #define printf_format
30 #endif
31 
32 // Always use long file support.
33 #define _FILE_OFFSET_BITS 64
34 
35 // This isn't in the spec, but it's how we determine what libc we're using.
36 
37 // Types various replacement prototypes need.
38 // This also lets us determine what libc we're using. Systems that
39 // have <features.h> will transitively include it, and ones that don't --
40 // macOS -- won't break.
41 #include <sys/types.h>
42 
43 // Various constants old build environments might not have even if kernel does
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 // Introduced in Linux 3.1
62 #ifndef SEEK_DATA
63 #define SEEK_DATA 3
64 #endif
65 #ifndef SEEK_HOLE
66 #define SEEK_HOLE 4
67 #endif
68 
69 // We don't define GNU_dammit because we're not part of the gnu project, and
70 // don't want to get any FSF on us. Unfortunately glibc (gnu libc)
71 // won't give us Linux syscall wrappers without claiming to be part of the
72 // gnu project (because Stallman's "GNU owns Linux" revisionist history
73 // crusade includes the kernel, even though Linux was inspired by Minix).
74 
75 // We use most non-posix Linux syscalls directly through the syscall() wrapper,
76 // but even many posix-2008 functions aren't provided by glibc unless you
77 // claim it's in the name of Gnu.
78 
79 #if defined(__GLIBC__)
80 // "Function prototypes shall be provided." but aren't.
81 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
82 char *crypt(const char *key, const char *salt);
83 
84 // According to posix, #include header, get a function definition. But glibc...
85 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
86 #include <wchar.h>
87 int wcwidth(wchar_t wc);
88 
89 // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
90 #include <time.h>
91 char *strptime(const char *buf, const char *format, struct tm *tm);
92 
93 // They didn't like posix basename so they defined another function with the
94 // same name and if you include libgen.h it #defines basename to something
95 // else (where they implemented the real basename), and that define breaks
96 // the table entry for the basename command. They didn't make a new function
97 // with a different name for their new behavior because gnu.
98 //
99 // Solution: don't use their broken header, provide an inline to redirect the
100 // correct name to the broken name.
101 
102 char *dirname(char *path);
103 char *__xpg_basename(char *path);
basename(char * path)104 static inline char *basename(char *path) { return __xpg_basename(path); }
105 char *strcasestr(const char *haystack, const char *needle);
106 #endif // defined(glibc)
107 
108 // getopt_long(), getopt_long_only(), and struct option.
109 #include <getopt.h>
110 
111 #if !defined(__GLIBC__)
112 // POSIX basename.
113 #include <libgen.h>
114 #endif
115 
116 // Work out how to do endianness
117 
118 #ifdef __APPLE__
119 
120 #include <libkern/OSByteOrder.h>
121 
122 #ifdef __BIG_ENDIAN__
123 #define IS_BIG_ENDIAN 1
124 #else
125 #define IS_BIG_ENDIAN 0
126 #endif
127 
128 #define bswap_16(x) OSSwapInt16(x)
129 #define bswap_32(x) OSSwapInt32(x)
130 #define bswap_64(x) OSSwapInt64(x)
131 
132 #elif defined(__FreeBSD__)
133 
134 #include <sys/endian.h>
135 
136 #if _BYTE_ORDER == _BIG_ENDIAN
137 #define IS_BIG_ENDIAN 1
138 #else
139 #define IS_BIG_ENDIAN 0
140 #endif
141 
142 #else
143 
144 #include <byteswap.h>
145 #include <endian.h>
146 
147 #if __BYTE_ORDER == __BIG_ENDIAN
148 #define IS_BIG_ENDIAN 1
149 #else
150 #define IS_BIG_ENDIAN 0
151 #endif
152 
153 #endif
154 
155 #if IS_BIG_ENDIAN
156 #define IS_LITTLE_ENDIAN 0
157 #define SWAP_BE16(x) (x)
158 #define SWAP_BE32(x) (x)
159 #define SWAP_BE64(x) (x)
160 #define SWAP_LE16(x) bswap_16(x)
161 #define SWAP_LE32(x) bswap_32(x)
162 #define SWAP_LE64(x) bswap_64(x)
163 #else
164 #define IS_LITTLE_ENDIAN 1
165 #define SWAP_BE16(x) bswap_16(x)
166 #define SWAP_BE32(x) bswap_32(x)
167 #define SWAP_BE64(x) bswap_64(x)
168 #define SWAP_LE16(x) (x)
169 #define SWAP_LE32(x) (x)
170 #define SWAP_LE64(x) (x)
171 #endif
172 
173 // Linux headers not listed by POSIX or LSB
174 #include <sys/mount.h>
175 #ifdef __linux__
176 #include <sys/statfs.h>
177 #include <sys/swap.h>
178 #include <sys/sysinfo.h>
179 #endif
180 
181 #ifdef __APPLE__
182 #include <util.h>
183 #elif !defined(__FreeBSD__)
184 #include <pty.h>
185 #else
186 #include <termios.h>
187 #ifndef IUTF8
188 #define IUTF8 0
189 #endif
190 #endif
191 
192 #if defined(__APPLE__) || defined(__linux__)
193 // Linux and macOS has both have getxattr and friends in <sys/xattr.h>, but
194 // they aren't compatible.
195 #include <sys/xattr.h>
196 ssize_t xattr_get(const char *, const char *, void *, size_t);
197 ssize_t xattr_lget(const char *, const char *, void *, size_t);
198 ssize_t xattr_fget(int fd, const char *, void *, size_t);
199 ssize_t xattr_list(const char *, char *, size_t);
200 ssize_t xattr_llist(const char *, char *, size_t);
201 ssize_t xattr_flist(int, char *, size_t);
202 ssize_t xattr_set(const char*, const char*, const void*, size_t, int);
203 ssize_t xattr_lset(const char*, const char*, const void*, size_t, int);
204 ssize_t xattr_fset(int, const char*, const void*, size_t, int);
205 #endif
206 
207 // macOS doesn't have these functions, but we can fake them.
208 #ifdef __APPLE__
209 int mknodat(int, const char*, mode_t, dev_t);
210 int posix_fallocate(int, off_t, off_t);
211 #endif
212 
213 // Android is missing some headers and functions
214 // "generated/config.h" is included first
215 #if CFG_TOYBOX_SHADOW
216 #include <shadow.h>
217 #endif
218 #if CFG_TOYBOX_UTMPX
219 #include <utmpx.h>
220 #else
221 struct utmpx {int ut_type;};
222 #define USER_PROCESS 0
getutxent(void)223 static inline struct utmpx *getutxent(void) {return 0;}
setutxent(void)224 static inline void setutxent(void) {;}
endutxent(void)225 static inline void endutxent(void) {;}
226 #endif
227 
228 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
229 #include <fcntl.h>
230 #ifndef O_NOFOLLOW
231 #define O_NOFOLLOW 0
232 #endif
233 #ifndef O_NOATIME
234 #define O_NOATIME 01000000
235 #endif
236 #ifndef O_CLOEXEC
237 #define O_CLOEXEC 02000000
238 #endif
239 #ifndef O_PATH
240 #define O_PATH   010000000
241 #endif
242 #ifndef SCHED_RESET_ON_FORK
243 #define SCHED_RESET_ON_FORK (1<<30)
244 #endif
245 
246 // Glibc won't give you linux-kernel constants unless you say "no, a BUD lite"
247 // even though linux has nothing to do with the FSF and never has.
248 #ifndef F_SETPIPE_SZ
249 #define F_SETPIPE_SZ 1031
250 #endif
251 
252 #ifndef F_GETPIPE_SZ
253 #define F_GETPIPE_SZ 1032
254 #endif
255 
256 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
257     && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
258 typedef double FLOAT;
259 #else
260 typedef float FLOAT;
261 #endif
262 
263 #ifndef __uClinux__
264 pid_t xfork(void);
265 #endif
266 
267 //#define strncpy(...) @@strncpyisbadmmkay@@
268 //#define strncat(...) @@strncatisbadmmkay@@
269 
270 // Support building the Android tools on glibc, so hermetic AOSP builds can
271 // use toybox before they're ready to switch to host bionic.
272 #ifdef __BIONIC__
273 #include <android/log.h>
274 #else
275 typedef enum android_LogPriority {
276   ANDROID_LOG_UNKNOWN = 0,
277   ANDROID_LOG_DEFAULT,
278   ANDROID_LOG_VERBOSE,
279   ANDROID_LOG_DEBUG,
280   ANDROID_LOG_INFO,
281   ANDROID_LOG_WARN,
282   ANDROID_LOG_ERROR,
283   ANDROID_LOG_FATAL,
284   ANDROID_LOG_SILENT,
285 } android_LogPriority;
__android_log_write(int pri,const char * tag,const char * msg)286 static inline int __android_log_write(int pri, const char *tag, const char *msg)
287 {
288   return -1;
289 }
290 #endif
291 
292 // libprocessgroup is an Android platform library not included in the NDK.
293 #if defined(__BIONIC__)
294 #if __has_include(<processgroup/sched_policy.h>)
295 #include <processgroup/sched_policy.h>
296 #define GOT_IT
297 #endif
298 #endif
299 #ifdef GOT_IT
300 #undef GOT_IT
301 #else
get_sched_policy(int tid,void * policy)302 static inline int get_sched_policy(int tid, void *policy) {return 0;}
get_sched_policy_name(int policy)303 static inline char *get_sched_policy_name(int policy) {return "unknown";}
304 #endif
305 
306 // Android NDKv18 has liblog.so but not liblog.c for static builds,
307 // stub it out for now.
308 #ifdef __ANDROID_NDK__
309 #define __android_log_write(a, b, c) (0)
310 #endif
311 
312 #ifndef SYSLOG_NAMES
313 typedef struct {char *c_name; int c_val;} CODE;
314 extern CODE prioritynames[], facilitynames[];
315 #endif
316 
317 #if CFG_TOYBOX_GETRANDOM
318 #include <sys/random.h>
319 #endif
320 int xgetrandom(void *buf, unsigned len, unsigned flags);
321 
322 // Android's bionic libc doesn't have confstr.
323 #ifdef __BIONIC__
324 #define _CS_PATH	0
325 #define _CS_V7_ENV	1
326 #include <string.h>
confstr(int a,char * b,int c)327 static inline void confstr(int a, char *b, int c) {strcpy(b, a ? "POSIXLY_CORRECT=1" : "/bin:/usr/bin");}
328 #endif
329 
330 // Paper over the differences between BSD kqueue and Linux inotify for tail.
331 
332 struct xnotify {
333   char **paths;
334   int max, *fds, count, kq;
335 };
336 
337 struct xnotify *xnotify_init(int max);
338 int xnotify_add(struct xnotify *not, int fd, char *path);
339 int xnotify_wait(struct xnotify *not, char **path);
340 
341 #ifdef __APPLE__
342 #define f_frsize f_iosize
343 #endif
344 
345 int sig_to_num(char *s);
346 char *num_to_sig(int sig);
347 
348 struct signame {
349   int num;
350   char *name;
351 };
352 void xsignal_all_killers(void *handler);
353 
354 // Different OSes encode major/minor device numbers differently.
355 int dev_minor(int dev);
356 int dev_major(int dev);
357 int dev_makedev(int major, int minor);
358 
359 char *fs_type_name(struct statfs *statfs);
360