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