Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 1 | // Workarounds for horrible build environment idiosyncrasies. |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 2 | |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 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 | |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 7 | // For musl |
| 8 | #define _ALL_SOURCE |
| 9 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 10 | // Test for gcc (using compiler builtin #define) |
| 11 | |
| 12 | #ifdef __GNUC__ |
| 13 | #define noreturn __attribute__((noreturn)) |
| 14 | #else |
| 15 | #define noreturn |
| 16 | #endif |
| 17 | |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 18 | // Always use long file support. |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 19 | #define _FILE_OFFSET_BITS 64 |
| 20 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 21 | // This isn't in the spec, but it's how we determine what libc we're using. |
Rob Landley | 628eb9b | 2012-06-16 14:19:56 -0500 | [diff] [blame] | 22 | |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 23 | #include <features.h> |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 24 | |
Rob Landley | 44b9d04 | 2013-02-04 08:07:32 -0600 | [diff] [blame] | 25 | // Various constants old build environments might not have even if kernel does |
| 26 | |
Rob Landley | 44b9d04 | 2013-02-04 08:07:32 -0600 | [diff] [blame] | 27 | #ifndef AT_FDCWD |
| 28 | #define AT_FDCWD -100 |
| 29 | #endif |
| 30 | |
| 31 | #ifndef AT_SYMLINK_NOFOLLOW |
| 32 | #define AT_SYMLINK_NOFOLLOW 0x100 |
| 33 | #endif |
| 34 | |
| 35 | #ifndef AT_REMOVEDIR |
| 36 | #define AT_REMOVEDIR 0x200 |
| 37 | #endif |
| 38 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 39 | // We don't define GNU_dammit because we're not part of the gnu project, and |
| 40 | // don't want to get any FSF on us. Unfortunately glibc (gnu libc) |
| 41 | // won't give us Linux syscall wrappers without claiming to be part of the |
| 42 | // gnu project (because Stallman's "GNU owns Linux" revisionist history |
| 43 | // crusade includes the kernel, even though Linux was inspired by Minix). |
| 44 | |
| 45 | // We use most non-posix Linux syscalls directly through the syscall() wrapper, |
| 46 | // but even many posix-2008 functions aren't provided by glibc unless you |
| 47 | // claim it's in the name of Gnu. |
| 48 | |
Rob Landley | 9f8217c | 2012-11-26 23:24:07 -0600 | [diff] [blame] | 49 | #if defined(__GLIBC__) |
| 50 | // "Function prototypes shall be provided." but aren't. |
| 51 | // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html |
| 52 | char *crypt(const char *key, const char *salt); |
| 53 | |
Rob Landley | 8fb7799 | 2014-07-20 16:34:36 -0500 | [diff] [blame] | 54 | // According to posix, #include header, get a function definition. But glibc... |
| 55 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html |
| 56 | #include <wchar.h> |
| 57 | int wcwidth(wchar_t wc); |
| 58 | |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 59 | // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html |
| 60 | #include <time.h> |
| 61 | char *strptime(const char *buf, const char *format, struct tm *tm); |
Rob Landley | 9f8217c | 2012-11-26 23:24:07 -0600 | [diff] [blame] | 62 | |
Rob Landley | de699ac | 2014-12-31 16:22:31 -0600 | [diff] [blame] | 63 | // They didn't like posix basename so they defined another function with the |
| 64 | // same name and if you include libgen.h it #defines basename to something |
| 65 | // else (where they implemented the real basename), and that define breaks |
| 66 | // the table entry for the basename command. They didn't make a new function |
| 67 | // with a different name for their new behavior because gnu. |
| 68 | // |
| 69 | // Implement our own in portability.c and don't use their broken header. |
| 70 | char *basename(char *path); |
| 71 | |
Rob Landley | 9f8217c | 2012-11-26 23:24:07 -0600 | [diff] [blame] | 72 | // uClibc pretends to be glibc and copied a lot of its bugs, but has a few more |
| 73 | #if defined(__UCLIBC__) |
| 74 | #include <unistd.h> |
| 75 | #include <stdio.h> |
| 76 | ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); |
Rob Landley | a1a83e6 | 2014-09-20 18:46:47 -0500 | [diff] [blame] | 77 | char *stpcpy(char *dest, const char *src); |
| 78 | pid_t getsid(pid_t pid); |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 79 | |
Rob Landley | 980458f | 2014-09-06 13:24:58 -0500 | [diff] [blame] | 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. |
| 83 | |
Rob Landley | 50fc9ed | 2014-12-04 21:46:59 -0600 | [diff] [blame] | 84 | #include <sys/mount.h> |
| 85 | #ifndef MS_MOVE |
Rob Landley | 980458f | 2014-09-06 13:24:58 -0500 | [diff] [blame] | 86 | #define MS_MOVE (1<<13) |
Rob Landley | 50fc9ed | 2014-12-04 21:46:59 -0600 | [diff] [blame] | 87 | #endif |
| 88 | #ifndef MS_REC |
Rob Landley | 980458f | 2014-09-06 13:24:58 -0500 | [diff] [blame] | 89 | #define MS_REC (1<<14) |
Rob Landley | 50fc9ed | 2014-12-04 21:46:59 -0600 | [diff] [blame] | 90 | #endif |
| 91 | #ifndef MS_SILENT |
Rob Landley | 980458f | 2014-09-06 13:24:58 -0500 | [diff] [blame] | 92 | #define MS_SILENT (1<<15) |
Rob Landley | 50fc9ed | 2014-12-04 21:46:59 -0600 | [diff] [blame] | 93 | #endif |
| 94 | #ifndef MS_UNBINDABLE |
Rob Landley | 980458f | 2014-09-06 13:24:58 -0500 | [diff] [blame] | 95 | #define MS_UNBINDABLE (1<<17) |
Rob Landley | 50fc9ed | 2014-12-04 21:46:59 -0600 | [diff] [blame] | 96 | #endif |
| 97 | #ifndef MS_PRIVATE |
Rob Landley | 980458f | 2014-09-06 13:24:58 -0500 | [diff] [blame] | 98 | #define MS_PRIVATE (1<<18) |
Rob Landley | 50fc9ed | 2014-12-04 21:46:59 -0600 | [diff] [blame] | 99 | #endif |
| 100 | #ifndef MS_SLAVE |
Rob Landley | 980458f | 2014-09-06 13:24:58 -0500 | [diff] [blame] | 101 | #define MS_SLAVE (1<<19) |
Rob Landley | 50fc9ed | 2014-12-04 21:46:59 -0600 | [diff] [blame] | 102 | #endif |
| 103 | #ifndef MS_SHARED |
Rob Landley | 980458f | 2014-09-06 13:24:58 -0500 | [diff] [blame] | 104 | #define MS_SHARED (1<<20) |
Rob Landley | 50fc9ed | 2014-12-04 21:46:59 -0600 | [diff] [blame] | 105 | #endif |
Rob Landley | 980458f | 2014-09-06 13:24:58 -0500 | [diff] [blame] | 106 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 107 | // When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit. |
Rob Landley | 44b9d04 | 2013-02-04 08:07:32 -0600 | [diff] [blame] | 108 | #elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10 |
Rob Landley | 62fd9d0 | 2012-12-01 17:59:38 -0600 | [diff] [blame] | 109 | #define fstatat fstatat64 |
| 110 | int fstatat64(int dirfd, const char *pathname, void *buf, int flags); |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 111 | int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz); |
| 112 | char *stpcpy(char *dest, const char *src); |
| 113 | #include <sys/stat.h> |
| 114 | int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags); |
| 115 | int openat(int dirfd, const char *pathname, int flags, ...); |
| 116 | #include <dirent.h> |
| 117 | DIR *fdopendir(int fd); |
| 118 | #include <unistd.h> |
| 119 | int fchownat(int dirfd, const char *pathname, |
| 120 | uid_t owner, gid_t group, int flags); |
| 121 | int isblank(int c); |
| 122 | int unlinkat(int dirfd, const char *pathname, int flags); |
| 123 | #include <stdio.h> |
| 124 | ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); |
Rob Landley | 44b9d04 | 2013-02-04 08:07:32 -0600 | [diff] [blame] | 125 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 126 | // Straight from posix-2008, things old glibc had but didn't prototype |
Rob Landley | 44b9d04 | 2013-02-04 08:07:32 -0600 | [diff] [blame] | 127 | |
| 128 | int faccessat(int fd, const char *path, int amode, int flag); |
| 129 | int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag); |
| 130 | int mkdirat(int fd, const char *path, mode_t mode); |
| 131 | int symlinkat(const char *path1, int fd, const char *path2); |
| 132 | int mknodat(int fd, const char *path, mode_t mode, dev_t dev); |
| 133 | #include <sys/time.h> |
| 134 | int futimens(int fd, const struct timespec times[2]); |
| 135 | int utimensat(int fd, const char *path, const struct timespec times[2], int flag); |
Rob Landley | 10d55b1 | 2013-12-19 15:11:45 -0600 | [diff] [blame] | 136 | |
| 137 | #ifndef MNT_DETACH |
| 138 | #define MNT_DETACH 2 |
| 139 | #endif |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 140 | #endif |
| 141 | |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 142 | #endif |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 143 | |
Elliott Hughes | e910826 | 2015-01-18 13:36:31 -0600 | [diff] [blame^] | 144 | #ifndef __GLIBC__ |
| 145 | // POSIX basename. |
| 146 | #include <libgen.h> |
| 147 | #endif |
| 148 | |
Rob Landley | 2ccab02 | 2014-10-02 07:24:38 -0500 | [diff] [blame] | 149 | #ifdef __MUSL__ |
| 150 | #include <unistd.h> |
| 151 | // Without this "rm -r dir" fails with "is directory". |
| 152 | #define faccessat(A, B, C, D) faccessat(A, B, C, 0) |
| 153 | #endif |
| 154 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 155 | // Work out how to do endianness |
Rob Landley | 2aa494d | 2007-02-13 16:41:51 -0500 | [diff] [blame] | 156 | |
| 157 | #ifndef __APPLE__ |
| 158 | #include <byteswap.h> |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 159 | #include <endian.h> |
| 160 | |
| 161 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 162 | #define IS_BIG_ENDIAN 1 |
Rob Landley | 2aa494d | 2007-02-13 16:41:51 -0500 | [diff] [blame] | 163 | #else |
| 164 | #define IS_BIG_ENDIAN 0 |
| 165 | #endif |
| 166 | |
Georgi Chorbadzhiyski | 522d906 | 2012-03-16 06:42:08 -0500 | [diff] [blame] | 167 | int clearenv(void); |
Rob Landley | 2aa494d | 2007-02-13 16:41:51 -0500 | [diff] [blame] | 168 | #else |
| 169 | |
| 170 | #ifdef __BIG_ENDIAN__ |
| 171 | #define IS_BIG_ENDIAN 1 |
| 172 | #else |
| 173 | #define IS_BIG_ENDIAN 0 |
| 174 | #endif |
| 175 | |
| 176 | #endif |
| 177 | |
| 178 | #if IS_BIG_ENDIAN |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 179 | #define IS_LITTLE_ENDIAN 0 |
| 180 | #define SWAP_BE16(x) (x) |
| 181 | #define SWAP_BE32(x) (x) |
| 182 | #define SWAP_BE64(x) (x) |
| 183 | #define SWAP_LE16(x) bswap_16(x) |
| 184 | #define SWAP_LE32(x) bswap_32(x) |
| 185 | #define SWAP_LE64(x) bswap_64(x) |
| 186 | #else |
| 187 | #define IS_LITTLE_ENDIAN 1 |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 188 | #define SWAP_BE16(x) bswap_16(x) |
| 189 | #define SWAP_BE32(x) bswap_32(x) |
| 190 | #define SWAP_BE64(x) bswap_64(x) |
| 191 | #define SWAP_LE16(x) (x) |
| 192 | #define SWAP_LE32(x) (x) |
| 193 | #define SWAP_LE64(x) (x) |
| 194 | #endif |
Rob Landley | fd1c5ba | 2007-02-03 14:10:00 -0500 | [diff] [blame] | 195 | |
Rob Landley | 69a9f25 | 2014-11-21 06:42:37 -0600 | [diff] [blame] | 196 | #if defined(__APPLE__) \ |
Rob Landley | 30e28cf | 2014-05-06 06:14:20 -0500 | [diff] [blame] | 197 | || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10) |
Georgi Chorbadzhiyski | 522d906 | 2012-03-16 06:42:08 -0500 | [diff] [blame] | 198 | ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); |
| 199 | ssize_t getline(char **lineptr, size_t *n, FILE *stream); |
| 200 | #endif |
Rob Landley | 25b043b | 2013-03-11 22:23:46 -0500 | [diff] [blame] | 201 | |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 202 | // Linux headers not listed by POSIX or LSB |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 203 | #include <sys/mount.h> |
| 204 | #include <sys/swap.h> |
| 205 | |
Isaac Dunham | 46ddf0e | 2014-11-19 16:38:46 -0600 | [diff] [blame] | 206 | // Android is missing some headers and functions |
Isaac Dunham | 46ddf0e | 2014-11-19 16:38:46 -0600 | [diff] [blame] | 207 | // "generated/config.h" is included first |
Rob Landley | 5614785 | 2014-11-19 16:55:12 -0600 | [diff] [blame] | 208 | #if CFG_TOYBOX_SHADOW |
Isaac Dunham | 46ddf0e | 2014-11-19 16:38:46 -0600 | [diff] [blame] | 209 | #include <shadow.h> |
| 210 | #endif |
Rob Landley | 5614785 | 2014-11-19 16:55:12 -0600 | [diff] [blame] | 211 | #if CFG_TOYBOX_UTMPX |
Isaac Dunham | 46ddf0e | 2014-11-19 16:38:46 -0600 | [diff] [blame] | 212 | #include <utmpx.h> |
| 213 | #endif |
Elliott Hughes | c2415d1 | 2015-01-16 13:49:23 -0600 | [diff] [blame] | 214 | |
Rob Landley | 15027d6 | 2014-04-15 21:59:42 -0500 | [diff] [blame] | 215 | // Some systems don't define O_NOFOLLOW, and it varies by architecture, so... |
| 216 | #include <fcntl.h> |
| 217 | #ifndef O_NOFOLLOW |
| 218 | #define O_NOFOLLOW 0 |
| 219 | #endif |
Rob Landley | 30e28cf | 2014-05-06 06:14:20 -0500 | [diff] [blame] | 220 | |
Ashwini Sharma | 7eb3e43 | 2014-08-12 07:09:01 -0500 | [diff] [blame] | 221 | #ifndef O_CLOEXEC |
| 222 | #define O_CLOEXEC 02000000 |
| 223 | #endif |
| 224 | |
Rob Landley | 30e28cf | 2014-05-06 06:14:20 -0500 | [diff] [blame] | 225 | #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \ |
| 226 | && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__ |
| 227 | typedef double FLOAT; |
| 228 | #else |
| 229 | typedef float FLOAT; |
| 230 | #endif |
| 231 | |
Rob Landley | 50fc9ed | 2014-12-04 21:46:59 -0600 | [diff] [blame] | 232 | #ifndef __uClinux__ |
| 233 | pid_t xfork(void); |
| 234 | #endif |
| 235 | |
| 236 | //#define strncpy(...) @@strncpyisbadmmkay@@ |
| 237 | //#define strncat(...) @@strcatisbadmmkay@@ |
Elliott Hughes | 7e2af1c | 2015-01-16 13:36:53 -0600 | [diff] [blame] | 238 | |
| 239 | #if CFG_TOYBOX_SELINUX |
| 240 | #include <selinux/selinux.h> |
Rob Landley | 3b91599 | 2015-01-16 13:43:09 -0600 | [diff] [blame] | 241 | #else |
| 242 | #define is_selinux_enabled() 0 |
| 243 | int getcon(void* con); |
Elliott Hughes | 7e2af1c | 2015-01-16 13:36:53 -0600 | [diff] [blame] | 244 | #endif |