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