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