1 /* $NetBSD: compat_defs.h,v 1.103 2015/09/21 21:50:16 pooka Exp $ */ 2 3 #ifndef __NETBSD_COMPAT_DEFS_H__ 4 #define __NETBSD_COMPAT_DEFS_H__ 5 6 /* 7 * On NetBSD, ensure that _NETBSD_SOURCE does not get defined, so that 8 * accidental attempts to use NetBSD-specific features instead of more 9 * portable features is likely to be noticed when the tools are built 10 * on NetBSD. Define enough other feature test macros to expose the 11 * features we need. 12 */ 13 #ifdef __NetBSD__ 14 #define _ISOC99_SOURCE 15 #define _POSIX_SOURCE 1 16 #define _POSIX_C_SOURCE 200112L 17 #define _XOPEN_SOURCE 600 18 #endif /* __NetBSD__ */ 19 20 /* 21 * Linux: <features.h> turns on _POSIX_SOURCE by default, even though the 22 * program (not the OS) should do that. Preload <features.h> and 23 * then override some of the feature test macros. 24 */ 25 26 #if defined(__linux__) && HAVE_FEATURES_H 27 #include <features.h> 28 #undef _POSIX_SOURCE 29 #undef _POSIX_C_SOURCE 30 #define __USE_ISOC99 1 31 #endif /* __linux__ && HAVE_FEATURES_H */ 32 33 /* System headers needed for (re)definitions below. */ 34 35 #include <sys/types.h> 36 #include <sys/mman.h> 37 #include <sys/param.h> 38 /* time.h needs to be pulled in first at least on netbsd w/o _NETBSD_SOURCE */ 39 #include <sys/time.h> 40 #include <sys/stat.h> 41 #include <errno.h> 42 #include <fcntl.h> 43 #include <limits.h> 44 #include <paths.h> 45 #include <stdarg.h> 46 #include <stdio.h> 47 #include <stdlib.h> 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 #if HAVE_RPC_TYPES_H 68 #include <rpc/types.h> 69 #endif 70 71 #ifdef _NETBSD_SOURCE 72 #error _NETBSD_SOURCE is *not* to be defined. 73 #endif 74 75 /* Need this since we can't depend on NetBSD's version to be around */ 76 #ifdef __UNCONST 77 #undef __UNCONST 78 #endif 79 #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a)) 80 #ifdef __UNVOLATILE 81 #undef __UNVOLATILE 82 #endif 83 #define __UNVOLATILE(a) ((void *)(unsigned long)(volatile void *)(a)) 84 85 86 #undef __predict_false 87 #define __predict_false(x) (x) 88 #undef __predict_true 89 #define __predict_true(x) (x) 90 91 /* We don't include <pwd.h> here, so that "compat_pwd.h" works. */ 92 struct passwd; 93 94 /* We don't include <grp.h> either */ 95 struct group; 96 97 /* Assume an ANSI compiler for the host. */ 98 99 #undef __P 100 #define __P(x) x 101 102 #ifndef __BEGIN_DECLS 103 #define __BEGIN_DECLS 104 #endif 105 #ifndef __END_DECLS 106 #define __END_DECLS 107 #endif 108 109 /* Some things in NetBSD <sys/cdefs.h>. */ 110 111 #ifndef __CONCAT 112 #define __CONCAT(x,y) x ## y 113 #endif 114 #if !defined(__attribute__) && !defined(__GNUC__) 115 #define __attribute__(x) 116 #endif 117 #if !defined(__packed) 118 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7) 119 #define __packed __attribute__((__packed__)) 120 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590) 121 #define __packed __attribute__((__packed__)) 122 #else 123 #define __packed error: no __packed for this compiler 124 #endif 125 #endif /* !__packed */ 126 #ifndef __RENAME 127 #define __RENAME(x) 128 #endif 129 #undef __aconst 130 #define __aconst 131 #undef __dead 132 #define __dead 133 #undef __printflike 134 #define __printflike(x,y) 135 #undef __format_arg 136 #define __format_arg(x) 137 #undef __restrict 138 #define __restrict 139 #undef __unused 140 #define __unused 141 #undef __arraycount 142 #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0])) 143 #undef __USE 144 #define __USE(a) ((void)(a)) 145 #undef __type_min_s 146 #define __type_min_s(t) ((t)((1ULL << (sizeof(t) * NBBY - 1)))) 147 #undef __type_max_s 148 #define __type_max_s(t) ((t)~((1ULL << (sizeof(t) * NBBY - 1)))) 149 #undef __type_min_u 150 #define __type_min_u(t) ((t)0ULL) 151 #undef __type_max_u 152 #define __type_max_u(t) ((t)~0ULL) 153 #undef __type_is_signed 154 #define __type_is_signed(t) (/*LINTED*/__type_min_s(t) + (t)1 < (t)1) 155 #undef __type_min 156 #define __type_min(t) (__type_is_signed(t) ? __type_min_s(t) : __type_min_u(t)) 157 #undef __type_max 158 #define __type_max(t) (__type_is_signed(t) ? __type_max_s(t) : __type_max_u(t)) 159 160 /* Dirent support. */ 161 162 #if HAVE_DIRENT_H 163 # if defined(__linux__) && defined(__USE_BSD) 164 # undef __USE_BSD 165 # include <dirent.h> 166 # define __USE_BSD 1 167 # undef d_fileno 168 # else 169 # include <dirent.h> 170 # if defined(__DARWIN_UNIX03) 171 # undef d_fileno 172 # endif 173 # endif 174 # define NAMLEN(dirent) (strlen((dirent)->d_name)) 175 #else 176 # define dirent direct 177 # define NAMLEN(dirent) ((dirent)->d_namlen) 178 # if HAVE_SYS_NDIR_H 179 # include <sys/ndir.h> 180 # endif 181 # if HAVE_SYS_DIR_H 182 # include <sys/dir.h> 183 # endif 184 # if HAVE_NDIR_H 185 # include <ndir.h> 186 # endif 187 #endif 188 189 /* Type substitutes. */ 190 191 #if !HAVE_ID_T 192 typedef unsigned int id_t; 193 #endif 194 195 #if !HAVE_SOCKLEN_T 196 /* 197 * This is defined as int for compatibility with legacy systems (and not 198 * unsigned int), since universally it was int in most systems that did not 199 * define it. 200 */ 201 typedef int socklen_t; 202 #endif 203 204 #if !HAVE_U_LONG 205 typedef unsigned long u_long; 206 #endif 207 208 #if !HAVE_U_CHAR 209 typedef unsigned char u_char; 210 #endif 211 212 #if !HAVE_U_INT 213 typedef unsigned int u_int; 214 #endif 215 216 #if !HAVE_U_SHORT 217 typedef unsigned short u_short; 218 #endif 219 220 /* Prototypes for replacement functions. */ 221 222 #if !HAVE_ATOLL 223 long long int atoll(const char *); 224 #endif 225 226 #if !HAVE_ASPRINTF 227 int asprintf(char **, const char *, ...); 228 #endif 229 230 #if !HAVE_ASNPRINTF 231 int asnprintf(char **, size_t, const char *, ...); 232 #endif 233 234 #if !HAVE_BASENAME 235 char *basename(char *); 236 #endif 237 238 #if !HAVE_DECL_OPTIND 239 int getopt(int, char *const *, const char *); 240 extern char *optarg; 241 extern int optind, opterr, optopt; 242 #endif 243 244 #if !HAVE_DIRNAME 245 char *dirname(char *); 246 #endif 247 248 #if !HAVE_DIRFD 249 #if HAVE_DIR_DD_FD 250 #define dirfd(dirp) ((dirp)->dd_fd) 251 #elif HAVE_DIR___DD_FD 252 #define dirfd(dirp) ((dirp)->__dd_fd) 253 #else 254 /*XXX: Very hacky but no other way to bring this into scope w/o defining 255 _NETBSD_SOURCE which we're avoiding. */ 256 #if defined(__NetBSD__) || defined(__minix) 257 struct _dirdesc { 258 int dd_fd; /* file descriptor associated with directory */ 259 long dd_loc; /* offset in current buffer */ 260 long dd_size; /* amount of data returned by getdents */ 261 char *dd_buf; /* data buffer */ 262 int dd_len; /* size of data buffer */ 263 off_t dd_seek; /* magic cookie returned by getdents */ 264 long dd_rewind; /* magic cookie for rewinding */ 265 int dd_flags; /* flags for readdir */ 266 void *dd_lock; /* lock for concurrent access */ 267 }; 268 #define dirfd(dirp) (((struct _dirdesc *)dirp)->dd_fd) 269 #else 270 #error cannot figure out how to turn a DIR * into a fd 271 #endif 272 #endif 273 #endif 274 275 #if !HAVE_ERR_H 276 void err(int, const char *, ...); 277 void errx(int, const char *, ...); 278 void warn(const char *, ...); 279 void warnx(const char *, ...); 280 void vwarnx(const char *, va_list); 281 #endif 282 #if !HAVE_DECL_WARNC 283 void warnc(int, const char *, ...); 284 #endif 285 #if !HAVE_DECL_VWARNC 286 void vwarnc(int, const char *, va_list); 287 #endif 288 #if !HAVE_DECL_ERRC 289 void errc(int, int, const char *, ...); 290 #endif 291 #if !HAVE_DECL_VERRC 292 void verrc(int, int, const char *, va_list); 293 #endif 294 295 #if !HAVE_ESETFUNC 296 void (*esetfunc(void (*)(int, const char *, ...)))(int, const char *, ...); 297 size_t estrlcpy(char *, const char *, size_t); 298 size_t estrlcat(char *, const char *, size_t); 299 char *estrdup(const char *); 300 char *estrndup(const char *, size_t); 301 void *ecalloc(size_t, size_t); 302 void *emalloc(size_t); 303 void *erealloc(void *, size_t); 304 FILE *efopen(const char *, const char *); 305 int easprintf(char **, const char *, ...); 306 int evasprintf(char **, const char *, va_list); 307 #endif 308 309 #if !HAVE_FGETLN || defined(__NetBSD__) || defined(__minix) 310 char *fgetln(FILE *, size_t *); 311 #endif 312 #if !HAVE_DPRINTF 313 int dprintf(int, const char *, ...); 314 #endif 315 316 #if !HAVE_FLOCK 317 # define LOCK_SH 0x01 318 # define LOCK_EX 0x02 319 # define LOCK_NB 0x04 320 # define LOCK_UN 0x08 321 int flock(int, int); 322 #endif 323 324 #if !HAVE_FPARSELN || BROKEN_FPARSELN || defined(__NetBSD__) || defined(__minix) 325 # define FPARSELN_UNESCESC 0x01 326 # define FPARSELN_UNESCCONT 0x02 327 # define FPARSELN_UNESCCOMM 0x04 328 # define FPARSELN_UNESCREST 0x08 329 # define FPARSELN_UNESCALL 0x0f 330 char *fparseln(FILE *, size_t *, size_t *, const char [3], int); 331 #endif 332 333 #if !HAVE_GETLINE 334 ssize_t getdelim(char **, size_t *, int, FILE *); 335 ssize_t getline(char **, size_t *, FILE *); 336 #endif 337 338 #if !HAVE_ISSETUGID 339 int issetugid(void); 340 #endif 341 342 #if !HAVE_ISBLANK && !defined(isblank) 343 #define isblank(x) ((x) == ' ' || (x) == '\t') 344 #endif 345 346 #define __nbcompat_bswap16(x) ((((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff)) 347 348 #define __nbcompat_bswap32(x) ((((x) << 24) & 0xff000000) | \ 349 (((x) << 8) & 0x00ff0000) | \ 350 (((x) >> 8) & 0x0000ff00) | \ 351 (((x) >> 24) & 0x000000ff)) 352 353 #define __nbcompat_bswap64(x) (((u_int64_t)bswap32((x)) << 32) | \ 354 ((u_int64_t)bswap32((x) >> 32))) 355 356 #if ! HAVE_DECL_BSWAP16 357 #ifdef bswap16 358 #undef bswap16 359 #endif 360 #define bswap16(x) __nbcompat_bswap16(x) 361 #endif 362 #if ! HAVE_DECL_BSWAP32 363 #ifdef bswap32 364 #undef bswap32 365 #endif 366 #define bswap32(x) __nbcompat_bswap32(x) 367 #endif 368 #if ! HAVE_DECL_BSWAP64 369 #ifdef bswap64 370 #undef bswap64 371 #endif 372 #define bswap64(x) __nbcompat_bswap64(x) 373 #endif 374 375 #if !HAVE_MKSTEMP 376 int mkstemp(char *); 377 #endif 378 379 #if !HAVE_MKDTEMP 380 char *mkdtemp(char *); 381 #endif 382 383 #if !HAVE_MKSTEMP || !HAVE_MKDTEMP 384 /* This is a prototype for the internal function defined in 385 * src/lib/lib/stdio/gettemp.c */ 386 int __nbcompat_gettemp(char *, int *, int); 387 #endif 388 389 #if !HAVE_PREAD 390 ssize_t pread(int, void *, size_t, off_t); 391 #endif 392 393 #if !HAVE_HEAPSORT 394 int heapsort (void *, size_t, size_t, int (*)(const void *, const void *)); 395 #endif 396 /* Make them use our version */ 397 # define heapsort __nbcompat_heapsort 398 399 char *flags_to_string(unsigned long, const char *); 400 int string_to_flags(char **, unsigned long *, unsigned long *); 401 402 /* 403 * HAVE_X_FROM_Y and HAVE_PWCACHE_FOODB go together, because we cannot 404 * supply an implementation of one without the others -- some parts are 405 * libc internal and this varies from system to system. 406 * 407 * XXX this is dubious anyway: we assume (see HAVE_DECLs below) that if the 408 * XXX host system has all of these functions, all of their interfaces 409 * XXX and interactions are exactly the same as in our libc/libutil -- ugh. 410 */ 411 #if !HAVE_USER_FROM_UID || !HAVE_UID_FROM_USER || !HAVE_GROUP_FROM_GID || \ 412 !HAVE_GID_FROM_GROUP || !HAVE_PWCACHE_USERDB || !HAVE_PWCACHE_GROUDB 413 /* Make them use our version */ 414 # define user_from_uid __nbcompat_user_from_uid 415 # define uid_from_user __nbcompat_uid_from_user 416 # define pwcache_userdb __nbcompat_pwcache_userdb 417 # define group_from_gid __nbcompat_group_from_gid 418 # define gid_from_group __nbcompat_gid_from_group 419 # define pwcache_groupdb __nbcompat_pwcache_groupdb 420 #endif 421 422 #if !HAVE_DECL_UID_FROM_USER 423 int uid_from_user(const char *, uid_t *); 424 #endif 425 426 #if !HAVE_DECL_USER_FROM_UID 427 const char *user_from_uid(uid_t, int); 428 #endif 429 430 #if !HAVE_DECL_PWCACHE_USERDB 431 int pwcache_userdb(int (*)(int), void (*)(void), 432 struct passwd * (*)(const char *), struct passwd * (*)(uid_t)); 433 #endif 434 435 #if !HAVE_DECL_GID_FROM_GROUP 436 int gid_from_group(const char *, gid_t *); 437 #endif 438 439 #if !HAVE_DECL_GROUP_FROM_GID 440 const char *group_from_gid(gid_t, int); 441 #endif 442 443 #if !HAVE_DECL_PWCACHE_GROUPDB 444 int pwcache_groupdb(int (*)(int), void (*)(void), 445 struct group * (*)(const char *), struct group * (*)(gid_t)); 446 #endif 447 448 #if !HAVE_DECL_STRNDUP 449 char *strndup(const char *, size_t); 450 #endif 451 #if !HAVE_DECL_STRNLEN 452 size_t strnlen(const char *, size_t); 453 #endif 454 #if !HAVE_DECL_LCHFLAGS 455 int lchflags(const char *, unsigned long); 456 #endif 457 #if !HAVE_DECL_LCHMOD 458 int lchmod(const char *, mode_t); 459 #endif 460 #if !HAVE_DECL_LCHOWN 461 int lchown(const char *, uid_t, gid_t); 462 #endif 463 464 #if !HAVE_PWRITE 465 ssize_t pwrite(int, const void *, size_t, off_t); 466 #endif 467 468 #if !HAVE_RAISE_DEFAULT_SIGNAL 469 int raise_default_signal(int); 470 #endif 471 472 #if !HAVE_REALLOCARR 473 int reallocarr(void *, size_t, size_t); 474 #endif 475 476 #if !HAVE_SETENV 477 int setenv(const char *, const char *, int); 478 #endif 479 480 #if !HAVE_DECL_SETGROUPENT 481 int setgroupent(int); 482 #endif 483 484 #if !HAVE_DECL_SETPASSENT 485 int setpassent(int); 486 #endif 487 488 #if !HAVE_SETPROGNAME || defined(__NetBSD__) || defined(__minix) 489 const char *getprogname(void); 490 void setprogname(const char *); 491 #endif 492 493 #if !HAVE_SNPRINTB_M 494 int snprintb(char *, size_t, const char *, uint64_t); 495 int snprintb_m(char *, size_t, const char *, uint64_t, size_t); 496 #endif 497 498 #if !HAVE_SNPRINTF 499 int snprintf(char *, size_t, const char *, ...); 500 #endif 501 502 #if !HAVE_STRLCAT 503 size_t strlcat(char *, const char *, size_t); 504 #endif 505 506 #if !HAVE_STRLCPY 507 size_t strlcpy(char *, const char *, size_t); 508 #endif 509 510 #if !HAVE_STRMODE 511 void strmode(mode_t, char *); 512 #endif 513 514 #if !HAVE_STRNDUP 515 char *strndup(const char *, size_t); 516 #endif 517 518 #if !HAVE_STRSEP || defined(__NetBSD__) || defined(__minix) 519 char *strsep(char **, const char *); 520 #endif 521 522 #if !HAVE_DECL_STRSUFTOLL 523 long long strsuftoll(const char *, const char *, long long, long long); 524 long long strsuftollx(const char *, const char *, 525 long long, long long, char *, size_t); 526 #endif 527 528 #if !HAVE_STRTOLL 529 long long strtoll(const char *, char **, int); 530 #endif 531 532 #if !HAVE_STRTOI 533 intmax_t strtoi(const char * __restrict, char ** __restrict, int, 534 intmax_t, intmax_t, int *); 535 #endif 536 537 #if !HAVE_STRTOU 538 uintmax_t strtou(const char * __restrict, char ** __restrict, int, 539 uintmax_t, uintmax_t, int *); 540 #endif 541 542 #if !HAVE_USER_FROM_UID 543 const char *user_from_uid(uid_t, int); 544 #endif 545 546 #if !HAVE_GROUP_FROM_GID 547 const char *group_from_gid(gid_t, int); 548 #endif 549 550 #if !HAVE_VASPRINTF 551 int vasprintf(char **, const char *, va_list); 552 #endif 553 554 #if !HAVE_VASNPRINTF 555 int vasnprintf(char **, size_t, const char *, va_list); 556 #endif 557 558 #if !HAVE_VSNPRINTF 559 int vsnprintf(char *, size_t, const char *, va_list); 560 #endif 561 562 /* 563 * getmode() and setmode() are always defined, as these function names 564 * exist but with very different meanings on other OS's. The compat 565 * versions here simply accept an octal mode number; the "u+x,g-w" type 566 * of syntax is not accepted. 567 */ 568 569 #define getmode __nbcompat_getmode 570 #define setmode __nbcompat_setmode 571 572 mode_t getmode(const void *, mode_t); 573 void *setmode(const char *); 574 575 /* Eliminate assertions embedded in binaries. */ 576 577 #undef _DIAGASSERT 578 #define _DIAGASSERT(x) 579 580 /* Various sources use this */ 581 #undef __RCSID 582 #define __RCSID(x) struct XXXNETBSD_RCSID 583 #undef __SCCSID 584 #define __SCCSID(x) 585 #undef __COPYRIGHT 586 #define __COPYRIGHT(x) struct XXXNETBSD_COPYRIGHT 587 #undef __KERNEL_RCSID 588 #define __KERNEL_RCSID(x,y) 589 590 /* Heimdal expects this one. */ 591 592 #undef RCSID 593 #define RCSID(x) 594 595 /* Some definitions not available on all systems. */ 596 597 #ifndef __inline 598 #define __inline inline 599 #endif 600 601 /* <errno.h> */ 602 603 #ifndef EFTYPE 604 #define EFTYPE EIO 605 #endif 606 607 /* <fcntl.h> */ 608 609 #ifndef O_EXLOCK 610 #define O_EXLOCK 0 611 #endif 612 #ifndef O_SHLOCK 613 #define O_SHLOCK 0 614 #endif 615 #ifndef O_CLOEXEC 616 #define O_CLOEXEC 0 617 #endif 618 619 /* <inttypes.h> */ 620 621 #if UCHAR_MAX == 0xffU /* char is an 8-bit type */ 622 #ifndef PRId8 623 #define PRId8 "hhd" 624 #endif 625 #ifndef PRIi8 626 #define PRIi8 "hhi" 627 #endif 628 #ifndef PRIo8 629 #define PRIo8 "hho" 630 #endif 631 #ifndef PRIu8 632 #define PRIu8 "hhu" 633 #endif 634 #ifndef PRIx8 635 #define PRIx8 "hhx" 636 #endif 637 #ifndef PRIX8 638 #define PRIX8 "hhX" 639 #endif 640 #ifndef SCNd8 641 #define SCNd8 "hhd" 642 #endif 643 #ifndef SCNi8 644 #define SCNi8 "hhi" 645 #endif 646 #ifndef SCNo8 647 #define SCNo8 "hho" 648 #endif 649 #ifndef SCNu8 650 #define SCNu8 "hhu" 651 #endif 652 #ifndef SCNx8 653 #define SCNx8 "hhx" 654 #endif 655 #ifndef SCNX8 656 #define SCNX8 "hhX" 657 #endif 658 #endif /* char is an 8-bit type */ 659 #if ! (defined(PRId8) && defined(PRIi8) && defined(PRIo8) && \ 660 defined(PRIu8) && defined(PRIx8) && defined(PRIX8)) 661 #error "Don't know how to define PRI[diouxX]8" 662 #endif 663 #if ! (defined(SCNd8) && defined(SCNi8) && defined(SCNo8) && \ 664 defined(SCNu8) && defined(SCNx8) && defined(SCNX8)) 665 #error "Don't know how to define SCN[diouxX]8" 666 #endif 667 668 #if USHRT_MAX == 0xffffU /* short is a 16-bit type */ 669 #ifndef PRId16 670 #define PRId16 "hd" 671 #endif 672 #ifndef PRIi16 673 #define PRIi16 "hi" 674 #endif 675 #ifndef PRIo16 676 #define PRIo16 "ho" 677 #endif 678 #ifndef PRIu16 679 #define PRIu16 "hu" 680 #endif 681 #ifndef PRIx16 682 #define PRIx16 "hx" 683 #endif 684 #ifndef PRIX16 685 #define PRIX16 "hX" 686 #endif 687 #ifndef SCNd16 688 #define SCNd16 "hd" 689 #endif 690 #ifndef SCNi16 691 #define SCNi16 "hi" 692 #endif 693 #ifndef SCNo16 694 #define SCNo16 "ho" 695 #endif 696 #ifndef SCNu16 697 #define SCNu16 "hu" 698 #endif 699 #ifndef SCNx16 700 #define SCNx16 "hx" 701 #endif 702 #ifndef SCNX16 703 #define SCNX16 "hX" 704 #endif 705 #endif /* short is a 16-bit type */ 706 #if ! (defined(PRId16) && defined(PRIi16) && defined(PRIo16) && \ 707 defined(PRIu16) && defined(PRIx16) && defined(PRIX16)) 708 #error "Don't know how to define PRI[diouxX]16" 709 #endif 710 #if ! (defined(SCNd16) && defined(SCNi16) && defined(SCNo16) && \ 711 defined(SCNu16) && defined(SCNx16) && defined(SCNX16)) 712 #error "Don't know how to define SCN[diouxX]16" 713 #endif 714 715 #if UINT_MAX == 0xffffffffU /* int is a 32-bit type */ 716 #ifndef PRId32 717 #define PRId32 "d" 718 #endif 719 #ifndef PRIi32 720 #define PRIi32 "i" 721 #endif 722 #ifndef PRIo32 723 #define PRIo32 "o" 724 #endif 725 #ifndef PRIu32 726 #define PRIu32 "u" 727 #endif 728 #ifndef PRIx32 729 #define PRIx32 "x" 730 #endif 731 #ifndef PRIX32 732 #define PRIX32 "X" 733 #endif 734 #ifndef SCNd32 735 #define SCNd32 "d" 736 #endif 737 #ifndef SCNi32 738 #define SCNi32 "i" 739 #endif 740 #ifndef SCNo32 741 #define SCNo32 "o" 742 #endif 743 #ifndef SCNu32 744 #define SCNu32 "u" 745 #endif 746 #ifndef SCNx32 747 #define SCNx32 "x" 748 #endif 749 #ifndef SCNX32 750 #define SCNX32 "X" 751 #endif 752 #endif /* int is a 32-bit type */ 753 #if ULONG_MAX == 0xffffffffU /* long is a 32-bit type */ 754 #ifndef PRId32 755 #define PRId32 "ld" 756 #endif 757 #ifndef PRIi32 758 #define PRIi32 "li" 759 #endif 760 #ifndef PRIo32 761 #define PRIo32 "lo" 762 #endif 763 #ifndef PRIu32 764 #define PRIu32 "lu" 765 #endif 766 #ifndef PRIx32 767 #define PRIx32 "lx" 768 #endif 769 #ifndef PRIX32 770 #define PRIX32 "lX" 771 #endif 772 #ifndef SCNd32 773 #define SCNd32 "ld" 774 #endif 775 #ifndef SCNi32 776 #define SCNi32 "li" 777 #endif 778 #ifndef SCNo32 779 #define SCNo32 "lo" 780 #endif 781 #ifndef SCNu32 782 #define SCNu32 "lu" 783 #endif 784 #ifndef SCNx32 785 #define SCNx32 "lx" 786 #endif 787 #ifndef SCNX32 788 #define SCNX32 "lX" 789 #endif 790 #endif /* long is a 32-bit type */ 791 #if ! (defined(PRId32) && defined(PRIi32) && defined(PRIo32) && \ 792 defined(PRIu32) && defined(PRIx32) && defined(PRIX32)) 793 #error "Don't know how to define PRI[diouxX]32" 794 #endif 795 #if ! (defined(SCNd32) && defined(SCNi32) && defined(SCNo32) && \ 796 defined(SCNu32) && defined(SCNx32) && defined(SCNX32)) 797 #error "Don't know how to define SCN[diouxX]32" 798 #endif 799 800 #if ULONG_MAX == 0xffffffffffffffffU /* long is a 64-bit type */ 801 #ifndef PRId64 802 #define PRId64 "ld" 803 #endif 804 #ifndef PRIi64 805 #define PRIi64 "li" 806 #endif 807 #ifndef PRIo64 808 #define PRIo64 "lo" 809 #endif 810 #ifndef PRIu64 811 #define PRIu64 "lu" 812 #endif 813 #ifndef PRIx64 814 #define PRIx64 "lx" 815 #endif 816 #ifndef PRIX64 817 #define PRIX64 "lX" 818 #endif 819 #ifndef SCNd64 820 #define SCNd64 "ld" 821 #endif 822 #ifndef SCNi64 823 #define SCNi64 "li" 824 #endif 825 #ifndef SCNo64 826 #define SCNo64 "lo" 827 #endif 828 #ifndef SCNu64 829 #define SCNu64 "lu" 830 #endif 831 #ifndef SCNx64 832 #define SCNx64 "lx" 833 #endif 834 #ifndef SCNX64 835 #define SCNX64 "lX" 836 #endif 837 #endif /* long is a 64-bit type */ 838 #if ULLONG_MAX == 0xffffffffffffffffU /* long long is a 64-bit type */ 839 #ifndef PRId64 840 #define PRId64 "lld" 841 #endif 842 #ifndef PRIi64 843 #define PRIi64 "lli" 844 #endif 845 #ifndef PRIo64 846 #define PRIo64 "llo" 847 #endif 848 #ifndef PRIu64 849 #define PRIu64 "llu" 850 #endif 851 #ifndef PRIx64 852 #define PRIx64 "llx" 853 #endif 854 #ifndef PRIX64 855 #define PRIX64 "llX" 856 #endif 857 #ifndef SCNd64 858 #define SCNd64 "lld" 859 #endif 860 #ifndef SCNi64 861 #define SCNi64 "lli" 862 #endif 863 #ifndef SCNo64 864 #define SCNo64 "llo" 865 #endif 866 #ifndef SCNu64 867 #define SCNu64 "llu" 868 #endif 869 #ifndef SCNx64 870 #define SCNx64 "llx" 871 #endif 872 #ifndef SCNX64 873 #define SCNX64 "llX" 874 #endif 875 #endif /* long long is a 64-bit type */ 876 #if ! (defined(PRId64) && defined(PRIi64) && defined(PRIo64) && \ 877 defined(PRIu64) && defined(PRIx64) && defined(PRIX64)) 878 #error "Don't know how to define PRI[diouxX]64" 879 #endif 880 #if ! (defined(SCNd64) && defined(SCNi64) && defined(SCNo64) && \ 881 defined(SCNu64) && defined(SCNx64) && defined(SCNX64)) 882 #error "Don't know how to define SCN[diouxX]64" 883 #endif 884 885 /* <limits.h> */ 886 887 #ifndef UID_MAX 888 #define UID_MAX 32767 889 #endif 890 #ifndef GID_MAX 891 #define GID_MAX UID_MAX 892 #endif 893 894 #ifndef UQUAD_MAX 895 #define UQUAD_MAX ((u_quad_t)-1) 896 #endif 897 #ifndef QUAD_MAX 898 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1)) 899 #endif 900 #ifndef QUAD_MIN 901 #define QUAD_MIN ((quad_t)(~QUAD_MAX)) 902 #endif 903 #ifndef ULLONG_MAX 904 #define ULLONG_MAX ((unsigned long long)-1) 905 #endif 906 #ifndef LLONG_MAX 907 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1)) 908 #endif 909 #ifndef LLONG_MIN 910 #define LLONG_MIN ((long long)(~LLONG_MAX)) 911 #endif 912 913 #ifndef MAXPATHLEN 914 #define MAXPATHLEN 4096 915 #endif 916 #ifndef PATH_MAX 917 #define PATH_MAX MAXPATHLEN 918 #endif 919 920 /* <paths.h> */ 921 922 /* The host's _PATH_BSHELL might be broken, so override it. */ 923 #undef _PATH_BSHELL 924 #define _PATH_BSHELL PATH_BSHELL 925 #ifndef _PATH_DEFPATH 926 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin" 927 #endif 928 #ifndef _PATH_DEV 929 #define _PATH_DEV "/dev/" 930 #endif 931 #ifndef _PATH_DEVNULL 932 #define _PATH_DEVNULL _PATH_DEV "null" 933 #endif 934 #ifndef _PATH_TMP 935 #define _PATH_TMP "/tmp/" 936 #endif 937 #ifndef _PATH_DEFTAPE 938 #define _PATH_DEFTAPE "/dev/nrst0" 939 #endif 940 #ifndef _PATH_VI 941 #define _PATH_VI "/usr/bin/vi" 942 #endif 943 944 /* <stdint.h> */ 945 946 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX) 947 #define SIZE_MAX SIZE_T_MAX 948 #endif 949 950 #ifndef UINT8_MAX 951 #define UINT8_MAX 0xffU 952 #endif 953 954 #ifndef UINT16_MAX 955 #define UINT16_MAX 0xffffU 956 #endif 957 958 #ifndef UINT32_MAX 959 #define UINT32_MAX 0xffffffffU 960 #endif 961 962 /* <stdlib.h> */ 963 964 #ifndef __GNUC__ 965 # if HAVE_ALLOCA_H 966 # include <alloca.h> 967 # else 968 # ifndef alloca /* predefined by HP cc +Olibcalls */ 969 char *alloca (); 970 # endif 971 # endif 972 #endif 973 974 /* avoid prototype conflicts with host */ 975 #define cgetcap __nbcompat_cgetcap 976 #define cgetclose __nbcompat_cgetclose 977 #define cgetent __nbcompat_cgetent 978 #define cgetfirst __nbcompat_cgetfirst 979 #define cgetmatch __nbcompat_cgetmatch 980 #define cgetnext __nbcompat_cgetnext 981 #define cgetnum __nbcompat_cgetnum 982 #define cgetset __nbcompat_cgetset 983 #define cgetstr __nbcompat_cgetstr 984 #define cgetustr __nbcompat_cgetustr 985 986 char *cgetcap(char *, const char *, int); 987 int cgetclose(void); 988 int cgetent(char **, const char * const *, const char *); 989 int cgetfirst(char **, const char * const *); 990 int cgetmatch(const char *, const char *); 991 int cgetnext(char **, const char * const *); 992 int cgetnum(char *, const char *, long *); 993 int cgetset(const char *); 994 int cgetstr(char *, const char *, char **); 995 int cgetustr(char *, const char *, char **); 996 997 /* <sys/endian.h> */ 998 999 #if WORDS_BIGENDIAN 1000 #if !HAVE_DECL_HTOBE16 1001 #define htobe16(x) (x) 1002 #endif 1003 #if !HAVE_DECL_HTOBE32 1004 #define htobe32(x) (x) 1005 #endif 1006 #if !HAVE_DECL_HTOBE64 1007 #define htobe64(x) (x) 1008 #endif 1009 #if !HAVE_DECL_HTOLE16 1010 #define htole16(x) bswap16((u_int16_t)(x)) 1011 #endif 1012 #if !HAVE_DECL_HTOLE32 1013 #define htole32(x) bswap32((u_int32_t)(x)) 1014 #endif 1015 #if !HAVE_DECL_HTOLE64 1016 #define htole64(x) bswap64((u_int64_t)(x)) 1017 #endif 1018 #else 1019 #if !HAVE_DECL_HTOBE16 1020 #define htobe16(x) bswap16((u_int16_t)(x)) 1021 #endif 1022 #if !HAVE_DECL_HTOBE32 1023 #define htobe32(x) bswap32((u_int32_t)(x)) 1024 #endif 1025 #if !HAVE_DECL_HTOBE64 1026 #define htobe64(x) bswap64((u_int64_t)(x)) 1027 #endif 1028 #if !HAVE_DECL_HTOLE16 1029 #define htole16(x) (x) 1030 #endif 1031 #if !HAVE_DECL_HTOLE32 1032 #define htole32(x) (x) 1033 #endif 1034 #if !HAVE_DECL_HTOLE64 1035 #define htole64(x) (x) 1036 #endif 1037 #endif 1038 #if !HAVE_DECL_BE16TOH 1039 #define be16toh(x) htobe16(x) 1040 #endif 1041 #if !HAVE_DECL_BE32TOH 1042 #define be32toh(x) htobe32(x) 1043 #endif 1044 #if !HAVE_DECL_BE64TOH 1045 #define be64toh(x) htobe64(x) 1046 #endif 1047 #if !HAVE_DECL_LE16TOH 1048 #define le16toh(x) htole16(x) 1049 #endif 1050 #if !HAVE_DECL_LE32TOH 1051 #define le32toh(x) htole32(x) 1052 #endif 1053 #if !HAVE_DECL_LE64TOH 1054 #define le64toh(x) htole64(x) 1055 #endif 1056 1057 #define __GEN_ENDIAN_ENC(bits, endian) \ 1058 static void \ 1059 endian ## bits ## enc(void *dst, uint ## bits ## _t u) \ 1060 { \ 1061 u = hto ## endian ## bits (u); \ 1062 memcpy(dst, &u, sizeof(u)); \ 1063 } 1064 #if !HAVE_DECL_BE16ENC 1065 __GEN_ENDIAN_ENC(16, be) 1066 #endif 1067 #if !HAVE_DECL_BE32ENC 1068 __GEN_ENDIAN_ENC(32, be) 1069 #endif 1070 #if !HAVE_DECL_BE64ENC 1071 __GEN_ENDIAN_ENC(64, be) 1072 #endif 1073 #if !HAVE_DECL_LE16ENC 1074 __GEN_ENDIAN_ENC(16, le) 1075 #endif 1076 #if !HAVE_DECL_LE32ENC 1077 __GEN_ENDIAN_ENC(32, le) 1078 #endif 1079 #if !HAVE_DECL_LE64ENC 1080 __GEN_ENDIAN_ENC(64, le) 1081 #endif 1082 #undef __GEN_ENDIAN_ENC 1083 1084 #define __GEN_ENDIAN_DEC(bits, endian) \ 1085 static uint ## bits ## _t \ 1086 endian ## bits ## dec(const void *buf) \ 1087 { \ 1088 uint ## bits ## _t u; \ 1089 memcpy(&u, buf, sizeof(u)); \ 1090 return endian ## bits ## toh (u); \ 1091 } 1092 #if !HAVE_DECL_BE16DEC 1093 __GEN_ENDIAN_DEC(16, be) 1094 #endif 1095 #if !HAVE_DECL_BE32DEC 1096 __GEN_ENDIAN_DEC(32, be) 1097 #endif 1098 #if !HAVE_DECL_BE64DEC 1099 __GEN_ENDIAN_DEC(64, be) 1100 #endif 1101 #if !HAVE_DECL_LE16DEC 1102 __GEN_ENDIAN_DEC(16, le) 1103 #endif 1104 #if !HAVE_DECL_LE32DEC 1105 __GEN_ENDIAN_DEC(32, le) 1106 #endif 1107 #if !HAVE_DECL_LE64DEC 1108 __GEN_ENDIAN_DEC(64, le) 1109 #endif 1110 #undef __GEN_ENDIAN_DEC 1111 1112 /* <sys/mman.h> */ 1113 1114 #ifndef MAP_FILE 1115 #define MAP_FILE 0 1116 #endif 1117 1118 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */ 1119 #ifndef MAP_ANON 1120 #ifdef MAP_ANONYMOUS 1121 #define MAP_ANON MAP_ANONYMOUS 1122 #endif 1123 #endif 1124 1125 /* <sys/param.h> */ 1126 1127 #undef BIG_ENDIAN 1128 #undef LITTLE_ENDIAN 1129 #undef PDP_ENDIAN 1130 #define BIG_ENDIAN 4321 1131 #define LITTLE_ENDIAN 1234 1132 #define PDP_ENDIAN 3412 1133 1134 #undef BYTE_ORDER 1135 #if WORDS_BIGENDIAN 1136 #define BYTE_ORDER BIG_ENDIAN 1137 #else 1138 #define BYTE_ORDER LITTLE_ENDIAN 1139 #endif 1140 1141 /* all references of DEV_BSIZE in tools are for NetBSD's file images */ 1142 #undef DEV_BSIZE 1143 #define DEV_BSIZE (1 << 9) 1144 1145 #undef MIN 1146 #undef MAX 1147 #define MIN(a,b) ((a) < (b) ? (a) : (b)) 1148 #define MAX(a,b) ((a) > (b) ? (a) : (b)) 1149 1150 #ifndef MAXBSIZE 1151 #define MAXBSIZE (64 * 1024) 1152 #endif 1153 #ifndef MAXFRAG 1154 #define MAXFRAG 8 1155 #endif 1156 #ifndef MAXPHYS 1157 #define MAXPHYS (64 * 1024) 1158 #endif 1159 #ifndef MAXHOSTNAMELEN 1160 #define MAXHOSTNAMELEN 256 1161 #endif 1162 1163 /* XXX needed by makefs; this should be done in a better way */ 1164 #undef btodb 1165 #define btodb(x) ((x) << 9) 1166 1167 #undef setbit 1168 #undef clrbit 1169 #undef isset 1170 #undef isclr 1171 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY)) 1172 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY))) 1173 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY))) 1174 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0) 1175 1176 #ifndef powerof2 1177 #define powerof2(x) ((((x)-1)&(x))==0) 1178 #endif 1179 1180 #undef roundup 1181 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) 1182 1183 /* <sys/stat.h> */ 1184 1185 #ifndef ALLPERMS 1186 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO) 1187 #endif 1188 #ifndef DEFFILEMODE 1189 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) 1190 #endif 1191 #ifndef S_ISTXT 1192 #ifdef S_ISVTX 1193 #define S_ISTXT S_ISVTX 1194 #else 1195 #define S_ISTXT 0 1196 #endif 1197 #endif 1198 1199 /* Protected by _NETBSD_SOURCE otherwise. */ 1200 #if HAVE_STRUCT_STAT_ST_FLAGS && (defined(__NetBSD__) || defined(__minix)) 1201 #define UF_SETTABLE 0x0000ffff 1202 #define UF_NODUMP 0x00000001 1203 #define UF_IMMUTABLE 0x00000002 1204 #define UF_APPEND 0x00000004 1205 #define UF_OPAQUE 0x00000008 1206 #define SF_SETTABLE 0xffff0000 1207 #define SF_ARCHIVED 0x00010000 1208 #define SF_IMMUTABLE 0x00020000 1209 #define SF_APPEND 0x00040000 1210 #endif 1211 1212 /* <sys/syslimits.h> */ 1213 1214 #ifndef LINE_MAX 1215 #define LINE_MAX 2048 1216 #endif 1217 1218 /* <sys/time.h> */ 1219 1220 #ifndef timercmp 1221 #define timercmp(tvp, uvp, cmp) \ 1222 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 1223 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 1224 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 1225 #endif 1226 #ifndef timeradd 1227 #define timeradd(tvp, uvp, vvp) \ 1228 do { \ 1229 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 1230 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 1231 if ((vvp)->tv_usec >= 1000000) { \ 1232 (vvp)->tv_sec++; \ 1233 (vvp)->tv_usec -= 1000000; \ 1234 } \ 1235 } while (/* CONSTCOND */ 0) 1236 #endif 1237 #ifndef timersub 1238 #define timersub(tvp, uvp, vvp) \ 1239 do { \ 1240 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 1241 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 1242 if ((vvp)->tv_usec < 0) { \ 1243 (vvp)->tv_sec--; \ 1244 (vvp)->tv_usec += 1000000; \ 1245 } \ 1246 } while (/* CONSTCOND */ 0) 1247 #endif 1248 1249 /* <sys/types.h> */ 1250 1251 #ifdef major 1252 #undef major 1253 #endif 1254 #define major(x) ((int32_t)((((x) & 0x000fff00) >> 8))) 1255 1256 #ifdef minor 1257 #undef minor 1258 #endif 1259 #define minor(x) ((int32_t)((((x) & 0xfff00000) >> 12) | \ 1260 (((x) & 0x000000ff) >> 0))) 1261 #ifdef makedev 1262 #undef makedev 1263 #endif 1264 #define makedev(x,y) ((dev_t)((((x) << 8) & 0x000fff00) | \ 1265 (((y) << 12) & 0xfff00000) | \ 1266 (((y) << 0) & 0x000000ff))) 1267 #ifndef NBBY 1268 #define NBBY 8 1269 #endif 1270 1271 #if !HAVE_U_QUAD_T 1272 /* #define, not typedef, as quad_t exists as a struct on some systems */ 1273 #define quad_t long long 1274 #define u_quad_t unsigned long long 1275 #define strtoq strtoll 1276 #define strtouq strtoull 1277 #endif 1278 1279 /* Has quad_t but these prototypes don't get pulled into scope. w/o we lose */ 1280 #if defined(__NetBSD__) || defined(__minix) 1281 quad_t strtoq(const char *, char **, int); 1282 u_quad_t strtouq(const char *, char **, int); 1283 #endif 1284 1285 #endif /* !__NETBSD_COMPAT_DEFS_H__ */ 1286