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