Home | History | Annotate | Line # | Download | only in compat
compat_defs.h revision 1.67
      1 /*	$NetBSD: compat_defs.h,v 1.67 2008/10/20 10:28:38 apb Exp $	*/
      2 
      3 #ifndef	__NETBSD_COMPAT_DEFS_H__
      4 #define	__NETBSD_COMPAT_DEFS_H__
      5 
      6 
      7 /* Work around some complete brain damage. */
      8 /*
      9  * Linux: <features.h> turns on _POSIX_SOURCE by default, even though the
     10  * program (not the OS) should do that.  Preload <features.h> to keep any
     11  * of this crap from being pulled in, and undefine _POSIX_SOURCE.
     12  */
     13 
     14 #if defined(__linux__) && HAVE_FEATURES_H
     15 #include <features.h>
     16 #define __USE_ISOC99 1
     17 #endif
     18 
     19 /* So _NETBSD_SOURCE doesn't end up defined. Define enough to pull in standard
     20    defs. Other platforms may need similiar defines. */
     21 #ifdef __NetBSD__
     22 #define	_ISOC99_SOURCE
     23 #define _POSIX_SOURCE	1
     24 #define _POSIX_C_SOURCE	200112L
     25 #define _XOPEN_SOURCE 600
     26 #else
     27 #undef _POSIX_SOURCE
     28 #undef _POSIX_C_SOURCE
     29 #endif
     30 
     31 /* System headers needed for (re)definitions below. */
     32 
     33 #include <sys/types.h>
     34 #include <sys/mman.h>
     35 #include <sys/param.h>
     36 /* time.h needs to be pulled in first at least on netbsd w/o _NETBSD_SOURCE */
     37 #include <sys/time.h>
     38 #include <sys/stat.h>
     39 #include <errno.h>
     40 #include <fcntl.h>
     41 #include <grp.h>
     42 #include <limits.h>
     43 #include <paths.h>
     44 #include <stdarg.h>
     45 #include <stdio.h>
     46 #include <stdlib.h>
     47 #include <string.h>
     48 
     49 #if HAVE_SYS_CDEFS_H
     50 #include <sys/cdefs.h>
     51 #endif
     52 #if HAVE_SYS_SYSLIMITS_H
     53 #include <sys/syslimits.h>
     54 #endif
     55 #if HAVE_SYS_SYSMACROS_H
     56 /* major(), minor() on SVR4 */
     57 #include <sys/sysmacros.h>
     58 #endif
     59 #if HAVE_INTTYPES_H
     60 #include <inttypes.h>
     61 #endif
     62 #if HAVE_STDDEF_H
     63 #include <stddef.h>
     64 #endif
     65 
     66 #ifdef _NETBSD_SOURCE
     67 #error _NETBSD_SOURCE is *not* to be defined.
     68 #endif
     69 
     70 /* Need this since we can't depend on NetBSD's version to be around */
     71 #ifdef __UNCONST
     72 #undef __UNCONST
     73 #endif
     74 #define __UNCONST(a)   ((void *)(unsigned long)(const void *)(a))
     75 
     76 /* We don't include <pwd.h> here, so that "compat_pwd.h" works. */
     77 struct passwd;
     78 
     79 /* Assume an ANSI compiler for the host. */
     80 
     81 #undef __P
     82 #define __P(x) x
     83 
     84 #ifndef __BEGIN_DECLS
     85 #define __BEGIN_DECLS
     86 #endif
     87 #ifndef __END_DECLS
     88 #define __END_DECLS
     89 #endif
     90 
     91 /* Some things usually in BSD <sys/cdefs.h>. */
     92 
     93 #ifndef __CONCAT
     94 #define	__CONCAT(x,y)	x ## y
     95 #endif
     96 #if !defined(__attribute__) && !defined(__GNUC__)
     97 #define __attribute__(x)
     98 #endif
     99 #if !defined(__packed)
    100 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
    101 #define __packed	__attribute__((__packed__))
    102 #else
    103 #define	__packed	error: no __packed for this compiler
    104 #endif
    105 #endif /* !__packed */
    106 #ifndef __RENAME
    107 #define __RENAME(x)
    108 #endif
    109 #undef __aconst
    110 #define __aconst
    111 #undef __dead
    112 #define __dead
    113 #undef __restrict
    114 #define __restrict
    115 #undef __unused
    116 #define __unused
    117 
    118 /* Dirent support. */
    119 
    120 #if HAVE_DIRENT_H
    121 # if defined(__linux__) && defined(__USE_BSD)
    122 #  undef __USE_BSD
    123 #  include <dirent.h>
    124 #  define __USE_BSD 1
    125 #  undef d_fileno
    126 # else
    127 #  include <dirent.h>
    128 #  if defined(__DARWIN_UNIX03)
    129 #   undef d_fileno
    130 #  endif
    131 # endif
    132 # define NAMLEN(dirent) (strlen((dirent)->d_name))
    133 #else
    134 # define dirent direct
    135 # define NAMLEN(dirent) ((dirent)->d_namlen)
    136 # if HAVE_SYS_NDIR_H
    137 #  include <sys/ndir.h>
    138 # endif
    139 # if HAVE_SYS_DIR_H
    140 #  include <sys/dir.h>
    141 # endif
    142 # if HAVE_NDIR_H
    143 #  include <ndir.h>
    144 # endif
    145 #endif
    146 
    147 /* Type substitutes. */
    148 
    149 #if !HAVE_ID_T
    150 typedef unsigned int id_t;
    151 #endif
    152 
    153 #if !HAVE_SOCKLEN_T
    154 typedef int socklen_t;
    155 #endif
    156 
    157 #if !HAVE_U_LONG
    158 typedef unsigned long u_long;
    159 #endif
    160 
    161 #if !HAVE_U_CHAR
    162 typedef unsigned char u_char;
    163 #endif
    164 
    165 #if !HAVE_U_INT
    166 typedef unsigned int u_int;
    167 #endif
    168 
    169 #if !HAVE_U_SHORT
    170 typedef unsigned short u_short;
    171 #endif
    172 
    173 /* Prototypes for replacement functions. */
    174 
    175 #if !HAVE_ATOLL
    176 long long int atoll(const char *);
    177 #endif
    178 
    179 #if !HAVE_ASPRINTF
    180 int asprintf(char **, const char *, ...);
    181 #endif
    182 
    183 #if !HAVE_ASNPRINTF
    184 int asnprintf(char **, size_t, const char *, ...);
    185 #endif
    186 
    187 #if !HAVE_BASENAME
    188 char *basename(char *);
    189 #endif
    190 
    191 #if !HAVE_DECL_OPTIND
    192 int getopt(int, char *const *, const char *);
    193 extern char *optarg;
    194 extern int optind, opterr, optopt;
    195 #endif
    196 
    197 #if !HAVE_DIRNAME
    198 char *dirname(char *);
    199 #endif
    200 
    201 #if !HAVE_DIRFD
    202 #if HAVE_DIR_DD_FD
    203 #define dirfd(dirp) ((dirp)->dd_fd)
    204 #elif HAVE_DIR___DD_FD
    205 #define dirfd(dirp) ((dirp)->__dd_fd)
    206 #else
    207 /*XXX: Very hacky but no other way to bring this into scope w/o defining
    208   _NETBSD_SOURCE which we're avoiding. */
    209 #ifdef __NetBSD__
    210 struct _dirdesc {
    211         int     dd_fd;          /* file descriptor associated with directory */
    212 	long    dd_loc;         /* offset in current buffer */
    213 	long    dd_size;        /* amount of data returned by getdents */
    214 	char    *dd_buf;        /* data buffer */
    215 	int     dd_len;         /* size of data buffer */
    216 	off_t   dd_seek;        /* magic cookie returned by getdents */
    217 	long    dd_rewind;      /* magic cookie for rewinding */
    218 	int     dd_flags;       /* flags for readdir */
    219 	void    *dd_lock;       /* lock for concurrent access */
    220 };
    221 #define dirfd(dirp)     (((struct _dirdesc *)dirp)->dd_fd)
    222 #else
    223 #error cannot figure out how to turn a DIR * into a fd
    224 #endif
    225 #endif
    226 #endif
    227 
    228 #if !HAVE_ERR_H
    229 void err(int, const char *, ...);
    230 void errx(int, const char *, ...);
    231 void warn(const char *, ...);
    232 void warnx(const char *, ...);
    233 #endif
    234 
    235 #if !HAVE_ESETFUNC
    236 void (*esetfunc(void (*)(int, const char *, ...)))(int, const char *, ...);
    237 size_t estrlcpy(char *, const char *, size_t);
    238 size_t estrlcat(char *, const char *, size_t);
    239 char *estrdup(const char *);
    240 char *estrndup(const char *, size_t);
    241 void *ecalloc(size_t, size_t);
    242 void *emalloc(size_t);
    243 void *erealloc(void *, size_t);
    244 FILE *efopen(const char *, const char *);
    245 int easprintf(char **, const char *, ...);
    246 int evasprintf(char **, const char *, va_list);
    247 #endif
    248 
    249 #if !HAVE_FGETLN || defined(__NetBSD__)
    250 char *fgetln(FILE *, size_t *);
    251 #endif
    252 
    253 #if !HAVE_FLOCK
    254 # define LOCK_SH		0x01
    255 # define LOCK_EX		0x02
    256 # define LOCK_NB		0x04
    257 # define LOCK_UN		0x08
    258 int flock(int, int);
    259 #endif
    260 
    261 #if !HAVE_FPARSELN || defined(__NetBSD__)
    262 # define FPARSELN_UNESCESC	0x01
    263 # define FPARSELN_UNESCCONT	0x02
    264 # define FPARSELN_UNESCCOMM	0x04
    265 # define FPARSELN_UNESCREST	0x08
    266 # define FPARSELN_UNESCALL	0x0f
    267 char *fparseln(FILE *, size_t *, size_t *, const char [3], int);
    268 #endif
    269 
    270 #if !HAVE_ISSETUGID
    271 int issetugid(void);
    272 #endif
    273 
    274 #if !HAVE_ISBLANK && !defined(isblank)
    275 #define isblank(x) ((x) == ' ' || (x) == '\t')
    276 #endif
    277 
    278 #define __nbcompat_bswap16(x)	((((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff))
    279 
    280 #define __nbcompat_bswap32(x)	((((x) << 24) & 0xff000000) | \
    281 				 (((x) <<  8) & 0x00ff0000) | \
    282 				 (((x) >>  8) & 0x0000ff00) | \
    283 				 (((x) >> 24) & 0x000000ff))
    284 
    285 #define __nbcompat_bswap64(x)	(((u_int64_t)bswap32((x)) << 32) | \
    286 				 ((u_int64_t)bswap32((x) >> 32)))
    287 
    288 #if ! HAVE_DECL_BSWAP16
    289 #ifdef bswap16
    290 #undef bswap16
    291 #endif
    292 #define bswap16(x)	__nbcompat_bswap16(x)
    293 #endif
    294 #if ! HAVE_DECL_BSWAP32
    295 #ifdef bswap32
    296 #undef bswap32
    297 #endif
    298 #define bswap32(x)	__nbcompat_bswap32(x)
    299 #endif
    300 #if ! HAVE_DECL_BSWAP64
    301 #ifdef bswap64
    302 #undef bswap64
    303 #endif
    304 #define bswap64(x)	__nbcompat_bswap64(x)
    305 #endif
    306 
    307 #if !HAVE_MKSTEMP
    308 int mkstemp(char *);
    309 #endif
    310 
    311 #if !HAVE_MKDTEMP
    312 char *mkdtemp(char *);
    313 #endif
    314 
    315 #if !HAVE_MKSTEMP || !HAVE_MKDTEMP
    316 /* This is a prototype for the internal function defined in
    317  * src/lib/lib/stdio/gettemp.c */
    318 int __nbcompat_gettemp(char *, int *, int);
    319 #endif
    320 
    321 #if !HAVE_PREAD
    322 ssize_t pread(int, void *, size_t, off_t);
    323 #endif
    324 
    325 #if !HAVE_HEAPSORT
    326 int heapsort (void *, size_t, size_t, int (*)(const void *, const void *));
    327 #endif
    328 /* Make them use our version */
    329 #  define heapsort __nbcompat_heapsort
    330 
    331 /*
    332  * HAVE_X_FROM_Y and HAVE_PWCACHE_FOODB go together, because we cannot
    333  * supply an implementation of one without the others -- some parts are
    334  * libc internal and this varies from system to system.
    335  *
    336  * XXX this is dubious anyway: we assume (see HAVE_DECLs below) that if the
    337  * XXX host system has all of these functions, all of their interfaces
    338  * XXX and interactions are exactly the same as in our libc/libutil -- ugh.
    339  */
    340 #if !HAVE_USER_FROM_UID || !HAVE_UID_FROM_USER || !HAVE_GROUP_FROM_GID || \
    341     !HAVE_GID_FROM_GROUP || !HAVE_PWCACHE_USERDB || !HAVE_PWCACHE_GROUDB
    342 /* Make them use our version */
    343 #  define user_from_uid __nbcompat_user_from_uid
    344 #  define uid_from_user __nbcompat_uid_from_user
    345 #  define pwcache_userdb __nbcompat_pwcache_userdb
    346 #  define group_from_gid __nbcompat_group_from_gid
    347 #  define gid_from_group __nbcompat_gid_from_group
    348 #  define pwcache_groupdb __nbcompat_pwcache_groupdb
    349 #endif
    350 
    351 #if !HAVE_DECL_UID_FROM_USER
    352 int uid_from_user(const char *, uid_t *);
    353 #endif
    354 #if !HAVE_DECL_USER_FROM_UID
    355 const char *user_from_uid(uid_t, int);
    356 #endif
    357 #if !HAVE_DECL_PWCACHE_USERDB
    358 int pwcache_userdb(int (*)(int), void (*)(void),
    359                 struct passwd * (*)(const char *), struct passwd * (*)(uid_t));
    360 #endif
    361 #if !HAVE_DECL_GID_FROM_GROUP
    362 int gid_from_group(const char *, gid_t *);
    363 #endif
    364 #if !HAVE_DECL_GROUP_FROM_GID
    365 const char *group_from_gid(gid_t, int);
    366 #endif
    367 #if !HAVE_DECL_PWCACHE_GROUPDB
    368 int pwcache_groupdb(int (*)(int), void (*)(void),
    369                 struct group * (*)(const char *), struct group * (*)(gid_t));
    370 #endif
    371 
    372 #if !HAVE_DECL_STRNDUP
    373 char		*strndup(const char *, size_t);
    374 #endif
    375 #if !HAVE_DECL_LCHFLAGS
    376 int		lchflags(const char *, unsigned long);
    377 #endif
    378 #if !HAVE_DECL_LCHMOD
    379 int		lchmod(const char *, mode_t);
    380 #endif
    381 #if !HAVE_DECL_LCHOWN
    382 int		lchown(const char *, uid_t, gid_t);
    383 #endif
    384 
    385 #if !HAVE_PWRITE
    386 ssize_t pwrite(int, const void *, size_t, off_t);
    387 #endif
    388 
    389 #if !HAVE_RAISE_DEFAULT_SIGNAL
    390 int raise_default_signal(int);
    391 #endif
    392 
    393 #if !HAVE_SETENV
    394 int setenv(const char *, const char *, int);
    395 #endif
    396 
    397 #if !HAVE_DECL_SETGROUPENT
    398 int setgroupent(int);
    399 #endif
    400 
    401 #if !HAVE_DECL_SETPASSENT
    402 int setpassent(int);
    403 #endif
    404 
    405 #if !HAVE_SETPROGNAME || defined(__NetBSD__)
    406 const char *getprogname(void);
    407 void setprogname(const char *);
    408 #endif
    409 
    410 #if !HAVE_SNPRINTF
    411 int snprintf(char *, size_t, const char *, ...);
    412 #endif
    413 
    414 #if !HAVE_STRLCAT
    415 size_t strlcat(char *, const char *, size_t);
    416 #endif
    417 
    418 #if !HAVE_STRLCPY
    419 size_t strlcpy(char *, const char *, size_t);
    420 #endif
    421 
    422 #if !HAVE_STRMODE
    423 void strmode(mode_t, char *);
    424 #endif
    425 
    426 #if !HAVE_STRNDUP
    427 char *strndup(const char *, size_t);
    428 #endif
    429 
    430 #if !HAVE_STRSEP || defined(__NetBSD__)
    431 char *strsep(char **, const char *);
    432 #endif
    433 
    434 #if !HAVE_DECL_STRSUFTOLL
    435 long long strsuftoll(const char *, const char *, long long, long long);
    436 long long strsuftollx(const char *, const char *,
    437 			long long, long long, char *, size_t);
    438 #endif
    439 
    440 #if !HAVE_STRTOLL
    441 long long strtoll(const char *, char **, int);
    442 #endif
    443 
    444 #if !HAVE_USER_FROM_UID
    445 const char *user_from_uid(uid_t, int);
    446 #endif
    447 
    448 #if !HAVE_GROUP_FROM_GID
    449 const char *group_from_gid(gid_t, int);
    450 #endif
    451 
    452 #if !HAVE_VASPRINTF
    453 int vasprintf(char **, const char *, va_list);
    454 #endif
    455 
    456 #if !HAVE_VASNPRINTF
    457 int vasnprintf(char **, size_t, const char *, va_list);
    458 #endif
    459 
    460 #if !HAVE_VSNPRINTF
    461 int vsnprintf(char *, size_t, const char *, va_list);
    462 #endif
    463 
    464 /*
    465  * getmode() and setmode() are always defined, as these function names
    466  * exist but with very different meanings on other OS's.  The compat
    467  * versions here simply accept an octal mode number; the "u+x,g-w" type
    468  * of syntax is not accepted.
    469  */
    470 
    471 #define getmode __nbcompat_getmode
    472 #define setmode __nbcompat_setmode
    473 
    474 mode_t getmode(const void *, mode_t);
    475 void *setmode(const char *);
    476 
    477 /* Eliminate assertions embedded in binaries. */
    478 
    479 #undef _DIAGASSERT
    480 #define _DIAGASSERT(x)
    481 
    482 /* Various sources use this */
    483 #undef	__RCSID
    484 #define	__RCSID(x)
    485 #undef	__SCCSID
    486 #define	__SCCSID(x)
    487 #undef	__COPYRIGHT
    488 #define	__COPYRIGHT(x)
    489 #undef	__KERNEL_RCSID
    490 #define	__KERNEL_RCSID(x,y)
    491 
    492 /* Heimdal expects this one. */
    493 
    494 #undef RCSID
    495 #define RCSID(x)
    496 
    497 /* Some definitions not available on all systems. */
    498 
    499 /* <errno.h> */
    500 
    501 #ifndef EFTYPE
    502 #define EFTYPE EIO
    503 #endif
    504 
    505 /* <fcntl.h> */
    506 
    507 #ifndef O_EXLOCK
    508 #define O_EXLOCK 0
    509 #endif
    510 #ifndef O_SHLOCK
    511 #define O_SHLOCK 0
    512 #endif
    513 
    514 /* <limits.h> */
    515 
    516 #ifndef UID_MAX
    517 #define UID_MAX 32767
    518 #endif
    519 #ifndef GID_MAX
    520 #define GID_MAX UID_MAX
    521 #endif
    522 
    523 #ifndef UQUAD_MAX
    524 #define UQUAD_MAX ((u_quad_t)-1)
    525 #endif
    526 #ifndef QUAD_MAX
    527 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
    528 #endif
    529 #ifndef QUAD_MIN
    530 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
    531 #endif
    532 #ifndef ULLONG_MAX
    533 #define ULLONG_MAX ((unsigned long long)-1)
    534 #endif
    535 #ifndef LLONG_MAX
    536 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
    537 #endif
    538 #ifndef LLONG_MIN
    539 #define LLONG_MIN ((long long)(~LLONG_MAX))
    540 #endif
    541 
    542 /* <paths.h> */
    543 
    544 /* The host's _PATH_BSHELL might be broken, so override it. */
    545 #undef _PATH_BSHELL
    546 #define _PATH_BSHELL PATH_BSHELL
    547 #ifndef _PATH_DEFPATH
    548 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
    549 #endif
    550 #ifndef _PATH_DEV
    551 #define _PATH_DEV "/dev/"
    552 #endif
    553 #ifndef _PATH_DEVNULL
    554 #define _PATH_DEVNULL _PATH_DEV "null"
    555 #endif
    556 #ifndef _PATH_TMP
    557 #define _PATH_TMP "/tmp/"
    558 #endif
    559 #ifndef _PATH_DEFTAPE
    560 #define _PATH_DEFTAPE "/dev/nrst0"
    561 #endif
    562 #ifndef _PATH_VI
    563 #define _PATH_VI "/usr/bin/vi"
    564 #endif
    565 
    566 /* <stdarg.h> */
    567 
    568 #ifndef _BSD_VA_LIST_
    569 #define _BSD_VA_LIST_ va_list
    570 #endif
    571 
    572 /* <stdint.h> */
    573 
    574 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX)
    575 #define SIZE_MAX SIZE_T_MAX
    576 #endif
    577 
    578 #ifndef UINT8_MAX
    579 #define UINT8_MAX 0xffU
    580 #endif
    581 
    582 #ifndef UINT16_MAX
    583 #define UINT16_MAX 0xffffU
    584 #endif
    585 
    586 #ifndef UINT32_MAX
    587 #define UINT32_MAX 0xffffffffU
    588 #endif
    589 
    590 /* <stdlib.h> */
    591 
    592 #ifndef __GNUC__
    593 # if HAVE_ALLOCA_H
    594 #  include <alloca.h>
    595 # else
    596 #  ifndef alloca /* predefined by HP cc +Olibcalls */
    597 char *alloca ();
    598 #  endif
    599 # endif
    600 #endif
    601 
    602 /* avoid prototype conflicts with host */
    603 #define cgetcap __nbcompat_cgetcap
    604 #define cgetclose __nbcompat_cgetclose
    605 #define cgetent __nbcompat_cgetent
    606 #define cgetfirst __nbcompat_cgetfirst
    607 #define cgetmatch __nbcompat_cgetmatch
    608 #define cgetnext __nbcompat_cgetnext
    609 #define cgetnum __nbcompat_cgetnum
    610 #define cgetset __nbcompat_cgetset
    611 #define cgetstr __nbcompat_cgetstr
    612 #define cgetustr __nbcompat_cgetustr
    613 
    614 char	*cgetcap(char *, const char *, int);
    615 int	 cgetclose(void);
    616 int	 cgetent(char **, const char * const *, const char *);
    617 int	 cgetfirst(char **, const char * const *);
    618 int	 cgetmatch(const char *, const char *);
    619 int	 cgetnext(char **, const char * const *);
    620 int	 cgetnum(char *, const char *, long *);
    621 int	 cgetset(const char *);
    622 int	 cgetstr(char *, const char *, char **);
    623 int	 cgetustr(char *, const char *, char **);
    624 
    625 /* <sys/endian.h> */
    626 
    627 #if WORDS_BIGENDIAN
    628 #if !HAVE_DECL_HTOBE16
    629 #define htobe16(x)	(x)
    630 #endif
    631 #if !HAVE_DECL_HTOBE32
    632 #define htobe32(x)	(x)
    633 #endif
    634 #if !HAVE_DECL_HTOBE64
    635 #define htobe64(x)	(x)
    636 #endif
    637 #if !HAVE_DECL_HTOLE16
    638 #define htole16(x)	bswap16((u_int16_t)(x))
    639 #endif
    640 #if !HAVE_DECL_HTOLE32
    641 #define htole32(x)	bswap32((u_int32_t)(x))
    642 #endif
    643 #if !HAVE_DECL_HTOLE64
    644 #define htole64(x)	bswap64((u_int64_t)(x))
    645 #endif
    646 #else
    647 #if !HAVE_DECL_HTOBE16
    648 #define htobe16(x)	bswap16((u_int16_t)(x))
    649 #endif
    650 #if !HAVE_DECL_HTOBE32
    651 #define htobe32(x)	bswap32((u_int32_t)(x))
    652 #endif
    653 #if !HAVE_DECL_HTOBE64
    654 #define htobe64(x)	bswap64((u_int64_t)(x))
    655 #endif
    656 #if !HAVE_DECL_HTOLE16
    657 #define htole16(x)	(x)
    658 #endif
    659 #if !HAVE_DECL_HTOLE32
    660 #define htole32(x)	(x)
    661 #endif
    662 #if !HAVE_DECL_HTOLE64
    663 #define htole64(x)	(x)
    664 #endif
    665 #endif
    666 #if !HAVE_DECL_BE16TOH
    667 #define be16toh(x)	htobe16(x)
    668 #endif
    669 #if !HAVE_DECL_BE32TOH
    670 #define be32toh(x)	htobe32(x)
    671 #endif
    672 #if !HAVE_DECL_BE64TOH
    673 #define be64toh(x)	htobe64(x)
    674 #endif
    675 #if !HAVE_DECL_LE16TOH
    676 #define le16toh(x)	htole16(x)
    677 #endif
    678 #if !HAVE_DECL_LE32TOH
    679 #define le32toh(x)	htole32(x)
    680 #endif
    681 #if !HAVE_DECL_LE64TOH
    682 #define le64toh(x)	htole64(x)
    683 #endif
    684 
    685 #define __GEN_ENDIAN_ENC(bits, endian) \
    686 static void \
    687 endian ## bits ## enc(void *dst, uint ## bits ## _t u) \
    688 { \
    689 	u = hto ## endian ## bits (u); \
    690 	memcpy(dst, &u, sizeof(u)); \
    691 }
    692 #if !HAVE_DECL_BE16ENC
    693 __GEN_ENDIAN_ENC(16, be)
    694 #endif
    695 #if !HAVE_DECL_BE32ENC
    696 __GEN_ENDIAN_ENC(32, be)
    697 #endif
    698 #if !HAVE_DECL_BE64ENC
    699 __GEN_ENDIAN_ENC(64, be)
    700 #endif
    701 #if !HAVE_DECL_LE16ENC
    702 __GEN_ENDIAN_ENC(16, le)
    703 #endif
    704 #if !HAVE_DECL_LE32ENC
    705 __GEN_ENDIAN_ENC(32, le)
    706 #endif
    707 #if !HAVE_DECL_LE64ENC
    708 __GEN_ENDIAN_ENC(64, le)
    709 #endif
    710 #undef __GEN_ENDIAN_ENC
    711 
    712 #define __GEN_ENDIAN_DEC(bits, endian) \
    713 static uint ## bits ## _t \
    714 endian ## bits ## dec(const void *buf) \
    715 { \
    716 	uint ## bits ## _t u; \
    717 	memcpy(&u, buf, sizeof(u)); \
    718 	return endian ## bits ## toh (u); \
    719 }
    720 #if !HAVE_DECL_BE16DEC
    721 __GEN_ENDIAN_DEC(16, be)
    722 #endif
    723 #if !HAVE_DECL_BE32DEC
    724 __GEN_ENDIAN_DEC(32, be)
    725 #endif
    726 #if !HAVE_DECL_BE64DEC
    727 __GEN_ENDIAN_DEC(64, be)
    728 #endif
    729 #if !HAVE_DECL_LE16DEC
    730 __GEN_ENDIAN_DEC(16, le)
    731 #endif
    732 #if !HAVE_DECL_LE32DEC
    733 __GEN_ENDIAN_DEC(32, le)
    734 #endif
    735 #if !HAVE_DECL_LE64DEC
    736 __GEN_ENDIAN_DEC(64, le)
    737 #endif
    738 #undef __GEN_ENDIAN_DEC
    739 
    740 /* <sys/mman.h> */
    741 
    742 #ifndef MAP_FILE
    743 #define MAP_FILE 0
    744 #endif
    745 
    746 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
    747 #ifndef MAP_ANON
    748 #ifdef MAP_ANONYMOUS
    749 #define MAP_ANON MAP_ANONYMOUS
    750 #endif
    751 #endif
    752 
    753 /* <sys/param.h> */
    754 
    755 #undef BIG_ENDIAN
    756 #undef LITTLE_ENDIAN
    757 #define BIG_ENDIAN 4321
    758 #define LITTLE_ENDIAN 1234
    759 
    760 #undef BYTE_ORDER
    761 #if WORDS_BIGENDIAN
    762 #define BYTE_ORDER BIG_ENDIAN
    763 #else
    764 #define BYTE_ORDER LITTLE_ENDIAN
    765 #endif
    766 
    767 #ifndef DEV_BSIZE
    768 #define DEV_BSIZE (1 << 9)
    769 #endif
    770 
    771 #undef MIN
    772 #undef MAX
    773 #define MIN(a,b) ((a) < (b) ? (a) : (b))
    774 #define MAX(a,b) ((a) > (b) ? (a) : (b))
    775 
    776 #ifndef MAXBSIZE
    777 #define MAXBSIZE (64 * 1024)
    778 #endif
    779 #ifndef MAXFRAG
    780 #define MAXFRAG 8
    781 #endif
    782 #ifndef MAXPHYS
    783 #define MAXPHYS (64 * 1024)
    784 #endif
    785 
    786 /* XXX needed by makefs; this should be done in a better way */
    787 #undef btodb
    788 #define btodb(x) ((x) << 9)
    789 
    790 #undef setbit
    791 #undef clrbit
    792 #undef isset
    793 #undef isclr
    794 #define	setbit(a,i)	((a)[(i)/NBBY] |= 1<<((i)%NBBY))
    795 #define	clrbit(a,i)	((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
    796 #define	isset(a,i)	((a)[(i)/NBBY] & (1<<((i)%NBBY)))
    797 #define	isclr(a,i)	(((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
    798 
    799 #ifndef powerof2
    800 #define powerof2(x) ((((x)-1)&(x))==0)
    801 #endif
    802 
    803 #undef roundup
    804 #define roundup(x, y)	((((x)+((y)-1))/(y))*(y))
    805 
    806 /* <sys/stat.h> */
    807 
    808 #ifndef ALLPERMS
    809 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
    810 #endif
    811 #ifndef DEFFILEMODE
    812 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
    813 #endif
    814 #ifndef S_ISTXT
    815 #ifdef S_ISVTX
    816 #define S_ISTXT S_ISVTX
    817 #else
    818 #define S_ISTXT 0
    819 #endif
    820 #endif
    821 
    822 /* Protected by _NETBSD_SOURCE otherwise. */
    823 #if HAVE_STRUCT_STAT_ST_FLAGS && defined(__NetBSD__)
    824 #define UF_SETTABLE     0x0000ffff
    825 #define UF_NODUMP       0x00000001
    826 #define UF_IMMUTABLE    0x00000002
    827 #define UF_APPEND       0x00000004
    828 #define UF_OPAQUE       0x00000008
    829 #define SF_SETTABLE     0xffff0000
    830 #define SF_ARCHIVED     0x00010000
    831 #define SF_IMMUTABLE    0x00020000
    832 #define SF_APPEND       0x00040000
    833 #endif
    834 
    835 /* <sys/syslimits.h> */
    836 
    837 #ifndef LINE_MAX
    838 #define LINE_MAX 2048
    839 #endif
    840 
    841 /* <sys/time.h> */
    842 
    843 #ifndef timercmp
    844 #define	timercmp(tvp, uvp, cmp)						\
    845 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
    846 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
    847 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
    848 #endif
    849 #ifndef timeradd
    850 #define	timeradd(tvp, uvp, vvp)						\
    851 	do {								\
    852 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
    853 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
    854 		if ((vvp)->tv_usec >= 1000000) {			\
    855 			(vvp)->tv_sec++;				\
    856 			(vvp)->tv_usec -= 1000000;			\
    857 		}							\
    858 	} while (/* CONSTCOND */ 0)
    859 #endif
    860 #ifndef timersub
    861 #define	timersub(tvp, uvp, vvp)						\
    862 	do {								\
    863 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
    864 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
    865 		if ((vvp)->tv_usec < 0) {				\
    866 			(vvp)->tv_sec--;				\
    867 			(vvp)->tv_usec += 1000000;			\
    868 		}							\
    869 	} while (/* CONSTCOND */ 0)
    870 #endif
    871 
    872 /* <sys/types.h> */
    873 
    874 #ifdef major
    875 #undef major
    876 #endif
    877 #define major(x)        ((int32_t)((((x) & 0x000fff00) >>  8)))
    878 
    879 #ifdef minor
    880 #undef minor
    881 #endif
    882 #define minor(x)        ((int32_t)((((x) & 0xfff00000) >> 12) | \
    883                                    (((x) & 0x000000ff) >>  0)))
    884 #ifdef makedev
    885 #undef makedev
    886 #endif
    887 #define makedev(x,y)    ((dev_t)((((x) <<  8) & 0x000fff00) | \
    888 			(((y) << 12) & 0xfff00000) | \
    889 			(((y) <<  0) & 0x000000ff)))
    890 #ifndef NBBY
    891 #define NBBY 8
    892 #endif
    893 
    894 #if !HAVE_U_QUAD_T
    895 /* #define, not typedef, as quad_t exists as a struct on some systems */
    896 #define quad_t long long
    897 #define u_quad_t unsigned long long
    898 #define strtoq strtoll
    899 #define strtouq strtoull
    900 #endif
    901 
    902 /* Has quad_t but these prototypes don't get pulled into scope. w/o we lose */
    903 #ifdef __NetBSD__
    904 quad_t   strtoq __P((const char *, char **, int));
    905 u_quad_t strtouq __P((const char *, char **, int));
    906 #endif
    907 
    908 #endif	/* !__NETBSD_COMPAT_DEFS_H__ */
    909