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