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