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 #else
15 #define noreturn
16 #endif
17
18 // Always use long file support.
19 #define _FILE_OFFSET_BITS 64
20
21 // This isn't in the spec, but it's how we determine what libc we're using.
22
23 #include <features.h>
24
25 // Various constants old build environments might not have even if kernel does
26
27 #ifndef AT_FDCWD
28 #define AT_FDCWD -100
29 #endif
30
31 #ifndef AT_SYMLINK_NOFOLLOW
32 #define AT_SYMLINK_NOFOLLOW 0x100
33 #endif
34
35 #ifndef AT_REMOVEDIR
36 #define AT_REMOVEDIR 0x200
37 #endif
38
39 // We don't define GNU_dammit because we're not part of the gnu project, and
40 // don't want to get any FSF on us. Unfortunately glibc (gnu libc)
41 // won't give us Linux syscall wrappers without claiming to be part of the
42 // gnu project (because Stallman's "GNU owns Linux" revisionist history
43 // crusade includes the kernel, even though Linux was inspired by Minix).
44
45 // We use most non-posix Linux syscalls directly through the syscall() wrapper,
46 // but even many posix-2008 functions aren't provided by glibc unless you
47 // claim it's in the name of Gnu.
48
49 #if defined(__GLIBC__)
50 // "Function prototypes shall be provided." but aren't.
51 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
52 char *crypt(const char *key, const char *salt);
53
54 // According to posix, #include header, get a function definition. But glibc...
55 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/wcwidth.html
56 #include <wchar.h>
57 int wcwidth(wchar_t wc);
58
59 // see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
60 #include <time.h>
61 char *strptime(const char *buf, const char *format, struct tm *tm);
62
63 // uClibc pretends to be glibc and copied a lot of its bugs, but has a few more
64 #if defined(__UCLIBC__)
65 #include <unistd.h>
66 #include <stdio.h>
67 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
68 char *stpcpy(char *dest, const char *src);
69 pid_t getsid(pid_t pid);
70
71 // uClibc's last-ever release was in 2012, so of course it doesn't define
72 // any flag newer than MS_MOVE, which was added in 2001 (linux 2.5.0.5),
73 // eleven years earlier.
74
75 #include <sys/mount.h>
76 #ifndef MS_MOVE
77 #define MS_MOVE       (1<<13)
78 #endif
79 #ifndef MS_REC
80 #define MS_REC        (1<<14)
81 #endif
82 #ifndef MS_SILENT
83 #define MS_SILENT     (1<<15)
84 #endif
85 #ifndef MS_UNBINDABLE
86 #define MS_UNBINDABLE (1<<17)
87 #endif
88 #ifndef MS_PRIVATE
89 #define MS_PRIVATE    (1<<18)
90 #endif
91 #ifndef MS_SLAVE
92 #define MS_SLAVE      (1<<19)
93 #endif
94 #ifndef MS_SHARED
95 #define MS_SHARED     (1<<20)
96 #endif
97
98 // When building under obsolete glibc (Ubuntu 8.04-ish), hold its hand a bit.
99 #elif __GLIBC__ == 2 && __GLIBC_MINOR__ < 10
100 #define fstatat fstatat64
101 int fstatat64(int dirfd, const char *pathname, void *buf, int flags);
102 int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
103 char *stpcpy(char *dest, const char *src);
104 #include <sys/stat.h>
105 int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);
106 int openat(int dirfd, const char *pathname, int flags, ...);
107 #include <dirent.h>
108 DIR *fdopendir(int fd);
109 #include <unistd.h>
110 int fchownat(int dirfd, const char *pathname,
111                     uid_t owner, gid_t group, int flags);
112 int isblank(int c);
113 int unlinkat(int dirfd, const char *pathname, int flags);
114 #include <stdio.h>
115 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
116
117 // Straight from posix-2008, things old glibc had but didn't prototype
118
119 int faccessat(int fd, const char *path, int amode, int flag);
120 int linkat(int fd1, const char *path1, int fd2, const char *path2, int flag);
121 int mkdirat(int fd, const char *path, mode_t mode);
122 int symlinkat(const char *path1, int fd, const char *path2);
123 int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
124 #include <sys/time.h>
125 int futimens(int fd, const struct timespec times[2]);
126 int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
127
128 #ifndef MNT_DETACH
129 #define MNT_DETACH 2
130 #endif
131 #endif
132
133 #endif
134
135 #ifdef __MUSL__
136 #include <unistd.h>
137 // Without this "rm -r dir" fails with "is directory".
138 #define faccessat(A, B, C, D) faccessat(A, B, C, 0)
139 #endif
140
141 // Work out how to do endianness
142
143 #ifndef __APPLE__
144 #include <byteswap.h>
145 #include <endian.h>
146
147 #if __BYTE_ORDER == __BIG_ENDIAN
148 #define IS_BIG_ENDIAN 1
149 #else
150 #define IS_BIG_ENDIAN 0
151 #endif
152
153 int clearenv(void);
154 #else
155
156 #ifdef __BIG_ENDIAN__
157 #define IS_BIG_ENDIAN 1
158 #else
159 #define IS_BIG_ENDIAN 0
160 #endif
161
162 #endif
163
164 #if IS_BIG_ENDIAN
165 #define IS_LITTLE_ENDIAN 0
166 #define SWAP_BE16(x) (x)
167 #define SWAP_BE32(x) (x)
168 #define SWAP_BE64(x) (x)
169 #define SWAP_LE16(x) bswap_16(x)
170 #define SWAP_LE32(x) bswap_32(x)
171 #define SWAP_LE64(x) bswap_64(x)
172 #else
173 #define IS_LITTLE_ENDIAN 1
174 #define SWAP_BE16(x) bswap_16(x)
175 #define SWAP_BE32(x) bswap_32(x)
176 #define SWAP_BE64(x) bswap_64(x)
177 #define SWAP_LE16(x) (x)
178 #define SWAP_LE32(x) (x)
179 #define SWAP_LE64(x) (x)
180 #endif
181
182 #if defined(__APPLE__) \
183     || (defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 10)
184 ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
185 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
186 #endif
187
188 // Linux headers not listed by POSIX or LSB
189 #include <sys/mount.h>
190 #include <sys/swap.h>
191
192 // Android is missing some headers and functions
193 // "generated/config.h" is included first
194 #if CFG_TOYBOX_SHADOW
195 #include <shadow.h>
196 #endif
197 #if CFG_TOYBOX_UTMPX
198 #include <utmpx.h>
199 #endif
200 #if CFG_TOYBOX_PTY
201 #include <pty.h>
202 #else
203 pid_t forkpty(int *amaster, char *name, void *termp, void *winp);
204 #endif
205
206
207 // Some systems don't define O_NOFOLLOW, and it varies by architecture, so...
208 #include <fcntl.h>
209 #ifndef O_NOFOLLOW
210 #define O_NOFOLLOW 0
211 #endif
212
213 #ifndef O_CLOEXEC
214 #define O_CLOEXEC 02000000
215 #endif
216
217 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
218     && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
219 typedef double FLOAT;
220 #else
221 typedef float FLOAT;
222 #endif
223
224 #ifndef __uClinux__
225 pid_t xfork(void);
226 #endif
227
228 //#define strncpy(...) @@strncpyisbadmmkay@@
229 //#define strncat(...) @@strcatisbadmmkay@@