Home | History | Annotate | Line # | Download | only in compat
compat_defs.h revision 1.12
      1 /*	$NetBSD: compat_defs.h,v 1.12 2002/04/22 23:39:38 bjh21 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 
     86 /* Dirent support. */
     87 
     88 #if HAVE_DIRENT_H
     89 # include <dirent.h>
     90 # define NAMLEN(dirent) (strlen((dirent)->d_name))
     91 #else
     92 # define dirent direct
     93 # define NAMLEN(dirent) ((dirent)->d_namlen)
     94 # if HAVE_SYS_NDIR_H
     95 #  include <sys/ndir.h>
     96 # endif
     97 # if HAVE_SYS_DIR_H
     98 #  include <sys/dir.h>
     99 # endif
    100 # if HAVE_NDIR_H
    101 #  include <ndir.h>
    102 # endif
    103 #endif
    104 
    105 /* Type substitutes. */
    106 
    107 #if !HAVE_ID_T
    108 typedef unsigned long id_t;
    109 #endif
    110 
    111 /* Prototypes for replacement functions. */
    112 
    113 #if !HAVE_ASPRINTF
    114 int asprintf(char **, const char *, ...);
    115 #endif
    116 
    117 #if !HAVE_ASNPRINTF
    118 int asnprintf(char **, size_t, const char *, ...);
    119 #endif
    120 
    121 #if !HAVE_BASENAME
    122 char *basename(char *);
    123 #endif
    124 
    125 #if !HAVE_DECL_OPTIND
    126 int getopt(int, char *const *, const char *);
    127 extern char *optarg;
    128 extern int optind, opterr, optopt;
    129 #endif
    130 
    131 #if !HAVE_DIRNAME
    132 char *dirname(char *);
    133 #endif
    134 
    135 #if !HAVE_DIRFD
    136 #if HAVE_DIR_DD_FD
    137 #define dirfd(dirp) ((dirp)->dd_fd)
    138 #else
    139 #error cannot figure out how to turn a DIR * into a fd
    140 #endif
    141 #endif
    142 
    143 #if !HAVE_ERR_H
    144 void err(int, const char *, ...);
    145 void errx(int, const char *, ...);
    146 void warn(const char *, ...);
    147 void warnx(const char *, ...);
    148 #endif
    149 
    150 #if !HAVE_FGETLN
    151 char *fgetln(FILE *, size_t *);
    152 #endif
    153 
    154 #if !HAVE_FLOCK
    155 # define LOCK_SH		0x01
    156 # define LOCK_EX		0x02
    157 # define LOCK_NB		0x04
    158 # define LOCK_UN		0x08
    159 int flock(int, int);
    160 #endif
    161 
    162 #if !HAVE_FPARSELN
    163 # define FPARSELN_UNESCESC	0x01
    164 # define FPARSELN_UNESCCONT	0x02
    165 # define FPARSELN_UNESCCOMM	0x04
    166 # define FPARSELN_UNESCREST	0x08
    167 # define FPARSELN_UNESCALL	0x0f
    168 char *fparseln(FILE *, size_t *, size_t *, const char [3], int);
    169 #endif
    170 
    171 #if !HAVE_ISSETUGID
    172 int issetugid(void);
    173 #endif
    174 
    175 #if !HAVE_ISBLANK && !defined(isblank)
    176 #define isblank(x) ((x) == ' ' || (x) == '\t')
    177 #endif
    178 
    179 #if !HAVE_MACHINE_BSWAP_H
    180 #define bswap16(x)	((((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff))
    181 
    182 #define bswap32(x)	((((x) << 24) & 0xff000000) | \
    183 			 (((x) <<  8) & 0x00ff0000) | \
    184 			 (((x) >>  8) & 0x0000ff00) | \
    185 			 (((x) >> 24) & 0x000000ff))
    186 
    187 #define bswap64(x)	(((u_int64_t)bswap32((x)) << 32) | \
    188 			 ((u_int64_t)bswap32((x) >> 32)))
    189 #endif
    190 
    191 #if !HAVE_PREAD
    192 ssize_t pread(int, void *, size_t, off_t);
    193 #endif
    194 
    195 #if !HAVE_PWCACHE_USERDB
    196 const char *user_from_uid(uid_t, int);
    197 int uid_from_user(const char *, uid_t *);
    198 int pwcache_userdb(int (*)(int), void (*)(void),
    199 		struct passwd * (*)(const char *), struct passwd * (*)(uid_t));
    200 const char *group_from_gid(gid_t, int);
    201 int gid_from_group(const char *, gid_t *);
    202 int pwcache_groupdb(int (*)(int), void (*)(void),
    203 		struct group * (*)(const char *), struct group * (*)(gid_t));
    204 #endif
    205 
    206 #if !HAVE_PWRITE
    207 ssize_t pwrite(int, const void *, size_t, off_t);
    208 #endif
    209 
    210 #if !HAVE_SETENV
    211 int setenv(const char *, const char *, int);
    212 #endif
    213 
    214 #if !HAVE_DECL_SETGROUPENT
    215 int setgroupent(int);
    216 #endif
    217 
    218 #if !HAVE_DECL_SETPASSENT
    219 int setpassent(int);
    220 #endif
    221 
    222 #if !HAVE_SETPROGNAME
    223 const char *getprogname(void);
    224 void setprogname(const char *);
    225 #endif
    226 
    227 #if !HAVE_SNPRINTF
    228 int snprintf(char *, size_t, const char *, ...);
    229 #endif
    230 
    231 #if !HAVE_STRLCAT
    232 size_t strlcat(char *, const char *, size_t);
    233 #endif
    234 
    235 #if !HAVE_STRLCPY
    236 size_t strlcpy(char *, const char *, size_t);
    237 #endif
    238 
    239 #if !HAVE_STRSEP
    240 char *strsep(char **, const char *);
    241 #endif
    242 
    243 #if !HAVE_VASPRINTF
    244 int vasprintf(char **, const char *, va_list);
    245 #endif
    246 
    247 #if !HAVE_VASNPRINTF
    248 int vasnprintf(char **, size_t, const char *, va_list);
    249 #endif
    250 
    251 #if !HAVE_VSNPRINTF
    252 int vsnprintf(char *, size_t, const char *, va_list);
    253 #endif
    254 
    255 /*
    256  * getmode() and setmode() are always defined, as these function names
    257  * exist but with very different meanings on other OS's.  The compat
    258  * versions here simply accept an octal mode number; the "u+x,g-w" type
    259  * of syntax is not accepted.
    260  */
    261 
    262 #define getmode __nbcompat_getmode
    263 #define setmode __nbcompat_setmode
    264 
    265 mode_t getmode(const void *, mode_t);
    266 void *setmode(const char *);
    267 
    268 /* Eliminate assertions embedded in binaries. */
    269 
    270 #undef _DIAGASSERT
    271 #define _DIAGASSERT(x)
    272 
    273 /* Heimdal expects this one. */
    274 
    275 #undef RCSID
    276 #define RCSID(x)
    277 
    278 /* Some definitions not available on all systems. */
    279 
    280 /* <errno.h> */
    281 
    282 #ifndef EFTYPE
    283 #define EFTYPE EIO
    284 #endif
    285 
    286 /* <fcntl.h> */
    287 
    288 #ifndef O_EXLOCK
    289 #define O_EXLOCK 0
    290 #endif
    291 #ifndef O_SHLOCK
    292 #define O_SHLOCK 0
    293 #endif
    294 
    295 /* <limits.h> */
    296 
    297 #ifndef UID_MAX
    298 #define UID_MAX 32767
    299 #endif
    300 #ifndef GID_MAX
    301 #define GID_MAX UID_MAX
    302 #endif
    303 
    304 #ifndef UQUAD_MAX
    305 #define UQUAD_MAX ((u_quad_t)-1)
    306 #endif
    307 #ifndef QUAD_MAX
    308 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
    309 #endif
    310 #ifndef QUAD_MIN
    311 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
    312 #endif
    313 #ifndef ULLONG_MAX
    314 #define ULLONG_MAX ((unsigned long long)-1)
    315 #endif
    316 #ifndef LLONG_MAX
    317 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
    318 #endif
    319 #ifndef LLONG_MIN
    320 #define LLONG_MIN ((long long)(~LLONG_MAX))
    321 #endif
    322 
    323 /* <paths.h> */
    324 
    325 #ifndef _PATH_BSHELL
    326 #if defined(__sun)
    327 /* Sun's /bin/sh is obnoxiously broken. */
    328 #define _PATH_BSHELL "/usr/xpg4/bin/sh"
    329 #else
    330 #define _PATH_BSHELL "/bin/sh"
    331 #endif
    332 #endif
    333 #ifndef _PATH_DEFPATH
    334 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
    335 #endif
    336 #ifndef _PATH_DEV
    337 #define _PATH_DEV "/dev/"
    338 #endif
    339 #ifndef _PATH_DEVNULL
    340 #define _PATH_DEVNULL _PATH_DEV "null"
    341 #endif
    342 #ifndef _PATH_TMP
    343 #define _PATH_TMP "/tmp/"
    344 #endif
    345 
    346 /* <stdarg.h> */
    347 
    348 #ifndef _BSD_VA_LIST_
    349 #define _BSD_VA_LIST_ va_list
    350 #endif
    351 
    352 /* <sys/endian.h> */
    353 
    354 #ifndef HAVE_SYS_ENDIAN_H
    355 #if WORDS_BIGENDIAN
    356 #define htobe16(x)	(x)
    357 #define htobe32(x)	(x)
    358 #define htobe64(x)	(x)
    359 #define htole16(x)	bswap16((u_int16_t)(x))
    360 #define htole32(x)	bswap32((u_int32_t)(x))
    361 #define htole64(x)	bswap64((u_int64_t)(x))
    362 #else
    363 #define htobe16(x)	bswap16((u_int16_t)(x))
    364 #define htobe32(x)	bswap32((u_int32_t)(x))
    365 #define htobe64(x)	bswap64((u_int64_t)(x))
    366 #define htole16(x)	(x)
    367 #define htole32(x)	(x)
    368 #define htole64(x)	(x)
    369 #endif
    370 #define be16toh(x)	htobe16(x)
    371 #define be32toh(x)	htobe32(x)
    372 #define be64toh(x)	htobe64(x)
    373 #define le16toh(x)	htole16(x)
    374 #define le32toh(x)	htole32(x)
    375 #define le64toh(x)	htole64(x)
    376 #endif
    377 
    378 /* <stdint.h> */
    379 
    380 #ifndef UINT32_MAX
    381 #define UINT32_MAX 0xffffffffU
    382 #endif
    383 
    384 /* <sys/mman.h> */
    385 
    386 #ifndef MAP_FILE
    387 #define MAP_FILE 0
    388 #endif
    389 
    390 /* <sys/param.h> */
    391 
    392 #undef BIG_ENDIAN
    393 #undef LITTLE_ENDIAN
    394 #define BIG_ENDIAN 4321
    395 #define LITTLE_ENDIAN 1234
    396 
    397 #undef BYTE_ORDER
    398 #if WORDS_BIGENDIAN
    399 #define BYTE_ORDER BIG_ENDIAN
    400 #else
    401 #define BYTE_ORDER LITTLE_ENDIAN
    402 #endif
    403 
    404 #ifndef DEV_BSIZE
    405 #define DEV_BSIZE (1 << 9)
    406 #endif
    407 
    408 #undef MIN
    409 #undef MAX
    410 #define MIN(a,b) ((a) < (b) ? (a) : (b))
    411 #define MAX(a,b) ((a) > (b) ? (a) : (b))
    412 
    413 #ifndef MAXBSIZE
    414 #define MAXBSIZE (64 * 1024)
    415 #endif
    416 #ifndef MAXFRAG
    417 #define MAXFRAG 8
    418 #endif
    419 #ifndef MAXPHYS
    420 #define MAXPHYS (64 * 1024)
    421 #endif
    422 
    423 /* XXX needed by makefs; this should be done in a better way */
    424 #undef btodb
    425 #define btodb(x) ((x) << 9)
    426 
    427 #undef setbit
    428 #undef clrbit
    429 #undef isset
    430 #undef isclr
    431 #define	setbit(a,i)	((a)[(i)/NBBY] |= 1<<((i)%NBBY))
    432 #define	clrbit(a,i)	((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
    433 #define	isset(a,i)	((a)[(i)/NBBY] & (1<<((i)%NBBY)))
    434 #define	isclr(a,i)	(((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
    435 
    436 #ifndef powerof2
    437 #define powerof2(x) ((((x)-1)&(x))==0)
    438 #endif
    439 
    440 #undef roundup
    441 #define roundup(x, y)	((((x)+((y)-1))/(y))*(y))
    442 
    443 /* <sys/stat.h> */
    444 
    445 #ifndef ALLPERMS
    446 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
    447 #endif
    448 #ifndef DEFFILEMODE
    449 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
    450 #endif
    451 #ifndef S_ISTXT
    452 #ifdef S_ISVTX
    453 #define S_ISTXT S_ISVTX
    454 #else
    455 #define S_ISTXT 0
    456 #endif
    457 #endif
    458 
    459 /* <sys/syslimits.h> */
    460 
    461 #ifndef LINE_MAX
    462 #define LINE_MAX 2048
    463 #endif
    464 
    465 /* <sys/time.h> */
    466 
    467 #ifndef timercmp
    468 #define	timercmp(tvp, uvp, cmp)						\
    469 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
    470 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
    471 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
    472 #endif
    473 #ifndef timeradd
    474 #define	timeradd(tvp, uvp, vvp)						\
    475 	do {								\
    476 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
    477 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
    478 		if ((vvp)->tv_usec >= 1000000) {			\
    479 			(vvp)->tv_sec++;				\
    480 			(vvp)->tv_usec -= 1000000;			\
    481 		}							\
    482 	} while (/* CONSTCOND */ 0)
    483 #endif
    484 #ifndef timersub
    485 #define	timersub(tvp, uvp, vvp)						\
    486 	do {								\
    487 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
    488 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
    489 		if ((vvp)->tv_usec < 0) {				\
    490 			(vvp)->tv_sec--;				\
    491 			(vvp)->tv_usec += 1000000;			\
    492 		}							\
    493 	} while (/* CONSTCOND */ 0)
    494 #endif
    495 
    496 /* <sys/types.h> */
    497 
    498 #if !HAVE_U_QUAD_T
    499 /* #define, not typedef, as quad_t exists as a struct on some systems */
    500 #define quad_t long long
    501 #define u_quad_t unsigned long long
    502 #define strtouq strtoull
    503 #endif
    504 
    505 #endif	/* !__NETBSD_COMPAT_DEFS_H__ */
    506