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