Home | History | Annotate | Line # | Download | only in compat
compat_defs.h revision 1.38
      1 /*	$NetBSD: compat_defs.h,v 1.38 2004/06/18 20:24:03 thorpej 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 #undef _POSIX_SOURCE
     19 #undef _POSIX_C_SOURCE
     20 
     21 /* System headers needed for (re)definitions below. */
     22 
     23 #include <sys/types.h>
     24 #include <sys/mman.h>
     25 #include <sys/param.h>
     26 #include <sys/stat.h>
     27 #include <sys/time.h>
     28 #include <errno.h>
     29 #include <fcntl.h>
     30 #include <grp.h>
     31 #include <limits.h>
     32 #include <paths.h>
     33 #include <stdarg.h>
     34 #include <stdio.h>
     35 
     36 /* So extra NetBSD extentions don't get pulled in */
     37 #ifdef __NetBSD__
     38 #define _POSIX_C_SOURCE
     39 #undef _NETBSD_SOURCE
     40 #endif
     41 
     42 #include <stdlib.h>
     43 
     44 #ifdef __NetBSD__
     45 #undef _POSIX_C_SOURCE
     46 #endif
     47 
     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 /* We don't include <pwd.h> here, so that "compat_pwd.h" works. */
     68 struct passwd;
     69 
     70 /* Assume an ANSI compiler for the host. */
     71 
     72 #undef __P
     73 #define __P(x) x
     74 
     75 #ifndef __BEGIN_DECLS
     76 #define __BEGIN_DECLS
     77 #endif
     78 #ifndef __END_DECLS
     79 #define __END_DECLS
     80 #endif
     81 
     82 /* Some things usually in BSD <sys/cdefs.h>. */
     83 
     84 #ifndef __CONCAT
     85 #define	__CONCAT(x,y)	x ## y
     86 #endif
     87 #if !defined(__attribute__) && !defined(__GNUC__)
     88 #define __attribute__(x)
     89 #endif
     90 #if !defined(__packed)
     91 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
     92 #define __packed	__attribute__((__packed__))
     93 #else
     94 #define	__packed	error: no __packed for this compiler
     95 #endif
     96 #endif /* !__packed */
     97 #ifndef __RENAME
     98 #define __RENAME(x)
     99 #endif
    100 #undef __aconst
    101 #define __aconst
    102 #undef __dead
    103 #define __dead
    104 #undef __restrict
    105 #define __restrict
    106 
    107 /* Dirent support. */
    108 
    109 #if HAVE_DIRENT_H
    110 # include <dirent.h>
    111 # define NAMLEN(dirent) (strlen((dirent)->d_name))
    112 #else
    113 # define dirent direct
    114 # define NAMLEN(dirent) ((dirent)->d_namlen)
    115 # if HAVE_SYS_NDIR_H
    116 #  include <sys/ndir.h>
    117 # endif
    118 # if HAVE_SYS_DIR_H
    119 #  include <sys/dir.h>
    120 # endif
    121 # if HAVE_NDIR_H
    122 #  include <ndir.h>
    123 # endif
    124 #endif
    125 
    126 /* Type substitutes. */
    127 
    128 #if !HAVE_ID_T
    129 typedef unsigned long id_t;
    130 #endif
    131 
    132 #if !HAVE_SOCKLEN_T
    133 typedef int socklen_t;
    134 #endif
    135 
    136 #if !HAVE_U_LONG
    137 typedef unsigned long u_long;
    138 #endif
    139 
    140 /* Prototypes for replacement functions. */
    141 
    142 #if !HAVE_ATOLL
    143 long long int atoll(const char *);
    144 #endif
    145 
    146 #if !HAVE_ASPRINTF
    147 int asprintf(char **, const char *, ...);
    148 #endif
    149 
    150 #if !HAVE_ASNPRINTF
    151 int asnprintf(char **, size_t, const char *, ...);
    152 #endif
    153 
    154 #if !HAVE_BASENAME
    155 char *basename(char *);
    156 #endif
    157 
    158 #if !HAVE_DECL_OPTIND
    159 int getopt(int, char *const *, const char *);
    160 extern char *optarg;
    161 extern int optind, opterr, optopt;
    162 #endif
    163 
    164 #if !HAVE_DIRNAME
    165 char *dirname(char *);
    166 #endif
    167 
    168 #if !HAVE_DIRFD
    169 #if HAVE_DIR_DD_FD
    170 #define dirfd(dirp) ((dirp)->dd_fd)
    171 #else
    172 #error cannot figure out how to turn a DIR * into a fd
    173 #endif
    174 #endif
    175 
    176 #if !HAVE_ERR_H
    177 void err(int, const char *, ...);
    178 void errx(int, const char *, ...);
    179 void warn(const char *, ...);
    180 void warnx(const char *, ...);
    181 #endif
    182 
    183 #if !HAVE_FGETLN
    184 char *fgetln(FILE *, size_t *);
    185 #endif
    186 
    187 #if !HAVE_FLOCK
    188 # define LOCK_SH		0x01
    189 # define LOCK_EX		0x02
    190 # define LOCK_NB		0x04
    191 # define LOCK_UN		0x08
    192 int flock(int, int);
    193 #endif
    194 
    195 #if !HAVE_FPARSELN
    196 # define FPARSELN_UNESCESC	0x01
    197 # define FPARSELN_UNESCCONT	0x02
    198 # define FPARSELN_UNESCCOMM	0x04
    199 # define FPARSELN_UNESCREST	0x08
    200 # define FPARSELN_UNESCALL	0x0f
    201 char *fparseln(FILE *, size_t *, size_t *, const char [3], int);
    202 #endif
    203 
    204 #if !HAVE_ISSETUGID
    205 int issetugid(void);
    206 #endif
    207 
    208 #if !HAVE_ISBLANK && !defined(isblank)
    209 #define isblank(x) ((x) == ' ' || (x) == '\t')
    210 #endif
    211 
    212 #if !HAVE_LCHFLAGS
    213 int lchflags(const char *, u_long);
    214 #endif
    215 
    216 #if !HAVE_LCHMOD
    217 int lchmod(const char *, mode_t);
    218 #endif
    219 
    220 #if !HAVE_LCHOWN
    221 int lchown(const char *, uid_t, gid_t);
    222 #endif
    223 
    224 #if !HAVE_MACHINE_BSWAP_H
    225 #define bswap16(x)	((((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff))
    226 
    227 #define bswap32(x)	((((x) << 24) & 0xff000000) | \
    228 			 (((x) <<  8) & 0x00ff0000) | \
    229 			 (((x) >>  8) & 0x0000ff00) | \
    230 			 (((x) >> 24) & 0x000000ff))
    231 
    232 #define bswap64(x)	(((u_int64_t)bswap32((x)) << 32) | \
    233 			 ((u_int64_t)bswap32((x) >> 32)))
    234 #endif
    235 
    236 #if !HAVE_MKSTEMP
    237 int mkstemp(char *);
    238 #endif
    239 
    240 #if !HAVE_MKDTEMP
    241 char *mkdtemp(char *);
    242 #endif
    243 
    244 #if !HAVE_MKSTEMP || !HAVE_MKDTEMP
    245 /* This is a prototype for the internal function. */
    246 int gettemp(char *, int *, int);
    247 #endif
    248 
    249 #if !HAVE_PREAD
    250 ssize_t pread(int, void *, size_t, off_t);
    251 #endif
    252 
    253 #if !HAVE_PWCACHE_USERDB
    254 int uid_from_user(const char *, uid_t *);
    255 int pwcache_userdb(int (*)(int), void (*)(void),
    256 		struct passwd * (*)(const char *), struct passwd * (*)(uid_t));
    257 int gid_from_group(const char *, gid_t *);
    258 int pwcache_groupdb(int (*)(int), void (*)(void),
    259 		struct group * (*)(const char *), struct group * (*)(gid_t));
    260 # if HAVE_USER_FROM_UID
    261 /* Make them use our version */
    262 #  define user_from_uid __nbcompat_user_from_uid
    263 # endif
    264 # if HAVE_GROUP_FROM_GID
    265 /* Make them use our version */
    266 #  define group_from_gid __nbcompat_group_from_gid
    267 # endif
    268 #endif
    269 
    270 #if !HAVE_PWRITE
    271 ssize_t pwrite(int, const void *, size_t, off_t);
    272 #endif
    273 
    274 #if !HAVE_SETENV
    275 int setenv(const char *, const char *, int);
    276 #endif
    277 
    278 #if !HAVE_DECL_SETGROUPENT
    279 int setgroupent(int);
    280 #endif
    281 
    282 #if !HAVE_DECL_SETPASSENT
    283 int setpassent(int);
    284 #endif
    285 
    286 #if !HAVE_SETPROGNAME
    287 const char *getprogname(void);
    288 void setprogname(const char *);
    289 #endif
    290 
    291 #if !HAVE_SNPRINTF
    292 int snprintf(char *, size_t, const char *, ...);
    293 #endif
    294 
    295 #if !HAVE_STRLCAT
    296 size_t strlcat(char *, const char *, size_t);
    297 #endif
    298 
    299 #if !HAVE_STRLCPY
    300 size_t strlcpy(char *, const char *, size_t);
    301 #endif
    302 
    303 #if !HAVE_STRSEP
    304 char *strsep(char **, const char *);
    305 #endif
    306 
    307 #if !HAVE_STRSUFTOLL
    308 long long strsuftoll(const char *, const char *, long long, long long);
    309 long long strsuftollx(const char *, const char *,
    310 			long long, long long, char *, size_t);
    311 #endif
    312 
    313 #if !HAVE_STRTOLL
    314 long long strtoll(const char *, char **, int);
    315 #endif
    316 
    317 #if !HAVE_USER_FROM_UID
    318 const char *user_from_uid(uid_t, int);
    319 #endif
    320 
    321 #if !HAVE_GROUP_FROM_GID
    322 const char *group_from_gid(gid_t, int);
    323 #endif
    324 
    325 #if !HAVE_VASPRINTF
    326 int vasprintf(char **, const char *, va_list);
    327 #endif
    328 
    329 #if !HAVE_VASNPRINTF
    330 int vasnprintf(char **, size_t, const char *, va_list);
    331 #endif
    332 
    333 #if !HAVE_VSNPRINTF
    334 int vsnprintf(char *, size_t, const char *, va_list);
    335 #endif
    336 
    337 /*
    338  * getmode() and setmode() are always defined, as these function names
    339  * exist but with very different meanings on other OS's.  The compat
    340  * versions here simply accept an octal mode number; the "u+x,g-w" type
    341  * of syntax is not accepted.
    342  */
    343 
    344 #define getmode __nbcompat_getmode
    345 #define setmode __nbcompat_setmode
    346 
    347 mode_t getmode(const void *, mode_t);
    348 void *setmode(const char *);
    349 
    350 /* Eliminate assertions embedded in binaries. */
    351 
    352 #undef _DIAGASSERT
    353 #define _DIAGASSERT(x)
    354 
    355 /* Various sources use this */
    356 #undef	__RCSID
    357 #define	__RCSID(x)
    358 #undef	__SCCSID
    359 #define	__SCCSID(x)
    360 #undef	__COPYRIGHT
    361 #define	__COPYRIGHT(x)
    362 #undef	__KERNEL_RCSID
    363 #define	__KERNEL_RCSID(x,y)
    364 
    365 /* Heimdal expects this one. */
    366 
    367 #undef RCSID
    368 #define RCSID(x)
    369 
    370 /* Some definitions not available on all systems. */
    371 
    372 /* <errno.h> */
    373 
    374 #ifndef EFTYPE
    375 #define EFTYPE EIO
    376 #endif
    377 
    378 /* <fcntl.h> */
    379 
    380 #ifndef O_EXLOCK
    381 #define O_EXLOCK 0
    382 #endif
    383 #ifndef O_SHLOCK
    384 #define O_SHLOCK 0
    385 #endif
    386 
    387 /* <limits.h> */
    388 
    389 #ifndef UID_MAX
    390 #define UID_MAX 32767
    391 #endif
    392 #ifndef GID_MAX
    393 #define GID_MAX UID_MAX
    394 #endif
    395 
    396 #ifndef UQUAD_MAX
    397 #define UQUAD_MAX ((u_quad_t)-1)
    398 #endif
    399 #ifndef QUAD_MAX
    400 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
    401 #endif
    402 #ifndef QUAD_MIN
    403 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
    404 #endif
    405 #ifndef ULLONG_MAX
    406 #define ULLONG_MAX ((unsigned long long)-1)
    407 #endif
    408 #ifndef LLONG_MAX
    409 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
    410 #endif
    411 #ifndef LLONG_MIN
    412 #define LLONG_MIN ((long long)(~LLONG_MAX))
    413 #endif
    414 
    415 /* <paths.h> */
    416 
    417 #ifndef _PATH_BSHELL
    418 #define _PATH_BSHELL PATH_BSHELL
    419 #endif
    420 #ifndef _PATH_DEFPATH
    421 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
    422 #endif
    423 #ifndef _PATH_DEV
    424 #define _PATH_DEV "/dev/"
    425 #endif
    426 #ifndef _PATH_DEVNULL
    427 #define _PATH_DEVNULL _PATH_DEV "null"
    428 #endif
    429 #ifndef _PATH_TMP
    430 #define _PATH_TMP "/tmp/"
    431 #endif
    432 #ifndef _PATH_DEFTAPE
    433 #define _PATH_DEFTAPE "/dev/nrst0"
    434 #endif
    435 
    436 /* <stdarg.h> */
    437 
    438 #ifndef _BSD_VA_LIST_
    439 #define _BSD_VA_LIST_ va_list
    440 #endif
    441 
    442 /* <stdint.h> */
    443 
    444 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX)
    445 #define SIZE_MAX SIZE_T_MAX
    446 #endif
    447 
    448 #ifndef UINT32_MAX
    449 #define UINT32_MAX 0xffffffffU
    450 #endif
    451 
    452 /* <stdlib.h> */
    453 
    454 #ifndef __GNUC__
    455 # if HAVE_ALLOCA_H
    456 #  include <alloca.h>
    457 # else
    458 #  ifndef alloca /* predefined by HP cc +Olibcalls */
    459 char *alloca ();
    460 #  endif
    461 # endif
    462 #endif
    463 
    464 /* avoid prototype conflicts with host */
    465 #define cgetcap __nbcompat_cgetcap
    466 #define cgetclose __nbcompat_cgetclose
    467 #define cgetent __nbcompat_cgetent
    468 #define cgetfirst __nbcompat_cgetfirst
    469 #define cgetmatch __nbcompat_cgetmatch
    470 #define cgetnext __nbcompat_cgetnext
    471 #define cgetnum __nbcompat_cgetnum
    472 #define cgetset __nbcompat_cgetset
    473 #define cgetstr __nbcompat_cgetstr
    474 #define cgetustr __nbcompat_cgetustr
    475 
    476 char	*cgetcap(char *, const char *, int);
    477 int	 cgetclose(void);
    478 int	 cgetent(char **, const char * const *, const char *);
    479 int	 cgetfirst(char **, const char * const *);
    480 int	 cgetmatch(const char *, const char *);
    481 int	 cgetnext(char **, const char * const *);
    482 int	 cgetnum(char *, const char *, long *);
    483 int	 cgetset(const char *);
    484 int	 cgetstr(char *, const char *, char **);
    485 int	 cgetustr(char *, const char *, char **);
    486 
    487 /* <sys/endian.h> */
    488 
    489 #ifdef HAVE_SYS_ENDIAN_H
    490 #include <sys/endian.h>
    491 #else
    492 #if WORDS_BIGENDIAN
    493 #define htobe16(x)	(x)
    494 #define htobe32(x)	(x)
    495 #define htobe64(x)	(x)
    496 #define htole16(x)	bswap16((u_int16_t)(x))
    497 #define htole32(x)	bswap32((u_int32_t)(x))
    498 #define htole64(x)	bswap64((u_int64_t)(x))
    499 #else
    500 #define htobe16(x)	bswap16((u_int16_t)(x))
    501 #define htobe32(x)	bswap32((u_int32_t)(x))
    502 #define htobe64(x)	bswap64((u_int64_t)(x))
    503 #define htole16(x)	(x)
    504 #define htole32(x)	(x)
    505 #define htole64(x)	(x)
    506 #endif
    507 #define be16toh(x)	htobe16(x)
    508 #define be32toh(x)	htobe32(x)
    509 #define be64toh(x)	htobe64(x)
    510 #define le16toh(x)	htole16(x)
    511 #define le32toh(x)	htole32(x)
    512 #define le64toh(x)	htole64(x)
    513 #endif
    514 
    515 /* <sys/mman.h> */
    516 
    517 #ifndef MAP_FILE
    518 #define MAP_FILE 0
    519 #endif
    520 
    521 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
    522 #ifndef MAP_ANON
    523 #ifdef MAP_ANONYMOUS
    524 #define MAP_ANON MAP_ANONYMOUS
    525 #endif
    526 #endif
    527 
    528 /* <sys/param.h> */
    529 
    530 #undef BIG_ENDIAN
    531 #undef LITTLE_ENDIAN
    532 #define BIG_ENDIAN 4321
    533 #define LITTLE_ENDIAN 1234
    534 
    535 #undef BYTE_ORDER
    536 #if WORDS_BIGENDIAN
    537 #define BYTE_ORDER BIG_ENDIAN
    538 #else
    539 #define BYTE_ORDER LITTLE_ENDIAN
    540 #endif
    541 
    542 #ifndef DEV_BSIZE
    543 #define DEV_BSIZE (1 << 9)
    544 #endif
    545 
    546 #undef MIN
    547 #undef MAX
    548 #define MIN(a,b) ((a) < (b) ? (a) : (b))
    549 #define MAX(a,b) ((a) > (b) ? (a) : (b))
    550 
    551 #ifndef MAXBSIZE
    552 #define MAXBSIZE (64 * 1024)
    553 #endif
    554 #ifndef MAXFRAG
    555 #define MAXFRAG 8
    556 #endif
    557 #ifndef MAXPHYS
    558 #define MAXPHYS (64 * 1024)
    559 #endif
    560 
    561 /* XXX needed by makefs; this should be done in a better way */
    562 #undef btodb
    563 #define btodb(x) ((x) << 9)
    564 
    565 #undef setbit
    566 #undef clrbit
    567 #undef isset
    568 #undef isclr
    569 #define	setbit(a,i)	((a)[(i)/NBBY] |= 1<<((i)%NBBY))
    570 #define	clrbit(a,i)	((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
    571 #define	isset(a,i)	((a)[(i)/NBBY] & (1<<((i)%NBBY)))
    572 #define	isclr(a,i)	(((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
    573 
    574 #ifndef powerof2
    575 #define powerof2(x) ((((x)-1)&(x))==0)
    576 #endif
    577 
    578 #undef roundup
    579 #define roundup(x, y)	((((x)+((y)-1))/(y))*(y))
    580 
    581 /* <sys/stat.h> */
    582 
    583 #ifndef ALLPERMS
    584 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
    585 #endif
    586 #ifndef DEFFILEMODE
    587 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
    588 #endif
    589 #ifndef S_ISTXT
    590 #ifdef S_ISVTX
    591 #define S_ISTXT S_ISVTX
    592 #else
    593 #define S_ISTXT 0
    594 #endif
    595 #endif
    596 
    597 /* <sys/syslimits.h> */
    598 
    599 #ifndef LINE_MAX
    600 #define LINE_MAX 2048
    601 #endif
    602 
    603 /* <sys/time.h> */
    604 
    605 #ifndef timercmp
    606 #define	timercmp(tvp, uvp, cmp)						\
    607 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
    608 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
    609 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
    610 #endif
    611 #ifndef timeradd
    612 #define	timeradd(tvp, uvp, vvp)						\
    613 	do {								\
    614 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
    615 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
    616 		if ((vvp)->tv_usec >= 1000000) {			\
    617 			(vvp)->tv_sec++;				\
    618 			(vvp)->tv_usec -= 1000000;			\
    619 		}							\
    620 	} while (/* CONSTCOND */ 0)
    621 #endif
    622 #ifndef timersub
    623 #define	timersub(tvp, uvp, vvp)						\
    624 	do {								\
    625 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
    626 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
    627 		if ((vvp)->tv_usec < 0) {				\
    628 			(vvp)->tv_sec--;				\
    629 			(vvp)->tv_usec += 1000000;			\
    630 		}							\
    631 	} while (/* CONSTCOND */ 0)
    632 #endif
    633 
    634 /* <sys/types.h> */
    635 
    636 #if !HAVE_U_QUAD_T
    637 /* #define, not typedef, as quad_t exists as a struct on some systems */
    638 #define quad_t long long
    639 #define u_quad_t unsigned long long
    640 #define strtoq strtoll
    641 #define strtouq strtoull
    642 #endif
    643 
    644 #endif	/* !__NETBSD_COMPAT_DEFS_H__ */
    645