1
2
3
4
5
6
7#ifndef BB_PLATFORM_H
8#define BB_PLATFORM_H 1
9
10
11
12#undef __GNUC_PREREQ
13#if defined __GNUC__ && defined __GNUC_MINOR__
14# define __GNUC_PREREQ(maj, min) \
15 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
16#else
17# define __GNUC_PREREQ(maj, min) 0
18#endif
19
20
21#if !__GNUC_PREREQ(2,92)
22# ifndef __restrict
23# define __restrict
24# endif
25#endif
26
27#if !__GNUC_PREREQ(2,7)
28# ifndef __attribute__
29# define __attribute__(x)
30# endif
31#endif
32
33#undef inline
34#if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
35
36#elif __GNUC_PREREQ(2,7)
37# define inline __inline__
38#else
39# define inline
40#endif
41
42#ifndef __const
43# define __const const
44#endif
45
46#define UNUSED_PARAM __attribute__ ((__unused__))
47#define NORETURN __attribute__ ((__noreturn__))
48
49#if __GNUC_PREREQ(4,5)
50# define bb_unreachable(altcode) __builtin_unreachable()
51#else
52# define bb_unreachable(altcode) altcode
53#endif
54
55
56
57
58
59
60
61
62
63#define RETURNS_MALLOC __attribute__ ((malloc))
64#define PACKED __attribute__ ((__packed__))
65#define ALIGNED(m) __attribute__ ((__aligned__(m)))
66
67
68#if __GNUC_PREREQ(3,0) && !defined(__NO_INLINE__)
69# define ALWAYS_INLINE __attribute__ ((always_inline)) inline
70
71# define NOINLINE __attribute__((__noinline__))
72# if !ENABLE_WERROR
73# define DEPRECATED __attribute__ ((__deprecated__))
74# define UNUSED_PARAM_RESULT __attribute__ ((warn_unused_result))
75# else
76# define DEPRECATED
77# define UNUSED_PARAM_RESULT
78# endif
79#else
80# define ALWAYS_INLINE inline
81# define NOINLINE
82# define DEPRECATED
83# define UNUSED_PARAM_RESULT
84#endif
85
86
87#define INIT_FUNC __attribute__ ((constructor))
88
89
90
91#if __GNUC_PREREQ(4,1)
92# define EXTERNALLY_VISIBLE __attribute__(( visibility("default") ))
93
94#else
95# define EXTERNALLY_VISIBLE
96#endif
97
98
99#if __GNUC_PREREQ(4,4)
100# define FIX_ALIASING __attribute__((__may_alias__))
101#else
102# define FIX_ALIASING
103#endif
104
105
106
107
108#if !__GNUC_PREREQ(2,8)
109# ifndef __extension__
110# define __extension__
111# endif
112#endif
113
114
115
116
117
118
119
120
121
122#ifndef FAST_FUNC
123# if __GNUC_PREREQ(3,0) && defined(i386)
124
125# define FAST_FUNC __attribute__((regparm(3),stdcall))
126
127# else
128# define FAST_FUNC
129# endif
130#endif
131
132
133
134#if __GNUC_PREREQ(4,1) && !defined(__CYGWIN__)
135# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN _Pragma("GCC visibility push(hidden)")
136# define POP_SAVED_FUNCTION_VISIBILITY _Pragma("GCC visibility pop")
137#else
138# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
139# define POP_SAVED_FUNCTION_VISIBILITY
140#endif
141
142
143#if !__GNUC_PREREQ(3,0)
144# include <stdarg.h>
145# if !defined va_copy && defined __va_copy
146# define va_copy(d,s) __va_copy((d),(s))
147# endif
148#endif
149
150
151
152
153#include <limits.h>
154#if defined(__digital__) && defined(__unix__)
155# include <sex.h>
156#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
157 || defined(__APPLE__)
158# include <sys/resource.h>
159# include <machine/endian.h>
160# define bswap_64 __bswap64
161# define bswap_32 __bswap32
162# define bswap_16 __bswap16
163#else
164# include <byteswap.h>
165# include <endian.h>
166#endif
167
168#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
169# define BB_BIG_ENDIAN 1
170# define BB_LITTLE_ENDIAN 0
171#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
172# define BB_BIG_ENDIAN 0
173# define BB_LITTLE_ENDIAN 1
174#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN
175# define BB_BIG_ENDIAN 1
176# define BB_LITTLE_ENDIAN 0
177#elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN
178# define BB_BIG_ENDIAN 0
179# define BB_LITTLE_ENDIAN 1
180#elif defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN
181# define BB_BIG_ENDIAN 1
182# define BB_LITTLE_ENDIAN 0
183#elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
184# define BB_BIG_ENDIAN 0
185# define BB_LITTLE_ENDIAN 1
186#elif defined(__386__)
187# define BB_BIG_ENDIAN 0
188# define BB_LITTLE_ENDIAN 1
189#else
190# error "Can't determine endianness"
191#endif
192
193#if ULONG_MAX > 0xffffffff
194# define bb_bswap_64(x) bswap_64(x)
195#endif
196
197
198#if BB_BIG_ENDIAN
199# define SWAP_BE16(x) (x)
200# define SWAP_BE32(x) (x)
201# define SWAP_BE64(x) (x)
202# define SWAP_LE16(x) bswap_16(x)
203# define SWAP_LE32(x) bswap_32(x)
204# define SWAP_LE64(x) bb_bswap_64(x)
205# define IF_BIG_ENDIAN(...) __VA_ARGS__
206# define IF_LITTLE_ENDIAN(...)
207#else
208# define SWAP_BE16(x) bswap_16(x)
209# define SWAP_BE32(x) bswap_32(x)
210# define SWAP_BE64(x) bb_bswap_64(x)
211# define SWAP_LE16(x) (x)
212# define SWAP_LE32(x) (x)
213# define SWAP_LE64(x) (x)
214# define IF_BIG_ENDIAN(...)
215# define IF_LITTLE_ENDIAN(...) __VA_ARGS__
216#endif
217
218
219
220
221#include <stdint.h>
222typedef int bb__aliased_int FIX_ALIASING;
223typedef long bb__aliased_long FIX_ALIASING;
224typedef uint16_t bb__aliased_uint16_t FIX_ALIASING;
225typedef uint32_t bb__aliased_uint32_t FIX_ALIASING;
226typedef uint64_t bb__aliased_uint64_t FIX_ALIASING;
227
228
229
230
231#if defined(i386) || defined(__x86_64__) || defined(__powerpc__)
232# define BB_UNALIGNED_MEMACCESS_OK 1
233# define move_from_unaligned_int(v, intp) ((v) = *(bb__aliased_int*)(intp))
234# define move_from_unaligned_long(v, longp) ((v) = *(bb__aliased_long*)(longp))
235# define move_from_unaligned16(v, u16p) ((v) = *(bb__aliased_uint16_t*)(u16p))
236# define move_from_unaligned32(v, u32p) ((v) = *(bb__aliased_uint32_t*)(u32p))
237# define move_to_unaligned16(u16p, v) (*(bb__aliased_uint16_t*)(u16p) = (v))
238# define move_to_unaligned32(u32p, v) (*(bb__aliased_uint32_t*)(u32p) = (v))
239# define move_to_unaligned64(u64p, v) (*(bb__aliased_uint64_t*)(u64p) = (v))
240
241#else
242# define BB_UNALIGNED_MEMACCESS_OK 0
243
244# define move_from_unaligned_int(v, intp) (memcpy(&(v), (intp), sizeof(int)))
245# define move_from_unaligned_long(v, longp) (memcpy(&(v), (longp), sizeof(long)))
246# define move_from_unaligned16(v, u16p) (memcpy(&(v), (u16p), 2))
247# define move_from_unaligned32(v, u32p) (memcpy(&(v), (u32p), 4))
248# define move_to_unaligned16(u16p, v) do { \
249 uint16_t __t = (v); \
250 memcpy((u16p), &__t, 2); \
251} while (0)
252# define move_to_unaligned32(u32p, v) do { \
253 uint32_t __t = (v); \
254 memcpy((u32p), &__t, 4); \
255} while (0)
256# define move_to_unaligned64(u64p, v) do { \
257 uint64_t __t = (v); \
258 memcpy((u64p), &__t, 8); \
259} while (0)
260#endif
261
262
263#define get_unaligned_le32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_LE32(v); })
264#define get_unaligned_be32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_BE32(v); })
265#define put_unaligned_le32(val, buf) move_to_unaligned32(buf, SWAP_LE32(val))
266#define put_unaligned_be32(val, buf) move_to_unaligned32(buf, SWAP_BE32(val))
267
268
269
270
271#define get_le32(u32p) ({ uint32_t v = *(bb__aliased_uint32_t*)(u32p); SWAP_LE32(v); })
272
273
274
275
276#if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__)
277
278typedef signed char smallint;
279typedef unsigned char smalluint;
280#else
281
282typedef int smallint;
283typedef unsigned smalluint;
284#endif
285
286
287#if (defined __digital__ && defined __unix__)
288
289# define bool smalluint
290#else
291
292# include <stdbool.h>
293#endif
294
295
296
297
298#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
299
300#ifdef __UCLIBC__
301# define UCLIBC_VERSION KERNEL_VERSION(__UCLIBC_MAJOR__, __UCLIBC_MINOR__, __UCLIBC_SUBLEVEL__)
302#else
303# define UCLIBC_VERSION 0
304#endif
305
306
307
308
309#if defined __GLIBC__ \
310 || defined __UCLIBC__ \
311 || defined __dietlibc__ \
312 || defined __BIONIC__ \
313 || defined _NEWLIB_VERSION
314# include <features.h>
315#endif
316
317
318#if defined(__digital__) && defined(__unix__)
319
320# define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me, __me); } while (0)
321#else
322# define bb_setpgrp() setpgrp()
323#endif
324
325
326#include <unistd.h>
327#define fdprintf dprintf
328
329
330#if !defined(__s390__)
331
332# define ALIGN1 __attribute__((aligned(1)))
333# define ALIGN2 __attribute__((aligned(2)))
334# define ALIGN4 __attribute__((aligned(4)))
335#else
336
337# define ALIGN1
338# define ALIGN2
339# define ALIGN4
340#endif
341
342
343
344
345
346
347#if ENABLE_NOMMU || \
348 (defined __UCLIBC__ && \
349 UCLIBC_VERSION > KERNEL_VERSION(0, 9, 28) && \
350 !defined __ARCH_USE_MMU__)
351# define BB_MMU 0
352# define USE_FOR_NOMMU(...) __VA_ARGS__
353# define USE_FOR_MMU(...)
354#else
355# define BB_MMU 1
356# define USE_FOR_NOMMU(...)
357# define USE_FOR_MMU(...) __VA_ARGS__
358#endif
359
360#if defined(__digital__) && defined(__unix__)
361# include <standards.h>
362# include <inttypes.h>
363# define PRIu32 "u"
364# if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
365# define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
366# endif
367# if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
368# define ADJ_FREQUENCY MOD_FREQUENCY
369# endif
370# if !defined ADJ_TIMECONST && defined MOD_TIMECONST
371# define ADJ_TIMECONST MOD_TIMECONST
372# endif
373# if !defined ADJ_TICK && defined MOD_CLKB
374# define ADJ_TICK MOD_CLKB
375# endif
376#endif
377
378#if defined(__CYGWIN__)
379# define MAXSYMLINKS SYMLOOP_MAX
380#endif
381
382#if defined(ANDROID) || defined(__ANDROID__)
383# define BB_ADDITIONAL_PATH ":/system/sbin:/system/bin:/system/xbin"
384# define SYS_ioprio_set __NR_ioprio_set
385# define SYS_ioprio_get __NR_ioprio_get
386#endif
387
388
389
390
391
392
393
394#define HAVE_CLEARENV 1
395#define HAVE_FDATASYNC 1
396#define HAVE_DPRINTF 1
397#define HAVE_MEMRCHR 1
398#define HAVE_MKDTEMP 1
399#define HAVE_TTYNAME_R 1
400#define HAVE_PTSNAME_R 1
401#define HAVE_SETBIT 1
402#define HAVE_SIGHANDLER_T 1
403#define HAVE_STPCPY 1
404#define HAVE_MEMPCPY 1
405#define HAVE_STRCASESTR 1
406#define HAVE_STRCHRNUL 1
407#define HAVE_STRSEP 1
408#define HAVE_STRSIGNAL 1
409#define HAVE_STRVERSCMP 1
410#define HAVE_VASPRINTF 1
411#define HAVE_USLEEP 1
412#define HAVE_UNLOCKED_STDIO 1
413#define HAVE_UNLOCKED_LINE_OPS 1
414#define HAVE_GETLINE 1
415#define HAVE_XTABS 1
416#define HAVE_MNTENT_H 1
417#define HAVE_NET_ETHERNET_H 1
418#define HAVE_SYS_STATFS_H 1
419#define HAVE_PRINTF_PERCENTM 1
420
421#if defined(__UCLIBC__)
422# if UCLIBC_VERSION < KERNEL_VERSION(0, 9, 32)
423# undef HAVE_STRVERSCMP
424# endif
425# if UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30)
426# ifndef __UCLIBC_SUSV3_LEGACY__
427# undef HAVE_USLEEP
428# endif
429# endif
430#endif
431
432#if defined(__WATCOMC__)
433# undef HAVE_DPRINTF
434# undef HAVE_GETLINE
435# undef HAVE_MEMRCHR
436# undef HAVE_MKDTEMP
437# undef HAVE_SETBIT
438# undef HAVE_STPCPY
439# undef HAVE_STRCASESTR
440# undef HAVE_STRCHRNUL
441# undef HAVE_STRSEP
442# undef HAVE_STRSIGNAL
443# undef HAVE_STRVERSCMP
444# undef HAVE_VASPRINTF
445# undef HAVE_UNLOCKED_STDIO
446# undef HAVE_UNLOCKED_LINE_OPS
447# undef HAVE_NET_ETHERNET_H
448#endif
449
450#if defined(__CYGWIN__)
451# undef HAVE_CLEARENV
452# undef HAVE_FDPRINTF
453# undef HAVE_MEMRCHR
454# undef HAVE_PTSNAME_R
455# undef HAVE_STRVERSCMP
456# undef HAVE_UNLOCKED_LINE_OPS
457#endif
458
459
460#if (defined __digital__ && defined __unix__) \
461 || defined __APPLE__ \
462 || defined __OpenBSD__ || defined __NetBSD__
463# undef HAVE_CLEARENV
464# undef HAVE_FDATASYNC
465# undef HAVE_GETLINE
466# undef HAVE_MNTENT_H
467# undef HAVE_PTSNAME_R
468# undef HAVE_SYS_STATFS_H
469# undef HAVE_SIGHANDLER_T
470# undef HAVE_STRVERSCMP
471# undef HAVE_XTABS
472# undef HAVE_DPRINTF
473# undef HAVE_UNLOCKED_STDIO
474# undef HAVE_UNLOCKED_LINE_OPS
475# undef HAVE_PRINTF_PERCENTM
476#endif
477
478#if defined(__dietlibc__)
479# undef HAVE_STRCHRNUL
480#endif
481
482#if defined(__APPLE__)
483# undef HAVE_STRCHRNUL
484#endif
485
486#if defined(__FreeBSD__)
487
488# undef HAVE_MEMPCPY
489# undef HAVE_CLEARENV
490# undef HAVE_FDATASYNC
491# undef HAVE_MNTENT_H
492# undef HAVE_PTSNAME_R
493# undef HAVE_SYS_STATFS_H
494# undef HAVE_SIGHANDLER_T
495# undef HAVE_STRVERSCMP
496# undef HAVE_XTABS
497# undef HAVE_UNLOCKED_LINE_OPS
498# undef HAVE_PRINTF_PERCENTM
499# include <osreldate.h>
500# if __FreeBSD_version < 1000029
501# undef HAVE_STRCHRNUL
502# endif
503#endif
504
505#if defined(__NetBSD__)
506# define HAVE_GETLINE 1
507#endif
508
509#if defined(__digital__) && defined(__unix__)
510# undef HAVE_STPCPY
511#endif
512
513#if defined(ANDROID) || defined(__ANDROID__)
514# if __ANDROID_API__ < 8
515
516# undef HAVE_DPRINTF
517# elif __ANDROID_API__ < 21
518
519# define dprintf fdprintf
520# else
521
522# endif
523# if __ANDROID_API__ < 21
524# undef HAVE_TTYNAME_R
525# undef HAVE_GETLINE
526# undef HAVE_STPCPY
527# endif
528# undef HAVE_MEMPCPY
529# undef HAVE_STRCHRNUL
530# undef HAVE_STRVERSCMP
531# undef HAVE_UNLOCKED_LINE_OPS
532# undef HAVE_NET_ETHERNET_H
533# undef HAVE_PRINTF_PERCENTM
534#endif
535
536
537
538
539
540
541#ifndef HAVE_DPRINTF
542extern int dprintf(int fd, const char *format, ...);
543#endif
544
545#ifndef HAVE_MEMRCHR
546extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC;
547#endif
548
549#ifndef HAVE_MKDTEMP
550extern char *mkdtemp(char *template) FAST_FUNC;
551#endif
552
553#ifndef HAVE_TTYNAME_R
554#define ttyname_r bb_ttyname_r
555extern int ttyname_r(int fd, char *buf, size_t buflen);
556#endif
557
558#ifndef HAVE_SETBIT
559# define setbit(a, b) ((a)[(b) >> 3] |= 1 << ((b) & 7))
560# define clrbit(a, b) ((a)[(b) >> 3] &= ~(1 << ((b) & 7)))
561#endif
562
563#ifndef HAVE_SIGHANDLER_T
564typedef void (*sighandler_t)(int);
565#endif
566
567#ifndef HAVE_STPCPY
568extern char *stpcpy(char *p, const char *to_add) FAST_FUNC;
569#endif
570
571#ifndef HAVE_MEMPCPY
572#include <string.h>
573
574
575
576#define mempcpy bb__mempcpy
577static ALWAYS_INLINE void *mempcpy(void *dest, const void *src, size_t len)
578{
579 return memcpy(dest, src, len) + len;
580}
581#endif
582
583#ifndef HAVE_STRCASESTR
584extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC;
585#endif
586
587#ifndef HAVE_STRCHRNUL
588extern char *strchrnul(const char *s, int c) FAST_FUNC;
589#endif
590
591#ifndef HAVE_STRSEP
592extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
593#endif
594
595#ifndef HAVE_STRSIGNAL
596
597# define strsignal(sig) get_signame(sig)
598#endif
599
600#ifndef HAVE_USLEEP
601extern int usleep(unsigned) FAST_FUNC;
602#endif
603
604#ifndef HAVE_VASPRINTF
605extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;
606#endif
607
608#ifndef HAVE_GETLINE
609# include <stdio.h>
610# include <sys/types.h>
611extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC;
612#endif
613
614#endif
615