Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Copyright 2006, Bernhard Reutner-Fischer |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 6 | */ |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 7 | #ifndef BB_PLATFORM_H |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 8 | #define BB_PLATFORM_H 1 |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 9 | |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 10 | |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 11 | /* 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 Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 15 | ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 16 | #else |
| 17 | # define __GNUC_PREREQ(maj, min) 0 |
| 18 | #endif |
| 19 | |
| 20 | /* __restrict is known in EGCS 1.2 and above. */ |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 21 | #if !__GNUC_PREREQ(2,92) |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 22 | # ifndef __restrict |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 23 | # define __restrict |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 24 | # endif |
| 25 | #endif |
| 26 | |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 27 | #if !__GNUC_PREREQ(2,7) |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 28 | # ifndef __attribute__ |
| 29 | # define __attribute__(x) |
| 30 | # endif |
| 31 | #endif |
| 32 | |
Denys Vlasenko | d206b16 | 2020-06-23 09:38:53 +0200 | [diff] [blame] | 33 | #if !__GNUC_PREREQ(5,0) |
| 34 | # define deprecated(msg) deprecated |
| 35 | #endif |
| 36 | |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 37 | #undef inline |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 38 | #if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 39 | /* it's a keyword */ |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 40 | #elif __GNUC_PREREQ(2,7) |
| 41 | # define inline __inline__ |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 42 | #else |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 43 | # define inline |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | #ifndef __const |
| 47 | # define __const const |
| 48 | #endif |
| 49 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 50 | #define UNUSED_PARAM __attribute__ ((__unused__)) |
| 51 | #define NORETURN __attribute__ ((__noreturn__)) |
Denys Vlasenko | 619d9b5 | 2017-07-28 15:28:33 +0200 | [diff] [blame] | 52 | |
| 53 | #if __GNUC_PREREQ(4,5) |
| 54 | # define bb_unreachable(altcode) __builtin_unreachable() |
| 55 | #else |
| 56 | # define bb_unreachable(altcode) altcode |
| 57 | #endif |
| 58 | |
Denys Vlasenko | 53283ad | 2009-11-02 14:20:34 +0100 | [diff] [blame] | 59 | /* "The malloc attribute is used to tell the compiler that a function |
| 60 | * may be treated as if any non-NULL pointer it returns cannot alias |
| 61 | * any other pointer valid when the function returns. This will often |
| 62 | * improve optimization. Standard functions with this property include |
| 63 | * malloc and calloc. realloc-like functions have this property as long |
| 64 | * as the old pointer is never referred to (including comparing it |
| 65 | * to the new pointer) after the function returns a non-NULL value." |
| 66 | */ |
| 67 | #define RETURNS_MALLOC __attribute__ ((malloc)) |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 68 | #define PACKED __attribute__ ((__packed__)) |
| 69 | #define ALIGNED(m) __attribute__ ((__aligned__(m))) |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 70 | |
Denis Vlasenko | 0f3a580 | 2008-03-20 13:13:09 +0000 | [diff] [blame] | 71 | /* __NO_INLINE__: some gcc's do not honor inlining! :( */ |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 72 | #if __GNUC_PREREQ(3,0) && !defined(__NO_INLINE__) |
| 73 | # define ALWAYS_INLINE __attribute__ ((always_inline)) inline |
Denis Vlasenko | 77f1ec1 | 2007-10-13 03:36:03 +0000 | [diff] [blame] | 74 | /* I've seen a toolchain where I needed __noinline__ instead of noinline */ |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 75 | # define NOINLINE __attribute__((__noinline__)) |
| 76 | # if !ENABLE_WERROR |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 77 | # define DEPRECATED __attribute__ ((__deprecated__)) |
| 78 | # define UNUSED_PARAM_RESULT __attribute__ ((warn_unused_result)) |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 79 | # else |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 80 | # define DEPRECATED |
| 81 | # define UNUSED_PARAM_RESULT |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 82 | # endif |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 83 | #else |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 84 | # define ALWAYS_INLINE inline |
| 85 | # define NOINLINE |
| 86 | # define DEPRECATED |
| 87 | # define UNUSED_PARAM_RESULT |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 88 | #endif |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 89 | |
Bartosz Golaszewski | bf0f2c7 | 2014-06-26 14:31:05 +0200 | [diff] [blame] | 90 | /* used by unit test machinery to run registration functions before calling main() */ |
| 91 | #define INIT_FUNC __attribute__ ((constructor)) |
Bartosz Golaszewski | 3ed81cf | 2014-06-22 16:30:41 +0200 | [diff] [blame] | 92 | |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 93 | /* -fwhole-program makes all symbols local. The attribute externally_visible |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 94 | * forces a symbol global. */ |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 95 | #if __GNUC_PREREQ(4,1) |
| 96 | # define EXTERNALLY_VISIBLE __attribute__(( visibility("default") )) |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 97 | //__attribute__ ((__externally_visible__)) |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 98 | #else |
| 99 | # define EXTERNALLY_VISIBLE |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 100 | #endif |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 101 | |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 102 | /* At 4.4 gcc become much more anal about this, need to use "aliased" types */ |
| 103 | #if __GNUC_PREREQ(4,4) |
| 104 | # define FIX_ALIASING __attribute__((__may_alias__)) |
| 105 | #else |
| 106 | # define FIX_ALIASING |
| 107 | #endif |
| 108 | |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 109 | /* We use __extension__ in some places to suppress -pedantic warnings |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 110 | * about GCC extensions. This feature didn't work properly before |
| 111 | * gcc 2.8. */ |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 112 | #if !__GNUC_PREREQ(2,8) |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 113 | # ifndef __extension__ |
| 114 | # define __extension__ |
| 115 | # endif |
| 116 | #endif |
| 117 | |
Denis Vlasenko | 42b8daf | 2008-06-27 03:55:18 +0000 | [diff] [blame] | 118 | /* FAST_FUNC is a qualifier which (possibly) makes function call faster |
| 119 | * and/or smaller by using modified ABI. It is usually only needed |
| 120 | * on non-static, busybox internal functions. Recent versions of gcc |
| 121 | * optimize statics automatically. FAST_FUNC on static is required |
Kang-Che Sung | f10f7a2 | 2017-01-05 09:25:03 +0800 | [diff] [blame] | 122 | * only if you need to match a function pointer's type. |
| 123 | * FAST_FUNC may not work well with -flto so allow user to disable this. |
| 124 | * (-DFAST_FUNC= ) |
| 125 | */ |
| 126 | #ifndef FAST_FUNC |
| 127 | # if __GNUC_PREREQ(3,0) && defined(i386) |
Denis Vlasenko | ac2b50e | 2008-06-27 04:30:48 +0000 | [diff] [blame] | 128 | /* stdcall makes callee to pop arguments from stack, not caller */ |
Kang-Che Sung | f10f7a2 | 2017-01-05 09:25:03 +0800 | [diff] [blame] | 129 | # define FAST_FUNC __attribute__((regparm(3),stdcall)) |
Denis Vlasenko | 42b8daf | 2008-06-27 03:55:18 +0000 | [diff] [blame] | 130 | /* #elif ... - add your favorite arch today! */ |
Kang-Che Sung | f10f7a2 | 2017-01-05 09:25:03 +0800 | [diff] [blame] | 131 | # else |
| 132 | # define FAST_FUNC |
| 133 | # endif |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 134 | #endif |
| 135 | |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 136 | /* Make all declarations hidden (-fvisibility flag only affects definitions) */ |
| 137 | /* (don't include system headers after this until corresponding pop!) */ |
Denys Vlasenko | 4dc35fb | 2011-07-08 04:41:38 +0200 | [diff] [blame] | 138 | #if __GNUC_PREREQ(4,1) && !defined(__CYGWIN__) |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 139 | # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN _Pragma("GCC visibility push(hidden)") |
| 140 | # define POP_SAVED_FUNCTION_VISIBILITY _Pragma("GCC visibility pop") |
| 141 | #else |
| 142 | # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN |
| 143 | # define POP_SAVED_FUNCTION_VISIBILITY |
| 144 | #endif |
| 145 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 146 | /* gcc-2.95 had no va_copy but only __va_copy. */ |
| 147 | #if !__GNUC_PREREQ(3,0) |
| 148 | # include <stdarg.h> |
| 149 | # if !defined va_copy && defined __va_copy |
| 150 | # define va_copy(d,s) __va_copy((d),(s)) |
| 151 | # endif |
| 152 | #endif |
| 153 | |
| 154 | |
Rob Landley | 5cf7c2d | 2006-02-21 06:44:43 +0000 | [diff] [blame] | 155 | /* ---- Endian Detection ------------------------------------ */ |
Rob Landley | 5cf7c2d | 2006-02-21 06:44:43 +0000 | [diff] [blame] | 156 | |
Denys Vlasenko | 9ff50b8 | 2010-10-18 11:40:26 +0200 | [diff] [blame] | 157 | #include <limits.h> |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 158 | #if defined(__digital__) && defined(__unix__) |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 159 | # include <sex.h> |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 160 | #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 161 | || defined(__APPLE__) |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 162 | # include <sys/resource.h> /* rlimit */ |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 163 | # include <machine/endian.h> |
| 164 | # define bswap_64 __bswap64 |
| 165 | # define bswap_32 __bswap32 |
| 166 | # define bswap_16 __bswap16 |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 167 | #else |
Rob Landley | 15d20a0 | 2006-05-29 05:00:44 +0000 | [diff] [blame] | 168 | # include <byteswap.h> |
| 169 | # include <endian.h> |
Bernhard Reutner-Fischer | e00fc16 | 2006-05-26 13:10:10 +0000 | [diff] [blame] | 170 | #endif |
| 171 | |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 172 | #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN |
Mike Frysinger | f885513 | 2006-03-28 02:35:56 +0000 | [diff] [blame] | 173 | # define BB_BIG_ENDIAN 1 |
| 174 | # define BB_LITTLE_ENDIAN 0 |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 175 | #elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN |
| 176 | # define BB_BIG_ENDIAN 0 |
| 177 | # define BB_LITTLE_ENDIAN 1 |
Waldemar Brodkorb | 95b83ba | 2010-08-06 09:17:26 +0200 | [diff] [blame] | 178 | #elif defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN |
| 179 | # define BB_BIG_ENDIAN 1 |
| 180 | # define BB_LITTLE_ENDIAN 0 |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 181 | #elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN |
Mike Frysinger | f885513 | 2006-03-28 02:35:56 +0000 | [diff] [blame] | 182 | # define BB_BIG_ENDIAN 0 |
| 183 | # define BB_LITTLE_ENDIAN 1 |
Denys Vlasenko | 867ffb9 | 2010-08-16 03:24:40 +0200 | [diff] [blame] | 184 | #elif defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN |
| 185 | # define BB_BIG_ENDIAN 1 |
| 186 | # define BB_LITTLE_ENDIAN 0 |
| 187 | #elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN |
| 188 | # define BB_BIG_ENDIAN 0 |
| 189 | # define BB_LITTLE_ENDIAN 1 |
| 190 | #elif defined(__386__) |
Waldemar Brodkorb | 95b83ba | 2010-08-06 09:17:26 +0200 | [diff] [blame] | 191 | # define BB_BIG_ENDIAN 0 |
| 192 | # define BB_LITTLE_ENDIAN 1 |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 193 | #else |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 194 | # error "Can't determine endianness" |
Rob Landley | 5cf7c2d | 2006-02-21 06:44:43 +0000 | [diff] [blame] | 195 | #endif |
| 196 | |
Denys Vlasenko | 9ff50b8 | 2010-10-18 11:40:26 +0200 | [diff] [blame] | 197 | #if ULONG_MAX > 0xffffffff |
Denys Vlasenko | d005c9f | 2021-04-24 11:54:50 +0200 | [diff] [blame] | 198 | /* inline 64-bit bswap only on 64-bit arches */ |
Denys Vlasenko | 9ff50b8 | 2010-10-18 11:40:26 +0200 | [diff] [blame] | 199 | # define bb_bswap_64(x) bswap_64(x) |
| 200 | #endif |
| 201 | |
Denis Vlasenko | 9b0f6e1 | 2008-11-01 13:40:32 +0000 | [diff] [blame] | 202 | /* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */ |
Rob Landley | bba7f08 | 2006-05-29 05:51:12 +0000 | [diff] [blame] | 203 | #if BB_BIG_ENDIAN |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 204 | # define SWAP_BE16(x) (x) |
| 205 | # define SWAP_BE32(x) (x) |
| 206 | # define SWAP_BE64(x) (x) |
| 207 | # define SWAP_LE16(x) bswap_16(x) |
| 208 | # define SWAP_LE32(x) bswap_32(x) |
Denys Vlasenko | 9ff50b8 | 2010-10-18 11:40:26 +0200 | [diff] [blame] | 209 | # define SWAP_LE64(x) bb_bswap_64(x) |
Denys Vlasenko | bcccad3 | 2010-10-16 20:46:35 +0200 | [diff] [blame] | 210 | # define IF_BIG_ENDIAN(...) __VA_ARGS__ |
| 211 | # define IF_LITTLE_ENDIAN(...) |
Rob Landley | bba7f08 | 2006-05-29 05:51:12 +0000 | [diff] [blame] | 212 | #else |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 213 | # define SWAP_BE16(x) bswap_16(x) |
| 214 | # define SWAP_BE32(x) bswap_32(x) |
Denys Vlasenko | 9ff50b8 | 2010-10-18 11:40:26 +0200 | [diff] [blame] | 215 | # define SWAP_BE64(x) bb_bswap_64(x) |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 216 | # define SWAP_LE16(x) (x) |
| 217 | # define SWAP_LE32(x) (x) |
| 218 | # define SWAP_LE64(x) (x) |
Denys Vlasenko | bcccad3 | 2010-10-16 20:46:35 +0200 | [diff] [blame] | 219 | # define IF_BIG_ENDIAN(...) |
| 220 | # define IF_LITTLE_ENDIAN(...) __VA_ARGS__ |
Rob Landley | bba7f08 | 2006-05-29 05:51:12 +0000 | [diff] [blame] | 221 | #endif |
| 222 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 223 | |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 224 | /* ---- Unaligned access ------------------------------------ */ |
| 225 | |
Denys Vlasenko | 1f4a987 | 2011-01-22 17:31:35 +0100 | [diff] [blame] | 226 | #include <stdint.h> |
Denys Vlasenko | b40da22 | 2011-01-21 01:16:09 +0100 | [diff] [blame] | 227 | typedef int bb__aliased_int FIX_ALIASING; |
Lauri Kasanen | b8173b6 | 2013-01-14 05:20:50 +0100 | [diff] [blame] | 228 | typedef long bb__aliased_long FIX_ALIASING; |
Denys Vlasenko | b40da22 | 2011-01-21 01:16:09 +0100 | [diff] [blame] | 229 | typedef uint16_t bb__aliased_uint16_t FIX_ALIASING; |
| 230 | typedef uint32_t bb__aliased_uint32_t FIX_ALIASING; |
Denys Vlasenko | 1f5e81f | 2013-06-27 01:03:19 +0200 | [diff] [blame] | 231 | typedef uint64_t bb__aliased_uint64_t FIX_ALIASING; |
Denys Vlasenko | b40da22 | 2011-01-21 01:16:09 +0100 | [diff] [blame] | 232 | |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 233 | /* NB: unaligned parameter should be a pointer, aligned one - |
| 234 | * a lvalue. This makes it more likely to not swap them by mistake |
| 235 | */ |
Joakim Tjernlund | 80f4275 | 2010-02-11 08:48:15 +0100 | [diff] [blame] | 236 | #if defined(i386) || defined(__x86_64__) || defined(__powerpc__) |
Denys Vlasenko | f7f70bf | 2015-02-02 16:07:07 +0100 | [diff] [blame] | 237 | # define BB_UNALIGNED_MEMACCESS_OK 1 |
Lauri Kasanen | b8173b6 | 2013-01-14 05:20:50 +0100 | [diff] [blame] | 238 | # define move_from_unaligned_int(v, intp) ((v) = *(bb__aliased_int*)(intp)) |
| 239 | # define move_from_unaligned_long(v, longp) ((v) = *(bb__aliased_long*)(longp)) |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 240 | # define move_from_unaligned16(v, u16p) ((v) = *(bb__aliased_uint16_t*)(u16p)) |
| 241 | # define move_from_unaligned32(v, u32p) ((v) = *(bb__aliased_uint32_t*)(u32p)) |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 242 | # define move_to_unaligned16(u16p, v) (*(bb__aliased_uint16_t*)(u16p) = (v)) |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 243 | # define move_to_unaligned32(u32p, v) (*(bb__aliased_uint32_t*)(u32p) = (v)) |
Denys Vlasenko | 219c9d4 | 2018-11-23 18:48:20 +0100 | [diff] [blame] | 244 | # define move_to_unaligned64(u64p, v) (*(bb__aliased_uint64_t*)(u64p) = (v)) |
Denis Vlasenko | 42b8daf | 2008-06-27 03:55:18 +0000 | [diff] [blame] | 245 | /* #elif ... - add your favorite arch today! */ |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 246 | #else |
Denys Vlasenko | f7f70bf | 2015-02-02 16:07:07 +0100 | [diff] [blame] | 247 | # define BB_UNALIGNED_MEMACCESS_OK 0 |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 248 | /* performs reasonably well (gcc usually inlines memcpy here) */ |
Denys Vlasenko | 57be1ee | 2009-11-26 15:26:31 +0100 | [diff] [blame] | 249 | # define move_from_unaligned_int(v, intp) (memcpy(&(v), (intp), sizeof(int))) |
Lauri Kasanen | b8173b6 | 2013-01-14 05:20:50 +0100 | [diff] [blame] | 250 | # define move_from_unaligned_long(v, longp) (memcpy(&(v), (longp), sizeof(long))) |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 251 | # define move_from_unaligned16(v, u16p) (memcpy(&(v), (u16p), 2)) |
| 252 | # define move_from_unaligned32(v, u32p) (memcpy(&(v), (u32p), 4)) |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 253 | # define move_to_unaligned16(u16p, v) do { \ |
| 254 | uint16_t __t = (v); \ |
Denys Vlasenko | e3e3216 | 2013-02-27 15:49:38 +0100 | [diff] [blame] | 255 | memcpy((u16p), &__t, 2); \ |
Denys Vlasenko | 5fb3849 | 2010-02-06 22:48:10 +0100 | [diff] [blame] | 256 | } while (0) |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 257 | # define move_to_unaligned32(u32p, v) do { \ |
Denis Vlasenko | 3be2308 | 2009-04-17 22:20:44 +0000 | [diff] [blame] | 258 | uint32_t __t = (v); \ |
| 259 | memcpy((u32p), &__t, 4); \ |
| 260 | } while (0) |
Denys Vlasenko | 219c9d4 | 2018-11-23 18:48:20 +0100 | [diff] [blame] | 261 | # define move_to_unaligned64(u64p, v) do { \ |
| 262 | uint64_t __t = (v); \ |
| 263 | memcpy((u64p), &__t, 8); \ |
| 264 | } while (0) |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 265 | #endif |
| 266 | |
Denys Vlasenko | 2c1258c | 2017-07-15 20:22:25 +0200 | [diff] [blame] | 267 | /* Unaligned, fixed-endian accessors */ |
| 268 | #define get_unaligned_le32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_LE32(v); }) |
| 269 | #define get_unaligned_be32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_BE32(v); }) |
| 270 | #define put_unaligned_le32(val, buf) move_to_unaligned32(buf, SWAP_LE32(val)) |
| 271 | #define put_unaligned_be32(val, buf) move_to_unaligned32(buf, SWAP_BE32(val)) |
| 272 | |
Denys Vlasenko | bac929b | 2017-08-10 12:40:48 +0200 | [diff] [blame] | 273 | /* unxz needs an aligned fixed-endian accessor. |
| 274 | * (however, the compiler does not realize it's aligned, the cast is still necessary) |
| 275 | */ |
| 276 | #define get_le32(u32p) ({ uint32_t v = *(bb__aliased_uint32_t*)(u32p); SWAP_LE32(v); }) |
| 277 | |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 278 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 279 | /* ---- Size-saving "small" ints (arch-dependent) ----------- */ |
Bernhard Reutner-Fischer | be86209 | 2007-03-19 15:15:06 +0000 | [diff] [blame] | 280 | |
Bernhard Reutner-Fischer | c966ba4 | 2007-01-18 10:32:09 +0000 | [diff] [blame] | 281 | #if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__) |
| 282 | /* add other arches which benefit from this... */ |
| 283 | typedef signed char smallint; |
| 284 | typedef unsigned char smalluint; |
| 285 | #else |
| 286 | /* for arches where byte accesses generate larger code: */ |
| 287 | typedef int smallint; |
| 288 | typedef unsigned smalluint; |
| 289 | #endif |
| 290 | |
Bernhard Reutner-Fischer | a8e2e18 | 2007-01-20 21:27:18 +0000 | [diff] [blame] | 291 | /* ISO C Standard: 7.16 Boolean type and values <stdbool.h> */ |
| 292 | #if (defined __digital__ && defined __unix__) |
| 293 | /* old system without (proper) C99 support */ |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 294 | # define bool smalluint |
Bernhard Reutner-Fischer | a8e2e18 | 2007-01-20 21:27:18 +0000 | [diff] [blame] | 295 | #else |
| 296 | /* modern system, so use it */ |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 297 | # include <stdbool.h> |
Bernhard Reutner-Fischer | a8e2e18 | 2007-01-20 21:27:18 +0000 | [diff] [blame] | 298 | #endif |
| 299 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 300 | |
| 301 | /*----- Kernel versioning ------------------------------------*/ |
| 302 | |
| 303 | #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) |
| 304 | |
Mike Frysinger | 445e754 | 2013-03-12 11:13:22 -0400 | [diff] [blame] | 305 | #ifdef __UCLIBC__ |
| 306 | # define UCLIBC_VERSION KERNEL_VERSION(__UCLIBC_MAJOR__, __UCLIBC_MINOR__, __UCLIBC_SUBLEVEL__) |
| 307 | #else |
| 308 | # define UCLIBC_VERSION 0 |
| 309 | #endif |
| 310 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 311 | |
| 312 | /* ---- Miscellaneous --------------------------------------- */ |
| 313 | |
| 314 | #if defined __GLIBC__ \ |
| 315 | || defined __UCLIBC__ \ |
| 316 | || defined __dietlibc__ \ |
Denys Vlasenko | 14bd16a | 2011-07-08 08:49:40 +0200 | [diff] [blame] | 317 | || defined __BIONIC__ \ |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 318 | || defined _NEWLIB_VERSION |
| 319 | # include <features.h> |
| 320 | #endif |
| 321 | |
| 322 | /* Define bb_setpgrp */ |
Alex Samorukov | d4624d3 | 2021-01-04 01:33:32 +0100 | [diff] [blame] | 323 | #if (defined(__digital__) && defined(__unix__)) || defined(__FreeBSD__) |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 324 | /* use legacy setpgrp(pid_t, pid_t) for now. move to platform.c */ |
| 325 | # define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me, __me); } while (0) |
| 326 | #else |
| 327 | # define bb_setpgrp() setpgrp() |
| 328 | #endif |
| 329 | |
| 330 | /* fdprintf is more readable, we used it before dprintf was standardized */ |
| 331 | #include <unistd.h> |
| 332 | #define fdprintf dprintf |
| 333 | |
| 334 | /* Useful for defeating gcc's alignment of "char message[]"-like data */ |
Denys Vlasenko | 2ffd710 | 2012-08-06 17:17:15 +0200 | [diff] [blame] | 335 | #if !defined(__s390__) |
| 336 | /* on s390[x], non-word-aligned data accesses require larger code */ |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 337 | # define ALIGN1 __attribute__((aligned(1))) |
| 338 | # define ALIGN2 __attribute__((aligned(2))) |
Denys Vlasenko | 0ecc116 | 2010-04-14 10:14:25 -0700 | [diff] [blame] | 339 | # define ALIGN4 __attribute__((aligned(4))) |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 340 | #else |
| 341 | /* Arches which MUST have 2 or 4 byte alignment for everything are here */ |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 342 | # define ALIGN1 |
| 343 | # define ALIGN2 |
Denys Vlasenko | 0ecc116 | 2010-04-14 10:14:25 -0700 | [diff] [blame] | 344 | # define ALIGN4 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 345 | #endif |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 346 | #define ALIGN8 __attribute__((aligned(8))) |
| 347 | #define ALIGN_PTR __attribute__((aligned(sizeof(void*)))) |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 348 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 349 | /* |
Bernhard Reutner-Fischer | 507cd75 | 2006-05-31 10:04:03 +0000 | [diff] [blame] | 350 | * For 0.9.29 and svn, __ARCH_USE_MMU__ indicates no-mmu reliably. |
| 351 | * For earlier versions there is no reliable way to check if we are building |
Denis Vlasenko | 9e58921 | 2008-01-24 01:33:42 +0000 | [diff] [blame] | 352 | * for a mmu-less system. |
Bernhard Reutner-Fischer | 507cd75 | 2006-05-31 10:04:03 +0000 | [diff] [blame] | 353 | */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 354 | #if ENABLE_NOMMU || \ |
Mike Frysinger | 445e754 | 2013-03-12 11:13:22 -0400 | [diff] [blame] | 355 | (defined __UCLIBC__ && \ |
| 356 | UCLIBC_VERSION > KERNEL_VERSION(0, 9, 28) && \ |
| 357 | !defined __ARCH_USE_MMU__) |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 358 | # define BB_MMU 0 |
| 359 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 360 | # define USE_FOR_MMU(...) |
Denis Vlasenko | 473dae0 | 2007-04-11 07:04:23 +0000 | [diff] [blame] | 361 | #else |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 362 | # define BB_MMU 1 |
| 363 | # define USE_FOR_NOMMU(...) |
| 364 | # define USE_FOR_MMU(...) __VA_ARGS__ |
Bernhard Reutner-Fischer | 507cd75 | 2006-05-31 10:04:03 +0000 | [diff] [blame] | 365 | #endif |
| 366 | |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 367 | #if defined(__digital__) && defined(__unix__) |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 368 | # include <standards.h> |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 369 | # include <inttypes.h> |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 370 | # define PRIu32 "u" |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 371 | # if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET |
| 372 | # define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET) |
| 373 | # endif |
| 374 | # if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY |
| 375 | # define ADJ_FREQUENCY MOD_FREQUENCY |
| 376 | # endif |
| 377 | # if !defined ADJ_TIMECONST && defined MOD_TIMECONST |
| 378 | # define ADJ_TIMECONST MOD_TIMECONST |
| 379 | # endif |
| 380 | # if !defined ADJ_TICK && defined MOD_CLKB |
| 381 | # define ADJ_TICK MOD_CLKB |
| 382 | # endif |
Rob Landley | 18958e9 | 2006-06-13 18:28:33 +0000 | [diff] [blame] | 383 | #endif |
| 384 | |
Denys Vlasenko | 4dc35fb | 2011-07-08 04:41:38 +0200 | [diff] [blame] | 385 | #if defined(__CYGWIN__) |
| 386 | # define MAXSYMLINKS SYMLOOP_MAX |
| 387 | #endif |
| 388 | |
Tias Guns | 3645195 | 2012-06-10 14:26:32 +0200 | [diff] [blame] | 389 | #if defined(ANDROID) || defined(__ANDROID__) |
| 390 | # define BB_ADDITIONAL_PATH ":/system/sbin:/system/bin:/system/xbin" |
| 391 | # define SYS_ioprio_set __NR_ioprio_set |
| 392 | # define SYS_ioprio_get __NR_ioprio_get |
| 393 | #endif |
| 394 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 395 | |
| 396 | /* ---- Who misses what? ------------------------------------ */ |
| 397 | |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 398 | /* Assume all these functions and header files exist by default. |
| 399 | * Platforms where it is not true will #undef them below. |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 400 | */ |
| 401 | #define HAVE_CLEARENV 1 |
| 402 | #define HAVE_FDATASYNC 1 |
| 403 | #define HAVE_DPRINTF 1 |
| 404 | #define HAVE_MEMRCHR 1 |
| 405 | #define HAVE_MKDTEMP 1 |
Matt Whitlock | cee5905 | 2015-04-25 21:32:48 +0200 | [diff] [blame] | 406 | #define HAVE_TTYNAME_R 1 |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 407 | #define HAVE_PTSNAME_R 1 |
| 408 | #define HAVE_SETBIT 1 |
| 409 | #define HAVE_SIGHANDLER_T 1 |
| 410 | #define HAVE_STPCPY 1 |
Denys Vlasenko | 33745b1 | 2021-02-18 13:44:27 +0100 | [diff] [blame] | 411 | #define HAVE_STPNCPY 1 |
Denys Vlasenko | 50a6d86 | 2015-01-25 22:08:46 +0100 | [diff] [blame] | 412 | #define HAVE_MEMPCPY 1 |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 413 | #define HAVE_STRCASESTR 1 |
| 414 | #define HAVE_STRCHRNUL 1 |
| 415 | #define HAVE_STRSEP 1 |
| 416 | #define HAVE_STRSIGNAL 1 |
Denys Vlasenko | 561f9c8 | 2011-06-21 16:38:29 +0200 | [diff] [blame] | 417 | #define HAVE_STRVERSCMP 1 |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 418 | #define HAVE_VASPRINTF 1 |
Bernhard Reutner-Fischer | ad16741 | 2014-04-13 16:37:57 +0200 | [diff] [blame] | 419 | #define HAVE_USLEEP 1 |
Dan Fandrich | 75214cf | 2011-06-30 02:59:17 +0200 | [diff] [blame] | 420 | #define HAVE_UNLOCKED_STDIO 1 |
| 421 | #define HAVE_UNLOCKED_LINE_OPS 1 |
Timo Teras | 0a5b310 | 2011-06-29 02:19:58 +0200 | [diff] [blame] | 422 | #define HAVE_GETLINE 1 |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 423 | #define HAVE_XTABS 1 |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 424 | #define HAVE_MNTENT_H 1 |
| 425 | #define HAVE_NET_ETHERNET_H 1 |
| 426 | #define HAVE_SYS_STATFS_H 1 |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 427 | #define HAVE_PRINTF_PERCENTM 1 |
Denys Vlasenko | c0943ac | 2021-04-14 22:14:36 +0200 | [diff] [blame] | 428 | #define HAVE_WAIT3 1 |
Ron Yorston | a1b0d38 | 2020-07-23 08:32:27 +0100 | [diff] [blame] | 429 | #define HAVE_DEV_FD 1 |
| 430 | #define DEV_FD_PREFIX "/dev/fd/" |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 431 | |
Bernhard Reutner-Fischer | ad16741 | 2014-04-13 16:37:57 +0200 | [diff] [blame] | 432 | #if defined(__UCLIBC__) |
| 433 | # if UCLIBC_VERSION < KERNEL_VERSION(0, 9, 32) |
| 434 | # undef HAVE_STRVERSCMP |
| 435 | # endif |
| 436 | # if UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30) |
| 437 | # ifndef __UCLIBC_SUSV3_LEGACY__ |
| 438 | # undef HAVE_USLEEP |
| 439 | # endif |
| 440 | # endif |
Denys Vlasenko | 1e18a01 | 2011-06-21 17:12:52 +0200 | [diff] [blame] | 441 | #endif |
| 442 | |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 443 | #if defined(__WATCOMC__) |
Denys Vlasenko | 47061b4 | 2011-04-17 23:14:19 +0200 | [diff] [blame] | 444 | # undef HAVE_DPRINTF |
Dan Fandrich | 0e79e7b | 2011-06-28 23:03:27 -0700 | [diff] [blame] | 445 | # undef HAVE_GETLINE |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 446 | # undef HAVE_MEMRCHR |
| 447 | # undef HAVE_MKDTEMP |
| 448 | # undef HAVE_SETBIT |
Dan Fandrich | dc50676 | 2011-02-12 22:26:57 -0800 | [diff] [blame] | 449 | # undef HAVE_STPCPY |
Denys Vlasenko | 33745b1 | 2021-02-18 13:44:27 +0100 | [diff] [blame] | 450 | # undef HAVE_STPNCPY |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 451 | # undef HAVE_STRCASESTR |
| 452 | # undef HAVE_STRCHRNUL |
Dan Fandrich | 0635ddd | 2010-06-18 22:36:45 -0700 | [diff] [blame] | 453 | # undef HAVE_STRSEP |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 454 | # undef HAVE_STRSIGNAL |
Denys Vlasenko | 561f9c8 | 2011-06-21 16:38:29 +0200 | [diff] [blame] | 455 | # undef HAVE_STRVERSCMP |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 456 | # undef HAVE_VASPRINTF |
Dan Fandrich | 75214cf | 2011-06-30 02:59:17 +0200 | [diff] [blame] | 457 | # undef HAVE_UNLOCKED_STDIO |
| 458 | # undef HAVE_UNLOCKED_LINE_OPS |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 459 | # undef HAVE_NET_ETHERNET_H |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 460 | #endif |
| 461 | |
Denys Vlasenko | 4dc35fb | 2011-07-08 04:41:38 +0200 | [diff] [blame] | 462 | #if defined(__CYGWIN__) |
| 463 | # undef HAVE_CLEARENV |
| 464 | # undef HAVE_FDPRINTF |
| 465 | # undef HAVE_MEMRCHR |
| 466 | # undef HAVE_PTSNAME_R |
| 467 | # undef HAVE_STRVERSCMP |
| 468 | # undef HAVE_UNLOCKED_LINE_OPS |
| 469 | #endif |
| 470 | |
Dan Fandrich | 0e79e7b | 2011-06-28 23:03:27 -0700 | [diff] [blame] | 471 | /* These BSD-derived OSes share many similarities */ |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 472 | #if (defined __digital__ && defined __unix__) \ |
| 473 | || defined __APPLE__ \ |
Denys Vlasenko | 8e0ad26 | 2014-01-08 15:10:54 +0100 | [diff] [blame] | 474 | || defined __OpenBSD__ || defined __NetBSD__ |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 475 | # undef HAVE_CLEARENV |
| 476 | # undef HAVE_FDATASYNC |
Dan Fandrich | 0e79e7b | 2011-06-28 23:03:27 -0700 | [diff] [blame] | 477 | # undef HAVE_GETLINE |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 478 | # undef HAVE_MNTENT_H |
| 479 | # undef HAVE_PTSNAME_R |
| 480 | # undef HAVE_SYS_STATFS_H |
| 481 | # undef HAVE_SIGHANDLER_T |
Denys Vlasenko | 561f9c8 | 2011-06-21 16:38:29 +0200 | [diff] [blame] | 482 | # undef HAVE_STRVERSCMP |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 483 | # undef HAVE_XTABS |
| 484 | # undef HAVE_DPRINTF |
Matthias Andree | 1285437 | 2011-08-28 05:04:07 +0200 | [diff] [blame] | 485 | # undef HAVE_UNLOCKED_STDIO |
| 486 | # undef HAVE_UNLOCKED_LINE_OPS |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 487 | # undef HAVE_PRINTF_PERCENTM |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 488 | #endif |
| 489 | |
Denys Vlasenko | 432fbd7 | 2014-01-07 14:09:47 +0100 | [diff] [blame] | 490 | #if defined(__dietlibc__) |
Dan Fandrich | 0e79e7b | 2011-06-28 23:03:27 -0700 | [diff] [blame] | 491 | # undef HAVE_STRCHRNUL |
| 492 | #endif |
| 493 | |
Denys Vlasenko | 432fbd7 | 2014-01-07 14:09:47 +0100 | [diff] [blame] | 494 | #if defined(__APPLE__) |
| 495 | # undef HAVE_STRCHRNUL |
| 496 | #endif |
| 497 | |
| 498 | #if defined(__FreeBSD__) |
Denys Vlasenko | 5104645 | 2015-02-02 03:51:47 +0100 | [diff] [blame] | 499 | /* users say mempcpy is not present in FreeBSD 9.x */ |
| 500 | # undef HAVE_MEMPCPY |
Denys Vlasenko | 8e0ad26 | 2014-01-08 15:10:54 +0100 | [diff] [blame] | 501 | # undef HAVE_CLEARENV |
| 502 | # undef HAVE_FDATASYNC |
| 503 | # undef HAVE_MNTENT_H |
| 504 | # undef HAVE_PTSNAME_R |
| 505 | # undef HAVE_SYS_STATFS_H |
| 506 | # undef HAVE_SIGHANDLER_T |
| 507 | # undef HAVE_STRVERSCMP |
| 508 | # undef HAVE_XTABS |
| 509 | # undef HAVE_UNLOCKED_LINE_OPS |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 510 | # undef HAVE_PRINTF_PERCENTM |
Denys Vlasenko | 8e0ad26 | 2014-01-08 15:10:54 +0100 | [diff] [blame] | 511 | # include <osreldate.h> |
Denys Vlasenko | 432fbd7 | 2014-01-07 14:09:47 +0100 | [diff] [blame] | 512 | # if __FreeBSD_version < 1000029 |
Denys Vlasenko | 8e0ad26 | 2014-01-08 15:10:54 +0100 | [diff] [blame] | 513 | # undef HAVE_STRCHRNUL /* FreeBSD added strchrnul() between 1000028 and 1000029 */ |
Denys Vlasenko | 432fbd7 | 2014-01-07 14:09:47 +0100 | [diff] [blame] | 514 | # endif |
| 515 | #endif |
| 516 | |
Dan Fandrich | 0e79e7b | 2011-06-28 23:03:27 -0700 | [diff] [blame] | 517 | #if defined(__NetBSD__) |
| 518 | # define HAVE_GETLINE 1 /* Recent NetBSD versions have getline() */ |
| 519 | #endif |
| 520 | |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 521 | #if defined(__digital__) && defined(__unix__) |
| 522 | # undef HAVE_STPCPY |
Denys Vlasenko | 33745b1 | 2021-02-18 13:44:27 +0100 | [diff] [blame] | 523 | # undef HAVE_STPNCPY |
Denys Vlasenko | 89f5bfd | 2011-05-12 23:03:18 +0200 | [diff] [blame] | 524 | #endif |
| 525 | |
Denys Vlasenko | e0894f5 | 2011-09-09 18:00:44 +0200 | [diff] [blame] | 526 | #if defined(ANDROID) || defined(__ANDROID__) |
Matt Whitlock | 778efe3 | 2015-05-03 18:59:50 +0200 | [diff] [blame] | 527 | # if __ANDROID_API__ < 8 |
Chris Renshaw | 6df9612 | 2015-12-17 16:42:01 +0100 | [diff] [blame] | 528 | /* ANDROID < 8 has no [f]dprintf at all */ |
Matt Whitlock | 778efe3 | 2015-05-03 18:59:50 +0200 | [diff] [blame] | 529 | # undef HAVE_DPRINTF |
Chris Renshaw | 6df9612 | 2015-12-17 16:42:01 +0100 | [diff] [blame] | 530 | # elif __ANDROID_API__ < 21 |
| 531 | /* ANDROID < 21 has fdprintf */ |
Matt Whitlock | 778efe3 | 2015-05-03 18:59:50 +0200 | [diff] [blame] | 532 | # define dprintf fdprintf |
Chris Renshaw | 6df9612 | 2015-12-17 16:42:01 +0100 | [diff] [blame] | 533 | # else |
| 534 | /* ANDROID >= 21 has standard dprintf */ |
| 535 | # endif |
Matt Whitlock | 778efe3 | 2015-05-03 18:59:50 +0200 | [diff] [blame] | 536 | # if __ANDROID_API__ < 21 |
| 537 | # undef HAVE_TTYNAME_R |
| 538 | # undef HAVE_GETLINE |
| 539 | # undef HAVE_STPCPY |
Denys Vlasenko | 33745b1 | 2021-02-18 13:44:27 +0100 | [diff] [blame] | 540 | # undef HAVE_STPNCPY |
Matt Whitlock | 778efe3 | 2015-05-03 18:59:50 +0200 | [diff] [blame] | 541 | # endif |
Denys Vlasenko | c0943ac | 2021-04-14 22:14:36 +0200 | [diff] [blame] | 542 | # if __ANDROID_API__ >= 21 |
| 543 | # undef HAVE_WAIT3 |
| 544 | # endif |
Matt Whitlock | f23e3ec | 2015-05-03 18:57:44 +0200 | [diff] [blame] | 545 | # undef HAVE_MEMPCPY |
Dan Fandrich | 71d7313 | 2011-06-03 20:51:58 +0200 | [diff] [blame] | 546 | # undef HAVE_STRCHRNUL |
Denys Vlasenko | 561f9c8 | 2011-06-21 16:38:29 +0200 | [diff] [blame] | 547 | # undef HAVE_STRVERSCMP |
Dan Fandrich | 75214cf | 2011-06-30 02:59:17 +0200 | [diff] [blame] | 548 | # undef HAVE_UNLOCKED_LINE_OPS |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 549 | # undef HAVE_NET_ETHERNET_H |
Ron Yorston | be366e5 | 2017-07-27 13:53:39 +0100 | [diff] [blame] | 550 | # undef HAVE_PRINTF_PERCENTM |
Dan Fandrich | 71d7313 | 2011-06-03 20:51:58 +0200 | [diff] [blame] | 551 | #endif |
| 552 | |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 553 | /* |
| 554 | * Now, define prototypes for all the functions defined in platform.c |
| 555 | * These must come after all the HAVE_* macros are defined (or not) |
| 556 | */ |
| 557 | |
Denys Vlasenko | 47061b4 | 2011-04-17 23:14:19 +0200 | [diff] [blame] | 558 | #ifndef HAVE_DPRINTF |
| 559 | extern int dprintf(int fd, const char *format, ...); |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 560 | #endif |
| 561 | |
| 562 | #ifndef HAVE_MEMRCHR |
| 563 | extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC; |
| 564 | #endif |
| 565 | |
| 566 | #ifndef HAVE_MKDTEMP |
| 567 | extern char *mkdtemp(char *template) FAST_FUNC; |
| 568 | #endif |
| 569 | |
Matt Whitlock | cee5905 | 2015-04-25 21:32:48 +0200 | [diff] [blame] | 570 | #ifndef HAVE_TTYNAME_R |
| 571 | #define ttyname_r bb_ttyname_r |
| 572 | extern int ttyname_r(int fd, char *buf, size_t buflen); |
| 573 | #endif |
| 574 | |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 575 | #ifndef HAVE_SETBIT |
| 576 | # define setbit(a, b) ((a)[(b) >> 3] |= 1 << ((b) & 7)) |
| 577 | # define clrbit(a, b) ((a)[(b) >> 3] &= ~(1 << ((b) & 7))) |
| 578 | #endif |
| 579 | |
Chris Rees | 9ad97d5 | 2011-01-20 00:51:52 +0100 | [diff] [blame] | 580 | #ifndef HAVE_SIGHANDLER_T |
| 581 | typedef void (*sighandler_t)(int); |
| 582 | #endif |
| 583 | |
Dan Fandrich | dc50676 | 2011-02-12 22:26:57 -0800 | [diff] [blame] | 584 | #ifndef HAVE_STPCPY |
| 585 | extern char *stpcpy(char *p, const char *to_add) FAST_FUNC; |
| 586 | #endif |
| 587 | |
Denys Vlasenko | 33745b1 | 2021-02-18 13:44:27 +0100 | [diff] [blame] | 588 | #ifndef HAVE_STPNCPY |
| 589 | extern char *stpncpy(char *p, const char *to_add, size_t n) FAST_FUNC; |
| 590 | #endif |
| 591 | |
Denys Vlasenko | 50a6d86 | 2015-01-25 22:08:46 +0100 | [diff] [blame] | 592 | #ifndef HAVE_MEMPCPY |
Denys Vlasenko | 8c05a74 | 2015-01-29 16:41:48 +0100 | [diff] [blame] | 593 | #include <string.h> |
Denys Vlasenko | 5104645 | 2015-02-02 03:51:47 +0100 | [diff] [blame] | 594 | /* In case we are wrong about !HAVE_MEMPCPY, and toolchain _does_ have |
| 595 | * mempcpy(), avoid colliding with it: |
| 596 | */ |
| 597 | #define mempcpy bb__mempcpy |
Denys Vlasenko | 50a6d86 | 2015-01-25 22:08:46 +0100 | [diff] [blame] | 598 | static ALWAYS_INLINE void *mempcpy(void *dest, const void *src, size_t len) |
| 599 | { |
| 600 | return memcpy(dest, src, len) + len; |
| 601 | } |
| 602 | #endif |
| 603 | |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 604 | #ifndef HAVE_STRCASESTR |
| 605 | extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC; |
| 606 | #endif |
| 607 | |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 608 | #ifndef HAVE_STRCHRNUL |
| 609 | extern char *strchrnul(const char *s, int c) FAST_FUNC; |
| 610 | #endif |
| 611 | |
Dan Fandrich | 0635ddd | 2010-06-18 22:36:45 -0700 | [diff] [blame] | 612 | #ifndef HAVE_STRSEP |
| 613 | extern char *strsep(char **stringp, const char *delim) FAST_FUNC; |
| 614 | #endif |
| 615 | |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 616 | #ifndef HAVE_STRSIGNAL |
| 617 | /* Not exactly the same: instead of "Stopped" it shows "STOP" etc */ |
| 618 | # define strsignal(sig) get_signame(sig) |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 619 | #endif |
| 620 | |
Bernhard Reutner-Fischer | ad16741 | 2014-04-13 16:37:57 +0200 | [diff] [blame] | 621 | #ifndef HAVE_USLEEP |
| 622 | extern int usleep(unsigned) FAST_FUNC; |
| 623 | #endif |
| 624 | |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 625 | #ifndef HAVE_VASPRINTF |
| 626 | extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC; |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 627 | #endif |
Denys Vlasenko | 5a49d28 | 2009-05-19 13:18:45 +0200 | [diff] [blame] | 628 | |
Timo Teras | 0a5b310 | 2011-06-29 02:19:58 +0200 | [diff] [blame] | 629 | #ifndef HAVE_GETLINE |
Denys Vlasenko | 14bd16a | 2011-07-08 08:49:40 +0200 | [diff] [blame] | 630 | # include <stdio.h> /* for FILE */ |
| 631 | # include <sys/types.h> /* size_t */ |
Timo Teras | 0a5b310 | 2011-06-29 02:19:58 +0200 | [diff] [blame] | 632 | extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC; |
| 633 | #endif |
| 634 | |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 635 | #endif |