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