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