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