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