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