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