platform.h 18 KB

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