OSDN Git Service

Patches to commands for issues reported from static analysis tool.
[android-x86/external-toybox.git] / lib / portability.h
1 // Workarounds for horrible build environment idiosyncrasies.
2
3 // 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
7 // The tendency of gcc to produce stupid warnings continues with
8 // warn_unused_result, which warns about things like ignoring the return code
9 // 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
16 // For musl
17 #define _ALL_SOURCE
18
19 // Test for gcc (using compiler builtin #define)
20
21 #ifdef __GNUC__
22 #define noreturn        __attribute__((noreturn))
23 #else
24 #define noreturn
25 #endif
26
27 // Always use long file support.
28 #define _FILE_OFFSET_BITS 64
29
30 // This isn't in the spec, but it's how we determine what libc we're using.
31
32 #include <features.h>
33
34 // Various constants old build environments might not have even if kernel does
35
36 #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
48 // 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
58 #if defined(__GLIBC__)
59 // "Function prototypes shall be provided." but aren't.
60 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
61 char *crypt(const char *key, const char *salt);
62
63 // 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>
66 int wcwidth(wchar_t wc);
67
68 // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
69 #include <time.h>
70 char *strptime(const char *buf, const char *format, struct tm *tm);
71
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>
76 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
77
78 // When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
79 #elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
80 #define fstatat fstatat64
81 int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
82 int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
83 char *stpcpy(char *dest, const char *src);
84 #include <sys/stat.h>
85 int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
86 int openat(int dirfd, const char *pathname, int flags, ...);
87 #include <dirent.h>
88 DIR *fdopendir(int fd);
89 #include <unistd.h>
90 int fchownat(int dirfd, const char *pathname,
91                     uid_t owner, gid_t group, int flags);
92 int isblank(int c);
93 int unlinkat(int dirfd, const char *pathname, int flags);
94 #include <stdio.h>
95 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
96
97 // Straight from posix-2008, things old glibc had but didn't prototype
98
99 int faccessat(int fd, const char *path, int amode, int flag);
100 int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
101 int mkdirat(int fd, const char *path, mode_t mode);
102 int symlinkat(const char *path1, int fd, const char *path2);
103 int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
104 #include <sys/time.h>
105 int futimens(int fd, const struct timespec times[2]);
106 int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
107
108 #ifndef MNT_DETACH
109 #define MNT_DETACH 2
110 #endif
111 #endif
112
113 #endif
114
115 // Work out how to do endianness
116
117 #ifndef __APPLE__
118 #include <byteswap.h>
119 #include <endian.h>
120
121 #if __BYTE_ORDER == __BIG_ENDIAN
122 #define IS_BIG_ENDIAN 1
123 #else
124 #define IS_BIG_ENDIAN 0
125 #endif
126
127 int clearenv(void);
128 #else
129
130 #ifdef __BIG_ENDIAN__
131 #define IS_BIG_ENDIAN 1
132 #else
133 #define IS_BIG_ENDIAN 0
134 #endif
135
136 #endif
137
138 #if IS_BIG_ENDIAN
139 #define IS_LITTLE_ENDIAN 0
140 #define SWAP_BE16(x) (x)
141 #define SWAP_BE32(x) (x)
142 #define SWAP_BE64(x) (x)
143 #define SWAP_LE16(x) bswap_16(x)
144 #define SWAP_LE32(x) bswap_32(x)
145 #define SWAP_LE64(x) bswap_64(x)
146 #else
147 #define IS_LITTLE_ENDIAN 1
148 #define SWAP_BE16(x) bswap_16(x)
149 #define SWAP_BE32(x) bswap_32(x)
150 #define SWAP_BE64(x) bswap_64(x)
151 #define SWAP_LE16(x) (x)
152 #define SWAP_LE32(x) (x)
153 #define SWAP_LE64(x) (x)
154 #endif
155
156 #if defined(__APPLE__) || defined(__ANDROID__) \
157     || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
158 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
159 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
160 #endif
161
162 // Linux headers not listed by POSIX or LSB
163 #include <shadow.h>
164 #include <sys/mount.h>
165 #include <sys/swap.h>
166
167 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
168 #include <fcntl.h>
169 #ifndef O_NOFOLLOW
170 #define O_NOFOLLOW 0
171 #endif
172
173 #ifndef O_CLOEXEC
174 #define O_CLOEXEC 02000000
175 #endif
176
177 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
178     && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
179 typedef double FLOAT;
180 #else
181 typedef float FLOAT;
182 #endif
183