OSDN Git Service

Merge remote-tracking branch \'toybox/master\' into HEAD
[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 // For musl
8 #define _ALL_SOURCE
9
10 // Test for gcc (using compiler builtin #define)
11
12 #ifdef __GNUC__
13 #define noreturn        __attribute__((noreturn))
14 #define printf_format   __attribute__((format(printf, 1, 2)))
15 #else
16 #define noreturn
17 #define printf_format
18 #endif
19
20 // Always use long file support.
21 #define _FILE_OFFSET_BITS 64
22
23 // This isn't in the spec, but it's how we determine what libc we're using.
24
25 #include <features.h>
26
27 // Types various replacement prototypes need
28 #include <sys/types.h>
29
30 // Various constants old build environments might not have even if kernel does
31
32 #ifndef AT_FDCWD
33 #define AT_FDCWD -100
34 #endif
35
36 #ifndef AT_SYMLINK_NOFOLLOW
37 #define AT_SYMLINK_NOFOLLOW 0x100
38 #endif
39
40 #ifndef AT_REMOVEDIR
41 #define AT_REMOVEDIR 0x200
42 #endif
43
44 #ifndef RLIMIT_RTTIME
45 #define RLIMIT_RTTIME 15
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 // 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 //
78 // Solution: don't use their broken header, provide an inline to redirect the
79 // correct name to the broken name.
80
81 char *dirname(char *path);
82 char *__xpg_basename(char *path);
83 static inline char *basename(char *path) { return __xpg_basename(path); }
84
85 // 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>
89 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
90 char *stpcpy(char *dest, const char *src);
91 pid_t getsid(pid_t pid);
92
93 // 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
97 #include <sys/mount.h>
98 #ifndef MS_MOVE
99 #define MS_MOVE       (1<<13)
100 #endif
101 #ifndef MS_REC
102 #define MS_REC        (1<<14)
103 #endif
104 #ifndef MS_SILENT
105 #define MS_SILENT     (1<<15)
106 #endif
107 #ifndef MS_UNBINDABLE
108 #define MS_UNBINDABLE (1<<17)
109 #endif
110 #ifndef MS_PRIVATE
111 #define MS_PRIVATE    (1<<18)
112 #endif
113 #ifndef MS_SLAVE
114 #define MS_SLAVE      (1<<19)
115 #endif
116 #ifndef MS_SHARED
117 #define MS_SHARED     (1<<20)
118 #endif
119 #ifndef MS_RELATIME
120 #define MS_RELATIME (1<<21)
121 #endif
122
123 // When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
124 #elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
125 #define fstatat fstatat64
126 int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
127 int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
128 char *stpcpy(char *dest, const char *src);
129 #include <sys/stat.h>
130 int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
131 int openat(int dirfd, const char *pathname, int flags, ...);
132 #include <dirent.h>
133 DIR *fdopendir(int fd);
134 #include <unistd.h>
135 int fchownat(int dirfd, const char *pathname,
136                     uid_t owner, gid_t group, int flags);
137 int isblank(int c);
138 int unlinkat(int dirfd, const char *pathname, int flags);
139 #include <stdio.h>
140 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
141
142 // Straight from posix-2008, things old glibc had but didn't prototype
143
144 int faccessat(int fd, const char *path, int amode, int flag);
145 int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
146 int mkdirat(int fd, const char *path, mode_t mode);
147 int symlinkat(const char *path1, int fd, const char *path2);
148 int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
149 #include <sys/time.h>
150 int futimens(int fd, const struct timespec times[2]);
151 int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
152
153 #ifndef MNT_DETACH
154 #define MNT_DETACH 2
155 #endif
156 #endif // Old glibc
157
158 #endif // glibc in general
159
160 #if !defined(__GLIBC__) && !defined(__BIONIC__)
161 // POSIX basename.
162 #include <libgen.h>
163 #endif
164
165 // glibc was handled above; for 32-bit bionic we need to avoid a collision
166 // with toybox's basename_r so we can't include <libgen.h> even though that
167 // would give us a POSIX basename(3).
168 #if defined(__BIONIC__)
169 char *basename(char *path);
170 char *dirname(char *path);
171 #endif
172
173 // Work out how to do endianness
174
175 #ifndef __APPLE__
176 #include <byteswap.h>
177 #include <endian.h>
178
179 #if __BYTE_ORDER == __BIG_ENDIAN
180 #define IS_BIG_ENDIAN 1
181 #else
182 #define IS_BIG_ENDIAN 0
183 #endif
184
185 int clearenv(void);
186 #else
187
188 #ifdef __BIG_ENDIAN__
189 #define IS_BIG_ENDIAN 1
190 #else
191 #define IS_BIG_ENDIAN 0
192 #endif
193
194 #endif
195
196 #if IS_BIG_ENDIAN
197 #define IS_LITTLE_ENDIAN 0
198 #define SWAP_BE16(x) (x)
199 #define SWAP_BE32(x) (x)
200 #define SWAP_BE64(x) (x)
201 #define SWAP_LE16(x) bswap_16(x)
202 #define SWAP_LE32(x) bswap_32(x)
203 #define SWAP_LE64(x) bswap_64(x)
204 #else
205 #define IS_LITTLE_ENDIAN 1
206 #define SWAP_BE16(x) bswap_16(x)
207 #define SWAP_BE32(x) bswap_32(x)
208 #define SWAP_BE64(x) bswap_64(x)
209 #define SWAP_LE16(x) (x)
210 #define SWAP_LE32(x) (x)
211 #define SWAP_LE64(x) (x)
212 #endif
213
214 #if defined(__APPLE__) \
215     || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
216 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
217 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
218 #endif
219
220 // Linux headers not listed by POSIX or LSB
221 #include <sys/mount.h>
222 #include <sys/swap.h>
223
224 // Android is missing some headers and functions
225 // "generated/config.h" is included first
226 #if CFG_TOYBOX_SHADOW
227 #include <shadow.h>
228 #endif
229 #if CFG_TOYBOX_UTMPX
230 #include <utmpx.h>
231 #else
232 struct utmpx {int ut_type;};
233 #define USER_PROCESS 0
234 static inline struct utmpx *getutxent(void) {return 0;}
235 static inline void setutxent(void) {;}
236 static inline void endutxent(void) {;}
237 #endif
238
239 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
240 #include <fcntl.h>
241 #ifndef O_NOFOLLOW
242 #define O_NOFOLLOW 0
243 #endif
244
245 #ifndef O_NOATIME
246 #define O_NOATIME 01000000
247 #endif
248
249 #ifndef O_CLOEXEC
250 #define O_CLOEXEC 02000000
251 #endif
252
253 #ifndef O_PATH
254 #define O_PATH   010000000
255 #endif
256
257 // Glibc won't give you linux-kernel constants unless you say "no, a BUD lite"
258 // even though linux has nothing to do with the FSF and never has.
259 #ifndef F_SETPIPE_SZ
260 #define F_SETPIPE_SZ 1031
261 #endif
262
263 #ifndef F_GETPIPE_SZ
264 #define F_GETPIPE_SZ 1032
265 #endif
266
267 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
268     && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
269 typedef double FLOAT;
270 #else
271 typedef float FLOAT;
272 #endif
273
274 #ifndef __uClinux__
275 pid_t xfork(void);
276 #endif
277
278 //#define strncpy(...) @@strncpyisbadmmkay@@
279 //#define strncat(...) @@strncatisbadmmkay@@
280