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