blob: aa1ee48833faf8116c6b7ab133072e937c5d8a23 [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))
Rob Landleyeb4b1142015-03-01 16:35:05 -060014#if CFG_TOYBOX_DEBUG
Elliott Hughes1be99e62015-03-01 16:16:50 -060015#define printf_format __attribute__((format(printf, 1, 2)))
Rob Landley9559c2c2013-02-24 11:11:02 -060016#else
Rob Landleyeb4b1142015-03-01 16:35:05 -060017#define printf_format
18#endif
19#else
Rob Landley9559c2c2013-02-24 11:11:02 -060020#define noreturn
Elliott Hughes1be99e62015-03-01 16:16:50 -060021#define printf_format
Rob Landley9559c2c2013-02-24 11:11:02 -060022#endif
23
Rob Landley4e798102012-11-13 06:32:03 -060024// Always use long file support.
Rob Landleyf05f6602012-03-07 19:04:50 -060025#define _FILE_OFFSET_BITS 64
26
Rob Landley9559c2c2013-02-24 11:11:02 -060027// This isn't in the spec, but it's how we determine what libc we're using.
Rob Landley628eb9b2012-06-16 14:19:56 -050028
Rob Landleyee00a7f2012-03-19 19:19:21 -050029#include <features.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060030
Rob Landley9398f052015-05-03 16:20:27 -050031// Types various replacement prototypes need
32#include <sys/types.h>
33
Rob Landley44b9d042013-02-04 08:07:32 -060034// Various constants old build environments might not have even if kernel does
35
Rob Landley44b9d042013-02-04 08:07:32 -060036#ifndef AT_FDCWD
37#define AT_FDCWD -100
38#endif
39
40#ifndef AT_SYMLINK_NOFOLLOW
41#define AT_SYMLINK_NOFOLLOW 0x100
42#endif
43
44#ifndef AT_REMOVEDIR
45#define AT_REMOVEDIR 0x200
46#endif
47
Rob Landley9559c2c2013-02-24 11:11:02 -060048// We don't define GNU_dammit because we're not part of the gnu project, and
49// don't want to get any FSF on us. Unfortunately glibc (gnu libc)
50// won't give us Linux syscall wrappers without claiming to be part of the
51// gnu project (because Stallman's "GNU owns Linux" revisionist history
52// crusade includes the kernel, even though Linux was inspired by Minix).
53
54// We use most non-posix Linux syscalls directly through the syscall() wrapper,
55// but even many posix-2008 functions aren't provided by glibc unless you
56// claim it's in the name of Gnu.
57
Rob Landley9f8217c2012-11-26 23:24:07 -060058#if defined(__GLIBC__)
59// "Function prototypes shall be provided." but aren't.
60// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
61char *crypt(const char *key, const char *salt);
62
Rob Landley8fb77992014-07-20 16:34:36 -050063// According to posix, #include header, get a function definition. But glibc...
64// http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
65#include <wchar.h>
66int wcwidth(wchar_t wc);
67
Rob Landleyee00a7f2012-03-19 19:19:21 -050068// see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
69#include <time.h>
70char *strptime(const char *buf, const char *format, struct tm *tm);
Rob Landley9f8217c2012-11-26 23:24:07 -060071
Rob Landleyde699ac2014-12-31 16:22:31 -060072// They didn't like posix basename so they defined another function with the
73// same name and if you include libgen.h it #defines basename to something
74// else (where they implemented the real basename), and that define breaks
75// the table entry for the basename command. They didn't make a new function
76// with a different name for their new behavior because gnu.
77//
Rob Landley468f1552015-01-18 13:44:24 -060078// Solution: don't use their broken header, provide an inline to redirect the
79// correct name to the broken name.
80
81char *dirname(char *path);
82char *__xpg_basename (char *path);
83static inline char *basename(char *path) { return __xpg_basename(path); }
Rob Landleyde699ac2014-12-31 16:22:31 -060084
Rob Landley9f8217c2012-11-26 23:24:07 -060085// uClibc pretends to be glibc and copied a lot of its bugs, but has a few more
86#if defined(__UCLIBC__)
87#include <unistd.h>
88#include <stdio.h>
89ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
Rob Landleya1a83e62014-09-20 18:46:47 -050090char *stpcpy(char *dest, const char *src);
91pid_t getsid(pid_t pid);
Rob Landley4e798102012-11-13 06:32:03 -060092
Rob Landley980458f2014-09-06 13:24:58 -050093// uClibc's last-ever release was in 2012, so of course it doesn't define
94// any flag newer than MS_MOVE, which was added in 2001 (linux 2.5.0.5),
95// eleven years earlier.
96
Rob Landley50fc9ed2014-12-04 21:46:59 -060097#include <sys/mount.h>
98#ifndef MS_MOVE
Rob Landley980458f2014-09-06 13:24:58 -050099#define MS_MOVE (1<<13)
Rob Landley50fc9ed2014-12-04 21:46:59 -0600100#endif
101#ifndef MS_REC
Rob Landley980458f2014-09-06 13:24:58 -0500102#define MS_REC (1<<14)
Rob Landley50fc9ed2014-12-04 21:46:59 -0600103#endif
104#ifndef MS_SILENT
Rob Landley980458f2014-09-06 13:24:58 -0500105#define MS_SILENT (1<<15)
Rob Landley50fc9ed2014-12-04 21:46:59 -0600106#endif
107#ifndef MS_UNBINDABLE
Rob Landley980458f2014-09-06 13:24:58 -0500108#define MS_UNBINDABLE (1<<17)
Rob Landley50fc9ed2014-12-04 21:46:59 -0600109#endif
110#ifndef MS_PRIVATE
Rob Landley980458f2014-09-06 13:24:58 -0500111#define MS_PRIVATE (1<<18)
Rob Landley50fc9ed2014-12-04 21:46:59 -0600112#endif
113#ifndef MS_SLAVE
Rob Landley980458f2014-09-06 13:24:58 -0500114#define MS_SLAVE (1<<19)
Rob Landley50fc9ed2014-12-04 21:46:59 -0600115#endif
116#ifndef MS_SHARED
Rob Landley980458f2014-09-06 13:24:58 -0500117#define MS_SHARED (1<<20)
Rob Landley50fc9ed2014-12-04 21:46:59 -0600118#endif
Rob Landley980458f2014-09-06 13:24:58 -0500119
Rob Landley9559c2c2013-02-24 11:11:02 -0600120// When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
Rob Landley44b9d042013-02-04 08:07:32 -0600121#elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
Rob Landley62fd9d02012-12-01 17:59:38 -0600122#define fstatat fstatat64
123int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
Rob Landley4e798102012-11-13 06:32:03 -0600124int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
125char *stpcpy(char *dest, const char *src);
126#include <sys/stat.h>
127int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
128int openat(int dirfd, const char *pathname, int flags, ...);
129#include <dirent.h>
130DIR *fdopendir(int fd);
131#include <unistd.h>
132int fchownat(int dirfd, const char *pathname,
133 uid_t owner, gid_t group, int flags);
134int isblank(int c);
135int unlinkat(int dirfd, const char *pathname, int flags);
136#include <stdio.h>
137ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
Rob Landley44b9d042013-02-04 08:07:32 -0600138
Rob Landley9559c2c2013-02-24 11:11:02 -0600139// Straight from posix-2008, things old glibc had but didn't prototype
Rob Landley44b9d042013-02-04 08:07:32 -0600140
141int faccessat(int fd, const char *path, int amode, int flag);
142int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
143int mkdirat(int fd, const char *path, mode_t mode);
144int symlinkat(const char *path1, int fd, const char *path2);
145int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
146#include <sys/time.h>
147int futimens(int fd, const struct timespec times[2]);
148int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
Rob Landley10d55b12013-12-19 15:11:45 -0600149
150#ifndef MNT_DETACH
151#define MNT_DETACH 2
152#endif
Rob Landley468f1552015-01-18 13:44:24 -0600153#endif // Old glibc
Rob Landley4e798102012-11-13 06:32:03 -0600154
Rob Landley468f1552015-01-18 13:44:24 -0600155#endif // glibc in general
Rob Landley90163772007-01-18 21:54:08 -0500156
Elliott Hughese9108262015-01-18 13:36:31 -0600157#ifndef __GLIBC__
158// POSIX basename.
159#include <libgen.h>
160#endif
161
Rob Landley9559c2c2013-02-24 11:11:02 -0600162// Work out how to do endianness
Rob Landley2aa494d2007-02-13 16:41:51 -0500163
164#ifndef __APPLE__
165#include <byteswap.h>
Rob Landley055cfcb2007-01-14 20:20:06 -0500166#include <endian.h>
167
168#if __BYTE_ORDER == __BIG_ENDIAN
169#define IS_BIG_ENDIAN 1
Rob Landley2aa494d2007-02-13 16:41:51 -0500170#else
171#define IS_BIG_ENDIAN 0
172#endif
173
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -0500174int clearenv(void);
Rob Landley2aa494d2007-02-13 16:41:51 -0500175#else
176
177#ifdef __BIG_ENDIAN__
178#define IS_BIG_ENDIAN 1
179#else
180#define IS_BIG_ENDIAN 0
181#endif
182
183#endif
184
185#if IS_BIG_ENDIAN
Rob Landley055cfcb2007-01-14 20:20:06 -0500186#define IS_LITTLE_ENDIAN 0
187#define SWAP_BE16(x) (x)
188#define SWAP_BE32(x) (x)
189#define SWAP_BE64(x) (x)
190#define SWAP_LE16(x) bswap_16(x)
191#define SWAP_LE32(x) bswap_32(x)
192#define SWAP_LE64(x) bswap_64(x)
193#else
194#define IS_LITTLE_ENDIAN 1
Rob Landley055cfcb2007-01-14 20:20:06 -0500195#define SWAP_BE16(x) bswap_16(x)
196#define SWAP_BE32(x) bswap_32(x)
197#define SWAP_BE64(x) bswap_64(x)
198#define SWAP_LE16(x) (x)
199#define SWAP_LE32(x) (x)
200#define SWAP_LE64(x) (x)
201#endif
Rob Landleyfd1c5ba2007-02-03 14:10:00 -0500202
Rob Landley69a9f252014-11-21 06:42:37 -0600203#if defined(__APPLE__) \
Rob Landley30e28cf2014-05-06 06:14:20 -0500204 || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -0500205ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
206ssize_t getline(char **lineptr, size_t *n, FILE *stream);
207#endif
Rob Landley25b043b2013-03-11 22:23:46 -0500208
Rob Landley5b405822014-03-29 18:11:00 -0500209// Linux headers not listed by POSIX or LSB
Rob Landley5b405822014-03-29 18:11:00 -0500210#include <sys/mount.h>
211#include <sys/swap.h>
212
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600213// Android is missing some headers and functions
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600214// "generated/config.h" is included first
Rob Landley56147852014-11-19 16:55:12 -0600215#if CFG_TOYBOX_SHADOW
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600216#include <shadow.h>
217#endif
Rob Landley56147852014-11-19 16:55:12 -0600218#if CFG_TOYBOX_UTMPX
Isaac Dunham46ddf0e2014-11-19 16:38:46 -0600219#include <utmpx.h>
220#endif
Elliott Hughesc2415d12015-01-16 13:49:23 -0600221
Rob Landley15027d62014-04-15 21:59:42 -0500222// Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
223#include <fcntl.h>
224#ifndef O_NOFOLLOW
225#define O_NOFOLLOW 0
226#endif
Rob Landley30e28cf2014-05-06 06:14:20 -0500227
Ashwini Sharma7eb3e432014-08-12 07:09:01 -0500228#ifndef O_CLOEXEC
229#define O_CLOEXEC 02000000
230#endif
231
Rob Landley06d37832015-05-01 14:40:49 -0500232#ifndef O_PATH
233#define O_PATH 010000000
234#endif
235
Rob Landley30e28cf2014-05-06 06:14:20 -0500236#if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
237 && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
238typedef double FLOAT;
239#else
240typedef float FLOAT;
241#endif
242
Rob Landley50fc9ed2014-12-04 21:46:59 -0600243#ifndef __uClinux__
244pid_t xfork(void);
245#endif
246
247//#define strncpy(...) @@strncpyisbadmmkay@@
Rob Landley72cd2e02015-05-08 20:20:29 -0500248//#define strncat(...) @@strncatisbadmmkay@@
Elliott Hughes7e2af1c2015-01-16 13:36:53 -0600249
250#if CFG_TOYBOX_SELINUX
251#include <selinux/selinux.h>
Rob Landley3b915992015-01-16 13:43:09 -0600252#else
253#define is_selinux_enabled() 0
254int getcon(void* con);
Elliott Hughes7e2af1c2015-01-16 13:36:53 -0600255#endif
Rob Landley08f51b52015-04-15 20:53:00 -0500256
257#if CFG_TOYBOX_SMACK
258#include <sys/smack.h>
Xavier Roche58c32692015-04-17 20:18:30 -0500259#include <sys/xattr.h>
260#include <linux/xattr.h>
261#else
262#define smack_new_label_from_path(...) (-1)
263#define smack_set_label_for_path(...) (-1)
264#define smack_set_label_for_self(...) (-1)
265#define XATTR_NAME_SMACK ""
266#define SMACK_LABEL_LEN (1) /* for just ? */
Rob Landley06d37832015-05-01 14:40:49 -0500267
268ssize_t fgetxattr (int fd, char *name, void *value, size_t size);
Rob Landley08f51b52015-04-15 20:53:00 -0500269#endif
Xavier Roche58c32692015-04-17 20:18:30 -0500270