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