1 /* $NetBSD: compat_defs.h,v 1.74 2009/11/06 18:26:06 joerg Exp $ */
3 #ifndef __NETBSD_COMPAT_DEFS_H__
4 #define __NETBSD_COMPAT_DEFS_H__
7 /* Work around some complete brain damage. */
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.
14 #if defined(__linux__) && HAVE_FEATURES_H
16 #define __USE_ISOC99 1
19 /* So _NETBSD_SOURCE doesn't end up defined. Define enough to pull in standard
20 defs. Other platforms may need similiar defines. */
22 #define _ISOC99_SOURCE
23 #define _POSIX_SOURCE 1
24 #define _POSIX_C_SOURCE 200112L
25 #define _XOPEN_SOURCE 600
28 #undef _POSIX_C_SOURCE
31 /* System headers needed for (re)definitions below. */
33 #include <sys/types.h>
35 #include <sys/param.h>
36 /* time.h needs to be pulled in first at least on netbsd w/o _NETBSD_SOURCE */
49 #include <sys/cdefs.h>
51 #if HAVE_SYS_SYSLIMITS_H
52 #include <sys/syslimits.h>
54 #if HAVE_SYS_SYSMACROS_H
55 /* major(), minor() on SVR4 */
56 #include <sys/sysmacros.h>
66 #error _NETBSD_SOURCE is *not* to be defined.
69 /* Need this since we can't depend on NetBSD's version to be around */
73 #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
75 /* We don't include <pwd.h> here, so that "compat_pwd.h" works. */
78 /* We don't include <grp.h> either */
81 /* Assume an ANSI compiler for the host. */
93 /* Some things usually in BSD <sys/cdefs.h>. */
96 #define __CONCAT(x,y) x ## y
98 #if !defined(__attribute__) && !defined(__GNUC__)
99 #define __attribute__(x)
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__))
107 #define __packed error: no __packed for this compiler
109 #endif /* !__packed */
122 /* Dirent support. */
125 # if defined(__linux__) && defined(__USE_BSD)
132 # if defined(__DARWIN_UNIX03)
136 # define NAMLEN(dirent) (strlen((dirent)->d_name))
138 # define dirent direct
139 # define NAMLEN(dirent) ((dirent)->d_namlen)
141 # include <sys/ndir.h>
144 # include <sys/dir.h>
151 /* Type substitutes. */
154 typedef unsigned int id_t
;
158 typedef int socklen_t
;
162 typedef unsigned long u_long
;
166 typedef unsigned char u_char
;
170 typedef unsigned int u_int
;
174 typedef unsigned short u_short
;
177 /* Prototypes for replacement functions. */
180 long long int atoll(const char *);
184 int asprintf(char **, const char *, ...);
188 int asnprintf(char **, size_t, const char *, ...);
192 char *basename(char *);
195 #if !HAVE_DECL_OPTIND
196 int getopt(int, char *const *, const char *);
198 extern int optind
, opterr
, optopt
;
202 char *dirname(char *);
207 #define dirfd(dirp) ((dirp)->dd_fd)
208 #elif HAVE_DIR___DD_FD
209 #define dirfd(dirp) ((dirp)->__dd_fd)
211 /*XXX: Very hacky but no other way to bring this into scope w/o defining
212 _NETBSD_SOURCE which we're avoiding. */
215 int dd_fd
; /* file descriptor associated with directory */
216 long dd_loc
; /* offset in current buffer */
217 long dd_size
; /* amount of data returned by getdents */
218 char *dd_buf
; /* data buffer */
219 int dd_len
; /* size of data buffer */
220 off_t dd_seek
; /* magic cookie returned by getdents */
221 long dd_rewind
; /* magic cookie for rewinding */
222 int dd_flags
; /* flags for readdir */
223 void *dd_lock
; /* lock for concurrent access */
225 #define dirfd(dirp) (((struct _dirdesc *)dirp)->dd_fd)
227 #error cannot figure out how to turn a DIR * into a fd
233 void err(int, const char *, ...);
234 void errx(int, const char *, ...);
235 void warn(const char *, ...);
236 void warnx(const char *, ...);
237 void vwarnx(const char *, va_list);
241 void (*esetfunc(void (*)(int, const char *, ...)))(int, const char *, ...);
242 size_t estrlcpy(char *, const char *, size_t);
243 size_t estrlcat(char *, const char *, size_t);
244 char *estrdup(const char *);
245 char *estrndup(const char *, size_t);
246 void *ecalloc(size_t, size_t);
247 void *emalloc(size_t);
248 void *erealloc(void *, size_t);
249 FILE *efopen(const char *, const char *);
250 int easprintf(char **, const char *, ...);
251 int evasprintf(char **, const char *, va_list);
254 #if !HAVE_FGETLN || defined(__NetBSD__)
255 char *fgetln(FILE *, size_t *);
259 # define LOCK_SH 0x01
260 # define LOCK_EX 0x02
261 # define LOCK_NB 0x04
262 # define LOCK_UN 0x08
266 #if !HAVE_FPARSELN || BROKEN_FPARSELN || defined(__NetBSD__)
267 # define FPARSELN_UNESCESC 0x01
268 # define FPARSELN_UNESCCONT 0x02
269 # define FPARSELN_UNESCCOMM 0x04
270 # define FPARSELN_UNESCREST 0x08
271 # define FPARSELN_UNESCALL 0x0f
272 char *fparseln(FILE *, size_t *, size_t *, const char [3], int);
279 #if !HAVE_ISBLANK && !defined(isblank)
280 #define isblank(x) ((x) == ' ' || (x) == '\t')
283 #define __nbcompat_bswap16(x) ((((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff))
285 #define __nbcompat_bswap32(x) ((((x) << 24) & 0xff000000) | \
286 (((x) << 8) & 0x00ff0000) | \
287 (((x) >> 8) & 0x0000ff00) | \
288 (((x) >> 24) & 0x000000ff))
290 #define __nbcompat_bswap64(x) (((u_int64_t)bswap32((x)) << 32) | \
291 ((u_int64_t)bswap32((x) >> 32)))
293 #if ! HAVE_DECL_BSWAP16
297 #define bswap16(x) __nbcompat_bswap16(x)
299 #if ! HAVE_DECL_BSWAP32
303 #define bswap32(x) __nbcompat_bswap32(x)
305 #if ! HAVE_DECL_BSWAP64
309 #define bswap64(x) __nbcompat_bswap64(x)
317 char *mkdtemp(char *);
320 #if !HAVE_MKSTEMP || !HAVE_MKDTEMP
321 /* This is a prototype for the internal function defined in
322 * src/lib/lib/stdio/gettemp.c */
323 int __nbcompat_gettemp(char *, int *, int);
327 ssize_t
pread(int, void *, size_t, off_t
);
331 int heapsort (void *, size_t, size_t, int (*)(const void *, const void *));
333 /* Make them use our version */
334 # define heapsort __nbcompat_heapsort
336 char *flags_to_string(unsigned long, const char *);
337 int string_to_flags(char **, unsigned long *, unsigned long *);
340 * HAVE_X_FROM_Y and HAVE_PWCACHE_FOODB go together, because we cannot
341 * supply an implementation of one without the others -- some parts are
342 * libc internal and this varies from system to system.
344 * XXX this is dubious anyway: we assume (see HAVE_DECLs below) that if the
345 * XXX host system has all of these functions, all of their interfaces
346 * XXX and interactions are exactly the same as in our libc/libutil -- ugh.
348 #if !HAVE_USER_FROM_UID || !HAVE_UID_FROM_USER || !HAVE_GROUP_FROM_GID || \
349 !HAVE_GID_FROM_GROUP || !HAVE_PWCACHE_USERDB || !HAVE_PWCACHE_GROUDB
350 /* Make them use our version */
351 # define user_from_uid __nbcompat_user_from_uid
352 # define uid_from_user __nbcompat_uid_from_user
353 # define pwcache_userdb __nbcompat_pwcache_userdb
354 # define group_from_gid __nbcompat_group_from_gid
355 # define gid_from_group __nbcompat_gid_from_group
356 # define pwcache_groupdb __nbcompat_pwcache_groupdb
359 #if !HAVE_DECL_UID_FROM_USER
360 int uid_from_user(const char *, uid_t
*);
363 #if !HAVE_DECL_USER_FROM_UID
364 const char *user_from_uid(uid_t
, int);
367 #if !HAVE_DECL_PWCACHE_USERDB
368 int pwcache_userdb(int (*)(int), void (*)(void),
369 struct passwd
* (*)(const char *), struct passwd
* (*)(uid_t
));
372 #if !HAVE_DECL_GID_FROM_GROUP
373 int gid_from_group(const char *, gid_t
*);
376 #if !HAVE_DECL_GROUP_FROM_GID
377 const char *group_from_gid(gid_t
, int);
380 #if !HAVE_DECL_PWCACHE_GROUPDB
381 int pwcache_groupdb(int (*)(int), void (*)(void),
382 struct group
* (*)(const char *), struct group
* (*)(gid_t
));
385 #if !HAVE_DECL_STRNDUP
386 char *strndup(const char *, size_t);
388 #if !HAVE_DECL_LCHFLAGS
389 int lchflags(const char *, unsigned long);
391 #if !HAVE_DECL_LCHMOD
392 int lchmod(const char *, mode_t
);
394 #if !HAVE_DECL_LCHOWN
395 int lchown(const char *, uid_t
, gid_t
);
399 ssize_t
pwrite(int, const void *, size_t, off_t
);
402 #if !HAVE_RAISE_DEFAULT_SIGNAL
403 int raise_default_signal(int);
407 int setenv(const char *, const char *, int);
410 #if !HAVE_DECL_SETGROUPENT
411 int setgroupent(int);
414 #if !HAVE_DECL_SETPASSENT
418 #if !HAVE_SETPROGNAME || defined(__NetBSD__)
419 const char *getprogname(void);
420 void setprogname(const char *);
424 int snprintf(char *, size_t, const char *, ...);
428 size_t strlcat(char *, const char *, size_t);
432 size_t strlcpy(char *, const char *, size_t);
436 void strmode(mode_t
, char *);
440 char *strndup(const char *, size_t);
443 #if !HAVE_STRSEP || defined(__NetBSD__)
444 char *strsep(char **, const char *);
447 #if !HAVE_DECL_STRSUFTOLL
448 long long strsuftoll(const char *, const char *, long long, long long);
449 long long strsuftollx(const char *, const char *,
450 long long, long long, char *, size_t);
454 long long strtoll(const char *, char **, int);
457 #if !HAVE_USER_FROM_UID
458 const char *user_from_uid(uid_t
, int);
461 #if !HAVE_GROUP_FROM_GID
462 const char *group_from_gid(gid_t
, int);
466 int vasprintf(char **, const char *, va_list);
470 int vasnprintf(char **, size_t, const char *, va_list);
474 int vsnprintf(char *, size_t, const char *, va_list);
478 * getmode() and setmode() are always defined, as these function names
479 * exist but with very different meanings on other OS's. The compat
480 * versions here simply accept an octal mode number; the "u+x,g-w" type
481 * of syntax is not accepted.
484 #define getmode __nbcompat_getmode
485 #define setmode __nbcompat_setmode
487 mode_t
getmode(const void *, mode_t
);
488 void *setmode(const char *);
490 /* Eliminate assertions embedded in binaries. */
493 #define _DIAGASSERT(x)
495 /* Various sources use this */
497 #define __RCSID(x) struct XXXNETBSD_RCSID
501 #define __COPYRIGHT(x) struct XXXNETBSD_COPYRIGHT
502 #undef __KERNEL_RCSID
503 #define __KERNEL_RCSID(x,y)
505 /* Heimdal expects this one. */
510 /* Some definitions not available on all systems. */
513 #define __inline inline
533 #if USHRT_MAX == 0xffffU /* short is a 16-bit type */
552 #endif /* short is a 16-bit type */
553 #if ! (defined(PRId16) && defined(PRIi16) && defined(PRIo16) && \
554 defined(PRIu16) && defined(PRIx16) && defined(PRIX16))
555 #error "Don't know how to define PRI[diouxX]16"
558 #if UINT_MAX == 0xffffffffU /* int is a 32-bit type */
577 #endif /* int is a 32-bit type */
578 #if ULONG_MAX == 0xffffffffU /* long is a 32-bit type */
597 #endif /* long is a 32-bit type */
598 #if ! (defined(PRId32) && defined(PRIi32) && defined(PRIo32) && \
599 defined(PRIu32) && defined(PRIx32) && defined(PRIX32))
600 #error "Don't know how to define PRI[diouxX]32"
603 #if ULONG_MAX == 0xffffffffffffffffU /* long is a 64-bit type */
622 #endif /* long is a 64-bit type */
623 #if ULLONG_MAX == 0xffffffffffffffffU /* long long is a 64-bit type */
642 #endif /* long long is a 64-bit type */
643 #if ! (defined(PRId64) && defined(PRIi64) && defined(PRIo64) && \
644 defined(PRIu64) && defined(PRIx64) && defined(PRIX64))
645 #error "Don't know how to define PRI[diouxX]64"
651 #define UID_MAX 32767
654 #define GID_MAX UID_MAX
658 #define UQUAD_MAX ((u_quad_t)-1)
661 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
664 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
667 #define ULLONG_MAX ((unsigned long long)-1)
670 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
673 #define LLONG_MIN ((long long)(~LLONG_MAX))
678 /* The host's _PATH_BSHELL might be broken, so override it. */
680 #define _PATH_BSHELL PATH_BSHELL
681 #ifndef _PATH_DEFPATH
682 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
685 #define _PATH_DEV "/dev/"
687 #ifndef _PATH_DEVNULL
688 #define _PATH_DEVNULL _PATH_DEV "null"
691 #define _PATH_TMP "/tmp/"
693 #ifndef _PATH_DEFTAPE
694 #define _PATH_DEFTAPE "/dev/nrst0"
697 #define _PATH_VI "/usr/bin/vi"
702 #ifndef _BSD_VA_LIST_
703 #define _BSD_VA_LIST_ va_list
708 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX)
709 #define SIZE_MAX SIZE_T_MAX
713 #define UINT8_MAX 0xffU
717 #define UINT16_MAX 0xffffU
721 #define UINT32_MAX 0xffffffffU
730 # ifndef alloca /* predefined by HP cc +Olibcalls */
736 /* avoid prototype conflicts with host */
737 #define cgetcap __nbcompat_cgetcap
738 #define cgetclose __nbcompat_cgetclose
739 #define cgetent __nbcompat_cgetent
740 #define cgetfirst __nbcompat_cgetfirst
741 #define cgetmatch __nbcompat_cgetmatch
742 #define cgetnext __nbcompat_cgetnext
743 #define cgetnum __nbcompat_cgetnum
744 #define cgetset __nbcompat_cgetset
745 #define cgetstr __nbcompat_cgetstr
746 #define cgetustr __nbcompat_cgetustr
748 char *cgetcap(char *, const char *, int);
750 int cgetent(char **, const char * const *, const char *);
751 int cgetfirst(char **, const char * const *);
752 int cgetmatch(const char *, const char *);
753 int cgetnext(char **, const char * const *);
754 int cgetnum(char *, const char *, long *);
755 int cgetset(const char *);
756 int cgetstr(char *, const char *, char **);
757 int cgetustr(char *, const char *, char **);
762 #if !HAVE_DECL_HTOBE16
763 #define htobe16(x) (x)
765 #if !HAVE_DECL_HTOBE32
766 #define htobe32(x) (x)
768 #if !HAVE_DECL_HTOBE64
769 #define htobe64(x) (x)
771 #if !HAVE_DECL_HTOLE16
772 #define htole16(x) bswap16((u_int16_t)(x))
774 #if !HAVE_DECL_HTOLE32
775 #define htole32(x) bswap32((u_int32_t)(x))
777 #if !HAVE_DECL_HTOLE64
778 #define htole64(x) bswap64((u_int64_t)(x))
781 #if !HAVE_DECL_HTOBE16
782 #define htobe16(x) bswap16((u_int16_t)(x))
784 #if !HAVE_DECL_HTOBE32
785 #define htobe32(x) bswap32((u_int32_t)(x))
787 #if !HAVE_DECL_HTOBE64
788 #define htobe64(x) bswap64((u_int64_t)(x))
790 #if !HAVE_DECL_HTOLE16
791 #define htole16(x) (x)
793 #if !HAVE_DECL_HTOLE32
794 #define htole32(x) (x)
796 #if !HAVE_DECL_HTOLE64
797 #define htole64(x) (x)
800 #if !HAVE_DECL_BE16TOH
801 #define be16toh(x) htobe16(x)
803 #if !HAVE_DECL_BE32TOH
804 #define be32toh(x) htobe32(x)
806 #if !HAVE_DECL_BE64TOH
807 #define be64toh(x) htobe64(x)
809 #if !HAVE_DECL_LE16TOH
810 #define le16toh(x) htole16(x)
812 #if !HAVE_DECL_LE32TOH
813 #define le32toh(x) htole32(x)
815 #if !HAVE_DECL_LE64TOH
816 #define le64toh(x) htole64(x)
819 #define __GEN_ENDIAN_ENC(bits, endian) \
821 endian ## bits ## enc(void *dst, uint ## bits ## _t u) \
823 u = hto ## endian ## bits (u); \
824 memcpy(dst, &u, sizeof(u)); \
826 #if !HAVE_DECL_BE16ENC
827 __GEN_ENDIAN_ENC(16, be
)
829 #if !HAVE_DECL_BE32ENC
830 __GEN_ENDIAN_ENC(32, be
)
832 #if !HAVE_DECL_BE64ENC
833 __GEN_ENDIAN_ENC(64, be
)
835 #if !HAVE_DECL_LE16ENC
836 __GEN_ENDIAN_ENC(16, le
)
838 #if !HAVE_DECL_LE32ENC
839 __GEN_ENDIAN_ENC(32, le
)
841 #if !HAVE_DECL_LE64ENC
842 __GEN_ENDIAN_ENC(64, le
)
844 #undef __GEN_ENDIAN_ENC
846 #define __GEN_ENDIAN_DEC(bits, endian) \
847 static uint ## bits ## _t \
848 endian ## bits ## dec(const void *buf) \
850 uint ## bits ## _t u; \
851 memcpy(&u, buf, sizeof(u)); \
852 return endian ## bits ## toh (u); \
854 #if !HAVE_DECL_BE16DEC
855 __GEN_ENDIAN_DEC(16, be
)
857 #if !HAVE_DECL_BE32DEC
858 __GEN_ENDIAN_DEC(32, be
)
860 #if !HAVE_DECL_BE64DEC
861 __GEN_ENDIAN_DEC(64, be
)
863 #if !HAVE_DECL_LE16DEC
864 __GEN_ENDIAN_DEC(16, le
)
866 #if !HAVE_DECL_LE32DEC
867 __GEN_ENDIAN_DEC(32, le
)
869 #if !HAVE_DECL_LE64DEC
870 __GEN_ENDIAN_DEC(64, le
)
872 #undef __GEN_ENDIAN_DEC
880 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
883 #define MAP_ANON MAP_ANONYMOUS
891 #define BIG_ENDIAN 4321
892 #define LITTLE_ENDIAN 1234
896 #define BYTE_ORDER BIG_ENDIAN
898 #define BYTE_ORDER LITTLE_ENDIAN
902 #define DEV_BSIZE (1 << 9)
907 #define MIN(a,b) ((a) < (b) ? (a) : (b))
908 #define MAX(a,b) ((a) > (b) ? (a) : (b))
911 #define MAXBSIZE (64 * 1024)
917 #define MAXPHYS (64 * 1024)
920 /* XXX needed by makefs; this should be done in a better way */
922 #define btodb(x) ((x) << 9)
928 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
929 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
930 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
931 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
934 #define powerof2(x) ((((x)-1)&(x))==0)
938 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
943 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
946 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
950 #define S_ISTXT S_ISVTX
956 /* Protected by _NETBSD_SOURCE otherwise. */
957 #if HAVE_STRUCT_STAT_ST_FLAGS && defined(__NetBSD__)
958 #define UF_SETTABLE 0x0000ffff
959 #define UF_NODUMP 0x00000001
960 #define UF_IMMUTABLE 0x00000002
961 #define UF_APPEND 0x00000004
962 #define UF_OPAQUE 0x00000008
963 #define SF_SETTABLE 0xffff0000
964 #define SF_ARCHIVED 0x00010000
965 #define SF_IMMUTABLE 0x00020000
966 #define SF_APPEND 0x00040000
969 /* <sys/syslimits.h> */
972 #define LINE_MAX 2048
978 #define timercmp(tvp, uvp, cmp) \
979 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
980 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
981 ((tvp)->tv_sec cmp (uvp)->tv_sec))
984 #define timeradd(tvp, uvp, vvp) \
986 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
987 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
988 if ((vvp)->tv_usec >= 1000000) { \
990 (vvp)->tv_usec -= 1000000; \
992 } while (/* CONSTCOND */ 0)
995 #define timersub(tvp, uvp, vvp) \
997 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
998 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
999 if ((vvp)->tv_usec < 0) { \
1001 (vvp)->tv_usec += 1000000; \
1003 } while (/* CONSTCOND */ 0)
1011 #define major(x) ((int32_t)((((x) & 0x000fff00) >> 8)))
1016 #define minor(x) ((int32_t)((((x) & 0xfff00000) >> 12) | \
1017 (((x) & 0x000000ff) >> 0)))
1021 #define makedev(x,y) ((dev_t)((((x) << 8) & 0x000fff00) | \
1022 (((y) << 12) & 0xfff00000) | \
1023 (((y) << 0) & 0x000000ff)))
1029 /* #define, not typedef, as quad_t exists as a struct on some systems */
1030 #define quad_t long long
1031 #define u_quad_t unsigned long long
1032 #define strtoq strtoll
1033 #define strtouq strtoull
1036 /* Has quad_t but these prototypes don't get pulled into scope. w/o we lose */
1038 quad_t strtoq
__P((const char *, char **, int));
1039 u_quad_t strtouq
__P((const char *, char **, int));
1042 #endif /* !__NETBSD_COMPAT_DEFS_H__ */