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
48 // Introduced in Linux 3.1
56 // We don't define GNU_dammit because we're not part of the gnu project, and
57 // don't want to get any FSF on us. Unfortunately glibc (gnu libc)
58 // won't give us Linux syscall wrappers without claiming to be part of the
59 // gnu project (because Stallman's "GNU owns Linux" revisionist history
60 // crusade includes the kernel, even though Linux was inspired by Minix).
62 // We use most non-posix Linux syscalls directly through the syscall() wrapper,
63 // but even many posix-2008 functions aren't provided by glibc unless you
64 // claim it's in the name of Gnu.
66 #if defined(__GLIBC__)
67 // "Function prototypes shall be provided." but aren't.
68 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
69 char *crypt(const char *key, const char *salt);
71 // According to posix, #include header, get a function definition. But glibc...
72 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
74 int wcwidth(wchar_t wc);
76 // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
78 char *strptime(const char *buf, const char *format, struct tm *tm);
80 // They didn't like posix basename so they defined another function with the
81 // same name and if you include libgen.h it #defines basename to something
82 // else (where they implemented the real basename), and that define breaks
83 // the table entry for the basename command. They didn't make a new function
84 // with a different name for their new behavior because gnu.
86 // Solution: don't use their broken header, provide an inline to redirect the
87 // correct name to the broken name.
89 char *dirname(char *path);
90 char *__xpg_basename(char *path);
91 static inline char *basename(char *path) { return __xpg_basename(path); }
93 // When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
94 #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
95 #define fstatat fstatat64
96 int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
97 int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
98 char *stpcpy(char *dest, const char *src);
100 int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
101 int openat(int dirfd, const char *pathname, int flags, ...);
103 DIR *fdopendir(int fd);
105 int fchownat(int dirfd, const char *pathname,
106 uid_t owner, gid_t group, int flags);
108 int unlinkat(int dirfd, const char *pathname, int flags);
110 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
112 // Straight from posix-2008, things old glibc had but didn't prototype
114 int faccessat(int fd, const char *path, int amode, int flag);
115 int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
116 int mkdirat(int fd, const char *path, mode_t mode);
117 int symlinkat(const char *path1, int fd, const char *path2);
118 int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
119 #include <sys/time.h>
120 int futimens(int fd, const struct timespec times[2]);
121 int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
128 #endif // glibc in general
130 #if !defined(__GLIBC__)
135 // Work out how to do endianness
138 #include <byteswap.h>
141 #if __BYTE_ORDER == __BIG_ENDIAN
142 #define IS_BIG_ENDIAN 1
144 #define IS_BIG_ENDIAN 0
150 #ifdef __BIG_ENDIAN__
151 #define IS_BIG_ENDIAN 1
153 #define IS_BIG_ENDIAN 0
159 #define IS_LITTLE_ENDIAN 0
160 #define SWAP_BE16(x) (x)
161 #define SWAP_BE32(x) (x)
162 #define SWAP_BE64(x) (x)
163 #define SWAP_LE16(x) bswap_16(x)
164 #define SWAP_LE32(x) bswap_32(x)
165 #define SWAP_LE64(x) bswap_64(x)
167 #define IS_LITTLE_ENDIAN 1
168 #define SWAP_BE16(x) bswap_16(x)
169 #define SWAP_BE32(x) bswap_32(x)
170 #define SWAP_BE64(x) bswap_64(x)
171 #define SWAP_LE16(x) (x)
172 #define SWAP_LE32(x) (x)
173 #define SWAP_LE64(x) (x)
176 #if defined(__APPLE__) \
177 || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
178 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
179 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
182 // Linux headers not listed by POSIX or LSB
183 #include <sys/mount.h>
184 #include <sys/swap.h>
186 // Android is missing some headers and functions
187 // "generated/config.h" is included first
188 #if CFG_TOYBOX_SHADOW
194 struct utmpx {int ut_type;};
195 #define USER_PROCESS 0
196 static inline struct utmpx *getutxent(void) {return 0;}
197 static inline void setutxent(void) {;}
198 static inline void endutxent(void) {;}
201 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
207 #define O_NOATIME 01000000
210 #define O_CLOEXEC 02000000
213 #define O_PATH 010000000
215 #ifndef SCHED_RESET_ON_FORK
216 #define SCHED_RESET_ON_FORK (1<<30)
219 // Glibc won't give you linux-kernel constants unless you say "no, a BUD lite"
220 // even though linux has nothing to do with the FSF and never has.
222 #define F_SETPIPE_SZ 1031
226 #define F_GETPIPE_SZ 1032
229 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
230 && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
231 typedef double FLOAT;
240 //#define strncpy(...) @@strncpyisbadmmkay@@
241 //#define strncat(...) @@strncatisbadmmkay@@
243 #if CFG_TOYBOX_ANDROID_SCHEDPOLICY
244 #include <cutils/sched_policy.h>
246 static inline int get_sched_policy(int tid, void *policy) {return 0;}
247 static inline char *get_sched_policy_name(int policy) {return "unknown";}