OSDN Git Service

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