Home | History | Annotate | Line # | Download | only in compat
compat_defs.h revision 1.60
      1 /*	$NetBSD: compat_defs.h,v 1.60 2007/08/07 08:22:04 apb Exp $	*/
      2 
      3 #ifndef	__NETBSD_COMPAT_DEFS_H__
      4 #define	__NETBSD_COMPAT_DEFS_H__
      5 
      6 /* Work around some complete brain damage. */
      7 
      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 #endif
     17 
     18 /* So _NETBSD_SOURCE doesn't end up defined. Define enough to pull in standard
     19    defs. Other platforms may need similiar defines. */
     20 #ifdef __NetBSD__
     21 #define	_ISOC99_SOURCE
     22 #define _POSIX_SOURCE	1
     23 #define _POSIX_C_SOURCE	200112L
     24 #define _XOPEN_SOURCE 600
     25 #else
     26 #undef _POSIX_SOURCE
     27 #undef _POSIX_C_SOURCE
     28 #endif
     29 
     30 /* System headers needed for (re)definitions below. */
     31 
     32 #include <sys/types.h>
     33 #include <sys/mman.h>
     34 #include <sys/param.h>
     35 /* time.h needs to be pulled in first at least on netbsd w/o _NETBSD_SOURCE */
     36 #include <sys/time.h>
     37 #include <sys/stat.h>
     38 #include <errno.h>
     39 #include <fcntl.h>
     40 #include <grp.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 /* Assume an ANSI compiler for the host. */
     79 
     80 #undef __P
     81 #define __P(x) x
     82 
     83 #ifndef __BEGIN_DECLS
     84 #define __BEGIN_DECLS
     85 #endif
     86 #ifndef __END_DECLS
     87 #define __END_DECLS
     88 #endif
     89 
     90 /* Some things usually in BSD <sys/cdefs.h>. */
     91 
     92 #ifndef __CONCAT
     93 #define	__CONCAT(x,y)	x ## y
     94 #endif
     95 #if !defined(__attribute__) && !defined(__GNUC__)
     96 #define __attribute__(x)
     97 #endif
     98 #if !defined(__packed)
     99 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
    100 #define __packed	__attribute__((__packed__))
    101 #else
    102 #define	__packed	error: no __packed for this compiler
    103 #endif
    104 #endif /* !__packed */
    105 #ifndef __RENAME
    106 #define __RENAME(x)
    107 #endif
    108 #undef __aconst
    109 #define __aconst
    110 #undef __dead
    111 #define __dead
    112 #undef __restrict
    113 #define __restrict
    114 #undef __unused
    115 #define __unused
    116 
    117 /* Dirent support. */
    118 
    119 #if HAVE_DIRENT_H
    120 # if defined(__linux__) && defined(__USE_BSD)
    121 #  undef __USE_BSD
    122 #  include <dirent.h>
    123 #  define __USE_BSD 1
    124 #  undef d_fileno
    125 # else
    126 #  include <dirent.h>
    127 #  if defined(__DARWIN_UNIX03)
    128 #   undef d_fileno
    129 #  endif
    130 # endif
    131 # define NAMLEN(dirent) (strlen((dirent)->d_name))
    132 #else
    133 # define dirent direct
    134 # define NAMLEN(dirent) ((dirent)->d_namlen)
    135 # if HAVE_SYS_NDIR_H
    136 #  include <sys/ndir.h>
    137 # endif
    138 # if HAVE_SYS_DIR_H
    139 #  include <sys/dir.h>
    140 # endif
    141 # if HAVE_NDIR_H
    142 #  include <ndir.h>
    143 # endif
    144 #endif
    145 
    146 /* Type substitutes. */
    147 
    148 #if !HAVE_ID_T
    149 typedef unsigned int id_t;
    150 #endif
    151 
    152 #if !HAVE_SOCKLEN_T
    153 typedef int socklen_t;
    154 #endif
    155 
    156 #if !HAVE_U_LONG
    157 typedef unsigned long u_long;
    158 #endif
    159 
    160 #if !HAVE_U_CHAR
    161 typedef unsigned char u_char;
    162 #endif
    163 
    164 #if !HAVE_U_INT
    165 typedef unsigned int u_int;
    166 #endif
    167 
    168 #if !HAVE_U_SHORT
    169 typedef unsigned short u_short;
    170 #endif
    171 
    172 /* Prototypes for replacement functions. */
    173 
    174 #if !HAVE_ATOLL
    175 long long int atoll(const char *);
    176 #endif
    177 
    178 #if !HAVE_ASPRINTF
    179 int asprintf(char **, const char *, ...);
    180 #endif
    181 
    182 #if !HAVE_ASNPRINTF
    183 int asnprintf(char **, size_t, const char *, ...);
    184 #endif
    185 
    186 #if !HAVE_BASENAME
    187 char *basename(char *);
    188 #endif
    189 
    190 #if !HAVE_DECL_OPTIND
    191 int getopt(int, char *const *, const char *);
    192 extern char *optarg;
    193 extern int optind, opterr, optopt;
    194 #endif
    195 
    196 #if !HAVE_DIRNAME
    197 char *dirname(char *);
    198 #endif
    199 
    200 #if !HAVE_DIRFD
    201 #if HAVE_DIR_DD_FD
    202 #define dirfd(dirp) ((dirp)->dd_fd)
    203 #elif HAVE_DIR___DD_FD
    204 #define dirfd(dirp) ((dirp)->__dd_fd)
    205 #else
    206 /*XXX: Very hacky but no other way to bring this into scope w/o defining
    207   _NETBSD_SOURCE which we're avoiding. */
    208 #ifdef __NetBSD__
    209 struct _dirdesc {
    210         int     dd_fd;          /* file descriptor associated with directory */
    211 	long    dd_loc;         /* offset in current buffer */
    212 	long    dd_size;        /* amount of data returned by getdents */
    213 	char    *dd_buf;        /* data buffer */
    214 	int     dd_len;         /* size of data buffer */
    215 	off_t   dd_seek;        /* magic cookie returned by getdents */
    216 	long    dd_rewind;      /* magic cookie for rewinding */
    217 	int     dd_flags;       /* flags for readdir */
    218 	void    *dd_lock;       /* lock for concurrent access */
    219 };
    220 #define dirfd(dirp)     (((struct _dirdesc *)dirp)->dd_fd)
    221 #else
    222 #error cannot figure out how to turn a DIR * into a fd
    223 #endif
    224 #endif
    225 #endif
    226 
    227 #if !HAVE_ERR_H
    228 void err(int, const char *, ...);
    229 void errx(int, const char *, ...);
    230 void warn(const char *, ...);
    231 void warnx(const char *, ...);
    232 #endif
    233 
    234 #if !HAVE_ESETFUNC
    235 void (*esetfunc(void (*)(int, const char *, ...)))(int, const char *, ...);
    236 size_t estrlcpy(char *, const char *, size_t);
    237 size_t estrlcat(char *, const char *, size_t);
    238 char *estrdup(const char *);
    239 char *estrndup(const char *, size_t);
    240 void *ecalloc(size_t, size_t);
    241 void *emalloc(size_t);
    242 void *erealloc(void *, size_t);
    243 FILE *efopen(const char *, const char *);
    244 int easprintf(char **, const char *, ...);
    245 int evasprintf(char **, const char *, va_list);
    246 #endif
    247 
    248 #if !HAVE_FGETLN || defined(__NetBSD__)
    249 char *fgetln(FILE *, size_t *);
    250 #endif
    251 
    252 #if !HAVE_FLOCK
    253 # define LOCK_SH		0x01
    254 # define LOCK_EX		0x02
    255 # define LOCK_NB		0x04
    256 # define LOCK_UN		0x08
    257 int flock(int, int);
    258 #endif
    259 
    260 #if !HAVE_FPARSELN || defined(__NetBSD__)
    261 # define FPARSELN_UNESCESC	0x01
    262 # define FPARSELN_UNESCCONT	0x02
    263 # define FPARSELN_UNESCCOMM	0x04
    264 # define FPARSELN_UNESCREST	0x08
    265 # define FPARSELN_UNESCALL	0x0f
    266 char *fparseln(FILE *, size_t *, size_t *, const char [3], int);
    267 #endif
    268 
    269 #if !HAVE_ISSETUGID
    270 int issetugid(void);
    271 #endif
    272 
    273 #if !HAVE_ISBLANK && !defined(isblank)
    274 #define isblank(x) ((x) == ' ' || (x) == '\t')
    275 #endif
    276 
    277 #if !HAVE_LCHFLAGS
    278 int lchflags(const char *, u_long);
    279 #endif
    280 
    281 #if !HAVE_LCHMOD
    282 int lchmod(const char *, mode_t);
    283 #endif
    284 
    285 #if !HAVE_LCHOWN
    286 int lchown(const char *, uid_t, gid_t);
    287 #endif
    288 
    289 #define __nbcompat_bswap16(x)	((((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff))
    290 
    291 #define __nbcompat_bswap32(x)	((((x) << 24) & 0xff000000) | \
    292 				 (((x) <<  8) & 0x00ff0000) | \
    293 				 (((x) >>  8) & 0x0000ff00) | \
    294 				 (((x) >> 24) & 0x000000ff))
    295 
    296 #define __nbcompat_bswap64(x)	(((u_int64_t)bswap32((x)) << 32) | \
    297 				 ((u_int64_t)bswap32((x) >> 32)))
    298 
    299 #if ! HAVE_DECL_BSWAP16
    300 #ifdef bswap16
    301 #undef bswap16
    302 #endif
    303 #define bswap16(x)	__nbcompat_bswap16(x)
    304 #endif
    305 #if ! HAVE_DECL_BSWAP32
    306 #ifdef bswap32
    307 #undef bswap32
    308 #endif
    309 #define bswap32(x)	__nbcompat_bswap32(x)
    310 #endif
    311 #if ! HAVE_DECL_BSWAP64
    312 #ifdef bswap64
    313 #undef bswap64
    314 #endif
    315 #define bswap64(x)	__nbcompat_bswap64(x)
    316 #endif
    317 
    318 #if !HAVE_MKSTEMP
    319 int mkstemp(char *);
    320 #endif
    321 
    322 #if !HAVE_MKDTEMP
    323 char *mkdtemp(char *);
    324 #endif
    325 
    326 #if !HAVE_MKSTEMP || !HAVE_MKDTEMP
    327 /* This is a prototype for the internal function. */
    328 int gettemp(char *, int *, int);
    329 #endif
    330 
    331 #if !HAVE_PREAD
    332 ssize_t pread(int, void *, size_t, off_t);
    333 #endif
    334 
    335 #if !HAVE_HEAPSORT
    336 int heapsort (void *, size_t, size_t, int (*)(const void *, const void *));
    337 #endif
    338 /* Make them use our version */
    339 #  define heapsort __nbcompat_heapsort
    340 
    341 #if !HAVE_PWCACHE_USERDB
    342 int uid_from_user(const char *, uid_t *);
    343 int pwcache_userdb(int (*)(int), void (*)(void),
    344 		struct passwd * (*)(const char *), struct passwd * (*)(uid_t));
    345 int gid_from_group(const char *, gid_t *);
    346 int pwcache_groupdb(int (*)(int), void (*)(void),
    347 		struct group * (*)(const char *), struct group * (*)(gid_t));
    348 #endif
    349 /* Make them use our version */
    350 #  define user_from_uid __nbcompat_user_from_uid
    351 /* Make them use our version */
    352 #  define group_from_gid __nbcompat_group_from_gid
    353 
    354 #if !HAVE_PWRITE
    355 ssize_t pwrite(int, const void *, size_t, off_t);
    356 #endif
    357 
    358 #if !HAVE_SETENV
    359 int setenv(const char *, const char *, int);
    360 #endif
    361 
    362 #if !HAVE_DECL_SETGROUPENT
    363 int setgroupent(int);
    364 #endif
    365 
    366 #if !HAVE_DECL_SETPASSENT
    367 int setpassent(int);
    368 #endif
    369 
    370 #if !HAVE_SETPROGNAME || defined(__NetBSD__)
    371 const char *getprogname(void);
    372 void setprogname(const char *);
    373 #endif
    374 
    375 #if !HAVE_SNPRINTF
    376 int snprintf(char *, size_t, const char *, ...);
    377 #endif
    378 
    379 #if !HAVE_STRLCAT
    380 size_t strlcat(char *, const char *, size_t);
    381 #endif
    382 
    383 #if !HAVE_STRLCPY
    384 size_t strlcpy(char *, const char *, size_t);
    385 #endif
    386 
    387 #if !HAVE_STRMODE
    388 void strmode(mode_t, char *);
    389 #endif
    390 
    391 #if !HAVE_STRNDUP
    392 char *strndup(const char *, size_t);
    393 #endif
    394 
    395 #if !HAVE_STRSEP || defined(__NetBSD__)
    396 char *strsep(char **, const char *);
    397 #endif
    398 
    399 #if !HAVE_DECL_STRSUFTOLL
    400 long long strsuftoll(const char *, const char *, long long, long long);
    401 long long strsuftollx(const char *, const char *,
    402 			long long, long long, char *, size_t);
    403 #endif
    404 
    405 #if !HAVE_STRTOLL
    406 long long strtoll(const char *, char **, int);
    407 #endif
    408 
    409 #if !HAVE_USER_FROM_UID
    410 const char *user_from_uid(uid_t, int);
    411 #endif
    412 
    413 #if !HAVE_GROUP_FROM_GID
    414 const char *group_from_gid(gid_t, int);
    415 #endif
    416 
    417 #if !HAVE_VASPRINTF
    418 int vasprintf(char **, const char *, va_list);
    419 #endif
    420 
    421 #if !HAVE_VASNPRINTF
    422 int vasnprintf(char **, size_t, const char *, va_list);
    423 #endif
    424 
    425 #if !HAVE_VSNPRINTF
    426 int vsnprintf(char *, size_t, const char *, va_list);
    427 #endif
    428 
    429 /*
    430  * getmode() and setmode() are always defined, as these function names
    431  * exist but with very different meanings on other OS's.  The compat
    432  * versions here simply accept an octal mode number; the "u+x,g-w" type
    433  * of syntax is not accepted.
    434  */
    435 
    436 #define getmode __nbcompat_getmode
    437 #define setmode __nbcompat_setmode
    438 
    439 mode_t getmode(const void *, mode_t);
    440 void *setmode(const char *);
    441 
    442 /* Eliminate assertions embedded in binaries. */
    443 
    444 #undef _DIAGASSERT
    445 #define _DIAGASSERT(x)
    446 
    447 /* Various sources use this */
    448 #undef	__RCSID
    449 #define	__RCSID(x)
    450 #undef	__SCCSID
    451 #define	__SCCSID(x)
    452 #undef	__COPYRIGHT
    453 #define	__COPYRIGHT(x)
    454 #undef	__KERNEL_RCSID
    455 #define	__KERNEL_RCSID(x,y)
    456 
    457 /* Heimdal expects this one. */
    458 
    459 #undef RCSID
    460 #define RCSID(x)
    461 
    462 /* Some definitions not available on all systems. */
    463 
    464 /* <errno.h> */
    465 
    466 #ifndef EFTYPE
    467 #define EFTYPE EIO
    468 #endif
    469 
    470 /* <fcntl.h> */
    471 
    472 #ifndef O_EXLOCK
    473 #define O_EXLOCK 0
    474 #endif
    475 #ifndef O_SHLOCK
    476 #define O_SHLOCK 0
    477 #endif
    478 
    479 /* <limits.h> */
    480 
    481 #ifndef UID_MAX
    482 #define UID_MAX 32767
    483 #endif
    484 #ifndef GID_MAX
    485 #define GID_MAX UID_MAX
    486 #endif
    487 
    488 #ifndef UQUAD_MAX
    489 #define UQUAD_MAX ((u_quad_t)-1)
    490 #endif
    491 #ifndef QUAD_MAX
    492 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
    493 #endif
    494 #ifndef QUAD_MIN
    495 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
    496 #endif
    497 #ifndef ULLONG_MAX
    498 #define ULLONG_MAX ((unsigned long long)-1)
    499 #endif
    500 #ifndef LLONG_MAX
    501 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
    502 #endif
    503 #ifndef LLONG_MIN
    504 #define LLONG_MIN ((long long)(~LLONG_MAX))
    505 #endif
    506 
    507 /* <paths.h> */
    508 
    509 /* The host's _PATH_BSHELL might be broken, so override it. */
    510 #undef _PATH_BSHELL
    511 #define _PATH_BSHELL PATH_BSHELL
    512 #ifndef _PATH_DEFPATH
    513 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
    514 #endif
    515 #ifndef _PATH_DEV
    516 #define _PATH_DEV "/dev/"
    517 #endif
    518 #ifndef _PATH_DEVNULL
    519 #define _PATH_DEVNULL _PATH_DEV "null"
    520 #endif
    521 #ifndef _PATH_TMP
    522 #define _PATH_TMP "/tmp/"
    523 #endif
    524 #ifndef _PATH_DEFTAPE
    525 #define _PATH_DEFTAPE "/dev/nrst0"
    526 #endif
    527 #ifndef _PATH_VI
    528 #define _PATH_VI "/usr/bin/vi"
    529 #endif
    530 
    531 /* <stdarg.h> */
    532 
    533 #ifndef _BSD_VA_LIST_
    534 #define _BSD_VA_LIST_ va_list
    535 #endif
    536 
    537 /* <stdint.h> */
    538 
    539 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX)
    540 #define SIZE_MAX SIZE_T_MAX
    541 #endif
    542 
    543 #ifndef UINT8_MAX
    544 #define UINT8_MAX 0xffU
    545 #endif
    546 
    547 #ifndef UINT16_MAX
    548 #define UINT16_MAX 0xffffU
    549 #endif
    550 
    551 #ifndef UINT32_MAX
    552 #define UINT32_MAX 0xffffffffU
    553 #endif
    554 
    555 /* <stdlib.h> */
    556 
    557 #ifndef __GNUC__
    558 # if HAVE_ALLOCA_H
    559 #  include <alloca.h>
    560 # else
    561 #  ifndef alloca /* predefined by HP cc +Olibcalls */
    562 char *alloca ();
    563 #  endif
    564 # endif
    565 #endif
    566 
    567 /* avoid prototype conflicts with host */
    568 #define cgetcap __nbcompat_cgetcap
    569 #define cgetclose __nbcompat_cgetclose
    570 #define cgetent __nbcompat_cgetent
    571 #define cgetfirst __nbcompat_cgetfirst
    572 #define cgetmatch __nbcompat_cgetmatch
    573 #define cgetnext __nbcompat_cgetnext
    574 #define cgetnum __nbcompat_cgetnum
    575 #define cgetset __nbcompat_cgetset
    576 #define cgetstr __nbcompat_cgetstr
    577 #define cgetustr __nbcompat_cgetustr
    578 
    579 char	*cgetcap(char *, const char *, int);
    580 int	 cgetclose(void);
    581 int	 cgetent(char **, const char * const *, const char *);
    582 int	 cgetfirst(char **, const char * const *);
    583 int	 cgetmatch(const char *, const char *);
    584 int	 cgetnext(char **, const char * const *);
    585 int	 cgetnum(char *, const char *, long *);
    586 int	 cgetset(const char *);
    587 int	 cgetstr(char *, const char *, char **);
    588 int	 cgetustr(char *, const char *, char **);
    589 
    590 /* <sys/endian.h> */
    591 
    592 #if WORDS_BIGENDIAN
    593 #if !HAVE_DECL_HTOBE16
    594 #define htobe16(x)	(x)
    595 #endif
    596 #if !HAVE_DECL_HTOBE32
    597 #define htobe32(x)	(x)
    598 #endif
    599 #if !HAVE_DECL_HTOBE64
    600 #define htobe64(x)	(x)
    601 #endif
    602 #if !HAVE_DECL_HTOLE16
    603 #define htole16(x)	bswap16((u_int16_t)(x))
    604 #endif
    605 #if !HAVE_DECL_HTOLE32
    606 #define htole32(x)	bswap32((u_int32_t)(x))
    607 #endif
    608 #if !HAVE_DECL_HTOLE64
    609 #define htole64(x)	bswap64((u_int64_t)(x))
    610 #endif
    611 #else
    612 #if !HAVE_DECL_HTOBE16
    613 #define htobe16(x)	bswap16((u_int16_t)(x))
    614 #endif
    615 #if !HAVE_DECL_HTOBE32
    616 #define htobe32(x)	bswap32((u_int32_t)(x))
    617 #endif
    618 #if !HAVE_DECL_HTOBE64
    619 #define htobe64(x)	bswap64((u_int64_t)(x))
    620 #endif
    621 #if !HAVE_DECL_HTOLE16
    622 #define htole16(x)	(x)
    623 #endif
    624 #if !HAVE_DECL_HTOLE32
    625 #define htole32(x)	(x)
    626 #endif
    627 #if !HAVE_DECL_HTOLE64
    628 #define htole64(x)	(x)
    629 #endif
    630 #endif
    631 #if !HAVE_DECL_BE16TOH
    632 #define be16toh(x)	htobe16(x)
    633 #endif
    634 #if !HAVE_DECL_BE32TOH
    635 #define be32toh(x)	htobe32(x)
    636 #endif
    637 #if !HAVE_DECL_BE64TOH
    638 #define be64toh(x)	htobe64(x)
    639 #endif
    640 #if !HAVE_DECL_LE16TOH
    641 #define le16toh(x)	htole16(x)
    642 #endif
    643 #if !HAVE_DECL_LE32TOH
    644 #define le32toh(x)	htole32(x)
    645 #endif
    646 #if !HAVE_DECL_LE64TOH
    647 #define le64toh(x)	htole64(x)
    648 #endif
    649 
    650 #define __GEN_ENDIAN_ENC(bits, endian) \
    651 static void \
    652 endian ## bits ## enc(void *dst, uint ## bits ## _t u) \
    653 { \
    654 	u = hto ## endian ## bits (u); \
    655 	memcpy(dst, &u, sizeof(u)); \
    656 }
    657 #if !HAVE_DECL_BE16ENC
    658 __GEN_ENDIAN_ENC(16, be)
    659 #endif
    660 #if !HAVE_DECL_BE32ENC
    661 __GEN_ENDIAN_ENC(32, be)
    662 #endif
    663 #if !HAVE_DECL_BE64ENC
    664 __GEN_ENDIAN_ENC(64, be)
    665 #endif
    666 #if !HAVE_DECL_LE16ENC
    667 __GEN_ENDIAN_ENC(16, le)
    668 #endif
    669 #if !HAVE_DECL_LE32ENC
    670 __GEN_ENDIAN_ENC(32, le)
    671 #endif
    672 #if !HAVE_DECL_LE64ENC
    673 __GEN_ENDIAN_ENC(64, le)
    674 #endif
    675 #undef __GEN_ENDIAN_ENC
    676 
    677 #define __GEN_ENDIAN_DEC(bits, endian) \
    678 static uint ## bits ## _t \
    679 endian ## bits ## dec(const void *buf) \
    680 { \
    681 	uint ## bits ## _t u; \
    682 	memcpy(&u, buf, sizeof(u)); \
    683 	return endian ## bits ## toh (u); \
    684 }
    685 #if !HAVE_DECL_BE16DEC
    686 __GEN_ENDIAN_DEC(16, be)
    687 #endif
    688 #if !HAVE_DECL_BE32DEC
    689 __GEN_ENDIAN_DEC(32, be)
    690 #endif
    691 #if !HAVE_DECL_BE64DEC
    692 __GEN_ENDIAN_DEC(64, be)
    693 #endif
    694 #if !HAVE_DECL_LE16DEC
    695 __GEN_ENDIAN_DEC(16, le)
    696 #endif
    697 #if !HAVE_DECL_LE32DEC
    698 __GEN_ENDIAN_DEC(32, le)
    699 #endif
    700 #if !HAVE_DECL_LE64DEC
    701 __GEN_ENDIAN_DEC(64, le)
    702 #endif
    703 #undef __GEN_ENDIAN_DEC
    704 
    705 /* <sys/mman.h> */
    706 
    707 #ifndef MAP_FILE
    708 #define MAP_FILE 0
    709 #endif
    710 
    711 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
    712 #ifndef MAP_ANON
    713 #ifdef MAP_ANONYMOUS
    714 #define MAP_ANON MAP_ANONYMOUS
    715 #endif
    716 #endif
    717 
    718 /* <sys/param.h> */
    719 
    720 #undef BIG_ENDIAN
    721 #undef LITTLE_ENDIAN
    722 #define BIG_ENDIAN 4321
    723 #define LITTLE_ENDIAN 1234
    724 
    725 #undef BYTE_ORDER
    726 #if WORDS_BIGENDIAN
    727 #define BYTE_ORDER BIG_ENDIAN
    728 #else
    729 #define BYTE_ORDER LITTLE_ENDIAN
    730 #endif
    731 
    732 #ifndef DEV_BSIZE
    733 #define DEV_BSIZE (1 << 9)
    734 #endif
    735 
    736 #undef MIN
    737 #undef MAX
    738 #define MIN(a,b) ((a) < (b) ? (a) : (b))
    739 #define MAX(a,b) ((a) > (b) ? (a) : (b))
    740 
    741 #ifndef MAXBSIZE
    742 #define MAXBSIZE (64 * 1024)
    743 #endif
    744 #ifndef MAXFRAG
    745 #define MAXFRAG 8
    746 #endif
    747 #ifndef MAXPHYS
    748 #define MAXPHYS (64 * 1024)
    749 #endif
    750 
    751 /* XXX needed by makefs; this should be done in a better way */
    752 #undef btodb
    753 #define btodb(x) ((x) << 9)
    754 
    755 #undef setbit
    756 #undef clrbit
    757 #undef isset
    758 #undef isclr
    759 #define	setbit(a,i)	((a)[(i)/NBBY] |= 1<<((i)%NBBY))
    760 #define	clrbit(a,i)	((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
    761 #define	isset(a,i)	((a)[(i)/NBBY] & (1<<((i)%NBBY)))
    762 #define	isclr(a,i)	(((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
    763 
    764 #ifndef powerof2
    765 #define powerof2(x) ((((x)-1)&(x))==0)
    766 #endif
    767 
    768 #undef roundup
    769 #define roundup(x, y)	((((x)+((y)-1))/(y))*(y))
    770 
    771 /* <sys/stat.h> */
    772 
    773 #ifndef ALLPERMS
    774 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
    775 #endif
    776 #ifndef DEFFILEMODE
    777 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
    778 #endif
    779 #ifndef S_ISTXT
    780 #ifdef S_ISVTX
    781 #define S_ISTXT S_ISVTX
    782 #else
    783 #define S_ISTXT 0
    784 #endif
    785 #endif
    786 
    787 /* Protected by _NETBSD_SOURCE otherwise. */
    788 #if HAVE_STRUCT_STAT_ST_FLAGS && defined(__NetBSD__)
    789 #define UF_SETTABLE     0x0000ffff
    790 #define UF_NODUMP       0x00000001
    791 #define UF_IMMUTABLE    0x00000002
    792 #define UF_APPEND       0x00000004
    793 #define UF_OPAQUE       0x00000008
    794 #define SF_SETTABLE     0xffff0000
    795 #define SF_ARCHIVED     0x00010000
    796 #define SF_IMMUTABLE    0x00020000
    797 #define SF_APPEND       0x00040000
    798 #endif
    799 
    800 /* <sys/syslimits.h> */
    801 
    802 #ifndef LINE_MAX
    803 #define LINE_MAX 2048
    804 #endif
    805 
    806 /* <sys/time.h> */
    807 
    808 #ifndef timercmp
    809 #define	timercmp(tvp, uvp, cmp)						\
    810 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
    811 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
    812 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
    813 #endif
    814 #ifndef timeradd
    815 #define	timeradd(tvp, uvp, vvp)						\
    816 	do {								\
    817 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
    818 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
    819 		if ((vvp)->tv_usec >= 1000000) {			\
    820 			(vvp)->tv_sec++;				\
    821 			(vvp)->tv_usec -= 1000000;			\
    822 		}							\
    823 	} while (/* CONSTCOND */ 0)
    824 #endif
    825 #ifndef timersub
    826 #define	timersub(tvp, uvp, vvp)						\
    827 	do {								\
    828 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
    829 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
    830 		if ((vvp)->tv_usec < 0) {				\
    831 			(vvp)->tv_sec--;				\
    832 			(vvp)->tv_usec += 1000000;			\
    833 		}							\
    834 	} while (/* CONSTCOND */ 0)
    835 #endif
    836 
    837 /* <sys/types.h> */
    838 
    839 #ifdef major
    840 #undef major
    841 #endif
    842 #define major(x)        ((int32_t)((((x) & 0x000fff00) >>  8)))
    843 
    844 #ifdef minor
    845 #undef minor
    846 #endif
    847 #define minor(x)        ((int32_t)((((x) & 0xfff00000) >> 12) | \
    848                                    (((x) & 0x000000ff) >>  0)))
    849 #ifdef makedev
    850 #undef makedev
    851 #endif
    852 #define makedev(x,y)    ((dev_t)((((x) <<  8) & 0x000fff00) | \
    853 			(((y) << 12) & 0xfff00000) | \
    854 			(((y) <<  0) & 0x000000ff)))
    855 #ifndef NBBY
    856 #define NBBY 8
    857 #endif
    858 
    859 #if !HAVE_U_QUAD_T
    860 /* #define, not typedef, as quad_t exists as a struct on some systems */
    861 #define quad_t long long
    862 #define u_quad_t unsigned long long
    863 #define strtoq strtoll
    864 #define strtouq strtoull
    865 #endif
    866 
    867 /* Has quad_t but these prototypes don't get pulled into scope. w/o we lose */
    868 #ifdef __NetBSD__
    869 quad_t   strtoq __P((const char *, char **, int));
    870 u_quad_t strtouq __P((const char *, char **, int));
    871 #endif
    872 
    873 #endif	/* !__NETBSD_COMPAT_DEFS_H__ */
    874