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