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