blob: 8bbbdb25b8f2d1230bb1cd290bdacf9e6f8a818b [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))
14#else
15#define noreturn
16#endif
17
Rob Landley4e798102012-11-13 06:32:03 -060018// Always use long file support.
Rob Landleyf05f6602012-03-07 19:04:50 -060019#define _FILE_OFFSET_BITS 64
20
Rob Landley9559c2c2013-02-24 11:11:02 -060021// This isn't in the spec, but it's how we determine what libc we're using.
Rob Landley628eb9b2012-06-16 14:19:56 -050022
Rob Landleyee00a7f2012-03-19 19:19:21 -050023#include <features.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060024
Rob Landley44b9d042013-02-04 08:07:32 -060025// Various constants old build environments might not have even if kernel does
26
Rob Landley44b9d042013-02-04 08:07:32 -060027#ifndef AT_FDCWD
28#define AT_FDCWD -100
29#endif
30
31#ifndef AT_SYMLINK_NOFOLLOW
32#define AT_SYMLINK_NOFOLLOW 0x100
33#endif
34
35#ifndef AT_REMOVEDIR
36#define AT_REMOVEDIR 0x200
37#endif
38
Rob Landley9559c2c2013-02-24 11:11:02 -060039// We don't define GNU_dammit because we're not part of the gnu project, and
40// don't want to get any FSF on us. Unfortunately glibc (gnu libc)
41// won't give us Linux syscall wrappers without claiming to be part of the
42// gnu project (because Stallman's "GNU owns Linux" revisionist history
43// crusade includes the kernel, even though Linux was inspired by Minix).
44
45// We use most non-posix Linux syscalls directly through the syscall() wrapper,
46// but even many posix-2008 functions aren't provided by glibc unless you
47// claim it's in the name of Gnu.
48
Rob Landley9f8217c2012-11-26 23:24:07 -060049#if defined(__GLIBC__)
50// "Function prototypes shall be provided." but aren't.
51// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
52char *crypt(const char *key, const char *salt);
53
Rob Landley8fb77992014-07-20 16:34:36 -050054// According to posix, #include header, get a function definition. But glibc...
55// http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
56#include <wchar.h>
57int wcwidth(wchar_t wc);
58
Rob Landleyee00a7f2012-03-19 19:19:21 -050059// see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
60#include <time.h>
61char *strptime(const char *buf, const char *format, struct tm *tm);
Rob Landley9f8217c2012-11-26 23:24:07 -060062
Rob Landleyde699ac2014-12-31 16:22:31 -060063// They didn't like posix basename so they defined another function with the
64// same name and if you include libgen.h it #defines basename to something
65// else (where they implemented the real basename), and that define breaks
66// the table entry for the basename command. They didn't make a new function
67// with a different name for their new behavior because gnu.
68//
69// Implement our own in portability.c and don't use their broken header.
70char *basename(char *path);
71
Rob Landley9f8217c2012-11-26 23:24:07 -060072// uClibc pretends to be glibc and copied a lot of its bugs, but has a few more
73#if defined(__UCLIBC__)
74#include <unistd.h>
75#include <stdio.h>
76ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
Rob Landleya1a83e62014-09-20 18:46:47 -050077char *stpcpy(char *dest, const char *src);
78pid_t getsid(pid_t pid);
Rob Landley4e798102012-11-13 06:32:03 -060079
Rob Landley980458f2014-09-06 13:24:58 -050080// uClibc's last-ever release was in 2012, so of course it doesn't define
81// any flag newer than MS_MOVE, which was added in 2001 (linux 2.5.0.5),
82// eleven years earlier.
83
Rob Landley50fc9ed2014-12-04 21:46:59 -060084#include <sys/mount.h>
85#ifndef MS_MOVE
Rob Landley980458f2014-09-06 13:24:58 -050086#define MS_MOVE (1<<13)
Rob Landley50fc9ed2014-12-04 21:46:59 -060087#endif
88#ifndef MS_REC
Rob Landley980458f2014-09-06 13:24:58 -050089#define MS_REC (1<<14)
Rob Landley50fc9ed2014-12-04 21:46:59 -060090#endif
91#ifndef MS_SILENT
Rob Landley980458f2014-09-06 13:24:58 -050092#define MS_SILENT (1<<15)
Rob Landley50fc9ed2014-12-04 21:46:59 -060093#endif
94#ifndef MS_UNBINDABLE
Rob Landley980458f2014-09-06 13:24:58 -050095#define MS_UNBINDABLE (1<<17)
Rob Landley50fc9ed2014-12-04 21:46:59 -060096#endif
97#ifndef MS_PRIVATE
Rob Landley980458f2014-09-06 13:24:58 -050098#define MS_PRIVATE (1<<18)
Rob Landley50fc9ed2014-12-04 21:46:59 -060099#endif
100#ifndef MS_SLAVE
Rob Landley980458f2014-09-06 13:24:58 -0500101#define MS_SLAVE (1<<19)
Rob Landley50fc9ed2014-12-04 21:46:59 -0600102#endif
103#ifndef MS_SHARED
Rob Landley980458f2014-09-06 13:24:58 -0500104#define MS_SHARED (1<<20)
Rob Landley50fc9ed2014-12-04 21:46:59 -0600105#endif
Rob Landley980458f2014-09-06 13:24:58 -0500106
Rob Landley9559c2c2013-02-24 11:11:02 -0600107// When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
Rob Landley44b9d042013-02-04 08:07:32 -0600108#elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
Rob Landley62fd9d02012-12-01 17:59:38 -0600109#define fstatat fstatat64
110int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
Rob Landley4e798102012-11-13 06:32:03 -0600111int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
112char *stpcpy(char *dest, const char *src);
113#include <sys/stat.h>
114int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
115int openat(int dirfd, const char *pathname, int flags, ...);
116#include <dirent.h>
117DIR *fdopendir(int fd);
118#include <unistd.h>
119int fchownat(int dirfd, const char *pathname,
120 uid_t owner, gid_t group, int flags);
121int isblank(int c);
122int unlinkat(int dirfd, const char *pathname, int flags);
123#include <stdio.h>
124ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
Rob Landley44b9d042013-02-04 08:07:32 -0600125
Rob Landley9559c2c2013-02-24 11:11:02 -0600126// Straight from posix-2008, things old glibc had but didn't prototype
Rob Landley44b9d042013-02-04 08:07:32 -0600127
128int faccessat(int fd, const char *path, int amode, int flag);
129int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
130int mkdirat(int fd, const char *path, mode_t mode);
131int symlinkat(const char *path1, int fd, const char *path2);
132int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
133#include <sys/time.h>
134int futimens(int fd, const struct timespec times[2]);
135int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
Rob Landley10d55b12013-12-19 15:11:45 -0600136
137#ifndef MNT_DETACH
138#define MNT_DETACH 2
139#endif
Rob Landley4e798102012-11-13 06:32:03 -0600140#endif
141
Rob Landleyee00a7f2012-03-19 19:19:21 -0500142#endif
Rob Landley90163772007-01-18 21:54:08 -0500143
Rob Landley2ccab022014-10-02 07:24:38 -0500144#ifdef __MUSL__
145#include <unistd.h>
146// Without this "rm -r dir" fails with "is directory".
147#define faccessat(A, B, C, D) faccessat(A, B, C, 0)
148#endif
149
Rob Landley9559c2c2013-02-24 11:11:02 -0600150// Work out how to do endianness
Rob Landley2aa494d2007-02-13 16:41:51 -0500151
152#ifndef __APPLE__
153#include <byteswap.h>
Rob Landley055cfcb2007-01-14 20:20:06 -0500154#include <endian.h>
155
156#if __BYTE_ORDER == __BIG_ENDIAN
157#define IS_BIG_ENDIAN 1
Rob Landley2aa494d2007-02-13 16:41:51 -0500158#else
159#define IS_BIG_ENDIAN 0
160#endif
161
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -0500162int clearenv(void);
Rob Landley2aa494d2007-02-13 16:41:51 -0500163#else
164
165#ifdef __BIG_ENDIAN__
166#define IS_BIG_ENDIAN 1
167#else
168#define IS_BIG_ENDIAN 0
169#endif
170
171#endif
172
173#if IS_BIG_ENDIAN
Rob Landley055cfcb2007-01-14 20:20:06 -0500174#define IS_LITTLE_ENDIAN 0
175#define SWAP_BE16(x) (x)
176#define SWAP_BE32(x) (x)
177#define SWAP_BE64(x) (x)
178#define SWAP_LE16(x) bswap_16(x)
179#define SWAP_LE32(x) bswap_32(x)
180#define SWAP_LE64(x) bswap_64(x)
181#else
182#define IS_LITTLE_ENDIAN 1
Rob Landley055cfcb2007-01-14 20:20:06 -0500183#define SWAP_BE16(x) bswap_16(x)
184#define SWAP_BE32(x) bswap_32(x)
185#define SWAP_BE64(x) bswap_64(x)
186#define SWAP_LE16(x) (x)
187#define SWAP_LE32(x) (x)
188#define SWAP_LE64(x) (x)
189#endif
Rob Landleyfd1c5ba2007-02-03 14:10:00 -0500190
Rob Landley69a9f252014-11-21 06:42:37 -0600191#if defined(__APPLE__) \
Rob Landley30e28cf2014-05-06 06:14:20 -0500192 || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -0500193ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
194ssize_t getline(char **lineptr, size_t *n, FILE *stream);
195#endif
Rob Landley25b043b2013-03-11 22:23:46 -0500196
Rob Landley5b405822014-03-29 18:11:00 -0500197// Linux headers not listed by POSIX or LSB
Rob Landley5b405822014-03-29 18:11:00 -0500198#include <sys/mount.h>
199#include <sys/swap.h>
200
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600201// Android is missing some headers and functions
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600202// "generated/config.h" is included first
Rob Landley56147852014-11-19 16:55:12 -0600203#if CFG_TOYBOX_SHADOW
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600204#include <shadow.h>
205#endif
Rob Landley56147852014-11-19 16:55:12 -0600206#if CFG_TOYBOX_UTMPX
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600207#include <utmpx.h>
208#endif
Rob Landley56147852014-11-19 16:55:12 -0600209#if CFG_TOYBOX_PTY
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600210#include <pty.h>
Rob Landley56147852014-11-19 16:55:12 -0600211#else
212pid_t forkpty(int *amaster, char *name, void *termp, void *winp);
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600213#endif
214
215
Rob Landley15027d62014-04-15 21:59:42 -0500216// Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
217#include <fcntl.h>
218#ifndef O_NOFOLLOW
219#define O_NOFOLLOW 0
220#endif
Rob Landley30e28cf2014-05-06 06:14:20 -0500221
Ashwini Sharma7eb3e432014-08-12 07:09:01 -0500222#ifndef O_CLOEXEC
223#define O_CLOEXEC 02000000
224#endif
225
Rob Landley30e28cf2014-05-06 06:14:20 -0500226#if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
227 && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
228typedef double FLOAT;
229#else
230typedef float FLOAT;
231#endif
232
Rob Landley50fc9ed2014-12-04 21:46:59 -0600233#ifndef __uClinux__
234pid_t xfork(void);
235#endif
236
237//#define strncpy(...) @@strncpyisbadmmkay@@
238//#define strncat(...) @@strcatisbadmmkay@@