blob: b5aa6836ea5a8743aed9ad6948b9ffb3b0efba85 [file] [log] [blame]
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00001/* vi: set sw=4 ts=4: */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Copyright 2006, Bernhard Reutner-Fischer
Denys Vlasenko867ffb92010-08-16 03:24:40 +02004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenko867ffb92010-08-16 03:24:40 +02006 */
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02007#ifndef BB_PLATFORM_H
Denis Vlasenkof81e8db2009-04-09 12:35:13 +00008#define BB_PLATFORM_H 1
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +00009
Dan Fandrich21a542d2009-10-27 11:05:00 +010010
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000011/* Convenience macros to test the version of gcc. */
12#undef __GNUC_PREREQ
13#if defined __GNUC__ && defined __GNUC_MINOR__
14# define __GNUC_PREREQ(maj, min) \
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000015 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000016#else
17# define __GNUC_PREREQ(maj, min) 0
18#endif
19
20/* __restrict is known in EGCS 1.2 and above. */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000021#if !__GNUC_PREREQ(2,92)
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000022# ifndef __restrict
Denys Vlasenko5a49d282009-05-19 13:18:45 +020023# define __restrict
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000024# endif
25#endif
26
Denis Vlasenko98636eb2008-05-09 17:59:34 +000027#if !__GNUC_PREREQ(2,7)
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000028# ifndef __attribute__
29# define __attribute__(x)
30# endif
31#endif
32
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000033#undef inline
Denis Vlasenkoa7189f02006-11-17 20:29:00 +000034#if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000035/* it's a keyword */
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020036#elif __GNUC_PREREQ(2,7)
37# define inline __inline__
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000038#else
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020039# define inline
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000040#endif
41
42#ifndef __const
43# define __const const
44#endif
45
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000046#define UNUSED_PARAM __attribute__ ((__unused__))
47#define NORETURN __attribute__ ((__noreturn__))
Denys Vlasenko53283ad2009-11-02 14:20:34 +010048/* "The malloc attribute is used to tell the compiler that a function
49 * may be treated as if any non-NULL pointer it returns cannot alias
50 * any other pointer valid when the function returns. This will often
51 * improve optimization. Standard functions with this property include
52 * malloc and calloc. realloc-like functions have this property as long
53 * as the old pointer is never referred to (including comparing it
54 * to the new pointer) after the function returns a non-NULL value."
55 */
56#define RETURNS_MALLOC __attribute__ ((malloc))
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000057#define PACKED __attribute__ ((__packed__))
58#define ALIGNED(m) __attribute__ ((__aligned__(m)))
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020059
Denis Vlasenko0f3a5802008-03-20 13:13:09 +000060/* __NO_INLINE__: some gcc's do not honor inlining! :( */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000061#if __GNUC_PREREQ(3,0) && !defined(__NO_INLINE__)
62# define ALWAYS_INLINE __attribute__ ((always_inline)) inline
Denis Vlasenko77f1ec12007-10-13 03:36:03 +000063/* I've seen a toolchain where I needed __noinline__ instead of noinline */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000064# define NOINLINE __attribute__((__noinline__))
65# if !ENABLE_WERROR
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000066# define DEPRECATED __attribute__ ((__deprecated__))
67# define UNUSED_PARAM_RESULT __attribute__ ((warn_unused_result))
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000068# else
Denys Vlasenko5a49d282009-05-19 13:18:45 +020069# define DEPRECATED
70# define UNUSED_PARAM_RESULT
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000071# endif
Denis Vlasenko98636eb2008-05-09 17:59:34 +000072#else
Denys Vlasenko5a49d282009-05-19 13:18:45 +020073# define ALWAYS_INLINE inline
74# define NOINLINE
75# define DEPRECATED
76# define UNUSED_PARAM_RESULT
Denis Vlasenko98636eb2008-05-09 17:59:34 +000077#endif
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +000078
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000079/* -fwhole-program makes all symbols local. The attribute externally_visible
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +020080 * forces a symbol global. */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000081#if __GNUC_PREREQ(4,1)
82# define EXTERNALLY_VISIBLE __attribute__(( visibility("default") ))
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000083//__attribute__ ((__externally_visible__))
Denis Vlasenko98636eb2008-05-09 17:59:34 +000084#else
85# define EXTERNALLY_VISIBLE
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +000086#endif
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000087
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010088/* At 4.4 gcc become much more anal about this, need to use "aliased" types */
89#if __GNUC_PREREQ(4,4)
90# define FIX_ALIASING __attribute__((__may_alias__))
91#else
92# define FIX_ALIASING
93#endif
94
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000095/* We use __extension__ in some places to suppress -pedantic warnings
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +020096 * about GCC extensions. This feature didn't work properly before
97 * gcc 2.8. */
Denis Vlasenko98636eb2008-05-09 17:59:34 +000098#if !__GNUC_PREREQ(2,8)
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000099# ifndef __extension__
100# define __extension__
101# endif
102#endif
103
Denis Vlasenko42b8daf2008-06-27 03:55:18 +0000104/* FAST_FUNC is a qualifier which (possibly) makes function call faster
105 * and/or smaller by using modified ABI. It is usually only needed
106 * on non-static, busybox internal functions. Recent versions of gcc
107 * optimize statics automatically. FAST_FUNC on static is required
108 * only if you need to match a function pointer's type */
Denis Vlasenkoac2b50e2008-06-27 04:30:48 +0000109#if __GNUC_PREREQ(3,0) && defined(i386) /* || defined(__x86_64__)? */
110/* stdcall makes callee to pop arguments from stack, not caller */
111# define FAST_FUNC __attribute__((regparm(3),stdcall))
Denis Vlasenko42b8daf2008-06-27 03:55:18 +0000112/* #elif ... - add your favorite arch today! */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000113#else
114# define FAST_FUNC
115#endif
116
Denis Vlasenkof81e8db2009-04-09 12:35:13 +0000117/* Make all declarations hidden (-fvisibility flag only affects definitions) */
118/* (don't include system headers after this until corresponding pop!) */
Denys Vlasenko4dc35fb2011-07-08 04:41:38 +0200119#if __GNUC_PREREQ(4,1) && !defined(__CYGWIN__)
Denis Vlasenkof81e8db2009-04-09 12:35:13 +0000120# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN _Pragma("GCC visibility push(hidden)")
121# define POP_SAVED_FUNCTION_VISIBILITY _Pragma("GCC visibility pop")
122#else
123# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
124# define POP_SAVED_FUNCTION_VISIBILITY
125#endif
126
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200127/* gcc-2.95 had no va_copy but only __va_copy. */
128#if !__GNUC_PREREQ(3,0)
129# include <stdarg.h>
130# if !defined va_copy && defined __va_copy
131# define va_copy(d,s) __va_copy((d),(s))
132# endif
133#endif
134
135
Rob Landley5cf7c2d2006-02-21 06:44:43 +0000136/* ---- Endian Detection ------------------------------------ */
Rob Landley5cf7c2d2006-02-21 06:44:43 +0000137
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200138#include <limits.h>
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200139#if defined(__digital__) && defined(__unix__)
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +0000140# include <sex.h>
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200141#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
142 || defined(__APPLE__)
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200143# include <sys/resource.h> /* rlimit */
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200144# include <machine/endian.h>
145# define bswap_64 __bswap64
146# define bswap_32 __bswap32
147# define bswap_16 __bswap16
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200148#else
Rob Landley15d20a02006-05-29 05:00:44 +0000149# include <byteswap.h>
150# include <endian.h>
Bernhard Reutner-Fischere00fc162006-05-26 13:10:10 +0000151#endif
152
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200153#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
Mike Frysingerf8855132006-03-28 02:35:56 +0000154# define BB_BIG_ENDIAN 1
155# define BB_LITTLE_ENDIAN 0
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200156#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
157# define BB_BIG_ENDIAN 0
158# define BB_LITTLE_ENDIAN 1
Waldemar Brodkorb95b83ba2010-08-06 09:17:26 +0200159#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN
160# define BB_BIG_ENDIAN 1
161# define BB_LITTLE_ENDIAN 0
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200162#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN
Mike Frysingerf8855132006-03-28 02:35:56 +0000163# define BB_BIG_ENDIAN 0
164# define BB_LITTLE_ENDIAN 1
Denys Vlasenko867ffb92010-08-16 03:24:40 +0200165#elif defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN
166# define BB_BIG_ENDIAN 1
167# define BB_LITTLE_ENDIAN 0
168#elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
169# define BB_BIG_ENDIAN 0
170# define BB_LITTLE_ENDIAN 1
171#elif defined(__386__)
Waldemar Brodkorb95b83ba2010-08-06 09:17:26 +0200172# define BB_BIG_ENDIAN 0
173# define BB_LITTLE_ENDIAN 1
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200174#else
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +0200175# error
176# error
177# error "Can't determine endianness !"
178# error "DO NOT USE 'MAKE' TO BUILD BIONIC BUSYBOX"
179# error
180# error
181 exit 1
Rob Landley5cf7c2d2006-02-21 06:44:43 +0000182#endif
183
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200184#if ULONG_MAX > 0xffffffff
185# define bb_bswap_64(x) bswap_64(x)
186#endif
187
Denis Vlasenko9b0f6e12008-11-01 13:40:32 +0000188/* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */
Rob Landleybba7f082006-05-29 05:51:12 +0000189#if BB_BIG_ENDIAN
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200190# define SWAP_BE16(x) (x)
191# define SWAP_BE32(x) (x)
192# define SWAP_BE64(x) (x)
193# define SWAP_LE16(x) bswap_16(x)
194# define SWAP_LE32(x) bswap_32(x)
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200195# define SWAP_LE64(x) bb_bswap_64(x)
Denys Vlasenkobcccad32010-10-16 20:46:35 +0200196# define IF_BIG_ENDIAN(...) __VA_ARGS__
197# define IF_LITTLE_ENDIAN(...)
Rob Landleybba7f082006-05-29 05:51:12 +0000198#else
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200199# define SWAP_BE16(x) bswap_16(x)
200# define SWAP_BE32(x) bswap_32(x)
Denys Vlasenko9ff50b82010-10-18 11:40:26 +0200201# define SWAP_BE64(x) bb_bswap_64(x)
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200202# define SWAP_LE16(x) (x)
203# define SWAP_LE32(x) (x)
204# define SWAP_LE64(x) (x)
Denys Vlasenkobcccad32010-10-16 20:46:35 +0200205# define IF_BIG_ENDIAN(...)
206# define IF_LITTLE_ENDIAN(...) __VA_ARGS__
Rob Landleybba7f082006-05-29 05:51:12 +0000207#endif
208
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200209
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000210/* ---- Unaligned access ------------------------------------ */
211
Denys Vlasenko1f4a9872011-01-22 17:31:35 +0100212#include <stdint.h>
Denys Vlasenkob40da222011-01-21 01:16:09 +0100213typedef int bb__aliased_int FIX_ALIASING;
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100214typedef long bb__aliased_long FIX_ALIASING;
Denys Vlasenkob40da222011-01-21 01:16:09 +0100215typedef uint16_t bb__aliased_uint16_t FIX_ALIASING;
216typedef uint32_t bb__aliased_uint32_t FIX_ALIASING;
Denys Vlasenko1f5e81f2013-06-27 01:03:19 +0200217typedef uint64_t bb__aliased_uint64_t FIX_ALIASING;
Denys Vlasenkob40da222011-01-21 01:16:09 +0100218
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000219/* NB: unaligned parameter should be a pointer, aligned one -
220 * a lvalue. This makes it more likely to not swap them by mistake
221 */
Joakim Tjernlund80f42752010-02-11 08:48:15 +0100222#if defined(i386) || defined(__x86_64__) || defined(__powerpc__)
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100223# define move_from_unaligned_int(v, intp) ((v) = *(bb__aliased_int*)(intp))
224# define move_from_unaligned_long(v, longp) ((v) = *(bb__aliased_long*)(longp))
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100225# define move_from_unaligned16(v, u16p) ((v) = *(bb__aliased_uint16_t*)(u16p))
226# define move_from_unaligned32(v, u32p) ((v) = *(bb__aliased_uint32_t*)(u32p))
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100227# define move_to_unaligned16(u16p, v) (*(bb__aliased_uint16_t*)(u16p) = (v))
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100228# define move_to_unaligned32(u32p, v) (*(bb__aliased_uint32_t*)(u32p) = (v))
Denis Vlasenko42b8daf2008-06-27 03:55:18 +0000229/* #elif ... - add your favorite arch today! */
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000230#else
231/* performs reasonably well (gcc usually inlines memcpy here) */
Denys Vlasenko57be1ee2009-11-26 15:26:31 +0100232# define move_from_unaligned_int(v, intp) (memcpy(&(v), (intp), sizeof(int)))
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100233# define move_from_unaligned_long(v, longp) (memcpy(&(v), (longp), sizeof(long)))
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200234# define move_from_unaligned16(v, u16p) (memcpy(&(v), (u16p), 2))
235# define move_from_unaligned32(v, u32p) (memcpy(&(v), (u32p), 4))
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100236# define move_to_unaligned16(u16p, v) do { \
237 uint16_t __t = (v); \
Leonid Lisovskiyd3634462013-02-27 18:32:58 +0100238 memcpy((u16p), &__t, 2); \
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100239} while (0)
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200240# define move_to_unaligned32(u32p, v) do { \
Denis Vlasenko3be23082009-04-17 22:20:44 +0000241 uint32_t __t = (v); \
242 memcpy((u32p), &__t, 4); \
243} while (0)
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000244#endif
245
Denis Vlasenkofc9e1082008-05-26 17:32:35 +0000246
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200247/* ---- Size-saving "small" ints (arch-dependent) ----------- */
Bernhard Reutner-Fischerbe862092007-03-19 15:15:06 +0000248
Bernhard Reutner-Fischerc966ba42007-01-18 10:32:09 +0000249#if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__)
250/* add other arches which benefit from this... */
251typedef signed char smallint;
252typedef unsigned char smalluint;
253#else
254/* for arches where byte accesses generate larger code: */
255typedef int smallint;
256typedef unsigned smalluint;
257#endif
258
Bernhard Reutner-Fischera8e2e182007-01-20 21:27:18 +0000259/* ISO C Standard: 7.16 Boolean type and values <stdbool.h> */
260#if (defined __digital__ && defined __unix__)
261/* old system without (proper) C99 support */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200262# define bool smalluint
Bernhard Reutner-Fischera8e2e182007-01-20 21:27:18 +0000263#else
264/* modern system, so use it */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200265# include <stdbool.h>
Bernhard Reutner-Fischera8e2e182007-01-20 21:27:18 +0000266#endif
267
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200268
269/*----- Kernel versioning ------------------------------------*/
270
271#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
272
Mike Frysinger445e7542013-03-12 11:13:22 -0400273#ifdef __UCLIBC__
274# define UCLIBC_VERSION KERNEL_VERSION(__UCLIBC_MAJOR__, __UCLIBC_MINOR__, __UCLIBC_SUBLEVEL__)
275#else
276# define UCLIBC_VERSION 0
277#endif
278
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200279
280/* ---- Miscellaneous --------------------------------------- */
281
282#if defined __GLIBC__ \
283 || defined __UCLIBC__ \
284 || defined __dietlibc__ \
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +0200285 || defined __BIONIC__ \
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200286 || defined _NEWLIB_VERSION
287# include <features.h>
288#endif
289
290/* Define bb_setpgrp */
291#if defined(__digital__) && defined(__unix__)
292/* use legacy setpgrp(pid_t, pid_t) for now. move to platform.c */
293# define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me, __me); } while (0)
294#else
295# define bb_setpgrp() setpgrp()
296#endif
297
298/* fdprintf is more readable, we used it before dprintf was standardized */
299#include <unistd.h>
300#define fdprintf dprintf
301
302/* Useful for defeating gcc's alignment of "char message[]"-like data */
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100303#if !defined(__s390__)
304 /* on s390[x], non-word-aligned data accesses require larger code */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200305# define ALIGN1 __attribute__((aligned(1)))
306# define ALIGN2 __attribute__((aligned(2)))
Denys Vlasenko0ecc1162010-04-14 10:14:25 -0700307# define ALIGN4 __attribute__((aligned(4)))
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000308#else
309/* Arches which MUST have 2 or 4 byte alignment for everything are here */
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200310# define ALIGN1
311# define ALIGN2
Denys Vlasenko0ecc1162010-04-14 10:14:25 -0700312# define ALIGN4
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000313#endif
314
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200315/*
Bernhard Reutner-Fischer507cd752006-05-31 10:04:03 +0000316 * For 0.9.29 and svn, __ARCH_USE_MMU__ indicates no-mmu reliably.
317 * For earlier versions there is no reliable way to check if we are building
Denis Vlasenko9e589212008-01-24 01:33:42 +0000318 * for a mmu-less system.
Bernhard Reutner-Fischer507cd752006-05-31 10:04:03 +0000319 */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +0000320#if ENABLE_NOMMU || \
Mike Frysinger445e7542013-03-12 11:13:22 -0400321 (defined __UCLIBC__ && \
322 UCLIBC_VERSION > KERNEL_VERSION(0, 9, 28) && \
323 !defined __ARCH_USE_MMU__)
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200324# define BB_MMU 0
325# define USE_FOR_NOMMU(...) __VA_ARGS__
326# define USE_FOR_MMU(...)
Denis Vlasenko473dae02007-04-11 07:04:23 +0000327#else
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200328# define BB_MMU 1
329# define USE_FOR_NOMMU(...)
330# define USE_FOR_MMU(...) __VA_ARGS__
Bernhard Reutner-Fischer507cd752006-05-31 10:04:03 +0000331#endif
332
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +0200333#if defined(__digital__) && defined(__unix__)
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000334# include <standards.h>
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000335# include <inttypes.h>
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000336# define PRIu32 "u"
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000337# if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
338# define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
339# endif
340# if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
341# define ADJ_FREQUENCY MOD_FREQUENCY
342# endif
343# if !defined ADJ_TIMECONST && defined MOD_TIMECONST
344# define ADJ_TIMECONST MOD_TIMECONST
345# endif
346# if !defined ADJ_TICK && defined MOD_CLKB
347# define ADJ_TICK MOD_CLKB
348# endif
Rob Landley18958e92006-06-13 18:28:33 +0000349#endif
350
Denys Vlasenko4dc35fb2011-07-08 04:41:38 +0200351#if defined(__CYGWIN__)
352# define MAXSYMLINKS SYMLOOP_MAX
353#endif
354
Tias Guns03dc3652012-06-10 14:26:32 +0200355#if defined(ANDROID) || defined(__ANDROID__)
Tanguy Pruvotb0919fd2014-08-05 08:35:55 +0200356# define BB_ADDITIONAL_PATH ":/vendor/bin:/system/sbin:/system/bin:/system/xbin"
Tias Guns03dc3652012-06-10 14:26:32 +0200357# define SYS_ioprio_set __NR_ioprio_set
358# define SYS_ioprio_get __NR_ioprio_get
359#endif
360
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200361
362/* ---- Who misses what? ------------------------------------ */
363
Dan Fandrichf533ec82011-06-10 05:17:59 +0200364/* Assume all these functions and header files exist by default.
365 * Platforms where it is not true will #undef them below.
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200366 */
367#define HAVE_CLEARENV 1
368#define HAVE_FDATASYNC 1
369#define HAVE_DPRINTF 1
370#define HAVE_MEMRCHR 1
371#define HAVE_MKDTEMP 1
372#define HAVE_PTSNAME_R 1
373#define HAVE_SETBIT 1
374#define HAVE_SIGHANDLER_T 1
375#define HAVE_STPCPY 1
376#define HAVE_STRCASESTR 1
377#define HAVE_STRCHRNUL 1
378#define HAVE_STRSEP 1
379#define HAVE_STRSIGNAL 1
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200380#define HAVE_STRVERSCMP 1
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200381#define HAVE_VASPRINTF 1
Dan Fandrich75214cf2011-06-30 02:59:17 +0200382#define HAVE_UNLOCKED_STDIO 1
383#define HAVE_UNLOCKED_LINE_OPS 1
Timo Teras0a5b3102011-06-29 02:19:58 +0200384#define HAVE_GETLINE 1
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200385#define HAVE_XTABS 1
Dan Fandrichf533ec82011-06-10 05:17:59 +0200386#define HAVE_MNTENT_H 1
387#define HAVE_NET_ETHERNET_H 1
388#define HAVE_SYS_STATFS_H 1
389
Mike Frysinger445e7542013-03-12 11:13:22 -0400390#if defined(__UCLIBC__) && UCLIBC_VERSION < KERNEL_VERSION(0, 9, 32)
391# undef HAVE_STRVERSCMP
Denys Vlasenko1e18a012011-06-21 17:12:52 +0200392#endif
393
Dan Fandrich21a542d2009-10-27 11:05:00 +0100394#if defined(__dietlibc__)
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100395# undef HAVE_STRCHRNUL
Dan Fandrich21a542d2009-10-27 11:05:00 +0100396#endif
397
398#if defined(__WATCOMC__)
Denys Vlasenko47061b42011-04-17 23:14:19 +0200399# undef HAVE_DPRINTF
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700400# undef HAVE_GETLINE
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100401# undef HAVE_MEMRCHR
402# undef HAVE_MKDTEMP
403# undef HAVE_SETBIT
Dan Fandrichdc506762011-02-12 22:26:57 -0800404# undef HAVE_STPCPY
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100405# undef HAVE_STRCASESTR
406# undef HAVE_STRCHRNUL
Dan Fandrich0635ddd2010-06-18 22:36:45 -0700407# undef HAVE_STRSEP
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100408# undef HAVE_STRSIGNAL
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200409# undef HAVE_STRVERSCMP
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100410# undef HAVE_VASPRINTF
Dan Fandrich75214cf2011-06-30 02:59:17 +0200411# undef HAVE_UNLOCKED_STDIO
412# undef HAVE_UNLOCKED_LINE_OPS
Dan Fandrichf533ec82011-06-10 05:17:59 +0200413# undef HAVE_NET_ETHERNET_H
Dan Fandrich21a542d2009-10-27 11:05:00 +0100414#endif
415
Denys Vlasenko4dc35fb2011-07-08 04:41:38 +0200416#if defined(__CYGWIN__)
417# undef HAVE_CLEARENV
418# undef HAVE_FDPRINTF
419# undef HAVE_MEMRCHR
420# undef HAVE_PTSNAME_R
421# undef HAVE_STRVERSCMP
422# undef HAVE_UNLOCKED_LINE_OPS
423#endif
424
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700425/* These BSD-derived OSes share many similarities */
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200426#if (defined __digital__ && defined __unix__) \
427 || defined __APPLE__ \
428 || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
429# undef HAVE_CLEARENV
430# undef HAVE_FDATASYNC
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700431# undef HAVE_GETLINE
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200432# undef HAVE_MNTENT_H
433# undef HAVE_PTSNAME_R
434# undef HAVE_SYS_STATFS_H
435# undef HAVE_SIGHANDLER_T
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200436# undef HAVE_STRVERSCMP
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200437# undef HAVE_XTABS
438# undef HAVE_DPRINTF
Denys Vlasenkocc272b02011-08-28 13:00:29 +0200439# undef HAVE_UNLOCKED_STDIO
440# undef HAVE_UNLOCKED_LINE_OPS
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200441#endif
442
Daniel Borcad9fc4d82013-11-27 00:27:46 +0100443#if defined(__FreeBSD__) || defined(__APPLE__)
Dan Fandrich0e79e7b2011-06-28 23:03:27 -0700444# undef HAVE_STRCHRNUL
445#endif
446
447#if defined(__NetBSD__)
448# define HAVE_GETLINE 1 /* Recent NetBSD versions have getline() */
449#endif
450
Denys Vlasenko89f5bfd2011-05-12 23:03:18 +0200451#if defined(__digital__) && defined(__unix__)
452# undef HAVE_STPCPY
453#endif
454
Tanguy Pruvot29d8ebe2011-10-29 15:15:34 +0200455#if defined(ANDROID) || defined(__ANDROID__)
Dan Fandrich71d73132011-06-03 20:51:58 +0200456# undef HAVE_DPRINTF
Dan Fandrich71d73132011-06-03 20:51:58 +0200457# undef HAVE_STRCHRNUL
Denys Vlasenko561f9c82011-06-21 16:38:29 +0200458# undef HAVE_STRVERSCMP
Dan Fandrich75214cf2011-06-30 02:59:17 +0200459# undef HAVE_UNLOCKED_LINE_OPS
Dan Fandrichf533ec82011-06-10 05:17:59 +0200460# undef HAVE_NET_ETHERNET_H
HazouPH84f25e72014-08-25 14:25:27 +0200461# ifndef BIONIC_L
Chih-Wei Huang29cf8c22014-07-30 17:04:54 +0800462# undef HAVE_STPCPY
463# endif
Dan Fandrich71d73132011-06-03 20:51:58 +0200464#endif
465
Dan Fandrich21a542d2009-10-27 11:05:00 +0100466/*
467 * Now, define prototypes for all the functions defined in platform.c
468 * These must come after all the HAVE_* macros are defined (or not)
469 */
470
Denys Vlasenko47061b42011-04-17 23:14:19 +0200471#ifndef HAVE_DPRINTF
472extern int dprintf(int fd, const char *format, ...);
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100473#endif
474
475#ifndef HAVE_MEMRCHR
476extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC;
477#endif
478
479#ifndef HAVE_MKDTEMP
480extern char *mkdtemp(char *template) FAST_FUNC;
481#endif
482
483#ifndef HAVE_SETBIT
484# define setbit(a, b) ((a)[(b) >> 3] |= 1 << ((b) & 7))
485# define clrbit(a, b) ((a)[(b) >> 3] &= ~(1 << ((b) & 7)))
486#endif
487
Chris Rees9ad97d52011-01-20 00:51:52 +0100488#ifndef HAVE_SIGHANDLER_T
489typedef void (*sighandler_t)(int);
490#endif
491
Dan Fandrichdc506762011-02-12 22:26:57 -0800492#ifndef HAVE_STPCPY
493extern char *stpcpy(char *p, const char *to_add) FAST_FUNC;
494#endif
495
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100496#ifndef HAVE_STRCASESTR
497extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC;
498#endif
499
Dan Fandrich21a542d2009-10-27 11:05:00 +0100500#ifndef HAVE_STRCHRNUL
501extern char *strchrnul(const char *s, int c) FAST_FUNC;
502#endif
503
Dan Fandrich0635ddd2010-06-18 22:36:45 -0700504#ifndef HAVE_STRSEP
505extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
506#endif
507
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100508#ifndef HAVE_STRSIGNAL
509/* Not exactly the same: instead of "Stopped" it shows "STOP" etc */
510# define strsignal(sig) get_signame(sig)
Dan Fandrich21a542d2009-10-27 11:05:00 +0100511#endif
512
Dan Fandrichfe4e23f2009-11-01 04:01:30 +0100513#ifndef HAVE_VASPRINTF
514extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;
Dan Fandrich21a542d2009-10-27 11:05:00 +0100515#endif
Denys Vlasenko5a49d282009-05-19 13:18:45 +0200516
Timo Teras0a5b3102011-06-29 02:19:58 +0200517#ifndef HAVE_GETLINE
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +0200518# include <stdio.h> /* for FILE */
519# include <sys/types.h> /* size_t */
Timo Teras0a5b3102011-06-29 02:19:58 +0200520extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC;
521#endif
522
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +0200523#if defined(__BIONIC__)
524#include "android.h"
Denis Vlasenkof81e8db2009-04-09 12:35:13 +0000525#endif
Tanguy Pruvot8aeb3712011-06-30 08:59:26 +0200526
527
528#endif /* BB_PLATFORM_H */