blob: 4903de1106a315883b6b2a35d018fdf5a6139772 [file] [log] [blame]
Rob Landley4e798102012-11-13 06:32:03 -06001// Workarounds for horrible build environment idiosyncrasies.
2// Instead of polluting the code with strange #ifdefs to work around bugs
3// in specific compiler, library, or OS versions, localize all that here
4// and in portability.c
5
Rob Landley7051a962010-01-06 05:28:32 -06006// The tendency of gcc to produce stupid warnings continues with
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -05007// warn_unused_result, which warns about things like ignoring the return code
Rob Landley7051a962010-01-06 05:28:32 -06008// of nice(2) (which is completely useless since -1 is a legitimate return
9// value on success and even the man page tells you to use errno instead).
10
11// This makes it stop.
12
13#undef _FORTIFY_SOURCE
14
Rob Landley4e798102012-11-13 06:32:03 -060015// Always use long file support.
Rob Landleyf05f6602012-03-07 19:04:50 -060016#define _FILE_OFFSET_BITS 64
17
Rob Landley628eb9b2012-06-16 14:19:56 -050018// This isn't in the spec, but it's how we determine what we're using.
19
Rob Landleyee00a7f2012-03-19 19:19:21 -050020#include <features.h>
Rob Landleyf05f6602012-03-07 19:04:50 -060021
Rob Landley9f8217c2012-11-26 23:24:07 -060022#ifndef O_DIRECTORY
23#define O_DIRECTORY 0200000
24#endif
25
26#if defined(__GLIBC__)
27// "Function prototypes shall be provided." but aren't.
28// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
29char *crypt(const char *key, const char *salt);
30
Rob Landleyee00a7f2012-03-19 19:19:21 -050031// An SUSv4 function that glibc refuses to #define without crazy #defines,
32// see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
33#include <time.h>
34char *strptime(const char *buf, const char *format, struct tm *tm);
Rob Landley9f8217c2012-11-26 23:24:07 -060035
36// uClibc pretends to be glibc and copied a lot of its bugs, but has a few more
37#if defined(__UCLIBC__)
38#include <unistd.h>
39#include <stdio.h>
40ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
Rob Landley4e798102012-11-13 06:32:03 -060041
42// When building under obsolete glibc, hold its hand a bit.
Rob Landley9f8217c2012-11-26 23:24:07 -060043#elif __GLIBC_MINOR__ < 10
Rob Landley4e798102012-11-13 06:32:03 -060044#define AT_FDCWD -100
45#define AT_SYMLINK_NOFOLLOW 0x100
46#define AT_REMOVEDIR 0x200
47int fstatat(int dirfd, const char *pathname, void *buf, int flags);
48int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
49char *stpcpy(char *dest, const char *src);
50#include <sys/stat.h>
51int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
52int openat(int dirfd, const char *pathname, int flags, ...);
53#include <dirent.h>
54DIR *fdopendir(int fd);
55#include <unistd.h>
56int fchownat(int dirfd, const char *pathname,
57 uid_t owner, gid_t group, int flags);
58int isblank(int c);
59int unlinkat(int dirfd, const char *pathname, int flags);
60#include <stdio.h>
61ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
62#endif
63
Rob Landleyee00a7f2012-03-19 19:19:21 -050064#endif
Rob Landley90163772007-01-18 21:54:08 -050065
Rob Landleyefa93b92007-11-15 21:12:24 -060066#ifdef __GNUC__
67#define noreturn __attribute__((noreturn))
68#else
69#define noreturn
70#endif
Rob Landley2aa494d2007-02-13 16:41:51 -050071
72#ifndef __APPLE__
73#include <byteswap.h>
Rob Landley055cfcb2007-01-14 20:20:06 -050074#include <endian.h>
75
76#if __BYTE_ORDER == __BIG_ENDIAN
77#define IS_BIG_ENDIAN 1
Rob Landley2aa494d2007-02-13 16:41:51 -050078#else
79#define IS_BIG_ENDIAN 0
80#endif
81
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -050082int clearenv(void);
Rob Landley2aa494d2007-02-13 16:41:51 -050083#else
84
85#ifdef __BIG_ENDIAN__
86#define IS_BIG_ENDIAN 1
87#else
88#define IS_BIG_ENDIAN 0
89#endif
90
91#endif
92
93#if IS_BIG_ENDIAN
Rob Landley055cfcb2007-01-14 20:20:06 -050094#define IS_LITTLE_ENDIAN 0
95#define SWAP_BE16(x) (x)
96#define SWAP_BE32(x) (x)
97#define SWAP_BE64(x) (x)
98#define SWAP_LE16(x) bswap_16(x)
99#define SWAP_LE32(x) bswap_32(x)
100#define SWAP_LE64(x) bswap_64(x)
101#else
102#define IS_LITTLE_ENDIAN 1
Rob Landley055cfcb2007-01-14 20:20:06 -0500103#define SWAP_BE16(x) bswap_16(x)
104#define SWAP_BE32(x) bswap_32(x)
105#define SWAP_BE64(x) bswap_64(x)
106#define SWAP_LE16(x) (x)
107#define SWAP_LE32(x) (x)
108#define SWAP_LE64(x) (x)
109#endif
Rob Landleyfd1c5ba2007-02-03 14:10:00 -0500110
111// Some versions of gcc produce spurious "may be uninitialized" warnings in
112// cases where it provably can't happen. Unfortunately, although this warning
113// is calculated and produced separately from the "is definitely used
114// uninitialized" warnings, there's no way to turn off the broken spurious "may
115// be" warnings without also turning off the non-broken "is" warnings.
116
117#if CFG_TOYBOX_DEBUG
118#define GCC_BUG =0
119#else
120#define GCC_BUG
121#endif
Georgi Chorbadzhiyski522d9062012-03-16 06:42:08 -0500122
123#if defined(__APPLE__) || defined(__ANDROID__)
124ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
125ssize_t getline(char **lineptr, size_t *n, FILE *stream);
126#endif