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