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