OSDN Git Service

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