1 // Workarounds for horrible build environment idiosyncrasies.
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
7 // The tendency of gcc to produce stupid warnings continues with
8 // warn_unused_result, which warns about things like ignoring the return code
9 // of nice(2) (which is completely useless since -1 is a legitimate return
10 // value on success and even the man page tells you to use errno instead).
12 // This makes it stop.
14 // Except on Android, where fortify is mandatory.
15 #if !defined(__ANDROID__)
16 #undef _FORTIFY_SOURCE
22 // Test for gcc (using compiler builtin #define)
25 #define noreturn __attribute__((noreturn))
30 // Always use long file support.
31 #define _FILE_OFFSET_BITS 64
33 // This isn't in the spec, but it's how we determine what libc we're using.
37 // Various constants old build environments might not have even if kernel does
43 #ifndef AT_SYMLINK_NOFOLLOW
44 #define AT_SYMLINK_NOFOLLOW 0x100
48 #define AT_REMOVEDIR 0x200
51 // We don't define GNU_dammit because we're not part of the gnu project, and
52 // don't want to get any FSF on us. Unfortunately glibc (gnu libc)
53 // won't give us Linux syscall wrappers without claiming to be part of the
54 // gnu project (because Stallman's "GNU owns Linux" revisionist history
55 // crusade includes the kernel, even though Linux was inspired by Minix).
57 // We use most non-posix Linux syscalls directly through the syscall() wrapper,
58 // but even many posix-2008 functions aren't provided by glibc unless you
59 // claim it's in the name of Gnu.
61 #if defined(__GLIBC__)
62 // "Function prototypes shall be provided." but aren't.
63 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
64 char *crypt(const char *key, const char *salt);
66 // According to posix, #include header, get a function definition. But glibc...
67 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
69 int wcwidth(wchar_t wc);
71 // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
73 char *strptime(const char *buf, const char *format, struct tm *tm);
75 // uClibc pretends to be glibc and copied a lot of its bugs, but has a few more
76 #if defined(__UCLIBC__)
79 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
80 char *stpcpy(char *dest, const char *src);
81 pid_t getsid(pid_t pid);
83 // uClibc's last-ever release was in 2012, so of course it doesn't define
84 // any flag newer than MS_MOVE, which was added in 2001 (linux 2.5.0.5),
85 // eleven years earlier.
87 #define MS_MOVE (1<<13)
88 #define MS_REC (1<<14)
89 #define MS_SILENT (1<<15)
90 #define MS_UNBINDABLE (1<<17)
91 #define MS_PRIVATE (1<<18)
92 #define MS_SLAVE (1<<19)
93 #define MS_SHARED (1<<20)
95 // When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
96 #elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
97 #define fstatat fstatat64
98 int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
99 int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
100 char *stpcpy(char *dest, const char *src);
101 #include <sys/stat.h>
102 int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
103 int openat(int dirfd, const char *pathname, int flags, ...);
105 DIR *fdopendir(int fd);
107 int fchownat(int dirfd, const char *pathname,
108 uid_t owner, gid_t group, int flags);
110 int unlinkat(int dirfd, const char *pathname, int flags);
112 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
114 // Straight from posix-2008, things old glibc had but didn't prototype
116 int faccessat(int fd, const char *path, int amode, int flag);
117 int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
118 int mkdirat(int fd, const char *path, mode_t mode);
119 int symlinkat(const char *path1, int fd, const char *path2);
120 int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
121 #include <sys/time.h>
122 int futimens(int fd, const struct timespec times[2]);
123 int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
134 // Without this "rm -r dir" fails with "is directory".
135 #define faccessat(A, B, C, D) faccessat(A, B, C, 0)
138 // Work out how to do endianness
141 #include <byteswap.h>
144 #if __BYTE_ORDER == __BIG_ENDIAN
145 #define IS_BIG_ENDIAN 1
147 #define IS_BIG_ENDIAN 0
153 #ifdef __BIG_ENDIAN__
154 #define IS_BIG_ENDIAN 1
156 #define IS_BIG_ENDIAN 0
162 #define IS_LITTLE_ENDIAN 0
163 #define SWAP_BE16(x) (x)
164 #define SWAP_BE32(x) (x)
165 #define SWAP_BE64(x) (x)
166 #define SWAP_LE16(x) bswap_16(x)
167 #define SWAP_LE32(x) bswap_32(x)
168 #define SWAP_LE64(x) bswap_64(x)
170 #define IS_LITTLE_ENDIAN 1
171 #define SWAP_BE16(x) bswap_16(x)
172 #define SWAP_BE32(x) bswap_32(x)
173 #define SWAP_BE64(x) bswap_64(x)
174 #define SWAP_LE16(x) (x)
175 #define SWAP_LE32(x) (x)
176 #define SWAP_LE64(x) (x)
179 #if defined(__APPLE__) \
180 || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
181 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
182 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
185 // Linux headers not listed by POSIX or LSB
186 #include <sys/mount.h>
187 #include <sys/swap.h>
189 // Android is missing some headers and functions
190 // "generated/config.h" is included first
191 #if CFG_TOYBOX_SHADOW
200 pid_t forkpty(int *amaster, char *name, void *termp, void *winp);
204 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
211 #define O_CLOEXEC 02000000
214 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
215 && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
216 typedef double FLOAT;