xref: /aosp_15_r20/external/toybox/lib/portability.h (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
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 #ifdef __APPLE__
132 
133 #include <libkern/OSByteOrder.h>
134 
135 #ifdef __BIG_ENDIAN__
136 #define IS_BIG_ENDIAN 1
137 #else
138 #define IS_BIG_ENDIAN 0
139 #endif
140 
141 #define bswap_16(x) OSSwapInt16(x)
142 #define bswap_32(x) OSSwapInt32(x)
143 #define bswap_64(x) OSSwapInt64(x)
144 
145 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
146 
147 #include <sys/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 #define bswap_16(x) bswap16(x)
156 #define bswap_32(x) bswap32(x)
157 #define bswap_64(x) bswap64(x)
158 
159 #else
160 
161 #include <byteswap.h>
162 #include <endian.h>
163 
164 #if __BYTE_ORDER == __BIG_ENDIAN
165 #define IS_BIG_ENDIAN 1
166 #else
167 #define IS_BIG_ENDIAN 0
168 #endif
169 
170 #endif
171 
172 #if IS_BIG_ENDIAN
173 #define IS_LITTLE_ENDIAN 0
174 #define SWAP_BE16(x) (x)
175 #define SWAP_BE32(x) (x)
176 #define SWAP_BE64(x) (x)
177 #define SWAP_LE16(x) bswap_16(x)
178 #define SWAP_LE32(x) bswap_32(x)
179 #define SWAP_LE64(x) bswap_64(x)
180 #else
181 #define IS_LITTLE_ENDIAN 1
182 #define SWAP_BE16(x) bswap_16(x)
183 #define SWAP_BE32(x) bswap_32(x)
184 #define SWAP_BE64(x) bswap_64(x)
185 #define SWAP_LE16(x) (x)
186 #define SWAP_LE32(x) (x)
187 #define SWAP_LE64(x) (x)
188 #endif
189 
190 // Linux headers not listed by POSIX or LSB
191 #include <sys/mount.h>
192 #ifdef __linux__
193 #include <sys/statfs.h>
194 #include <sys/swap.h>
195 #include <sys/sysinfo.h>
196 #endif
197 
198 #ifdef __APPLE__
199 #include <util.h>
200 #elif !defined(__FreeBSD__) && !defined(__OpenBSD__)
201 #include <pty.h>
202 #else
203 #include <termios.h>
204 #ifndef IUTF8
205 #define IUTF8 0
206 #endif
207 #endif
208 
209 #ifdef __linux__
210 #include <sys/personality.h>
211 #else
212 #define PER_LINUX32 0
213 int personality(int);
214 #endif
215 
216 #if defined(__APPLE__) || defined(__linux__)
217 // Linux and macOS has both have getxattr and friends in <sys/xattr.h>, but
218 // they aren't compatible.
219 #include <sys/xattr.h>
220 ssize_t xattr_get(const char *, const char *, void *, size_t);
221 ssize_t xattr_lget(const char *, const char *, void *, size_t);
222 ssize_t xattr_fget(int fd, const char *, void *, size_t);
223 ssize_t xattr_list(const char *, char *, size_t);
224 ssize_t xattr_llist(const char *, char *, size_t);
225 ssize_t xattr_flist(int, char *, size_t);
226 ssize_t xattr_set(const char*, const char*, const void*, size_t, int);
227 ssize_t xattr_lset(const char*, const char*, const void*, size_t, int);
228 ssize_t xattr_fset(int, const char*, const void*, size_t, int);
229 #endif
230 
231 #if defined(__APPLE__)
232 // macOS doesn't have these functions, but we can fake them.
233 int mknodat(int, const char*, mode_t, dev_t);
234 int posix_fallocate(int, off_t, off_t);
235 
236 // macOS keeps newlocale(3) in the non-POSIX <xlocale.h> rather than <locale.h>.
237 #include <xlocale.h>
238 #endif
239 
240 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
statfs_bsize(struct statfs * sf)241 static inline long statfs_bsize(struct statfs *sf) { return sf->f_iosize; }
statfs_frsize(struct statfs * sf)242 static inline long statfs_frsize(struct statfs *sf) { return sf->f_bsize; }
243 #else
statfs_bsize(struct statfs * sf)244 static inline long statfs_bsize(struct statfs *sf) { return sf->f_bsize; }
statfs_frsize(struct statfs * sf)245 static inline long statfs_frsize(struct statfs *sf) { return sf->f_frsize; }
246 #endif
247 
248 
249 // Android is missing some headers and functions
250 // "generated/config.h" is included first
251 #if __has_include(<shadow.h>)
252 #include <shadow.h>
253 #endif
254 #if __has_include(<utmpx.h>)
255 #include <utmpx.h>
256 #else
257 struct utmpx {int ut_type;};
258 #define USER_PROCESS 0
getutxent(void)259 static inline struct utmpx *getutxent(void) {return 0;}
setutxent(void)260 static inline void setutxent(void) {;}
endutxent(void)261 static inline void endutxent(void) {;}
262 #endif
263 
264 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
265 #include <fcntl.h>
266 #if defined(__APPLE__)
267 #define O_PATH 0
268 #else
269 #ifndef O_NOFOLLOW
270 #define O_NOFOLLOW 0
271 #endif
272 #ifndef O_NOATIME
273 #define O_NOATIME 01000000
274 #endif
275 #ifndef O_CLOEXEC
276 #define O_CLOEXEC 02000000
277 #endif
278 #ifndef O_PATH
279 #define O_PATH   010000000
280 #endif
281 #ifndef SCHED_RESET_ON_FORK
282 #define SCHED_RESET_ON_FORK (1<<30)
283 #endif
284 #endif
285 
286 // Glibc won't give you linux-kernel constants unless you say "no, a BUD lite"
287 // even though linux has nothing to do with the FSF and never has.
288 #ifndef F_SETPIPE_SZ
289 #define F_SETPIPE_SZ 1031
290 #endif
291 
292 #ifndef F_GETPIPE_SZ
293 #define F_GETPIPE_SZ 1032
294 #endif
295 
296 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
297     && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
298 typedef double FLOAT;
299 #else
300 typedef float FLOAT;
301 #endif
302 
303 #ifndef __uClinux__
304 pid_t xfork(void);
305 #endif
306 
307 // gratuitously memsets ALL the extra space with zeroes (not just a terminator)
308 // but to make up for it truncating doesn't null terminate the output at all.
309 // There are occasions to use it, but it is NOT A GENERAL PURPOSE FUNCTION.
310 // #define strncpy(...) @@strncpyisbadmmkay@@
311 // strncat writes a null terminator one byte PAST the buffer size it's given.
312 #define strncat(...) strncatisbadmmkay(__VA_ARGS__)
313 
314 // Support building the Android tools on glibc, so hermetic AOSP builds can
315 // use toybox before they're ready to switch to host bionic.
316 #ifdef __BIONIC__
317 #include <android/log.h>
318 #else
319 typedef enum android_LogPriority {
320   ANDROID_LOG_UNKNOWN = 0,
321   ANDROID_LOG_DEFAULT,
322   ANDROID_LOG_VERBOSE,
323   ANDROID_LOG_DEBUG,
324   ANDROID_LOG_INFO,
325   ANDROID_LOG_WARN,
326   ANDROID_LOG_ERROR,
327   ANDROID_LOG_FATAL,
328   ANDROID_LOG_SILENT,
329 } android_LogPriority;
330 #endif
331 #if !defined(__BIONIC__) || defined(__ANDROID_NDK__)
332 // Android NDKv18 has liblog.so but not liblog.a for static builds.
stub_out_log_write(int pri,const char * tag,const char * msg)333 static inline int stub_out_log_write(int pri, const char *tag, const char *msg)
334 {
335   return -1;
336 }
337 #ifdef __ANDROID_NDK__
338 #define __android_log_write(a, b, c) stub_out_log_write(a, b, c)
339 #endif
340 
341 #endif
342 
343 #ifndef SYSLOG_NAMES
344 typedef struct {char *c_name; int c_val;} CODE;
345 extern CODE prioritynames[], facilitynames[];
346 #endif
347 
348 #if __has_include (<sys/random.h>)
349 #include <sys/random.h>
350 #endif
351 void xgetrandom(void *buf, unsigned len);
352 
353 // Android's bionic libc doesn't have confstr.
354 #ifdef __BIONIC__
355 #define _CS_PATH	0
356 #define _CS_V7_ENV	1
357 #include <string.h>
confstr(int a,char * b,int c)358 static inline void confstr(int a, char *b, int c) {strcpy(b, a ? "POSIXLY_CORRECT=1" : "/bin:/usr/bin");}
359 #endif
360 
361 // Paper over the differences between BSD kqueue and Linux inotify for tail.
362 
363 struct xnotify {
364   char **paths;
365   int max, *fds, count, kq;
366 };
367 
368 struct xnotify *xnotify_init(int max);
369 int xnotify_add(struct xnotify *not, int fd, char *path);
370 int xnotify_wait(struct xnotify *not, char **path);
371 
372 int sig_to_num(char *s);
373 char *num_to_sig(int sig);
374 
375 struct signame {
376   int num;
377   char *name;
378 };
379 void xsignal_all_killers(void *handler);
380 
381 // Different OSes encode major/minor device numbers differently.
382 int dev_minor(int dev);
383 int dev_major(int dev);
384 int dev_makedev(int major, int minor);
385 
386 char *fs_type_name(struct statfs *statfs);
387 
388 int get_block_device_size(int fd, unsigned long long *size);
389 int rename_exchange(char *file1, char *file2);
390 
391 #ifdef __APPLE__
392 // Apple doesn't have POSIX timers; this is "just enough" for timeout(1).
393 typedef int timer_t;
394 struct itimerspec {
395   struct timespec it_value;
396 };
397 int timer_create(clock_t c, struct sigevent *se, timer_t *t);
398 int timer_settime(timer_t t, int flags, struct itimerspec *new, void *old);
399 #elif defined(__GLIBC__)
400 // Work around a glibc bug that interacts badly with a gcc bug.
401 #include <syscall.h>
402 #include <signal.h>
403 #include <time.h>
404 int timer_create_wrap(clockid_t c, struct sigevent *se, timer_t *t);
405 #define timer_create(...) timer_create_wrap(__VA_ARGS__)
406 int timer_settime_wrap(timer_t t, int flags, struct itimerspec *val,
407   struct itimerspec *old);
408 #define timer_settime(...) timer_settime_wrap(__VA_ARGS__)
409 #endif
410