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 | 7051a96 | 2010-01-06 05:28:32 -0600 | [diff] [blame] | 7 | // The tendency of gcc to produce stupid warnings continues with |
Georgi Chorbadzhiyski | 522d906 | 2012-03-16 06:42:08 -0500 | [diff] [blame] | 8 | // warn_unused_result, which warns about things like ignoring the return code |
Rob Landley | 7051a96 | 2010-01-06 05:28:32 -0600 | [diff] [blame] | 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). |
| 11 | |
| 12 | // This makes it stop. |
| 13 | |
| 14 | #undef _FORTIFY_SOURCE |
| 15 | |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 16 | // For musl |
| 17 | #define _ALL_SOURCE |
| 18 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 19 | // Test for gcc (using compiler builtin #define) |
| 20 | |
| 21 | #ifdef __GNUC__ |
| 22 | #define noreturn __attribute__((noreturn)) |
| 23 | #else |
| 24 | #define noreturn |
| 25 | #endif |
| 26 | |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 27 | // Always use long file support. |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 28 | #define _FILE_OFFSET_BITS 64 |
| 29 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 30 | // 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] | 31 | |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 32 | #include <features.h> |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 33 | |
Rob Landley | 44b9d04 | 2013-02-04 08:07:32 -0600 | [diff] [blame] | 34 | // Various constants old build environments might not have even if kernel does |
| 35 | |
Rob Landley | 44b9d04 | 2013-02-04 08:07:32 -0600 | [diff] [blame] | 36 | #ifndef AT_FDCWD |
| 37 | #define AT_FDCWD -100 |
| 38 | #endif |
| 39 | |
| 40 | #ifndef AT_SYMLINK_NOFOLLOW |
| 41 | #define AT_SYMLINK_NOFOLLOW 0x100 |
| 42 | #endif |
| 43 | |
| 44 | #ifndef AT_REMOVEDIR |
| 45 | #define AT_REMOVEDIR 0x200 |
| 46 | #endif |
| 47 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 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). |
| 53 | |
| 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. |
| 57 | |
Rob Landley | 9f8217c | 2012-11-26 23:24:07 -0600 | [diff] [blame] | 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); |
| 62 | |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 63 | // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html |
| 64 | #include <time.h> |
| 65 | char *strptime(const char *buf, const char *format, struct tm *tm); |
Rob Landley | 9f8217c | 2012-11-26 23:24:07 -0600 | [diff] [blame] | 66 | |
| 67 | // uClibc pretends to be glibc and copied a lot of its bugs, but has a few more |
| 68 | #if defined(__UCLIBC__) |
| 69 | #include <unistd.h> |
| 70 | #include <stdio.h> |
| 71 | ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 72 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 73 | // 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] | 74 | #elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10 |
Rob Landley | 62fd9d0 | 2012-12-01 17:59:38 -0600 | [diff] [blame] | 75 | #define fstatat fstatat64 |
| 76 | int fstatat64(int dirfd, const char *pathname, void *buf, int flags); |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 77 | int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz); |
| 78 | char *stpcpy(char *dest, const char *src); |
| 79 | #include <sys/stat.h> |
| 80 | int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags); |
| 81 | int openat(int dirfd, const char *pathname, int flags, ...); |
| 82 | #include <dirent.h> |
| 83 | DIR *fdopendir(int fd); |
| 84 | #include <unistd.h> |
| 85 | int fchownat(int dirfd, const char *pathname, |
| 86 | uid_t owner, gid_t group, int flags); |
| 87 | int isblank(int c); |
| 88 | int unlinkat(int dirfd, const char *pathname, int flags); |
| 89 | #include <stdio.h> |
| 90 | ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); |
Rob Landley | 44b9d04 | 2013-02-04 08:07:32 -0600 | [diff] [blame] | 91 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 92 | // Straight from posix-2008, things old glibc had but didn't prototype |
Rob Landley | 44b9d04 | 2013-02-04 08:07:32 -0600 | [diff] [blame] | 93 | |
| 94 | int faccessat(int fd, const char *path, int amode, int flag); |
| 95 | int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag); |
| 96 | int mkdirat(int fd, const char *path, mode_t mode); |
| 97 | int symlinkat(const char *path1, int fd, const char *path2); |
| 98 | int mknodat(int fd, const char *path, mode_t mode, dev_t dev); |
| 99 | #include <sys/time.h> |
| 100 | int futimens(int fd, const struct timespec times[2]); |
| 101 | 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] | 102 | |
| 103 | #ifndef MNT_DETACH |
| 104 | #define MNT_DETACH 2 |
| 105 | #endif |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 106 | #endif |
| 107 | |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 108 | #endif |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 109 | |
Rob Landley | 9559c2c | 2013-02-24 11:11:02 -0600 | [diff] [blame] | 110 | // Work out how to do endianness |
Rob Landley | 2aa494d | 2007-02-13 16:41:51 -0500 | [diff] [blame] | 111 | |
| 112 | #ifndef __APPLE__ |
| 113 | #include <byteswap.h> |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 114 | #include <endian.h> |
| 115 | |
| 116 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 117 | #define IS_BIG_ENDIAN 1 |
Rob Landley | 2aa494d | 2007-02-13 16:41:51 -0500 | [diff] [blame] | 118 | #else |
| 119 | #define IS_BIG_ENDIAN 0 |
| 120 | #endif |
| 121 | |
Georgi Chorbadzhiyski | 522d906 | 2012-03-16 06:42:08 -0500 | [diff] [blame] | 122 | int clearenv(void); |
Rob Landley | 2aa494d | 2007-02-13 16:41:51 -0500 | [diff] [blame] | 123 | #else |
| 124 | |
| 125 | #ifdef __BIG_ENDIAN__ |
| 126 | #define IS_BIG_ENDIAN 1 |
| 127 | #else |
| 128 | #define IS_BIG_ENDIAN 0 |
| 129 | #endif |
| 130 | |
| 131 | #endif |
| 132 | |
| 133 | #if IS_BIG_ENDIAN |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 134 | #define IS_LITTLE_ENDIAN 0 |
| 135 | #define SWAP_BE16(x) (x) |
| 136 | #define SWAP_BE32(x) (x) |
| 137 | #define SWAP_BE64(x) (x) |
| 138 | #define SWAP_LE16(x) bswap_16(x) |
| 139 | #define SWAP_LE32(x) bswap_32(x) |
| 140 | #define SWAP_LE64(x) bswap_64(x) |
| 141 | #else |
| 142 | #define IS_LITTLE_ENDIAN 1 |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 143 | #define SWAP_BE16(x) bswap_16(x) |
| 144 | #define SWAP_BE32(x) bswap_32(x) |
| 145 | #define SWAP_BE64(x) bswap_64(x) |
| 146 | #define SWAP_LE16(x) (x) |
| 147 | #define SWAP_LE32(x) (x) |
| 148 | #define SWAP_LE64(x) (x) |
| 149 | #endif |
Rob Landley | fd1c5ba | 2007-02-03 14:10:00 -0500 | [diff] [blame] | 150 | |
Isaac Dunham | 714a0db | 2014-01-28 10:10:25 -0600 | [diff] [blame] | 151 | #if defined(__APPLE__) || defined(__ANDROID__) || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10) |
Georgi Chorbadzhiyski | 522d906 | 2012-03-16 06:42:08 -0500 | [diff] [blame] | 152 | ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); |
| 153 | ssize_t getline(char **lineptr, size_t *n, FILE *stream); |
| 154 | #endif |
Rob Landley | 25b043b | 2013-03-11 22:23:46 -0500 | [diff] [blame] | 155 | |
Rob Landley | 5b40582 | 2014-03-29 18:11:00 -0500 | [diff] [blame] | 156 | // Linux headers not listed by POSIX or LSB |
| 157 | #include <shadow.h> |
| 158 | #include <sys/mount.h> |
| 159 | #include <sys/swap.h> |
| 160 | |
Rob Landley | 0201900 | 2013-12-19 15:02:18 -0600 | [diff] [blame] | 161 | // compile time probes for stuff libc didn't provide |
Rob Landley | 25b043b | 2013-03-11 22:23:46 -0500 | [diff] [blame] | 162 | #include "generated/portability.h" |