compat_defs.h revision 1.67.2.2 1 /* $NetBSD: compat_defs.h,v 1.67.2.2 2010/01/07 07:42:14 snj 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> here, so that "compat_pwd.h" works. */
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 #else
105 #define __packed error: no __packed for this compiler
106 #endif
107 #endif /* !__packed */
108 #ifndef __RENAME
109 #define __RENAME(x)
110 #endif
111 #undef __aconst
112 #define __aconst
113 #undef __dead
114 #define __dead
115 #undef __restrict
116 #define __restrict
117 #undef __unused
118 #define __unused
119
120 /* Dirent support. */
121
122 #if HAVE_DIRENT_H
123 # if defined(__linux__) && defined(__USE_BSD)
124 # undef __USE_BSD
125 # include <dirent.h>
126 # define __USE_BSD 1
127 # undef d_fileno
128 # else
129 # include <dirent.h>
130 # if defined(__DARWIN_UNIX03)
131 # undef d_fileno
132 # endif
133 # endif
134 # define NAMLEN(dirent) (strlen((dirent)->d_name))
135 #else
136 # define dirent direct
137 # define NAMLEN(dirent) ((dirent)->d_namlen)
138 # if HAVE_SYS_NDIR_H
139 # include <sys/ndir.h>
140 # endif
141 # if HAVE_SYS_DIR_H
142 # include <sys/dir.h>
143 # endif
144 # if HAVE_NDIR_H
145 # include <ndir.h>
146 # endif
147 #endif
148
149 /* Type substitutes. */
150
151 #if !HAVE_ID_T
152 typedef unsigned int id_t;
153 #endif
154
155 #if !HAVE_SOCKLEN_T
156 typedef int socklen_t;
157 #endif
158
159 #if !HAVE_U_LONG
160 typedef unsigned long u_long;
161 #endif
162
163 #if !HAVE_U_CHAR
164 typedef unsigned char u_char;
165 #endif
166
167 #if !HAVE_U_INT
168 typedef unsigned int u_int;
169 #endif
170
171 #if !HAVE_U_SHORT
172 typedef unsigned short u_short;
173 #endif
174
175 /* Prototypes for replacement functions. */
176
177 #if !HAVE_ATOLL
178 long long int atoll(const char *);
179 #endif
180
181 #if !HAVE_ASPRINTF
182 int asprintf(char **, const char *, ...);
183 #endif
184
185 #if !HAVE_ASNPRINTF
186 int asnprintf(char **, size_t, const char *, ...);
187 #endif
188
189 #if !HAVE_BASENAME
190 char *basename(char *);
191 #endif
192
193 #if !HAVE_DECL_OPTIND
194 int getopt(int, char *const *, const char *);
195 extern char *optarg;
196 extern int optind, opterr, optopt;
197 #endif
198
199 #if !HAVE_DIRNAME
200 char *dirname(char *);
201 #endif
202
203 #if !HAVE_DIRFD
204 #if HAVE_DIR_DD_FD
205 #define dirfd(dirp) ((dirp)->dd_fd)
206 #elif HAVE_DIR___DD_FD
207 #define dirfd(dirp) ((dirp)->__dd_fd)
208 #else
209 /*XXX: Very hacky but no other way to bring this into scope w/o defining
210 _NETBSD_SOURCE which we're avoiding. */
211 #ifdef __NetBSD__
212 struct _dirdesc {
213 int dd_fd; /* file descriptor associated with directory */
214 long dd_loc; /* offset in current buffer */
215 long dd_size; /* amount of data returned by getdents */
216 char *dd_buf; /* data buffer */
217 int dd_len; /* size of data buffer */
218 off_t dd_seek; /* magic cookie returned by getdents */
219 long dd_rewind; /* magic cookie for rewinding */
220 int dd_flags; /* flags for readdir */
221 void *dd_lock; /* lock for concurrent access */
222 };
223 #define dirfd(dirp) (((struct _dirdesc *)dirp)->dd_fd)
224 #else
225 #error cannot figure out how to turn a DIR * into a fd
226 #endif
227 #endif
228 #endif
229
230 #if !HAVE_ERR_H
231 void err(int, const char *, ...);
232 void errx(int, const char *, ...);
233 void warn(const char *, ...);
234 void warnx(const char *, ...);
235 #endif
236
237 #if !HAVE_ESETFUNC
238 void (*esetfunc(void (*)(int, const char *, ...)))(int, const char *, ...);
239 size_t estrlcpy(char *, const char *, size_t);
240 size_t estrlcat(char *, const char *, size_t);
241 char *estrdup(const char *);
242 char *estrndup(const char *, size_t);
243 void *ecalloc(size_t, size_t);
244 void *emalloc(size_t);
245 void *erealloc(void *, size_t);
246 FILE *efopen(const char *, const char *);
247 int easprintf(char **, const char *, ...);
248 int evasprintf(char **, const char *, va_list);
249 #endif
250
251 #if !HAVE_FGETLN || defined(__NetBSD__)
252 char *fgetln(FILE *, size_t *);
253 #endif
254
255 #if !HAVE_FLOCK
256 # define LOCK_SH 0x01
257 # define LOCK_EX 0x02
258 # define LOCK_NB 0x04
259 # define LOCK_UN 0x08
260 int flock(int, int);
261 #endif
262
263 #if !HAVE_FPARSELN || BROKEN_FPARSELN || defined(__NetBSD__)
264 # define FPARSELN_UNESCESC 0x01
265 # define FPARSELN_UNESCCONT 0x02
266 # define FPARSELN_UNESCCOMM 0x04
267 # define FPARSELN_UNESCREST 0x08
268 # define FPARSELN_UNESCALL 0x0f
269 char *fparseln(FILE *, size_t *, size_t *, const char [3], int);
270 #endif
271
272 #if !HAVE_ISSETUGID
273 int issetugid(void);
274 #endif
275
276 #if !HAVE_ISBLANK && !defined(isblank)
277 #define isblank(x) ((x) == ' ' || (x) == '\t')
278 #endif
279
280 #define __nbcompat_bswap16(x) ((((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff))
281
282 #define __nbcompat_bswap32(x) ((((x) << 24) & 0xff000000) | \
283 (((x) << 8) & 0x00ff0000) | \
284 (((x) >> 8) & 0x0000ff00) | \
285 (((x) >> 24) & 0x000000ff))
286
287 #define __nbcompat_bswap64(x) (((u_int64_t)bswap32((x)) << 32) | \
288 ((u_int64_t)bswap32((x) >> 32)))
289
290 #if ! HAVE_DECL_BSWAP16
291 #ifdef bswap16
292 #undef bswap16
293 #endif
294 #define bswap16(x) __nbcompat_bswap16(x)
295 #endif
296 #if ! HAVE_DECL_BSWAP32
297 #ifdef bswap32
298 #undef bswap32
299 #endif
300 #define bswap32(x) __nbcompat_bswap32(x)
301 #endif
302 #if ! HAVE_DECL_BSWAP64
303 #ifdef bswap64
304 #undef bswap64
305 #endif
306 #define bswap64(x) __nbcompat_bswap64(x)
307 #endif
308
309 #if !HAVE_MKSTEMP
310 int mkstemp(char *);
311 #endif
312
313 #if !HAVE_MKDTEMP
314 char *mkdtemp(char *);
315 #endif
316
317 #if !HAVE_MKSTEMP || !HAVE_MKDTEMP
318 /* This is a prototype for the internal function defined in
319 * src/lib/lib/stdio/gettemp.c */
320 int __nbcompat_gettemp(char *, int *, int);
321 #endif
322
323 #if !HAVE_PREAD
324 ssize_t pread(int, void *, size_t, off_t);
325 #endif
326
327 #if !HAVE_HEAPSORT
328 int heapsort (void *, size_t, size_t, int (*)(const void *, const void *));
329 #endif
330 /* Make them use our version */
331 # define heapsort __nbcompat_heapsort
332
333 /*
334 * HAVE_X_FROM_Y and HAVE_PWCACHE_FOODB go together, because we cannot
335 * supply an implementation of one without the others -- some parts are
336 * libc internal and this varies from system to system.
337 *
338 * XXX this is dubious anyway: we assume (see HAVE_DECLs below) that if the
339 * XXX host system has all of these functions, all of their interfaces
340 * XXX and interactions are exactly the same as in our libc/libutil -- ugh.
341 */
342 #if !HAVE_USER_FROM_UID || !HAVE_UID_FROM_USER || !HAVE_GROUP_FROM_GID || \
343 !HAVE_GID_FROM_GROUP || !HAVE_PWCACHE_USERDB || !HAVE_PWCACHE_GROUDB
344 /* Make them use our version */
345 # define user_from_uid __nbcompat_user_from_uid
346 # define uid_from_user __nbcompat_uid_from_user
347 # define pwcache_userdb __nbcompat_pwcache_userdb
348 # define group_from_gid __nbcompat_group_from_gid
349 # define gid_from_group __nbcompat_gid_from_group
350 # define pwcache_groupdb __nbcompat_pwcache_groupdb
351 #endif
352
353 #if !HAVE_DECL_UID_FROM_USER
354 int uid_from_user(const char *, uid_t *);
355 #endif
356 #if !HAVE_DECL_USER_FROM_UID
357 const char *user_from_uid(uid_t, int);
358 #endif
359 #if !HAVE_DECL_PWCACHE_USERDB
360 int pwcache_userdb(int (*)(int), void (*)(void),
361 struct passwd * (*)(const char *), struct passwd * (*)(uid_t));
362 #endif
363 #if !HAVE_DECL_GID_FROM_GROUP
364 int gid_from_group(const char *, gid_t *);
365 #endif
366 #if !HAVE_DECL_GROUP_FROM_GID
367 const char *group_from_gid(gid_t, int);
368 #endif
369 #if !HAVE_DECL_PWCACHE_GROUPDB
370 int pwcache_groupdb(int (*)(int), void (*)(void),
371 struct group * (*)(const char *), struct group * (*)(gid_t));
372 #endif
373
374 #if !HAVE_DECL_STRNDUP
375 char *strndup(const char *, size_t);
376 #endif
377 #if !HAVE_DECL_LCHFLAGS
378 int lchflags(const char *, unsigned long);
379 #endif
380 #if !HAVE_DECL_LCHMOD
381 int lchmod(const char *, mode_t);
382 #endif
383 #if !HAVE_DECL_LCHOWN
384 int lchown(const char *, uid_t, gid_t);
385 #endif
386
387 #if !HAVE_PWRITE
388 ssize_t pwrite(int, const void *, size_t, off_t);
389 #endif
390
391 #if !HAVE_RAISE_DEFAULT_SIGNAL
392 int raise_default_signal(int);
393 #endif
394
395 #if !HAVE_SETENV
396 int setenv(const char *, const char *, int);
397 #endif
398
399 #if !HAVE_DECL_SETGROUPENT
400 int setgroupent(int);
401 #endif
402
403 #if !HAVE_DECL_SETPASSENT
404 int setpassent(int);
405 #endif
406
407 #if !HAVE_SETPROGNAME || defined(__NetBSD__)
408 const char *getprogname(void);
409 void setprogname(const char *);
410 #endif
411
412 #if !HAVE_SNPRINTF
413 int snprintf(char *, size_t, const char *, ...);
414 #endif
415
416 #if !HAVE_STRLCAT
417 size_t strlcat(char *, const char *, size_t);
418 #endif
419
420 #if !HAVE_STRLCPY
421 size_t strlcpy(char *, const char *, size_t);
422 #endif
423
424 #if !HAVE_STRMODE
425 void strmode(mode_t, char *);
426 #endif
427
428 #if !HAVE_STRNDUP
429 char *strndup(const char *, size_t);
430 #endif
431
432 #if !HAVE_STRSEP || defined(__NetBSD__)
433 char *strsep(char **, const char *);
434 #endif
435
436 #if !HAVE_DECL_STRSUFTOLL
437 long long strsuftoll(const char *, const char *, long long, long long);
438 long long strsuftollx(const char *, const char *,
439 long long, long long, char *, size_t);
440 #endif
441
442 #if !HAVE_STRTOLL
443 long long strtoll(const char *, char **, int);
444 #endif
445
446 #if !HAVE_USER_FROM_UID
447 const char *user_from_uid(uid_t, int);
448 #endif
449
450 #if !HAVE_GROUP_FROM_GID
451 const char *group_from_gid(gid_t, int);
452 #endif
453
454 #if !HAVE_VASPRINTF
455 int vasprintf(char **, const char *, va_list);
456 #endif
457
458 #if !HAVE_VASNPRINTF
459 int vasnprintf(char **, size_t, const char *, va_list);
460 #endif
461
462 #if !HAVE_VSNPRINTF
463 int vsnprintf(char *, size_t, const char *, va_list);
464 #endif
465
466 /*
467 * getmode() and setmode() are always defined, as these function names
468 * exist but with very different meanings on other OS's. The compat
469 * versions here simply accept an octal mode number; the "u+x,g-w" type
470 * of syntax is not accepted.
471 */
472
473 #define getmode __nbcompat_getmode
474 #define setmode __nbcompat_setmode
475
476 mode_t getmode(const void *, mode_t);
477 void *setmode(const char *);
478
479 /* Eliminate assertions embedded in binaries. */
480
481 #undef _DIAGASSERT
482 #define _DIAGASSERT(x)
483
484 /* Various sources use this */
485 #undef __RCSID
486 #define __RCSID(x)
487 #undef __SCCSID
488 #define __SCCSID(x)
489 #undef __COPYRIGHT
490 #define __COPYRIGHT(x)
491 #undef __KERNEL_RCSID
492 #define __KERNEL_RCSID(x,y)
493
494 /* Heimdal expects this one. */
495
496 #undef RCSID
497 #define RCSID(x)
498
499 /* Some definitions not available on all systems. */
500
501 /* <errno.h> */
502
503 #ifndef EFTYPE
504 #define EFTYPE EIO
505 #endif
506
507 /* <fcntl.h> */
508
509 #ifndef O_EXLOCK
510 #define O_EXLOCK 0
511 #endif
512 #ifndef O_SHLOCK
513 #define O_SHLOCK 0
514 #endif
515
516 /* <limits.h> */
517
518 #ifndef UID_MAX
519 #define UID_MAX 32767
520 #endif
521 #ifndef GID_MAX
522 #define GID_MAX UID_MAX
523 #endif
524
525 #ifndef UQUAD_MAX
526 #define UQUAD_MAX ((u_quad_t)-1)
527 #endif
528 #ifndef QUAD_MAX
529 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
530 #endif
531 #ifndef QUAD_MIN
532 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
533 #endif
534 #ifndef ULLONG_MAX
535 #define ULLONG_MAX ((unsigned long long)-1)
536 #endif
537 #ifndef LLONG_MAX
538 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
539 #endif
540 #ifndef LLONG_MIN
541 #define LLONG_MIN ((long long)(~LLONG_MAX))
542 #endif
543
544 /* <paths.h> */
545
546 /* The host's _PATH_BSHELL might be broken, so override it. */
547 #undef _PATH_BSHELL
548 #define _PATH_BSHELL PATH_BSHELL
549 #ifndef _PATH_DEFPATH
550 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
551 #endif
552 #ifndef _PATH_DEV
553 #define _PATH_DEV "/dev/"
554 #endif
555 #ifndef _PATH_DEVNULL
556 #define _PATH_DEVNULL _PATH_DEV "null"
557 #endif
558 #ifndef _PATH_TMP
559 #define _PATH_TMP "/tmp/"
560 #endif
561 #ifndef _PATH_DEFTAPE
562 #define _PATH_DEFTAPE "/dev/nrst0"
563 #endif
564 #ifndef _PATH_VI
565 #define _PATH_VI "/usr/bin/vi"
566 #endif
567
568 /* <stdarg.h> */
569
570 #ifndef _BSD_VA_LIST_
571 #define _BSD_VA_LIST_ va_list
572 #endif
573
574 /* <stdint.h> */
575
576 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX)
577 #define SIZE_MAX SIZE_T_MAX
578 #endif
579
580 #ifndef UINT8_MAX
581 #define UINT8_MAX 0xffU
582 #endif
583
584 #ifndef UINT16_MAX
585 #define UINT16_MAX 0xffffU
586 #endif
587
588 #ifndef UINT32_MAX
589 #define UINT32_MAX 0xffffffffU
590 #endif
591
592 /* <stdlib.h> */
593
594 #ifndef __GNUC__
595 # if HAVE_ALLOCA_H
596 # include <alloca.h>
597 # else
598 # ifndef alloca /* predefined by HP cc +Olibcalls */
599 char *alloca ();
600 # endif
601 # endif
602 #endif
603
604 /* avoid prototype conflicts with host */
605 #define cgetcap __nbcompat_cgetcap
606 #define cgetclose __nbcompat_cgetclose
607 #define cgetent __nbcompat_cgetent
608 #define cgetfirst __nbcompat_cgetfirst
609 #define cgetmatch __nbcompat_cgetmatch
610 #define cgetnext __nbcompat_cgetnext
611 #define cgetnum __nbcompat_cgetnum
612 #define cgetset __nbcompat_cgetset
613 #define cgetstr __nbcompat_cgetstr
614 #define cgetustr __nbcompat_cgetustr
615
616 char *cgetcap(char *, const char *, int);
617 int cgetclose(void);
618 int cgetent(char **, const char * const *, const char *);
619 int cgetfirst(char **, const char * const *);
620 int cgetmatch(const char *, const char *);
621 int cgetnext(char **, const char * const *);
622 int cgetnum(char *, const char *, long *);
623 int cgetset(const char *);
624 int cgetstr(char *, const char *, char **);
625 int cgetustr(char *, const char *, char **);
626
627 /* <sys/endian.h> */
628
629 #if WORDS_BIGENDIAN
630 #if !HAVE_DECL_HTOBE16
631 #define htobe16(x) (x)
632 #endif
633 #if !HAVE_DECL_HTOBE32
634 #define htobe32(x) (x)
635 #endif
636 #if !HAVE_DECL_HTOBE64
637 #define htobe64(x) (x)
638 #endif
639 #if !HAVE_DECL_HTOLE16
640 #define htole16(x) bswap16((u_int16_t)(x))
641 #endif
642 #if !HAVE_DECL_HTOLE32
643 #define htole32(x) bswap32((u_int32_t)(x))
644 #endif
645 #if !HAVE_DECL_HTOLE64
646 #define htole64(x) bswap64((u_int64_t)(x))
647 #endif
648 #else
649 #if !HAVE_DECL_HTOBE16
650 #define htobe16(x) bswap16((u_int16_t)(x))
651 #endif
652 #if !HAVE_DECL_HTOBE32
653 #define htobe32(x) bswap32((u_int32_t)(x))
654 #endif
655 #if !HAVE_DECL_HTOBE64
656 #define htobe64(x) bswap64((u_int64_t)(x))
657 #endif
658 #if !HAVE_DECL_HTOLE16
659 #define htole16(x) (x)
660 #endif
661 #if !HAVE_DECL_HTOLE32
662 #define htole32(x) (x)
663 #endif
664 #if !HAVE_DECL_HTOLE64
665 #define htole64(x) (x)
666 #endif
667 #endif
668 #if !HAVE_DECL_BE16TOH
669 #define be16toh(x) htobe16(x)
670 #endif
671 #if !HAVE_DECL_BE32TOH
672 #define be32toh(x) htobe32(x)
673 #endif
674 #if !HAVE_DECL_BE64TOH
675 #define be64toh(x) htobe64(x)
676 #endif
677 #if !HAVE_DECL_LE16TOH
678 #define le16toh(x) htole16(x)
679 #endif
680 #if !HAVE_DECL_LE32TOH
681 #define le32toh(x) htole32(x)
682 #endif
683 #if !HAVE_DECL_LE64TOH
684 #define le64toh(x) htole64(x)
685 #endif
686
687 #define __GEN_ENDIAN_ENC(bits, endian) \
688 static void \
689 endian ## bits ## enc(void *dst, uint ## bits ## _t u) \
690 { \
691 u = hto ## endian ## bits (u); \
692 memcpy(dst, &u, sizeof(u)); \
693 }
694 #if !HAVE_DECL_BE16ENC
695 __GEN_ENDIAN_ENC(16, be)
696 #endif
697 #if !HAVE_DECL_BE32ENC
698 __GEN_ENDIAN_ENC(32, be)
699 #endif
700 #if !HAVE_DECL_BE64ENC
701 __GEN_ENDIAN_ENC(64, be)
702 #endif
703 #if !HAVE_DECL_LE16ENC
704 __GEN_ENDIAN_ENC(16, le)
705 #endif
706 #if !HAVE_DECL_LE32ENC
707 __GEN_ENDIAN_ENC(32, le)
708 #endif
709 #if !HAVE_DECL_LE64ENC
710 __GEN_ENDIAN_ENC(64, le)
711 #endif
712 #undef __GEN_ENDIAN_ENC
713
714 #define __GEN_ENDIAN_DEC(bits, endian) \
715 static uint ## bits ## _t \
716 endian ## bits ## dec(const void *buf) \
717 { \
718 uint ## bits ## _t u; \
719 memcpy(&u, buf, sizeof(u)); \
720 return endian ## bits ## toh (u); \
721 }
722 #if !HAVE_DECL_BE16DEC
723 __GEN_ENDIAN_DEC(16, be)
724 #endif
725 #if !HAVE_DECL_BE32DEC
726 __GEN_ENDIAN_DEC(32, be)
727 #endif
728 #if !HAVE_DECL_BE64DEC
729 __GEN_ENDIAN_DEC(64, be)
730 #endif
731 #if !HAVE_DECL_LE16DEC
732 __GEN_ENDIAN_DEC(16, le)
733 #endif
734 #if !HAVE_DECL_LE32DEC
735 __GEN_ENDIAN_DEC(32, le)
736 #endif
737 #if !HAVE_DECL_LE64DEC
738 __GEN_ENDIAN_DEC(64, le)
739 #endif
740 #undef __GEN_ENDIAN_DEC
741
742 /* <sys/mman.h> */
743
744 #ifndef MAP_FILE
745 #define MAP_FILE 0
746 #endif
747
748 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
749 #ifndef MAP_ANON
750 #ifdef MAP_ANONYMOUS
751 #define MAP_ANON MAP_ANONYMOUS
752 #endif
753 #endif
754
755 /* <sys/param.h> */
756
757 #undef BIG_ENDIAN
758 #undef LITTLE_ENDIAN
759 #define BIG_ENDIAN 4321
760 #define LITTLE_ENDIAN 1234
761
762 #undef BYTE_ORDER
763 #if WORDS_BIGENDIAN
764 #define BYTE_ORDER BIG_ENDIAN
765 #else
766 #define BYTE_ORDER LITTLE_ENDIAN
767 #endif
768
769 #ifndef DEV_BSIZE
770 #define DEV_BSIZE (1 << 9)
771 #endif
772
773 #undef MIN
774 #undef MAX
775 #define MIN(a,b) ((a) < (b) ? (a) : (b))
776 #define MAX(a,b) ((a) > (b) ? (a) : (b))
777
778 #ifndef MAXBSIZE
779 #define MAXBSIZE (64 * 1024)
780 #endif
781 #ifndef MAXFRAG
782 #define MAXFRAG 8
783 #endif
784 #ifndef MAXPHYS
785 #define MAXPHYS (64 * 1024)
786 #endif
787
788 /* XXX needed by makefs; this should be done in a better way */
789 #undef btodb
790 #define btodb(x) ((x) << 9)
791
792 #undef setbit
793 #undef clrbit
794 #undef isset
795 #undef isclr
796 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
797 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
798 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
799 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
800
801 #ifndef powerof2
802 #define powerof2(x) ((((x)-1)&(x))==0)
803 #endif
804
805 #undef roundup
806 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
807
808 /* <sys/stat.h> */
809
810 #ifndef ALLPERMS
811 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
812 #endif
813 #ifndef DEFFILEMODE
814 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
815 #endif
816 #ifndef S_ISTXT
817 #ifdef S_ISVTX
818 #define S_ISTXT S_ISVTX
819 #else
820 #define S_ISTXT 0
821 #endif
822 #endif
823
824 /* Protected by _NETBSD_SOURCE otherwise. */
825 #if HAVE_STRUCT_STAT_ST_FLAGS && defined(__NetBSD__)
826 #define UF_SETTABLE 0x0000ffff
827 #define UF_NODUMP 0x00000001
828 #define UF_IMMUTABLE 0x00000002
829 #define UF_APPEND 0x00000004
830 #define UF_OPAQUE 0x00000008
831 #define SF_SETTABLE 0xffff0000
832 #define SF_ARCHIVED 0x00010000
833 #define SF_IMMUTABLE 0x00020000
834 #define SF_APPEND 0x00040000
835 #endif
836
837 /* <sys/syslimits.h> */
838
839 #ifndef LINE_MAX
840 #define LINE_MAX 2048
841 #endif
842
843 /* <sys/time.h> */
844
845 #ifndef timercmp
846 #define timercmp(tvp, uvp, cmp) \
847 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
848 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
849 ((tvp)->tv_sec cmp (uvp)->tv_sec))
850 #endif
851 #ifndef timeradd
852 #define timeradd(tvp, uvp, vvp) \
853 do { \
854 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
855 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
856 if ((vvp)->tv_usec >= 1000000) { \
857 (vvp)->tv_sec++; \
858 (vvp)->tv_usec -= 1000000; \
859 } \
860 } while (/* CONSTCOND */ 0)
861 #endif
862 #ifndef timersub
863 #define timersub(tvp, uvp, vvp) \
864 do { \
865 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
866 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
867 if ((vvp)->tv_usec < 0) { \
868 (vvp)->tv_sec--; \
869 (vvp)->tv_usec += 1000000; \
870 } \
871 } while (/* CONSTCOND */ 0)
872 #endif
873
874 /* <sys/types.h> */
875
876 #ifdef major
877 #undef major
878 #endif
879 #define major(x) ((int32_t)((((x) & 0x000fff00) >> 8)))
880
881 #ifdef minor
882 #undef minor
883 #endif
884 #define minor(x) ((int32_t)((((x) & 0xfff00000) >> 12) | \
885 (((x) & 0x000000ff) >> 0)))
886 #ifdef makedev
887 #undef makedev
888 #endif
889 #define makedev(x,y) ((dev_t)((((x) << 8) & 0x000fff00) | \
890 (((y) << 12) & 0xfff00000) | \
891 (((y) << 0) & 0x000000ff)))
892 #ifndef NBBY
893 #define NBBY 8
894 #endif
895
896 #if !HAVE_U_QUAD_T
897 /* #define, not typedef, as quad_t exists as a struct on some systems */
898 #define quad_t long long
899 #define u_quad_t unsigned long long
900 #define strtoq strtoll
901 #define strtouq strtoull
902 #endif
903
904 /* Has quad_t but these prototypes don't get pulled into scope. w/o we lose */
905 #ifdef __NetBSD__
906 quad_t strtoq __P((const char *, char **, int));
907 u_quad_t strtouq __P((const char *, char **, int));
908 #endif
909
910 #endif /* !__NETBSD_COMPAT_DEFS_H__ */
911