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