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