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 // We don't define GNU_dammit because we're not part of the gnu project, and
45 // don't want to get any FSF on us. Unfortunately glibc (gnu libc)
46 // won't give us Linux syscall wrappers without claiming to be part of the
47 // gnu project (because Stallman's "GNU owns Linux" revisionist history
48 // crusade includes the kernel, even though Linux was inspired by Minix).
49
50 // We use most non-posix Linux syscalls directly through the syscall() wrapper,
51 // but even many posix-2008 functions aren't provided by glibc unless you
52 // claim it's in the name of Gnu.
53
54 #if defined(__GLIBC__)
55 // "Function prototypes shall be provided." but aren't.
56 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
57 char *crypt(const char *key, const char *salt);
58
59 // According to posix, #include header, get a function definition. But glibc...
60 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
61 #include <wchar.h>
62 int wcwidth(wchar_t wc);
63
64 // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
65 #include <time.h>
66 char *strptime(const char *buf, const char *format, struct tm *tm);
67
68 // They didn't like posix basename so they defined another function with the
69 // same name and if you include libgen.h it #defines basename to something
70 // else (where they implemented the real basename), and that define breaks
71 // the table entry for the basename command. They didn't make a new function
72 // with a different name for their new behavior because gnu.
73 //
74 // Solution: don't use their broken header, provide an inline to redirect the
75 // correct name to the broken name.
76
77 char *dirname(char *path);
78 char *__xpg_basename(char *path);
79 static inline char *basename(char *path) { return __xpg_basename(path); }
80
81 // uClibc pretends to be glibc and copied a lot of its bugs, but has a few more
82 #if defined(__UCLIBC__)
83 #include <unistd.h>
84 #include <stdio.h>
85 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
86 char *stpcpy(char *dest, const char *src);
87 pid_t getsid(pid_t pid);
88
89 // uClibc's last-ever release was in 2012, so of course it doesn't define
90 // any flag newer than MS_MOVE, which was added in 2001 (linux 2.5.0.5),
91 // eleven years earlier.
92
93 #include <sys/mount.h>
94 #ifndef MS_MOVE
95 #define MS_MOVE       (1<<13)
96 #endif
97 #ifndef MS_REC
98 #define MS_REC        (1<<14)
99 #endif
100 #ifndef MS_SILENT
101 #define MS_SILENT     (1<<15)
102 #endif
103 #ifndef MS_UNBINDABLE
104 #define MS_UNBINDABLE (1<<17)
105 #endif
106 #ifndef MS_PRIVATE
107 #define MS_PRIVATE    (1<<18)
108 #endif
109 #ifndef MS_SLAVE
110 #define MS_SLAVE      (1<<19)
111 #endif
112 #ifndef MS_SHARED
113 #define MS_SHARED     (1<<20)
114 #endif
115
116 // When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
117 #elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
118 #define fstatat fstatat64
119 int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
120 int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
121 char *stpcpy(char *dest, const char *src);
122 #include <sys/stat.h>
123 int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
124 int openat(int dirfd, const char *pathname, int flags, ...);
125 #include <dirent.h>
126 DIR *fdopendir(int fd);
127 #include <unistd.h>
128 int fchownat(int dirfd, const char *pathname,
129                     uid_t owner, gid_t group, int flags);
130 int isblank(int c);
131 int unlinkat(int dirfd, const char *pathname, int flags);
132 #include <stdio.h>
133 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
134
135 // Straight from posix-2008, things old glibc had but didn't prototype
136
137 int faccessat(int fd, const char *path, int amode, int flag);
138 int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
139 int mkdirat(int fd, const char *path, mode_t mode);
140 int symlinkat(const char *path1, int fd, const char *path2);
141 int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
142 #include <sys/time.h>
143 int futimens(int fd, const struct timespec times[2]);
144 int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
145
146 #ifndef MNT_DETACH
147 #define MNT_DETACH 2
148 #endif
149 #endif // Old glibc
150
151 #endif // glibc in general
152
153 #if !defined(__GLIBC__) && !defined(__BIONIC__)
154 // POSIX basename.
155 #include <libgen.h>
156 #endif
157
158 // glibc was handled above; for 32-bit bionic we need to avoid a collision
159 // with toybox's basename_r so we can't include <libgen.h> even though that
160 // would give us a POSIX basename(3).
161 #if defined(__BIONIC__)
162 char *basename(char *path);
163 char *dirname(char *path);
164 #endif
165
166 // Work out how to do endianness
167
168 #ifndef __APPLE__
169 #include <byteswap.h>
170 #include <endian.h>
171
172 #if __BYTE_ORDER == __BIG_ENDIAN
173 #define IS_BIG_ENDIAN 1
174 #else
175 #define IS_BIG_ENDIAN 0
176 #endif
177
178 int clearenv(void);
179 #else
180
181 #ifdef __BIG_ENDIAN__
182 #define IS_BIG_ENDIAN 1
183 #else
184 #define IS_BIG_ENDIAN 0
185 #endif
186
187 #endif
188
189 #if IS_BIG_ENDIAN
190 #define IS_LITTLE_ENDIAN 0
191 #define SWAP_BE16(x) (x)
192 #define SWAP_BE32(x) (x)
193 #define SWAP_BE64(x) (x)
194 #define SWAP_LE16(x) bswap_16(x)
195 #define SWAP_LE32(x) bswap_32(x)
196 #define SWAP_LE64(x) bswap_64(x)
197 #else
198 #define IS_LITTLE_ENDIAN 1
199 #define SWAP_BE16(x) bswap_16(x)
200 #define SWAP_BE32(x) bswap_32(x)
201 #define SWAP_BE64(x) bswap_64(x)
202 #define SWAP_LE16(x) (x)
203 #define SWAP_LE32(x) (x)
204 #define SWAP_LE64(x) (x)
205 #endif
206
207 #if defined(__APPLE__) \
208     || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
209 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
210 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
211 #endif
212
213 // Linux headers not listed by POSIX or LSB
214 #include <sys/mount.h>
215 #include <sys/swap.h>
216
217 // Android is missing some headers and functions
218 // "generated/config.h" is included first
219 #if CFG_TOYBOX_SHADOW
220 #include <shadow.h>
221 #endif
222 #if CFG_TOYBOX_UTMPX
223 #include <utmpx.h>
224 #else
225 struct utmpx {int ut_type;};
226 #define USER_PROCESS 0
227 static inline struct utmpx *getutxent(void) {return 0;}
228 static inline void setutxent(void) {;}
229 static inline void endutxent(void) {;}
230 #endif
231
232 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
233 #include <fcntl.h>
234 #ifndef O_NOFOLLOW
235 #define O_NOFOLLOW 0
236 #endif
237
238 #ifndef O_NOATIME
239 #define O_NOATIME 01000000
240 #endif
241
242 #ifndef O_CLOEXEC
243 #define O_CLOEXEC 02000000
244 #endif
245
246 #ifndef O_PATH
247 #define O_PATH   010000000
248 #endif
249
250 // Glibc won't give you linux-kernel constants unless you say "no, a BUD lite"
251 // even though linux has nothing to do with the FSF and never has.
252 #ifndef F_SETPIPE_SZ
253 #define F_SETPIPE_SZ 1031
254 #endif
255
256 #ifndef F_GETPIPE_SZ
257 #define F_GETPIPE_SZ 1032
258 #endif
259
260 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
261     && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
262 typedef double FLOAT;
263 #else
264 typedef float FLOAT;
265 #endif
266
267 #ifndef __uClinux__
268 pid_t xfork(void);
269 #endif
270
271 //#define strncpy(...) @@strncpyisbadmmkay@@
272 //#define strncat(...) @@strncatisbadmmkay@@
273