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