style fixes
[platform/upstream/busybox.git] / include / platform.h
1 /* vi: set sw=4 ts=4: */
2 /*
3    Copyright 2006, Bernhard Fischer
4
5    Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6 */
7 #ifndef __PLATFORM_H
8 #define __PLATFORM_H    1
9
10 /* Convenience macros to test the version of gcc. */
11 #undef __GNUC_PREREQ
12 #if defined __GNUC__ && defined __GNUC_MINOR__
13 # define __GNUC_PREREQ(maj, min) \
14                 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
15 #else
16 # define __GNUC_PREREQ(maj, min) 0
17 #endif
18
19 /* __restrict is known in EGCS 1.2 and above. */
20 #if !__GNUC_PREREQ (2,92)
21 # ifndef __restrict
22 #  define __restrict     /* Ignore */
23 # endif
24 #endif
25
26 /* Define macros for some gcc attributes.  This permits us to use the
27    macros freely, and know that they will come into play for the
28    version of gcc in which they are supported.  */
29
30 #if !__GNUC_PREREQ (2,7)
31 # ifndef __attribute__
32 #  define __attribute__(x)
33 # endif
34 #endif
35
36 #undef inline
37 #if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
38 /* it's a keyword */
39 #else
40 # if __GNUC_PREREQ (2,7)
41 #  define inline __inline__
42 # else
43 #  define inline
44 # endif
45 #endif
46
47 #ifndef __const
48 # define __const const
49 #endif
50
51 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
52 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
53 # define ATTRIBUTE_PACKED __attribute__ ((__packed__))
54 # define ATTRIBUTE_ALIGNED(m) __attribute__ ((__aligned__(m)))
55 # if __GNUC_PREREQ (3,0)
56 #  define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline)) inline
57 #  define ATTRIBUTE_DEPRECATED __attribute__ ((__deprecated__))
58 # else
59 #  define ATTRIBUTE_ALWAYS_INLINE inline
60 #  define ATTRIBUTE_DEPRECATED /* n/a */
61 # endif
62
63 /* -fwhole-program makes all symbols local. The attribute externally_visible
64    forces a symbol global.  */
65 # if __GNUC_PREREQ (4,1)
66 #  define ATTRIBUTE_EXTERNALLY_VISIBLE __attribute__ ((__externally_visible__))
67 # else
68 #  define ATTRIBUTE_EXTERNALLY_VISIBLE
69 # endif /* GNUC >= 4.1 */
70
71 /* We use __extension__ in some places to suppress -pedantic warnings
72    about GCC extensions.  This feature didn't work properly before
73    gcc 2.8.  */
74 #if !__GNUC_PREREQ (2,8)
75 # ifndef __extension__
76 #  define __extension__
77 # endif
78 #endif
79
80 /* gcc-2.95 had no va_copy but only __va_copy. */
81 #if !__GNUC_PREREQ (3,0)
82 # include <stdarg.h>
83 # if !defined va_copy && defined __va_copy
84 #  define va_copy(d,s) __va_copy((d),(s))
85 # endif
86 #endif
87
88 /* ---- Endian Detection ------------------------------------ */
89
90 #if (defined __digital__ && defined __unix__)
91 # include <sex.h>
92 # define __BIG_ENDIAN__ (BYTE_ORDER == BIG_ENDIAN)
93 # define __BYTE_ORDER BYTE_ORDER
94 #elif !defined __APPLE__
95 # include <byteswap.h>
96 # include <endian.h>
97 #endif
98
99 #ifdef __BIG_ENDIAN__
100 # define BB_BIG_ENDIAN 1
101 # define BB_LITTLE_ENDIAN 0
102 #elif __BYTE_ORDER == __BIG_ENDIAN
103 # define BB_BIG_ENDIAN 1
104 # define BB_LITTLE_ENDIAN 0
105 #else
106 # define BB_BIG_ENDIAN 0
107 # define BB_LITTLE_ENDIAN 1
108 #endif
109
110 #if BB_BIG_ENDIAN
111 #define SWAP_BE16(x) (x)
112 #define SWAP_BE32(x) (x)
113 #define SWAP_BE64(x) (x)
114 #define SWAP_LE16(x) bswap_16(x)
115 #define SWAP_LE32(x) bswap_32(x)
116 #define SWAP_LE64(x) bswap_64(x)
117 #else
118 #define SWAP_BE16(x) bswap_16(x)
119 #define SWAP_BE32(x) bswap_32(x)
120 #define SWAP_BE64(x) bswap_64(x)
121 #define SWAP_LE16(x) (x)
122 #define SWAP_LE32(x) (x)
123 #define SWAP_LE64(x) (x)
124 #endif
125
126 /* ---- Networking ------------------------------------------ */
127 #ifndef __APPLE__
128 # include <arpa/inet.h>
129 #else
130 # include <netinet/in.h>
131 #endif
132
133 #ifndef __socklen_t_defined
134 typedef int socklen_t;
135 #endif
136
137 /* ---- Compiler dependent settings ------------------------- */
138 #if (defined __digital__ && defined __unix__)
139 # undef HAVE_MNTENT_H
140 #else
141 # define HAVE_MNTENT_H 1
142 #endif /* ___digital__ && __unix__ */
143
144 /* linux/loop.h relies on __u64. Make sure we have that as a proper type
145  * until userspace is widely fixed.  */
146 #ifndef __GNUC__
147 #if defined __INTEL_COMPILER
148 __extension__ typedef __signed__ long long __s64;
149 __extension__ typedef unsigned long long __u64;
150 #endif /* __INTEL_COMPILER */
151 #endif /* ifndef __GNUC__ */
152
153 /*----- Kernel versioning ------------------------------------*/
154 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
155
156 /* ---- miscellaneous --------------------------------------- */
157
158 #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ < 5 && \
159         !defined(__dietlibc__) && \
160         !defined(_NEWLIB_VERSION) && \
161         !(defined __digital__ && defined __unix__)
162 # error "Sorry, this libc version is not supported :("
163 #endif
164
165 // Don't perpetuate e2fsck crap into the headers.  Clean up e2fsck instead.
166
167 #if defined __GLIBC__ || defined __UCLIBC__ \
168         || defined __dietlibc__ || defined _NEWLIB_VERSION
169 #include <features.h>
170 #define HAVE_FEATURES_H
171 #include <stdint.h>
172 #define HAVE_STDINT_H
173 #else
174 /* Largest integral types.  */
175 #if __BIG_ENDIAN__
176 typedef long                intmax_t;
177 typedef unsigned long       uintmax_t;
178 #else
179 __extension__
180 typedef long long           intmax_t;
181 __extension__
182 typedef unsigned long long  uintmax_t;
183 #endif
184 #endif
185
186 /* Size-saving "small" ints (arch-dependent) */
187 #if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__)
188 /* add other arches which benefit from this... */
189 typedef signed char smallint;
190 typedef unsigned char smalluint;
191 #else
192 /* for arches where byte accesses generate larger code: */
193 typedef int smallint;
194 typedef unsigned smalluint;
195 #endif
196
197 /* ISO C Standard:  7.16  Boolean type and values  <stdbool.h> */
198 #if (defined __digital__ && defined __unix__)
199 /* old system without (proper) C99 support */
200 #define bool smalluint
201 #else
202 /* modern system, so use it */
203 #include <stdbool.h>
204 #endif
205
206
207 /* uclibc does not implement daemon() for no-mmu systems.
208  * For 0.9.29 and svn, __ARCH_USE_MMU__ indicates no-mmu reliably.
209  * For earlier versions there is no reliable way to check if we are building
210  * for a mmu-less system; the user should pass EXTRA_CFLAGS="-DBB_NOMMU"
211  * on his own.
212  */
213 #if defined __UCLIBC__ && __UCLIBC_MAJOR__ >= 0 && __UCLIBC_MINOR__ >= 9 && \
214     __UCLIBC_SUBLEVEL__ > 28 && !defined __ARCH_USE_MMU__
215 #define BB_MMU 0
216 #define BB_NOMMU 1
217 #define USE_FOR_NOMMU(...) __VA_ARGS__
218 #define USE_FOR_MMU(...)
219 #else
220 #define BB_MMU 1
221 /* BB_NOMMU is not defined in this case! */
222 #define USE_FOR_NOMMU(...)
223 #define USE_FOR_MMU(...) __VA_ARGS__
224 #endif
225
226 /* Platforms that haven't got dprintf need to implement fdprintf() in
227  * libbb.  This would require a platform.c.  It's not going to be cleaned
228  * out of the tree, so stop saying it should be. */
229 #if !defined(__dietlibc__)
230 /* Needed for: glibc */
231 /* Not needed for: dietlibc */
232 /* Others: ?? (add as needed) */
233 #define fdprintf dprintf
234 #endif
235
236 #if defined(__dietlibc__)
237 static ATTRIBUTE_ALWAYS_INLINE char* strchrnul(const char *s, char c) {
238         while (*s && *s != c) ++s;
239         return (char*)s;
240 }
241 #endif
242
243 /* Don't use lchown with glibc older than 2.1.x ... uClibc lacks it */
244 #if (defined __GLIBC__ && __GLIBC__ <= 2 && __GLIBC_MINOR__ < 1) || \
245     defined __UC_LIBC__
246 # define lchown chown
247 #endif
248
249 /* THIS SHOULD BE CLEANED OUT OF THE TREE ENTIRELY */
250 /* FIXME: fix tar.c! */
251 #ifndef FNM_LEADING_DIR
252 #define FNM_LEADING_DIR 0
253 #endif
254
255 #if (defined __digital__ && defined __unix__)
256 #include <standards.h>
257 #define HAVE_STANDARDS_H
258 #include <inttypes.h>
259 #define HAVE_INTTYPES_H
260 #define PRIu32 "u"
261
262 /* use legacy setpgrp(pid_t,pid_t) for now.  move to platform.c */
263 #define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me,__me); } while (0)
264
265 #if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
266 #define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
267 #endif
268 #if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
269 #define ADJ_FREQUENCY MOD_FREQUENCY
270 #endif
271 #if !defined ADJ_TIMECONST && defined MOD_TIMECONST
272 #define ADJ_TIMECONST MOD_TIMECONST
273 #endif
274 #if !defined ADJ_TICK && defined MOD_CLKB
275 #define ADJ_TICK MOD_CLKB
276 #endif
277
278 #else
279 #define bb_setpgrp() setpgrp()
280 #endif
281
282 #if defined(__linux__)
283 #include <sys/mount.h>
284 // Make sure we have all the new mount flags we actually try to use.
285 #ifndef MS_BIND
286 #define MS_BIND        (1<<12)
287 #endif
288 #ifndef MS_MOVE
289 #define MS_MOVE        (1<<13)
290 #endif
291 #ifndef MS_RECURSIVE
292 #define MS_RECURSIVE   (1<<14)
293 #endif
294 #ifndef MS_SILENT
295 #define MS_SILENT      (1<<15)
296 #endif
297
298 // The shared subtree stuff, which went in around 2.6.15
299 #ifndef MS_UNBINDABLE
300 #define MS_UNBINDABLE  (1<<17)
301 #endif
302 #ifndef MS_PRIVATE
303 #define MS_PRIVATE     (1<<18)
304 #endif
305 #ifndef MS_SLAVE
306 #define MS_SLAVE       (1<<19)
307 #endif
308 #ifndef MS_SHARED
309 #define MS_SHARED      (1<<20)
310 #endif
311
312
313 #if !defined(BLKSSZGET)
314 #define BLKSSZGET _IO(0x12, 104)
315 #endif
316 #if !defined(BLKGETSIZE64)
317 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
318 #endif
319 #endif
320
321 #endif  /* platform.h   */