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