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