compat_defs.h revision 1.98 1 /* $NetBSD: compat_defs.h,v 1.98 2014/09/01 11:20:06 apb 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_USER_FROM_UID
524 const char *user_from_uid(uid_t, int);
525 #endif
526
527 #if !HAVE_GROUP_FROM_GID
528 const char *group_from_gid(gid_t, int);
529 #endif
530
531 #if !HAVE_VASPRINTF
532 int vasprintf(char **, const char *, va_list);
533 #endif
534
535 #if !HAVE_VASNPRINTF
536 int vasnprintf(char **, size_t, const char *, va_list);
537 #endif
538
539 #if !HAVE_VSNPRINTF
540 int vsnprintf(char *, size_t, const char *, va_list);
541 #endif
542
543 /*
544 * getmode() and setmode() are always defined, as these function names
545 * exist but with very different meanings on other OS's. The compat
546 * versions here simply accept an octal mode number; the "u+x,g-w" type
547 * of syntax is not accepted.
548 */
549
550 #define getmode __nbcompat_getmode
551 #define setmode __nbcompat_setmode
552
553 mode_t getmode(const void *, mode_t);
554 void *setmode(const char *);
555
556 /* Eliminate assertions embedded in binaries. */
557
558 #undef _DIAGASSERT
559 #define _DIAGASSERT(x)
560
561 /* Various sources use this */
562 #undef __RCSID
563 #define __RCSID(x) struct XXXNETBSD_RCSID
564 #undef __SCCSID
565 #define __SCCSID(x)
566 #undef __COPYRIGHT
567 #define __COPYRIGHT(x) struct XXXNETBSD_COPYRIGHT
568 #undef __KERNEL_RCSID
569 #define __KERNEL_RCSID(x,y)
570
571 /* Heimdal expects this one. */
572
573 #undef RCSID
574 #define RCSID(x)
575
576 /* Some definitions not available on all systems. */
577
578 #ifndef __inline
579 #define __inline inline
580 #endif
581
582 /* <errno.h> */
583
584 #ifndef EFTYPE
585 #define EFTYPE EIO
586 #endif
587
588 /* <fcntl.h> */
589
590 #ifndef O_EXLOCK
591 #define O_EXLOCK 0
592 #endif
593 #ifndef O_SHLOCK
594 #define O_SHLOCK 0
595 #endif
596
597 /* <inttypes.h> */
598
599 #if UCHAR_MAX == 0xffU /* char is an 8-bit type */
600 #ifndef PRId8
601 #define PRId8 "hhd"
602 #endif
603 #ifndef PRIi8
604 #define PRIi8 "hhi"
605 #endif
606 #ifndef PRIo8
607 #define PRIo8 "hho"
608 #endif
609 #ifndef PRIu8
610 #define PRIu8 "hhu"
611 #endif
612 #ifndef PRIx8
613 #define PRIx8 "hhx"
614 #endif
615 #ifndef PRIX8
616 #define PRIX8 "hhX"
617 #endif
618 #ifndef SCNd8
619 #define SCNd8 "hhd"
620 #endif
621 #ifndef SCNi8
622 #define SCNi8 "hhi"
623 #endif
624 #ifndef SCNo8
625 #define SCNo8 "hho"
626 #endif
627 #ifndef SCNu8
628 #define SCNu8 "hhu"
629 #endif
630 #ifndef SCNx8
631 #define SCNx8 "hhx"
632 #endif
633 #ifndef SCNX8
634 #define SCNX8 "hhX"
635 #endif
636 #endif /* char is an 8-bit type */
637 #if ! (defined(PRId8) && defined(PRIi8) && defined(PRIo8) && \
638 defined(PRIu8) && defined(PRIx8) && defined(PRIX8))
639 #error "Don't know how to define PRI[diouxX]8"
640 #endif
641 #if ! (defined(SCNd8) && defined(SCNi8) && defined(SCNo8) && \
642 defined(SCNu8) && defined(SCNx8) && defined(SCNX8))
643 #error "Don't know how to define SCN[diouxX]8"
644 #endif
645
646 #if USHRT_MAX == 0xffffU /* short is a 16-bit type */
647 #ifndef PRId16
648 #define PRId16 "hd"
649 #endif
650 #ifndef PRIi16
651 #define PRIi16 "hi"
652 #endif
653 #ifndef PRIo16
654 #define PRIo16 "ho"
655 #endif
656 #ifndef PRIu16
657 #define PRIu16 "hu"
658 #endif
659 #ifndef PRIx16
660 #define PRIx16 "hx"
661 #endif
662 #ifndef PRIX16
663 #define PRIX16 "hX"
664 #endif
665 #ifndef SCNd16
666 #define SCNd16 "hd"
667 #endif
668 #ifndef SCNi16
669 #define SCNi16 "hi"
670 #endif
671 #ifndef SCNo16
672 #define SCNo16 "ho"
673 #endif
674 #ifndef SCNu16
675 #define SCNu16 "hu"
676 #endif
677 #ifndef SCNx16
678 #define SCNx16 "hx"
679 #endif
680 #ifndef SCNX16
681 #define SCNX16 "hX"
682 #endif
683 #endif /* short is a 16-bit type */
684 #if ! (defined(PRId16) && defined(PRIi16) && defined(PRIo16) && \
685 defined(PRIu16) && defined(PRIx16) && defined(PRIX16))
686 #error "Don't know how to define PRI[diouxX]16"
687 #endif
688 #if ! (defined(SCNd16) && defined(SCNi16) && defined(SCNo16) && \
689 defined(SCNu16) && defined(SCNx16) && defined(SCNX16))
690 #error "Don't know how to define SCN[diouxX]16"
691 #endif
692
693 #if UINT_MAX == 0xffffffffU /* int is a 32-bit type */
694 #ifndef PRId32
695 #define PRId32 "d"
696 #endif
697 #ifndef PRIi32
698 #define PRIi32 "i"
699 #endif
700 #ifndef PRIo32
701 #define PRIo32 "o"
702 #endif
703 #ifndef PRIu32
704 #define PRIu32 "u"
705 #endif
706 #ifndef PRIx32
707 #define PRIx32 "x"
708 #endif
709 #ifndef PRIX32
710 #define PRIX32 "X"
711 #endif
712 #ifndef SCNd32
713 #define SCNd32 "d"
714 #endif
715 #ifndef SCNi32
716 #define SCNi32 "i"
717 #endif
718 #ifndef SCNo32
719 #define SCNo32 "o"
720 #endif
721 #ifndef SCNu32
722 #define SCNu32 "u"
723 #endif
724 #ifndef SCNx32
725 #define SCNx32 "x"
726 #endif
727 #ifndef SCNX32
728 #define SCNX32 "X"
729 #endif
730 #endif /* int is a 32-bit type */
731 #if ULONG_MAX == 0xffffffffU /* long is a 32-bit type */
732 #ifndef PRId32
733 #define PRId32 "ld"
734 #endif
735 #ifndef PRIi32
736 #define PRIi32 "li"
737 #endif
738 #ifndef PRIo32
739 #define PRIo32 "lo"
740 #endif
741 #ifndef PRIu32
742 #define PRIu32 "lu"
743 #endif
744 #ifndef PRIx32
745 #define PRIx32 "lx"
746 #endif
747 #ifndef PRIX32
748 #define PRIX32 "lX"
749 #endif
750 #ifndef SCNd32
751 #define SCNd32 "ld"
752 #endif
753 #ifndef SCNi32
754 #define SCNi32 "li"
755 #endif
756 #ifndef SCNo32
757 #define SCNo32 "lo"
758 #endif
759 #ifndef SCNu32
760 #define SCNu32 "lu"
761 #endif
762 #ifndef SCNx32
763 #define SCNx32 "lx"
764 #endif
765 #ifndef SCNX32
766 #define SCNX32 "lX"
767 #endif
768 #endif /* long is a 32-bit type */
769 #if ! (defined(PRId32) && defined(PRIi32) && defined(PRIo32) && \
770 defined(PRIu32) && defined(PRIx32) && defined(PRIX32))
771 #error "Don't know how to define PRI[diouxX]32"
772 #endif
773 #if ! (defined(SCNd32) && defined(SCNi32) && defined(SCNo32) && \
774 defined(SCNu32) && defined(SCNx32) && defined(SCNX32))
775 #error "Don't know how to define SCN[diouxX]32"
776 #endif
777
778 #if ULONG_MAX == 0xffffffffffffffffU /* long is a 64-bit type */
779 #ifndef PRId64
780 #define PRId64 "ld"
781 #endif
782 #ifndef PRIi64
783 #define PRIi64 "li"
784 #endif
785 #ifndef PRIo64
786 #define PRIo64 "lo"
787 #endif
788 #ifndef PRIu64
789 #define PRIu64 "lu"
790 #endif
791 #ifndef PRIx64
792 #define PRIx64 "lx"
793 #endif
794 #ifndef PRIX64
795 #define PRIX64 "lX"
796 #endif
797 #ifndef SCNd64
798 #define SCNd64 "ld"
799 #endif
800 #ifndef SCNi64
801 #define SCNi64 "li"
802 #endif
803 #ifndef SCNo64
804 #define SCNo64 "lo"
805 #endif
806 #ifndef SCNu64
807 #define SCNu64 "lu"
808 #endif
809 #ifndef SCNx64
810 #define SCNx64 "lx"
811 #endif
812 #ifndef SCNX64
813 #define SCNX64 "lX"
814 #endif
815 #endif /* long is a 64-bit type */
816 #if ULLONG_MAX == 0xffffffffffffffffU /* long long is a 64-bit type */
817 #ifndef PRId64
818 #define PRId64 "lld"
819 #endif
820 #ifndef PRIi64
821 #define PRIi64 "lli"
822 #endif
823 #ifndef PRIo64
824 #define PRIo64 "llo"
825 #endif
826 #ifndef PRIu64
827 #define PRIu64 "llu"
828 #endif
829 #ifndef PRIx64
830 #define PRIx64 "llx"
831 #endif
832 #ifndef PRIX64
833 #define PRIX64 "llX"
834 #endif
835 #ifndef SCNd64
836 #define SCNd64 "lld"
837 #endif
838 #ifndef SCNi64
839 #define SCNi64 "lli"
840 #endif
841 #ifndef SCNo64
842 #define SCNo64 "llo"
843 #endif
844 #ifndef SCNu64
845 #define SCNu64 "llu"
846 #endif
847 #ifndef SCNx64
848 #define SCNx64 "llx"
849 #endif
850 #ifndef SCNX64
851 #define SCNX64 "llX"
852 #endif
853 #endif /* long long is a 64-bit type */
854 #if ! (defined(PRId64) && defined(PRIi64) && defined(PRIo64) && \
855 defined(PRIu64) && defined(PRIx64) && defined(PRIX64))
856 #error "Don't know how to define PRI[diouxX]64"
857 #endif
858 #if ! (defined(SCNd64) && defined(SCNi64) && defined(SCNo64) && \
859 defined(SCNu64) && defined(SCNx64) && defined(SCNX64))
860 #error "Don't know how to define SCN[diouxX]64"
861 #endif
862
863 /* <limits.h> */
864
865 #ifndef UID_MAX
866 #define UID_MAX 32767
867 #endif
868 #ifndef GID_MAX
869 #define GID_MAX UID_MAX
870 #endif
871
872 #ifndef UQUAD_MAX
873 #define UQUAD_MAX ((u_quad_t)-1)
874 #endif
875 #ifndef QUAD_MAX
876 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
877 #endif
878 #ifndef QUAD_MIN
879 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
880 #endif
881 #ifndef ULLONG_MAX
882 #define ULLONG_MAX ((unsigned long long)-1)
883 #endif
884 #ifndef LLONG_MAX
885 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
886 #endif
887 #ifndef LLONG_MIN
888 #define LLONG_MIN ((long long)(~LLONG_MAX))
889 #endif
890
891 /* <paths.h> */
892
893 /* The host's _PATH_BSHELL might be broken, so override it. */
894 #undef _PATH_BSHELL
895 #define _PATH_BSHELL PATH_BSHELL
896 #ifndef _PATH_DEFPATH
897 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
898 #endif
899 #ifndef _PATH_DEV
900 #define _PATH_DEV "/dev/"
901 #endif
902 #ifndef _PATH_DEVNULL
903 #define _PATH_DEVNULL _PATH_DEV "null"
904 #endif
905 #ifndef _PATH_TMP
906 #define _PATH_TMP "/tmp/"
907 #endif
908 #ifndef _PATH_DEFTAPE
909 #define _PATH_DEFTAPE "/dev/nrst0"
910 #endif
911 #ifndef _PATH_VI
912 #define _PATH_VI "/usr/bin/vi"
913 #endif
914
915 /* <stdint.h> */
916
917 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX)
918 #define SIZE_MAX SIZE_T_MAX
919 #endif
920
921 #ifndef UINT8_MAX
922 #define UINT8_MAX 0xffU
923 #endif
924
925 #ifndef UINT16_MAX
926 #define UINT16_MAX 0xffffU
927 #endif
928
929 #ifndef UINT32_MAX
930 #define UINT32_MAX 0xffffffffU
931 #endif
932
933 /* <stdlib.h> */
934
935 #ifndef __GNUC__
936 # if HAVE_ALLOCA_H
937 # include <alloca.h>
938 # else
939 # ifndef alloca /* predefined by HP cc +Olibcalls */
940 char *alloca ();
941 # endif
942 # endif
943 #endif
944
945 /* avoid prototype conflicts with host */
946 #define cgetcap __nbcompat_cgetcap
947 #define cgetclose __nbcompat_cgetclose
948 #define cgetent __nbcompat_cgetent
949 #define cgetfirst __nbcompat_cgetfirst
950 #define cgetmatch __nbcompat_cgetmatch
951 #define cgetnext __nbcompat_cgetnext
952 #define cgetnum __nbcompat_cgetnum
953 #define cgetset __nbcompat_cgetset
954 #define cgetstr __nbcompat_cgetstr
955 #define cgetustr __nbcompat_cgetustr
956
957 char *cgetcap(char *, const char *, int);
958 int cgetclose(void);
959 int cgetent(char **, const char * const *, const char *);
960 int cgetfirst(char **, const char * const *);
961 int cgetmatch(const char *, const char *);
962 int cgetnext(char **, const char * const *);
963 int cgetnum(char *, const char *, long *);
964 int cgetset(const char *);
965 int cgetstr(char *, const char *, char **);
966 int cgetustr(char *, const char *, char **);
967
968 /* <sys/endian.h> */
969
970 #if WORDS_BIGENDIAN
971 #if !HAVE_DECL_HTOBE16
972 #define htobe16(x) (x)
973 #endif
974 #if !HAVE_DECL_HTOBE32
975 #define htobe32(x) (x)
976 #endif
977 #if !HAVE_DECL_HTOBE64
978 #define htobe64(x) (x)
979 #endif
980 #if !HAVE_DECL_HTOLE16
981 #define htole16(x) bswap16((u_int16_t)(x))
982 #endif
983 #if !HAVE_DECL_HTOLE32
984 #define htole32(x) bswap32((u_int32_t)(x))
985 #endif
986 #if !HAVE_DECL_HTOLE64
987 #define htole64(x) bswap64((u_int64_t)(x))
988 #endif
989 #else
990 #if !HAVE_DECL_HTOBE16
991 #define htobe16(x) bswap16((u_int16_t)(x))
992 #endif
993 #if !HAVE_DECL_HTOBE32
994 #define htobe32(x) bswap32((u_int32_t)(x))
995 #endif
996 #if !HAVE_DECL_HTOBE64
997 #define htobe64(x) bswap64((u_int64_t)(x))
998 #endif
999 #if !HAVE_DECL_HTOLE16
1000 #define htole16(x) (x)
1001 #endif
1002 #if !HAVE_DECL_HTOLE32
1003 #define htole32(x) (x)
1004 #endif
1005 #if !HAVE_DECL_HTOLE64
1006 #define htole64(x) (x)
1007 #endif
1008 #endif
1009 #if !HAVE_DECL_BE16TOH
1010 #define be16toh(x) htobe16(x)
1011 #endif
1012 #if !HAVE_DECL_BE32TOH
1013 #define be32toh(x) htobe32(x)
1014 #endif
1015 #if !HAVE_DECL_BE64TOH
1016 #define be64toh(x) htobe64(x)
1017 #endif
1018 #if !HAVE_DECL_LE16TOH
1019 #define le16toh(x) htole16(x)
1020 #endif
1021 #if !HAVE_DECL_LE32TOH
1022 #define le32toh(x) htole32(x)
1023 #endif
1024 #if !HAVE_DECL_LE64TOH
1025 #define le64toh(x) htole64(x)
1026 #endif
1027
1028 #define __GEN_ENDIAN_ENC(bits, endian) \
1029 static void \
1030 endian ## bits ## enc(void *dst, uint ## bits ## _t u) \
1031 { \
1032 u = hto ## endian ## bits (u); \
1033 memcpy(dst, &u, sizeof(u)); \
1034 }
1035 #if !HAVE_DECL_BE16ENC
1036 __GEN_ENDIAN_ENC(16, be)
1037 #endif
1038 #if !HAVE_DECL_BE32ENC
1039 __GEN_ENDIAN_ENC(32, be)
1040 #endif
1041 #if !HAVE_DECL_BE64ENC
1042 __GEN_ENDIAN_ENC(64, be)
1043 #endif
1044 #if !HAVE_DECL_LE16ENC
1045 __GEN_ENDIAN_ENC(16, le)
1046 #endif
1047 #if !HAVE_DECL_LE32ENC
1048 __GEN_ENDIAN_ENC(32, le)
1049 #endif
1050 #if !HAVE_DECL_LE64ENC
1051 __GEN_ENDIAN_ENC(64, le)
1052 #endif
1053 #undef __GEN_ENDIAN_ENC
1054
1055 #define __GEN_ENDIAN_DEC(bits, endian) \
1056 static uint ## bits ## _t \
1057 endian ## bits ## dec(const void *buf) \
1058 { \
1059 uint ## bits ## _t u; \
1060 memcpy(&u, buf, sizeof(u)); \
1061 return endian ## bits ## toh (u); \
1062 }
1063 #if !HAVE_DECL_BE16DEC
1064 __GEN_ENDIAN_DEC(16, be)
1065 #endif
1066 #if !HAVE_DECL_BE32DEC
1067 __GEN_ENDIAN_DEC(32, be)
1068 #endif
1069 #if !HAVE_DECL_BE64DEC
1070 __GEN_ENDIAN_DEC(64, be)
1071 #endif
1072 #if !HAVE_DECL_LE16DEC
1073 __GEN_ENDIAN_DEC(16, le)
1074 #endif
1075 #if !HAVE_DECL_LE32DEC
1076 __GEN_ENDIAN_DEC(32, le)
1077 #endif
1078 #if !HAVE_DECL_LE64DEC
1079 __GEN_ENDIAN_DEC(64, le)
1080 #endif
1081 #undef __GEN_ENDIAN_DEC
1082
1083 /* <sys/mman.h> */
1084
1085 #ifndef MAP_FILE
1086 #define MAP_FILE 0
1087 #endif
1088
1089 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
1090 #ifndef MAP_ANON
1091 #ifdef MAP_ANONYMOUS
1092 #define MAP_ANON MAP_ANONYMOUS
1093 #endif
1094 #endif
1095
1096 /* <sys/param.h> */
1097
1098 #undef BIG_ENDIAN
1099 #undef LITTLE_ENDIAN
1100 #undef PDP_ENDIAN
1101 #define BIG_ENDIAN 4321
1102 #define LITTLE_ENDIAN 1234
1103 #define PDP_ENDIAN 3412
1104
1105 #undef BYTE_ORDER
1106 #if WORDS_BIGENDIAN
1107 #define BYTE_ORDER BIG_ENDIAN
1108 #else
1109 #define BYTE_ORDER LITTLE_ENDIAN
1110 #endif
1111
1112 /* all references of DEV_BSIZE in tools are for NetBSD's file images */
1113 #undef DEV_BSIZE
1114 #define DEV_BSIZE (1 << 9)
1115
1116 #undef MIN
1117 #undef MAX
1118 #define MIN(a,b) ((a) < (b) ? (a) : (b))
1119 #define MAX(a,b) ((a) > (b) ? (a) : (b))
1120
1121 #ifndef MAXBSIZE
1122 #define MAXBSIZE (64 * 1024)
1123 #endif
1124 #ifndef MAXFRAG
1125 #define MAXFRAG 8
1126 #endif
1127 #ifndef MAXPHYS
1128 #define MAXPHYS (64 * 1024)
1129 #endif
1130
1131 /* XXX needed by makefs; this should be done in a better way */
1132 #undef btodb
1133 #define btodb(x) ((x) << 9)
1134
1135 #undef setbit
1136 #undef clrbit
1137 #undef isset
1138 #undef isclr
1139 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
1140 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
1141 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
1142 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
1143
1144 #ifndef powerof2
1145 #define powerof2(x) ((((x)-1)&(x))==0)
1146 #endif
1147
1148 #undef roundup
1149 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
1150
1151 /* <sys/stat.h> */
1152
1153 #ifndef ALLPERMS
1154 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
1155 #endif
1156 #ifndef DEFFILEMODE
1157 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
1158 #endif
1159 #ifndef S_ISTXT
1160 #ifdef S_ISVTX
1161 #define S_ISTXT S_ISVTX
1162 #else
1163 #define S_ISTXT 0
1164 #endif
1165 #endif
1166
1167 /* Protected by _NETBSD_SOURCE otherwise. */
1168 #if HAVE_STRUCT_STAT_ST_FLAGS && defined(__NetBSD__)
1169 #define UF_SETTABLE 0x0000ffff
1170 #define UF_NODUMP 0x00000001
1171 #define UF_IMMUTABLE 0x00000002
1172 #define UF_APPEND 0x00000004
1173 #define UF_OPAQUE 0x00000008
1174 #define SF_SETTABLE 0xffff0000
1175 #define SF_ARCHIVED 0x00010000
1176 #define SF_IMMUTABLE 0x00020000
1177 #define SF_APPEND 0x00040000
1178 #endif
1179
1180 /* <sys/syslimits.h> */
1181
1182 #ifndef LINE_MAX
1183 #define LINE_MAX 2048
1184 #endif
1185
1186 /* <sys/time.h> */
1187
1188 #ifndef timercmp
1189 #define timercmp(tvp, uvp, cmp) \
1190 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
1191 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1192 ((tvp)->tv_sec cmp (uvp)->tv_sec))
1193 #endif
1194 #ifndef timeradd
1195 #define timeradd(tvp, uvp, vvp) \
1196 do { \
1197 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
1198 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
1199 if ((vvp)->tv_usec >= 1000000) { \
1200 (vvp)->tv_sec++; \
1201 (vvp)->tv_usec -= 1000000; \
1202 } \
1203 } while (/* CONSTCOND */ 0)
1204 #endif
1205 #ifndef timersub
1206 #define timersub(tvp, uvp, vvp) \
1207 do { \
1208 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
1209 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
1210 if ((vvp)->tv_usec < 0) { \
1211 (vvp)->tv_sec--; \
1212 (vvp)->tv_usec += 1000000; \
1213 } \
1214 } while (/* CONSTCOND */ 0)
1215 #endif
1216
1217 /* <sys/types.h> */
1218
1219 #ifdef major
1220 #undef major
1221 #endif
1222 #define major(x) ((int32_t)((((x) & 0x000fff00) >> 8)))
1223
1224 #ifdef minor
1225 #undef minor
1226 #endif
1227 #define minor(x) ((int32_t)((((x) & 0xfff00000) >> 12) | \
1228 (((x) & 0x000000ff) >> 0)))
1229 #ifdef makedev
1230 #undef makedev
1231 #endif
1232 #define makedev(x,y) ((dev_t)((((x) << 8) & 0x000fff00) | \
1233 (((y) << 12) & 0xfff00000) | \
1234 (((y) << 0) & 0x000000ff)))
1235 #ifndef NBBY
1236 #define NBBY 8
1237 #endif
1238
1239 #if !HAVE_U_QUAD_T
1240 /* #define, not typedef, as quad_t exists as a struct on some systems */
1241 #define quad_t long long
1242 #define u_quad_t unsigned long long
1243 #define strtoq strtoll
1244 #define strtouq strtoull
1245 #endif
1246
1247 /* Has quad_t but these prototypes don't get pulled into scope. w/o we lose */
1248 #ifdef __NetBSD__
1249 quad_t strtoq(const char *, char **, int);
1250 u_quad_t strtouq(const char *, char **, int);
1251 #endif
1252
1253 #endif /* !__NETBSD_COMPAT_DEFS_H__ */
1254