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
10 // Test for gcc (using compiler builtin #define)
13 #define noreturn __attribute__((noreturn))
15 #define printf_format __attribute__((format(printf, 1, 2)))
24 // Always use long file support.
25 #define _FILE_OFFSET_BITS 64
27 // This isn't in the spec, but it's how we determine what libc we're using.
31 // Types various replacement prototypes need
32 #include <sys/types.h>
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 // They didn't like posix basename so they defined another function with the
73 // same name and if you include libgen.h it #defines basename to something
74 // else (where they implemented the real basename), and that define breaks
75 // the table entry for the basename command. They didn't make a new function
76 // with a different name for their new behavior because gnu.
78 // Solution: don't use their broken header, provide an inline to redirect the
79 // correct name to the broken name.
81 char *dirname(char *path);
82 char *__xpg_basename (char *path);
83 static inline char *basename(char *path) { return __xpg_basename(path); }
85 // uClibc pretends to be glibc and copied a lot of its bugs, but has a few more
86 #if defined(__UCLIBC__)
89 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
90 char *stpcpy(char *dest, const char *src);
91 pid_t getsid(pid_t pid);
93 // uClibc's last-ever release was in 2012, so of course it doesn't define
94 // any flag newer than MS_MOVE, which was added in 2001 (linux 2.5.0.5),
95 // eleven years earlier.
97 #include <sys/mount.h>
99 #define MS_MOVE (1<<13)
102 #define MS_REC (1<<14)
105 #define MS_SILENT (1<<15)
107 #ifndef MS_UNBINDABLE
108 #define MS_UNBINDABLE (1<<17)
111 #define MS_PRIVATE (1<<18)
114 #define MS_SLAVE (1<<19)
117 #define MS_SHARED (1<<20)
120 // When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
121 #elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
122 #define fstatat fstatat64
123 int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
124 int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
125 char *stpcpy(char *dest, const char *src);
126 #include <sys/stat.h>
127 int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
128 int openat(int dirfd, const char *pathname, int flags, ...);
130 DIR *fdopendir(int fd);
132 int fchownat(int dirfd, const char *pathname,
133 uid_t owner, gid_t group, int flags);
135 int unlinkat(int dirfd, const char *pathname, int flags);
137 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
139 // Straight from posix-2008, things old glibc had but didn't prototype
141 int faccessat(int fd, const char *path, int amode, int flag);
142 int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
143 int mkdirat(int fd, const char *path, mode_t mode);
144 int symlinkat(const char *path1, int fd, const char *path2);
145 int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
146 #include <sys/time.h>
147 int futimens(int fd, const struct timespec times[2]);
148 int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
155 #endif // glibc in general
162 // Work out how to do endianness
165 #include <byteswap.h>
168 #if __BYTE_ORDER == __BIG_ENDIAN
169 #define IS_BIG_ENDIAN 1
171 #define IS_BIG_ENDIAN 0
177 #ifdef __BIG_ENDIAN__
178 #define IS_BIG_ENDIAN 1
180 #define IS_BIG_ENDIAN 0
186 #define IS_LITTLE_ENDIAN 0
187 #define SWAP_BE16(x) (x)
188 #define SWAP_BE32(x) (x)
189 #define SWAP_BE64(x) (x)
190 #define SWAP_LE16(x) bswap_16(x)
191 #define SWAP_LE32(x) bswap_32(x)
192 #define SWAP_LE64(x) bswap_64(x)
194 #define IS_LITTLE_ENDIAN 1
195 #define SWAP_BE16(x) bswap_16(x)
196 #define SWAP_BE32(x) bswap_32(x)
197 #define SWAP_BE64(x) bswap_64(x)
198 #define SWAP_LE16(x) (x)
199 #define SWAP_LE32(x) (x)
200 #define SWAP_LE64(x) (x)
203 #if defined(__APPLE__) \
204 || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
205 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
206 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
209 // Linux headers not listed by POSIX or LSB
210 #include <sys/mount.h>
211 #include <sys/swap.h>
213 // Android is missing some headers and functions
214 // "generated/config.h" is included first
215 #if CFG_TOYBOX_SHADOW
222 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
229 #define O_NOATIME 01000000
233 #define O_CLOEXEC 02000000
237 #define O_PATH 010000000
240 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
241 && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
242 typedef double FLOAT;
251 //#define strncpy(...) @@strncpyisbadmmkay@@
252 //#define strncat(...) @@strncatisbadmmkay@@