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 #undef _FORTIFY_SOURCE
19 // Test for gcc (using compiler builtin #define)
22 #define noreturn __attribute__((noreturn))
27 // Always use long file support.
28 #define _FILE_OFFSET_BITS 64
30 // This isn't in the spec, but it's how we determine what libc we're using.
34 // Various constants old build environments might not have even if kernel does
40 #ifndef AT_SYMLINK_NOFOLLOW
41 #define AT_SYMLINK_NOFOLLOW 0x100
45 #define AT_REMOVEDIR 0x200
48 // We don't define GNU_dammit because we're not part of the gnu project, and
49 // don't want to get any FSF on us. Unfortunately glibc (gnu libc)
50 // won't give us Linux syscall wrappers without claiming to be part of the
51 // gnu project (because Stallman's "GNU owns Linux" revisionist history
52 // crusade includes the kernel, even though Linux was inspired by Minix).
54 // We use most non-posix Linux syscalls directly through the syscall() wrapper,
55 // but even many posix-2008 functions aren't provided by glibc unless you
56 // claim it's in the name of Gnu.
58 #if defined(__GLIBC__)
59 // "Function prototypes shall be provided." but aren't.
60 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
61 char *crypt(const char *key, const char *salt);
63 // According to posix, #include header, get a function definition. But glibc...
64 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
66 int wcwidth(wchar_t wc);
68 // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
70 char *strptime(const char *buf, const char *format, struct tm *tm);
72 // uClibc pretends to be glibc and copied a lot of its bugs, but has a few more
73 #if defined(__UCLIBC__)
76 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
77 char *stpcpy(char *dest, const char *src);
78 pid_t getsid(pid_t pid);
80 // uClibc's last-ever release was in 2012, so of course it doesn't define
81 // any flag newer than MS_MOVE, which was added in 2001 (linux 2.5.0.5),
82 // eleven years earlier.
84 #define MS_MOVE (1<<13)
85 #define MS_REC (1<<14)
86 #define MS_SILENT (1<<15)
87 #define MS_UNBINDABLE (1<<17)
88 #define MS_PRIVATE (1<<18)
89 #define MS_SLAVE (1<<19)
90 #define MS_SHARED (1<<20)
92 // When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
93 #elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
94 #define fstatat fstatat64
95 int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
96 int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
97 char *stpcpy(char *dest, const char *src);
99 int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
100 int openat(int dirfd, const char *pathname, int flags, ...);
102 DIR *fdopendir(int fd);
104 int fchownat(int dirfd, const char *pathname,
105 uid_t owner, gid_t group, int flags);
107 int unlinkat(int dirfd, const char *pathname, int flags);
109 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
111 // Straight from posix-2008, things old glibc had but didn't prototype
113 int faccessat(int fd, const char *path, int amode, int flag);
114 int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
115 int mkdirat(int fd, const char *path, mode_t mode);
116 int symlinkat(const char *path1, int fd, const char *path2);
117 int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
118 #include <sys/time.h>
119 int futimens(int fd, const struct timespec times[2]);
120 int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
131 // Without this "rm -r dir" fails with "is directory".
132 #define faccessat(A, B, C, D) faccessat(A, B, C, 0)
135 // Work out how to do endianness
138 #include <byteswap.h>
141 #if __BYTE_ORDER == __BIG_ENDIAN
142 #define IS_BIG_ENDIAN 1
144 #define IS_BIG_ENDIAN 0
150 #ifdef __BIG_ENDIAN__
151 #define IS_BIG_ENDIAN 1
153 #define IS_BIG_ENDIAN 0
159 #define IS_LITTLE_ENDIAN 0
160 #define SWAP_BE16(x) (x)
161 #define SWAP_BE32(x) (x)
162 #define SWAP_BE64(x) (x)
163 #define SWAP_LE16(x) bswap_16(x)
164 #define SWAP_LE32(x) bswap_32(x)
165 #define SWAP_LE64(x) bswap_64(x)
167 #define IS_LITTLE_ENDIAN 1
168 #define SWAP_BE16(x) bswap_16(x)
169 #define SWAP_BE32(x) bswap_32(x)
170 #define SWAP_BE64(x) bswap_64(x)
171 #define SWAP_LE16(x) (x)
172 #define SWAP_LE32(x) (x)
173 #define SWAP_LE64(x) (x)
176 #if defined(__APPLE__) || defined(__ANDROID__) \
177 || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
178 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
179 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
182 // Linux headers not listed by POSIX or LSB
183 #include <sys/mount.h>
184 #include <sys/swap.h>
186 // Android is missing some headers and functions
187 #if defined(__ANDROID__)
188 int sethostname(const char *name, size_t len);
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;