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