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