Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 1 | // Workarounds for horrible build environment idiosyncrasies. |
| 2 | // Instead of polluting the code with strange #ifdefs to work around bugs |
| 3 | // in specific compiler, library, or OS versions, localize all that here |
| 4 | // and in portability.c |
| 5 | |
Rob Landley | 7051a96 | 2010-01-06 05:28:32 -0600 | [diff] [blame] | 6 | // The tendency of gcc to produce stupid warnings continues with |
Georgi Chorbadzhiyski | 522d906 | 2012-03-16 06:42:08 -0500 | [diff] [blame] | 7 | // warn_unused_result, which warns about things like ignoring the return code |
Rob Landley | 7051a96 | 2010-01-06 05:28:32 -0600 | [diff] [blame] | 8 | // of nice(2) (which is completely useless since -1 is a legitimate return |
| 9 | // value on success and even the man page tells you to use errno instead). |
| 10 | |
| 11 | // This makes it stop. |
| 12 | |
| 13 | #undef _FORTIFY_SOURCE |
| 14 | |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 15 | // Always use long file support. |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 16 | #define _FILE_OFFSET_BITS 64 |
| 17 | |
Rob Landley | 628eb9b | 2012-06-16 14:19:56 -0500 | [diff] [blame] | 18 | // This isn't in the spec, but it's how we determine what we're using. |
| 19 | |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 20 | #include <features.h> |
Rob Landley | f05f660 | 2012-03-07 19:04:50 -0600 | [diff] [blame] | 21 | |
Rob Landley | 9f8217c | 2012-11-26 23:24:07 -0600 | [diff] [blame] | 22 | #ifndef O_DIRECTORY |
| 23 | #define O_DIRECTORY 0200000 |
| 24 | #endif |
| 25 | |
| 26 | #if defined(__GLIBC__) |
| 27 | // "Function prototypes shall be provided." but aren't. |
| 28 | // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html |
| 29 | char *crypt(const char *key, const char *salt); |
| 30 | |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 31 | // An SUSv4 function that glibc refuses to #define without crazy #defines, |
| 32 | // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html |
| 33 | #include <time.h> |
| 34 | char *strptime(const char *buf, const char *format, struct tm *tm); |
Rob Landley | 9f8217c | 2012-11-26 23:24:07 -0600 | [diff] [blame] | 35 | |
| 36 | // uClibc pretends to be glibc and copied a lot of its bugs, but has a few more |
| 37 | #if defined(__UCLIBC__) |
| 38 | #include <unistd.h> |
| 39 | #include <stdio.h> |
| 40 | ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 41 | |
| 42 | // When building under obsolete glibc, hold its hand a bit. |
Rob Landley | 9f8217c | 2012-11-26 23:24:07 -0600 | [diff] [blame] | 43 | #elif __GLIBC_MINOR__ < 10 |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 44 | #define AT_FDCWD -100 |
| 45 | #define AT_SYMLINK_NOFOLLOW 0x100 |
| 46 | #define AT_REMOVEDIR 0x200 |
Rob Landley | 62fd9d0 | 2012-12-01 17:59:38 -0600 | [diff] [blame^] | 47 | #define fstatat fstatat64 |
| 48 | int fstatat64(int dirfd, const char *pathname, void *buf, int flags); |
Rob Landley | 4e79810 | 2012-11-13 06:32:03 -0600 | [diff] [blame] | 49 | int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz); |
| 50 | char *stpcpy(char *dest, const char *src); |
| 51 | #include <sys/stat.h> |
| 52 | int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags); |
| 53 | int openat(int dirfd, const char *pathname, int flags, ...); |
| 54 | #include <dirent.h> |
| 55 | DIR *fdopendir(int fd); |
| 56 | #include <unistd.h> |
| 57 | int fchownat(int dirfd, const char *pathname, |
| 58 | uid_t owner, gid_t group, int flags); |
| 59 | int isblank(int c); |
| 60 | int unlinkat(int dirfd, const char *pathname, int flags); |
| 61 | #include <stdio.h> |
| 62 | ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); |
| 63 | #endif |
| 64 | |
Rob Landley | ee00a7f | 2012-03-19 19:19:21 -0500 | [diff] [blame] | 65 | #endif |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 66 | |
Rob Landley | efa93b9 | 2007-11-15 21:12:24 -0600 | [diff] [blame] | 67 | #ifdef __GNUC__ |
| 68 | #define noreturn __attribute__((noreturn)) |
| 69 | #else |
| 70 | #define noreturn |
| 71 | #endif |
Rob Landley | 2aa494d | 2007-02-13 16:41:51 -0500 | [diff] [blame] | 72 | |
| 73 | #ifndef __APPLE__ |
| 74 | #include <byteswap.h> |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 75 | #include <endian.h> |
| 76 | |
| 77 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 78 | #define IS_BIG_ENDIAN 1 |
Rob Landley | 2aa494d | 2007-02-13 16:41:51 -0500 | [diff] [blame] | 79 | #else |
| 80 | #define IS_BIG_ENDIAN 0 |
| 81 | #endif |
| 82 | |
Georgi Chorbadzhiyski | 522d906 | 2012-03-16 06:42:08 -0500 | [diff] [blame] | 83 | int clearenv(void); |
Rob Landley | 2aa494d | 2007-02-13 16:41:51 -0500 | [diff] [blame] | 84 | #else |
| 85 | |
| 86 | #ifdef __BIG_ENDIAN__ |
| 87 | #define IS_BIG_ENDIAN 1 |
| 88 | #else |
| 89 | #define IS_BIG_ENDIAN 0 |
| 90 | #endif |
| 91 | |
| 92 | #endif |
| 93 | |
| 94 | #if IS_BIG_ENDIAN |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 95 | #define IS_LITTLE_ENDIAN 0 |
| 96 | #define SWAP_BE16(x) (x) |
| 97 | #define SWAP_BE32(x) (x) |
| 98 | #define SWAP_BE64(x) (x) |
| 99 | #define SWAP_LE16(x) bswap_16(x) |
| 100 | #define SWAP_LE32(x) bswap_32(x) |
| 101 | #define SWAP_LE64(x) bswap_64(x) |
| 102 | #else |
| 103 | #define IS_LITTLE_ENDIAN 1 |
Rob Landley | 055cfcb | 2007-01-14 20:20:06 -0500 | [diff] [blame] | 104 | #define SWAP_BE16(x) bswap_16(x) |
| 105 | #define SWAP_BE32(x) bswap_32(x) |
| 106 | #define SWAP_BE64(x) bswap_64(x) |
| 107 | #define SWAP_LE16(x) (x) |
| 108 | #define SWAP_LE32(x) (x) |
| 109 | #define SWAP_LE64(x) (x) |
| 110 | #endif |
Rob Landley | fd1c5ba | 2007-02-03 14:10:00 -0500 | [diff] [blame] | 111 | |
| 112 | // Some versions of gcc produce spurious "may be uninitialized" warnings in |
| 113 | // cases where it provably can't happen. Unfortunately, although this warning |
| 114 | // is calculated and produced separately from the "is definitely used |
| 115 | // uninitialized" warnings, there's no way to turn off the broken spurious "may |
| 116 | // be" warnings without also turning off the non-broken "is" warnings. |
| 117 | |
| 118 | #if CFG_TOYBOX_DEBUG |
| 119 | #define GCC_BUG =0 |
| 120 | #else |
| 121 | #define GCC_BUG |
| 122 | #endif |
Georgi Chorbadzhiyski | 522d906 | 2012-03-16 06:42:08 -0500 | [diff] [blame] | 123 | |
| 124 | #if defined(__APPLE__) || defined(__ANDROID__) |
| 125 | ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); |
| 126 | ssize_t getline(char **lineptr, size_t *n, FILE *stream); |
| 127 | #endif |