blob: e62de1392bbd20ed6c7c0404fbaec92dd3ba57bd [file] [log] [blame]
Rob Landley4e798102012-11-13 06:32:03 -06001// Workarounds for horrible build environment idiosyncrasies.
Rob Landley9559c2c2013-02-24 11:11:02 -06002
Rob Landley4e798102012-11-13 06:32:03 -06003// 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 Landley5b405822014-03-29 18:11:00 -05007// For musl
8#define _ALL_SOURCE
9
Rob Landley9559c2c2013-02-24 11:11:02 -060010// Test for gcc (using compiler builtin #define)
11
12#ifdef __GNUC__
13#define noreturn __attribute__((noreturn))
Elliott Hughes1be99e62015-03-01 16:16:50 -060014#define printf_format __attribute__((format(printf, 1, 2)))
Rob Landley9559c2c2013-02-24 11:11:02 -060015#else
16#define noreturn
Elliott Hughes1be99e62015-03-01 16:16:50 -060017#define printf_format
Rob Landley9559c2c2013-02-24 11:11:02 -060018#endif
19
Rob Landley4e798102012-11-13 06:32:03 -060020// Always use long file support.
Rob Landleyf05f6602012-03-07 19:04:50 -060021#define _FILE_OFFSET_BITS 64
22
Rob Landley9559c2c2013-02-24 11:11:02 -060023// This isn't in the spec, but it's how we determine what libc we're using.
Rob Landley628eb9b2012-06-16 14:19:56 -050024
Rob Landleyee00a7f2012-03-19 19:19:21 -050025#include <features.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060026
Rob Landley9398f052015-05-03 16:20:27 -050027// Types various replacement prototypes need
28#include <sys/types.h>
29
Rob Landley44b9d042013-02-04 08:07:32 -060030// Various constants old build environments might not have even if kernel does
31
Rob Landley44b9d042013-02-04 08:07:32 -060032#ifndef AT_FDCWD
33#define AT_FDCWD -100
34#endif
35
36#ifndef AT_SYMLINK_NOFOLLOW
37#define AT_SYMLINK_NOFOLLOW 0x100
38#endif
39
40#ifndef AT_REMOVEDIR
41#define AT_REMOVEDIR 0x200
42#endif
43
Rob Landley42cad122016-02-18 20:31:22 -060044#ifndef RLIMIT_RTTIME
45#define RLIMIT_RTTIME 15
46#endif
47
Rob Landleyf86f2f42017-05-21 13:11:42 -050048// Introduced in Linux 3.1
Elliott Hughes461b90c2017-01-04 10:45:55 -080049#ifndef SEEK_DATA
50#define SEEK_DATA 3
51#endif
Rob Landleyf86f2f42017-05-21 13:11:42 -050052#ifndef SEEK_HOLE
53#define SEEK_HOLE 4
54#endif
Elliott Hughes461b90c2017-01-04 10:45:55 -080055
Rob Landley9559c2c2013-02-24 11:11:02 -060056// 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).
61
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.
65
Rob Landley9f8217c2012-11-26 23:24:07 -060066#if defined(__GLIBC__)
67// "Function prototypes shall be provided." but aren't.
68// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
69char *crypt(const char *key, const char *salt);
70
Rob Landley8fb77992014-07-20 16:34:36 -050071// According to posix, #include header, get a function definition. But glibc...
72// http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
73#include <wchar.h>
74int wcwidth(wchar_t wc);
75
Rob Landleyee00a7f2012-03-19 19:19:21 -050076// see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
77#include <time.h>
78char *strptime(const char *buf, const char *format, struct tm *tm);
Rob Landley9f8217c2012-11-26 23:24:07 -060079
Rob Landleyde699ac2014-12-31 16:22:31 -060080// 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.
85//
Rob Landley468f1552015-01-18 13:44:24 -060086// Solution: don't use their broken header, provide an inline to redirect the
87// correct name to the broken name.
88
89char *dirname(char *path);
Elliott Hughesba555802015-07-11 14:20:31 -050090char *__xpg_basename(char *path);
Rob Landley468f1552015-01-18 13:44:24 -060091static inline char *basename(char *path) { return __xpg_basename(path); }
Rob Landleyde699ac2014-12-31 16:22:31 -060092
Rob Landley9559c2c2013-02-24 11:11:02 -060093// When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
Rob Landley71921dc2017-05-08 22:09:08 -050094#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
Rob Landley62fd9d02012-12-01 17:59:38 -060095#define fstatat fstatat64
96int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
Rob Landley4e798102012-11-13 06:32:03 -060097int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
98char *stpcpy(char *dest, const char *src);
99#include <sys/stat.h>
100int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
101int openat(int dirfd, const char *pathname, int flags, ...);
102#include <dirent.h>
103DIR *fdopendir(int fd);
104#include <unistd.h>
105int fchownat(int dirfd, const char *pathname,
106 uid_t owner, gid_t group, int flags);
107int isblank(int c);
108int unlinkat(int dirfd, const char *pathname, int flags);
109#include <stdio.h>
110ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
Rob Landley44b9d042013-02-04 08:07:32 -0600111
Rob Landley9559c2c2013-02-24 11:11:02 -0600112// Straight from posix-2008, things old glibc had but didn't prototype
Rob Landley44b9d042013-02-04 08:07:32 -0600113
114int faccessat(int fd, const char *path, int amode, int flag);
115int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
116int mkdirat(int fd, const char *path, mode_t mode);
117int symlinkat(const char *path1, int fd, const char *path2);
118int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
119#include <sys/time.h>
120int futimens(int fd, const struct timespec times[2]);
121int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
Rob Landley10d55b12013-12-19 15:11:45 -0600122
123#ifndef MNT_DETACH
124#define MNT_DETACH 2
125#endif
Rob Landley468f1552015-01-18 13:44:24 -0600126#endif // Old glibc
Rob Landley4e798102012-11-13 06:32:03 -0600127
Rob Landley468f1552015-01-18 13:44:24 -0600128#endif // glibc in general
Rob Landley90163772007-01-18 21:54:08 -0500129
Rob Landley1c028ca2016-04-08 18:25:59 -0500130#if !defined(__GLIBC__)
Elliott Hughese9108262015-01-18 13:36:31 -0600131// POSIX basename.
132#include <libgen.h>
133#endif
134
Rob Landley9559c2c2013-02-24 11:11:02 -0600135// Work out how to do endianness
Rob Landley2aa494d2007-02-13 16:41:51 -0500136
137#ifndef __APPLE__
138#include <byteswap.h>
Rob Landley055cfcb2007-01-14 20:20:06 -0500139#include <endian.h>
140
141#if __BYTE_ORDER == __BIG_ENDIAN
142#define IS_BIG_ENDIAN 1
Rob Landley2aa494d2007-02-13 16:41:51 -0500143#else
144#define IS_BIG_ENDIAN 0
145#endif
146
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -0500147int clearenv(void);
Rob Landley2aa494d2007-02-13 16:41:51 -0500148#else
149
150#ifdef __BIG_ENDIAN__
151#define IS_BIG_ENDIAN 1
152#else
153#define IS_BIG_ENDIAN 0
154#endif
155
156#endif
157
158#if IS_BIG_ENDIAN
Rob Landley055cfcb2007-01-14 20:20:06 -0500159#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)
166#else
167#define IS_LITTLE_ENDIAN 1
Rob Landley055cfcb2007-01-14 20:20:06 -0500168#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)
174#endif
Rob Landleyfd1c5ba2007-02-03 14:10:00 -0500175
Rob Landley69a9f252014-11-21 06:42:37 -0600176#if defined(__APPLE__) \
Rob Landley30e28cf2014-05-06 06:14:20 -0500177 || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -0500178ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
179ssize_t getline(char **lineptr, size_t *n, FILE *stream);
180#endif
Rob Landley25b043b2013-03-11 22:23:46 -0500181
Rob Landley5b405822014-03-29 18:11:00 -0500182// Linux headers not listed by POSIX or LSB
Rob Landley5b405822014-03-29 18:11:00 -0500183#include <sys/mount.h>
184#include <sys/swap.h>
185
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600186// Android is missing some headers and functions
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600187// "generated/config.h" is included first
Rob Landley56147852014-11-19 16:55:12 -0600188#if CFG_TOYBOX_SHADOW
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600189#include <shadow.h>
190#endif
Rob Landley56147852014-11-19 16:55:12 -0600191#if CFG_TOYBOX_UTMPX
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600192#include <utmpx.h>
Rob Landleye96dd072015-08-30 06:00:32 -0500193#else
194struct utmpx {int ut_type;};
195#define USER_PROCESS 0
196static inline struct utmpx *getutxent(void) {return 0;}
197static inline void setutxent(void) {;}
198static inline void endutxent(void) {;}
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600199#endif
Elliott Hughesc2415d12015-01-16 13:49:23 -0600200
Rob Landley15027d62014-04-15 21:59:42 -0500201// Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
202#include <fcntl.h>
203#ifndef O_NOFOLLOW
204#define O_NOFOLLOW 0
205#endif
Rob Landleyc565b062015-05-18 02:00:43 -0500206#ifndef O_NOATIME
207#define O_NOATIME 01000000
208#endif
Ashwini Sharma7eb3e432014-08-12 07:09:01 -0500209#ifndef O_CLOEXEC
210#define O_CLOEXEC 02000000
211#endif
Rob Landley06d37832015-05-01 14:40:49 -0500212#ifndef O_PATH
213#define O_PATH 010000000
214#endif
Rob Landleyf86f2f42017-05-21 13:11:42 -0500215#ifndef SCHED_RESET_ON_FORK
216#define SCHED_RESET_ON_FORK (1<<30)
217#endif
Rob Landley06d37832015-05-01 14:40:49 -0500218
Rob Landley2f3f26e2016-02-09 17:23:31 -0600219// 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.
221#ifndef F_SETPIPE_SZ
222#define F_SETPIPE_SZ 1031
223#endif
224
225#ifndef F_GETPIPE_SZ
226#define F_GETPIPE_SZ 1032
227#endif
228
Rob Landley30e28cf2014-05-06 06:14:20 -0500229#if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
230 && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
231typedef double FLOAT;
232#else
233typedef float FLOAT;
234#endif
235
Rob Landley50fc9ed2014-12-04 21:46:59 -0600236#ifndef __uClinux__
237pid_t xfork(void);
238#endif
239
240//#define strncpy(...) @@strncpyisbadmmkay@@
Rob Landley72cd2e02015-05-08 20:20:29 -0500241//#define strncat(...) @@strncatisbadmmkay@@
Elliott Hughes7e2af1c2015-01-16 13:36:53 -0600242
Rob Landleybe3e3182017-04-30 02:46:36 -0500243#if CFG_TOYBOX_ANDROID_SCHEDPOLICY
Elliott Hughese0dbc6b2016-04-29 18:04:20 -0700244#include <cutils/sched_policy.h>
245#else
Rob Landley454eea52016-05-04 18:37:50 -0500246static inline int get_sched_policy(int tid, void *policy) {return 0;}
247static inline char *get_sched_policy_name(int policy) {return "unknown";}
Elliott Hughese0dbc6b2016-04-29 18:04:20 -0700248#endif