Home | History | Annotate | Line # | Download | only in compat
compat_defs.h revision 1.37
      1 /*	$NetBSD: compat_defs.h,v 1.37 2004/06/18 20:17:01 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 const char *group_from_gid(gid_t, int);
    320 #endif
    321 
    322 #if !HAVE_VASPRINTF
    323 int vasprintf(char **, const char *, va_list);
    324 #endif
    325 
    326 #if !HAVE_VASNPRINTF
    327 int vasnprintf(char **, size_t, const char *, va_list);
    328 #endif
    329 
    330 #if !HAVE_VSNPRINTF
    331 int vsnprintf(char *, size_t, const char *, va_list);
    332 #endif
    333 
    334 /*
    335  * getmode() and setmode() are always defined, as these function names
    336  * exist but with very different meanings on other OS's.  The compat
    337  * versions here simply accept an octal mode number; the "u+x,g-w" type
    338  * of syntax is not accepted.
    339  */
    340 
    341 #define getmode __nbcompat_getmode
    342 #define setmode __nbcompat_setmode
    343 
    344 mode_t getmode(const void *, mode_t);
    345 void *setmode(const char *);
    346 
    347 /* Eliminate assertions embedded in binaries. */
    348 
    349 #undef _DIAGASSERT
    350 #define _DIAGASSERT(x)
    351 
    352 /* Various sources use this */
    353 #undef	__RCSID
    354 #define	__RCSID(x)
    355 #undef	__SCCSID
    356 #define	__SCCSID(x)
    357 #undef	__COPYRIGHT
    358 #define	__COPYRIGHT(x)
    359 #undef	__KERNEL_RCSID
    360 #define	__KERNEL_RCSID(x,y)
    361 
    362 /* Heimdal expects this one. */
    363 
    364 #undef RCSID
    365 #define RCSID(x)
    366 
    367 /* Some definitions not available on all systems. */
    368 
    369 /* <errno.h> */
    370 
    371 #ifndef EFTYPE
    372 #define EFTYPE EIO
    373 #endif
    374 
    375 /* <fcntl.h> */
    376 
    377 #ifndef O_EXLOCK
    378 #define O_EXLOCK 0
    379 #endif
    380 #ifndef O_SHLOCK
    381 #define O_SHLOCK 0
    382 #endif
    383 
    384 /* <limits.h> */
    385 
    386 #ifndef UID_MAX
    387 #define UID_MAX 32767
    388 #endif
    389 #ifndef GID_MAX
    390 #define GID_MAX UID_MAX
    391 #endif
    392 
    393 #ifndef UQUAD_MAX
    394 #define UQUAD_MAX ((u_quad_t)-1)
    395 #endif
    396 #ifndef QUAD_MAX
    397 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
    398 #endif
    399 #ifndef QUAD_MIN
    400 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
    401 #endif
    402 #ifndef ULLONG_MAX
    403 #define ULLONG_MAX ((unsigned long long)-1)
    404 #endif
    405 #ifndef LLONG_MAX
    406 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
    407 #endif
    408 #ifndef LLONG_MIN
    409 #define LLONG_MIN ((long long)(~LLONG_MAX))
    410 #endif
    411 
    412 /* <paths.h> */
    413 
    414 #ifndef _PATH_BSHELL
    415 #define _PATH_BSHELL PATH_BSHELL
    416 #endif
    417 #ifndef _PATH_DEFPATH
    418 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
    419 #endif
    420 #ifndef _PATH_DEV
    421 #define _PATH_DEV "/dev/"
    422 #endif
    423 #ifndef _PATH_DEVNULL
    424 #define _PATH_DEVNULL _PATH_DEV "null"
    425 #endif
    426 #ifndef _PATH_TMP
    427 #define _PATH_TMP "/tmp/"
    428 #endif
    429 #ifndef _PATH_DEFTAPE
    430 #define _PATH_DEFTAPE "/dev/nrst0"
    431 #endif
    432 
    433 /* <stdarg.h> */
    434 
    435 #ifndef _BSD_VA_LIST_
    436 #define _BSD_VA_LIST_ va_list
    437 #endif
    438 
    439 /* <stdint.h> */
    440 
    441 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX)
    442 #define SIZE_MAX SIZE_T_MAX
    443 #endif
    444 
    445 #ifndef UINT32_MAX
    446 #define UINT32_MAX 0xffffffffU
    447 #endif
    448 
    449 /* <stdlib.h> */
    450 
    451 #ifndef __GNUC__
    452 # if HAVE_ALLOCA_H
    453 #  include <alloca.h>
    454 # else
    455 #  ifndef alloca /* predefined by HP cc +Olibcalls */
    456 char *alloca ();
    457 #  endif
    458 # endif
    459 #endif
    460 
    461 /* avoid prototype conflicts with host */
    462 #define cgetcap __nbcompat_cgetcap
    463 #define cgetclose __nbcompat_cgetclose
    464 #define cgetent __nbcompat_cgetent
    465 #define cgetfirst __nbcompat_cgetfirst
    466 #define cgetmatch __nbcompat_cgetmatch
    467 #define cgetnext __nbcompat_cgetnext
    468 #define cgetnum __nbcompat_cgetnum
    469 #define cgetset __nbcompat_cgetset
    470 #define cgetstr __nbcompat_cgetstr
    471 #define cgetustr __nbcompat_cgetustr
    472 
    473 char	*cgetcap(char *, const char *, int);
    474 int	 cgetclose(void);
    475 int	 cgetent(char **, const char * const *, const char *);
    476 int	 cgetfirst(char **, const char * const *);
    477 int	 cgetmatch(const char *, const char *);
    478 int	 cgetnext(char **, const char * const *);
    479 int	 cgetnum(char *, const char *, long *);
    480 int	 cgetset(const char *);
    481 int	 cgetstr(char *, const char *, char **);
    482 int	 cgetustr(char *, const char *, char **);
    483 
    484 /* <sys/endian.h> */
    485 
    486 #ifdef HAVE_SYS_ENDIAN_H
    487 #include <sys/endian.h>
    488 #else
    489 #if WORDS_BIGENDIAN
    490 #define htobe16(x)	(x)
    491 #define htobe32(x)	(x)
    492 #define htobe64(x)	(x)
    493 #define htole16(x)	bswap16((u_int16_t)(x))
    494 #define htole32(x)	bswap32((u_int32_t)(x))
    495 #define htole64(x)	bswap64((u_int64_t)(x))
    496 #else
    497 #define htobe16(x)	bswap16((u_int16_t)(x))
    498 #define htobe32(x)	bswap32((u_int32_t)(x))
    499 #define htobe64(x)	bswap64((u_int64_t)(x))
    500 #define htole16(x)	(x)
    501 #define htole32(x)	(x)
    502 #define htole64(x)	(x)
    503 #endif
    504 #define be16toh(x)	htobe16(x)
    505 #define be32toh(x)	htobe32(x)
    506 #define be64toh(x)	htobe64(x)
    507 #define le16toh(x)	htole16(x)
    508 #define le32toh(x)	htole32(x)
    509 #define le64toh(x)	htole64(x)
    510 #endif
    511 
    512 /* <sys/mman.h> */
    513 
    514 #ifndef MAP_FILE
    515 #define MAP_FILE 0
    516 #endif
    517 
    518 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
    519 #ifndef MAP_ANON
    520 #ifdef MAP_ANONYMOUS
    521 #define MAP_ANON MAP_ANONYMOUS
    522 #endif
    523 #endif
    524 
    525 /* <sys/param.h> */
    526 
    527 #undef BIG_ENDIAN
    528 #undef LITTLE_ENDIAN
    529 #define BIG_ENDIAN 4321
    530 #define LITTLE_ENDIAN 1234
    531 
    532 #undef BYTE_ORDER
    533 #if WORDS_BIGENDIAN
    534 #define BYTE_ORDER BIG_ENDIAN
    535 #else
    536 #define BYTE_ORDER LITTLE_ENDIAN
    537 #endif
    538 
    539 #ifndef DEV_BSIZE
    540 #define DEV_BSIZE (1 << 9)
    541 #endif
    542 
    543 #undef MIN
    544 #undef MAX
    545 #define MIN(a,b) ((a) < (b) ? (a) : (b))
    546 #define MAX(a,b) ((a) > (b) ? (a) : (b))
    547 
    548 #ifndef MAXBSIZE
    549 #define MAXBSIZE (64 * 1024)
    550 #endif
    551 #ifndef MAXFRAG
    552 #define MAXFRAG 8
    553 #endif
    554 #ifndef MAXPHYS
    555 #define MAXPHYS (64 * 1024)
    556 #endif
    557 
    558 /* XXX needed by makefs; this should be done in a better way */
    559 #undef btodb
    560 #define btodb(x) ((x) << 9)
    561 
    562 #undef setbit
    563 #undef clrbit
    564 #undef isset
    565 #undef isclr
    566 #define	setbit(a,i)	((a)[(i)/NBBY] |= 1<<((i)%NBBY))
    567 #define	clrbit(a,i)	((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
    568 #define	isset(a,i)	((a)[(i)/NBBY] & (1<<((i)%NBBY)))
    569 #define	isclr(a,i)	(((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
    570 
    571 #ifndef powerof2
    572 #define powerof2(x) ((((x)-1)&(x))==0)
    573 #endif
    574 
    575 #undef roundup
    576 #define roundup(x, y)	((((x)+((y)-1))/(y))*(y))
    577 
    578 /* <sys/stat.h> */
    579 
    580 #ifndef ALLPERMS
    581 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
    582 #endif
    583 #ifndef DEFFILEMODE
    584 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
    585 #endif
    586 #ifndef S_ISTXT
    587 #ifdef S_ISVTX
    588 #define S_ISTXT S_ISVTX
    589 #else
    590 #define S_ISTXT 0
    591 #endif
    592 #endif
    593 
    594 /* <sys/syslimits.h> */
    595 
    596 #ifndef LINE_MAX
    597 #define LINE_MAX 2048
    598 #endif
    599 
    600 /* <sys/time.h> */
    601 
    602 #ifndef timercmp
    603 #define	timercmp(tvp, uvp, cmp)						\
    604 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
    605 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
    606 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
    607 #endif
    608 #ifndef timeradd
    609 #define	timeradd(tvp, uvp, vvp)						\
    610 	do {								\
    611 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
    612 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
    613 		if ((vvp)->tv_usec >= 1000000) {			\
    614 			(vvp)->tv_sec++;				\
    615 			(vvp)->tv_usec -= 1000000;			\
    616 		}							\
    617 	} while (/* CONSTCOND */ 0)
    618 #endif
    619 #ifndef timersub
    620 #define	timersub(tvp, uvp, vvp)						\
    621 	do {								\
    622 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
    623 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
    624 		if ((vvp)->tv_usec < 0) {				\
    625 			(vvp)->tv_sec--;				\
    626 			(vvp)->tv_usec += 1000000;			\
    627 		}							\
    628 	} while (/* CONSTCOND */ 0)
    629 #endif
    630 
    631 /* <sys/types.h> */
    632 
    633 #if !HAVE_U_QUAD_T
    634 /* #define, not typedef, as quad_t exists as a struct on some systems */
    635 #define quad_t long long
    636 #define u_quad_t unsigned long long
    637 #define strtoq strtoll
    638 #define strtouq strtoull
    639 #endif
    640 
    641 #endif	/* !__NETBSD_COMPAT_DEFS_H__ */
    642