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))
14 #define printf_format __attribute__((format(printf, 1, 2)))
20 // Always use long file support.
21 #define _FILE_OFFSET_BITS 64
23 // This isn't in the spec, but it's how we determine what libc we're using.
27 // Types various replacement prototypes need
28 #include <sys/types.h>
30 // Various constants old build environments might not have even if kernel does
36 #ifndef AT_SYMLINK_NOFOLLOW
37 #define AT_SYMLINK_NOFOLLOW 0x100
41 #define AT_REMOVEDIR 0x200
45 #define RLIMIT_RTTIME 15
52 // We don't define GNU_dammit because we're not part of the gnu project, and
53 // don't want to get any FSF on us. Unfortunately glibc (gnu libc)
54 // won't give us Linux syscall wrappers without claiming to be part of the
55 // gnu project (because Stallman's "GNU owns Linux" revisionist history
56 // crusade includes the kernel, even though Linux was inspired by Minix).
58 // We use most non-posix Linux syscalls directly through the syscall() wrapper,
59 // but even many posix-2008 functions aren't provided by glibc unless you
60 // claim it's in the name of Gnu.
62 #if defined(__GLIBC__)
63 // "Function prototypes shall be provided." but aren't.
64 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
65 char *crypt(const char *key, const char *salt);
67 // According to posix, #include header, get a function definition. But glibc...
68 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
70 int wcwidth(wchar_t wc);
72 // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
74 char *strptime(const char *buf, const char *format, struct tm *tm);
76 // They didn't like posix basename so they defined another function with the
77 // same name and if you include libgen.h it #defines basename to something
78 // else (where they implemented the real basename), and that define breaks
79 // the table entry for the basename command. They didn't make a new function
80 // with a different name for their new behavior because gnu.
82 // Solution: don't use their broken header, provide an inline to redirect the
83 // correct name to the broken name.
85 char *dirname(char *path);
86 char *__xpg_basename(char *path);
87 static inline char *basename(char *path) { return __xpg_basename(path); }
89 // When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
90 #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
91 #define fstatat fstatat64
92 int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
93 int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
94 char *stpcpy(char *dest, const char *src);
96 int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
97 int openat(int dirfd, const char *pathname, int flags, ...);
99 DIR *fdopendir(int fd);
101 int fchownat(int dirfd, const char *pathname,
102 uid_t owner, gid_t group, int flags);
104 int unlinkat(int dirfd, const char *pathname, int flags);
106 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
108 // Straight from posix-2008, things old glibc had but didn't prototype
110 int faccessat(int fd, const char *path, int amode, int flag);
111 int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
112 int mkdirat(int fd, const char *path, mode_t mode);
113 int symlinkat(const char *path1, int fd, const char *path2);
114 int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
115 #include <sys/time.h>
116 int futimens(int fd, const struct timespec times[2]);
117 int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
124 #endif // glibc in general
126 #if !defined(__GLIBC__)
131 // Work out how to do endianness
134 #include <byteswap.h>
137 #if __BYTE_ORDER == __BIG_ENDIAN
138 #define IS_BIG_ENDIAN 1
140 #define IS_BIG_ENDIAN 0
146 #ifdef __BIG_ENDIAN__
147 #define IS_BIG_ENDIAN 1
149 #define IS_BIG_ENDIAN 0
155 #define IS_LITTLE_ENDIAN 0
156 #define SWAP_BE16(x) (x)
157 #define SWAP_BE32(x) (x)
158 #define SWAP_BE64(x) (x)
159 #define SWAP_LE16(x) bswap_16(x)
160 #define SWAP_LE32(x) bswap_32(x)
161 #define SWAP_LE64(x) bswap_64(x)
163 #define IS_LITTLE_ENDIAN 1
164 #define SWAP_BE16(x) bswap_16(x)
165 #define SWAP_BE32(x) bswap_32(x)
166 #define SWAP_BE64(x) bswap_64(x)
167 #define SWAP_LE16(x) (x)
168 #define SWAP_LE32(x) (x)
169 #define SWAP_LE64(x) (x)
172 #if defined(__APPLE__) \
173 || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
174 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
175 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
178 // Linux headers not listed by POSIX or LSB
179 #include <sys/mount.h>
180 #include <sys/swap.h>
182 // Android is missing some headers and functions
183 // "generated/config.h" is included first
184 #if CFG_TOYBOX_SHADOW
190 struct utmpx {int ut_type;};
191 #define USER_PROCESS 0
192 static inline struct utmpx *getutxent(void) {return 0;}
193 static inline void setutxent(void) {;}
194 static inline void endutxent(void) {;}
197 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
204 #define O_NOATIME 01000000
208 #define O_CLOEXEC 02000000
212 #define O_PATH 010000000
215 // Glibc won't give you linux-kernel constants unless you say "no, a BUD lite"
216 // even though linux has nothing to do with the FSF and never has.
218 #define F_SETPIPE_SZ 1031
222 #define F_GETPIPE_SZ 1032
225 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
226 && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
227 typedef double FLOAT;
236 //#define strncpy(...) @@strncpyisbadmmkay@@
237 //#define strncat(...) @@strncatisbadmmkay@@
239 #if CFG_TOYBOX_ANDROID_SCHEDPOLICY
240 #include <cutils/sched_policy.h>
242 static inline int get_sched_policy(int tid, void *policy) {return 0;}
243 static inline char *get_sched_policy_name(int policy) {return "unknown";}