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