blob: 866d9a4d473d637b25b100c9bc081807b2248bd1 [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 Landley7051a962010-01-06 05:28:32 -06007// The tendency of gcc to produce stupid warnings continues with
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -05008// warn_unused_result, which warns about things like ignoring the return code
Rob Landley7051a962010-01-06 05:28:32 -06009// of nice(2) (which is completely useless since -1 is a legitimate return
10// value on success and even the man page tells you to use errno instead).
11
12// This makes it stop.
13
14#undef _FORTIFY_SOURCE
15
Rob Landley5b405822014-03-29 18:11:00 -050016// For musl
17#define _ALL_SOURCE
18
Rob Landley9559c2c2013-02-24 11:11:02 -060019// Test for gcc (using compiler builtin #define)
20
21#ifdef __GNUC__
22#define noreturn __attribute__((noreturn))
23#else
24#define noreturn
25#endif
26
Rob Landley4e798102012-11-13 06:32:03 -060027// Always use long file support.
Rob Landleyf05f6602012-03-07 19:04:50 -060028#define _FILE_OFFSET_BITS 64
29
Rob Landley9559c2c2013-02-24 11:11:02 -060030// This isn't in the spec, but it's how we determine what libc we're using.
Rob Landley628eb9b2012-06-16 14:19:56 -050031
Rob Landleyee00a7f2012-03-19 19:19:21 -050032#include <features.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060033
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
72// 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 Landley4e798102012-11-13 06:32:03 -060077
Rob Landley980458f2014-09-06 13:24:58 -050078// uClibc's last-ever release was in 2012, so of course it doesn't define
79// any flag newer than MS_MOVE, which was added in 2001 (linux 2.5.0.5),
80// eleven years earlier.
81
82#define MS_MOVE (1<<13)
83#define MS_REC (1<<14)
84#define MS_SILENT (1<<15)
85#define MS_UNBINDABLE (1<<17)
86#define MS_PRIVATE (1<<18)
87#define MS_SLAVE (1<<19)
88#define MS_SHARED (1<<20)
89
Rob Landley9559c2c2013-02-24 11:11:02 -060090// When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
Rob Landley44b9d042013-02-04 08:07:32 -060091#elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
Rob Landley62fd9d02012-12-01 17:59:38 -060092#define fstatat fstatat64
93int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
Rob Landley4e798102012-11-13 06:32:03 -060094int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
95char *stpcpy(char *dest, const char *src);
96#include <sys/stat.h>
97int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
98int openat(int dirfd, const char *pathname, int flags, ...);
99#include <dirent.h>
100DIR *fdopendir(int fd);
101#include <unistd.h>
102int fchownat(int dirfd, const char *pathname,
103 uid_t owner, gid_t group, int flags);
104int isblank(int c);
105int unlinkat(int dirfd, const char *pathname, int flags);
106#include <stdio.h>
107ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
Rob Landley44b9d042013-02-04 08:07:32 -0600108
Rob Landley9559c2c2013-02-24 11:11:02 -0600109// Straight from posix-2008, things old glibc had but didn't prototype
Rob Landley44b9d042013-02-04 08:07:32 -0600110
111int faccessat(int fd, const char *path, int amode, int flag);
112int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
113int mkdirat(int fd, const char *path, mode_t mode);
114int symlinkat(const char *path1, int fd, const char *path2);
115int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
116#include <sys/time.h>
117int futimens(int fd, const struct timespec times[2]);
118int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
Rob Landley10d55b12013-12-19 15:11:45 -0600119
120#ifndef MNT_DETACH
121#define MNT_DETACH 2
122#endif
Rob Landley4e798102012-11-13 06:32:03 -0600123#endif
124
Rob Landleyee00a7f2012-03-19 19:19:21 -0500125#endif
Rob Landley90163772007-01-18 21:54:08 -0500126
Rob Landley9559c2c2013-02-24 11:11:02 -0600127// Work out how to do endianness
Rob Landley2aa494d2007-02-13 16:41:51 -0500128
129#ifndef __APPLE__
130#include <byteswap.h>
Rob Landley055cfcb2007-01-14 20:20:06 -0500131#include <endian.h>
132
133#if __BYTE_ORDER == __BIG_ENDIAN
134#define IS_BIG_ENDIAN 1
Rob Landley2aa494d2007-02-13 16:41:51 -0500135#else
136#define IS_BIG_ENDIAN 0
137#endif
138
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -0500139int clearenv(void);
Rob Landley2aa494d2007-02-13 16:41:51 -0500140#else
141
142#ifdef __BIG_ENDIAN__
143#define IS_BIG_ENDIAN 1
144#else
145#define IS_BIG_ENDIAN 0
146#endif
147
148#endif
149
150#if IS_BIG_ENDIAN
Rob Landley055cfcb2007-01-14 20:20:06 -0500151#define IS_LITTLE_ENDIAN 0
152#define SWAP_BE16(x) (x)
153#define SWAP_BE32(x) (x)
154#define SWAP_BE64(x) (x)
155#define SWAP_LE16(x) bswap_16(x)
156#define SWAP_LE32(x) bswap_32(x)
157#define SWAP_LE64(x) bswap_64(x)
158#else
159#define IS_LITTLE_ENDIAN 1
Rob Landley055cfcb2007-01-14 20:20:06 -0500160#define SWAP_BE16(x) bswap_16(x)
161#define SWAP_BE32(x) bswap_32(x)
162#define SWAP_BE64(x) bswap_64(x)
163#define SWAP_LE16(x) (x)
164#define SWAP_LE32(x) (x)
165#define SWAP_LE64(x) (x)
166#endif
Rob Landleyfd1c5ba2007-02-03 14:10:00 -0500167
Rob Landley30e28cf2014-05-06 06:14:20 -0500168#if defined(__APPLE__) || defined(__ANDROID__) \
169 || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -0500170ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
171ssize_t getline(char **lineptr, size_t *n, FILE *stream);
172#endif
Rob Landley25b043b2013-03-11 22:23:46 -0500173
Rob Landley5b405822014-03-29 18:11:00 -0500174// Linux headers not listed by POSIX or LSB
175#include <shadow.h>
176#include <sys/mount.h>
177#include <sys/swap.h>
178
Rob Landley15027d62014-04-15 21:59:42 -0500179// Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
180#include <fcntl.h>
181#ifndef O_NOFOLLOW
182#define O_NOFOLLOW 0
183#endif
Rob Landley30e28cf2014-05-06 06:14:20 -0500184
Ashwini Sharma7eb3e432014-08-12 07:09:01 -0500185#ifndef O_CLOEXEC
186#define O_CLOEXEC 02000000
187#endif
188
Rob Landley30e28cf2014-05-06 06:14:20 -0500189#if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
190 && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
191typedef double FLOAT;
192#else
193typedef float FLOAT;
194#endif
195