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