1 /* $NetBSD: compat_defs.h,v 1.73 2009/10/02 18:03:19 christos 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
349 # define user_from_uid __nbcompat_user_from_uid
350 # undef HAVE_DECL_USER_FROM_UID
353 #if !HAVE_UID_FROM_USER
354 # define uid_from_user __nbcompat_uid_from_user
355 # undef HAVE_DECL_UID_FROM_USER
358 #if !HAVE_PWCACHE_USERDB
359 # define pwcache_userdb __nbcompat_pwcache_userdb
360 # undef HAVE_DECL_PWCACHE_USERDB
363 #if !HAVE_GROUP_FROM_GID
364 # define group_from_gid __nbcompat_group_from_gid
365 # undef HAVE_DECL_GROUP_FROM_GID
368 #if !HAVE_GID_FROM_GROUP
369 # define gid_from_group __nbcompat_gid_from_group
370 # undef HAVE_DECL_GID_FROM_GROUP
373 #if !HAVE_PWCACHE_GROUDB
374 # define pwcache_groupdb __nbcompat_pwcache_groupdb
375 # undef HAVE_DECL_PWCACHE_GROUPDB
378 #if !HAVE_DECL_UID_FROM_USER
379 int uid_from_user(const char *, uid_t
*);
382 #if !HAVE_DECL_USER_FROM_UID
383 const char *user_from_uid(uid_t
, int);
386 #if !HAVE_DECL_PWCACHE_USERDB
387 int pwcache_userdb(int (*)(int), void (*)(void),
388 struct passwd
* (*)(const char *), struct passwd
* (*)(uid_t
));
391 #if !HAVE_DECL_GID_FROM_GROUP
392 int gid_from_group(const char *, gid_t
*);
395 #if !HAVE_DECL_GROUP_FROM_GID
396 const char *group_from_gid(gid_t
, int);
399 #if !HAVE_DECL_PWCACHE_GROUPDB
400 int pwcache_groupdb(int (*)(int), void (*)(void),
401 struct group
* (*)(const char *), struct group
* (*)(gid_t
));
404 #if !HAVE_DECL_STRNDUP
405 char *strndup(const char *, size_t);
407 #if !HAVE_DECL_LCHFLAGS
408 int lchflags(const char *, unsigned long);
410 #if !HAVE_DECL_LCHMOD
411 int lchmod(const char *, mode_t
);
413 #if !HAVE_DECL_LCHOWN
414 int lchown(const char *, uid_t
, gid_t
);
418 ssize_t
pwrite(int, const void *, size_t, off_t
);
421 #if !HAVE_RAISE_DEFAULT_SIGNAL
422 int raise_default_signal(int);
426 int setenv(const char *, const char *, int);
429 #if !HAVE_DECL_SETGROUPENT
430 int setgroupent(int);
433 #if !HAVE_DECL_SETPASSENT
437 #if !HAVE_SETPROGNAME || defined(__NetBSD__)
438 const char *getprogname(void);
439 void setprogname(const char *);
443 int snprintf(char *, size_t, const char *, ...);
447 size_t strlcat(char *, const char *, size_t);
451 size_t strlcpy(char *, const char *, size_t);
455 void strmode(mode_t
, char *);
459 char *strndup(const char *, size_t);
462 #if !HAVE_STRSEP || defined(__NetBSD__)
463 char *strsep(char **, const char *);
466 #if !HAVE_DECL_STRSUFTOLL
467 long long strsuftoll(const char *, const char *, long long, long long);
468 long long strsuftollx(const char *, const char *,
469 long long, long long, char *, size_t);
473 long long strtoll(const char *, char **, int);
476 #if !HAVE_USER_FROM_UID
477 const char *user_from_uid(uid_t
, int);
480 #if !HAVE_GROUP_FROM_GID
481 const char *group_from_gid(gid_t
, int);
485 int vasprintf(char **, const char *, va_list);
489 int vasnprintf(char **, size_t, const char *, va_list);
493 int vsnprintf(char *, size_t, const char *, va_list);
497 * getmode() and setmode() are always defined, as these function names
498 * exist but with very different meanings on other OS's. The compat
499 * versions here simply accept an octal mode number; the "u+x,g-w" type
500 * of syntax is not accepted.
503 #define getmode __nbcompat_getmode
504 #define setmode __nbcompat_setmode
506 mode_t
getmode(const void *, mode_t
);
507 void *setmode(const char *);
509 /* Eliminate assertions embedded in binaries. */
512 #define _DIAGASSERT(x)
514 /* Various sources use this */
516 #define __RCSID(x) struct XXXNETBSD_RCSID
520 #define __COPYRIGHT(x) struct XXXNETBSD_COPYRIGHT
521 #undef __KERNEL_RCSID
522 #define __KERNEL_RCSID(x,y)
524 /* Heimdal expects this one. */
529 /* Some definitions not available on all systems. */
532 #define __inline inline
552 #if USHRT_MAX == 0xffffU /* short is a 16-bit type */
571 #endif /* short is a 16-bit type */
572 #if ! (defined(PRId16) && defined(PRIi16) && defined(PRIo16) && \
573 defined(PRIu16) && defined(PRIx16) && defined(PRIX16))
574 #error "Don't know how to define PRI[diouxX]16"
577 #if UINT_MAX == 0xffffffffU /* int is a 32-bit type */
596 #endif /* int is a 32-bit type */
597 #if ULONG_MAX == 0xffffffffU /* long is a 32-bit type */
616 #endif /* long is a 32-bit type */
617 #if ! (defined(PRId32) && defined(PRIi32) && defined(PRIo32) && \
618 defined(PRIu32) && defined(PRIx32) && defined(PRIX32))
619 #error "Don't know how to define PRI[diouxX]32"
622 #if ULONG_MAX == 0xffffffffffffffffU /* long is a 64-bit type */
641 #endif /* long is a 64-bit type */
642 #if ULLONG_MAX == 0xffffffffffffffffU /* long long is a 64-bit type */
661 #endif /* long long is a 64-bit type */
662 #if ! (defined(PRId64) && defined(PRIi64) && defined(PRIo64) && \
663 defined(PRIu64) && defined(PRIx64) && defined(PRIX64))
664 #error "Don't know how to define PRI[diouxX]64"
670 #define UID_MAX 32767
673 #define GID_MAX UID_MAX
677 #define UQUAD_MAX ((u_quad_t)-1)
680 #define QUAD_MAX ((quad_t)(UQUAD_MAX >> 1))
683 #define QUAD_MIN ((quad_t)(~QUAD_MAX))
686 #define ULLONG_MAX ((unsigned long long)-1)
689 #define LLONG_MAX ((long long)(ULLONG_MAX >> 1))
692 #define LLONG_MIN ((long long)(~LLONG_MAX))
697 /* The host's _PATH_BSHELL might be broken, so override it. */
699 #define _PATH_BSHELL PATH_BSHELL
700 #ifndef _PATH_DEFPATH
701 #define _PATH_DEFPATH "/usr/bin:/bin:/usr/local/bin"
704 #define _PATH_DEV "/dev/"
706 #ifndef _PATH_DEVNULL
707 #define _PATH_DEVNULL _PATH_DEV "null"
710 #define _PATH_TMP "/tmp/"
712 #ifndef _PATH_DEFTAPE
713 #define _PATH_DEFTAPE "/dev/nrst0"
716 #define _PATH_VI "/usr/bin/vi"
721 #ifndef _BSD_VA_LIST_
722 #define _BSD_VA_LIST_ va_list
727 #if !defined(SIZE_MAX) && defined(SIZE_T_MAX)
728 #define SIZE_MAX SIZE_T_MAX
732 #define UINT8_MAX 0xffU
736 #define UINT16_MAX 0xffffU
740 #define UINT32_MAX 0xffffffffU
749 # ifndef alloca /* predefined by HP cc +Olibcalls */
755 /* avoid prototype conflicts with host */
756 #define cgetcap __nbcompat_cgetcap
757 #define cgetclose __nbcompat_cgetclose
758 #define cgetent __nbcompat_cgetent
759 #define cgetfirst __nbcompat_cgetfirst
760 #define cgetmatch __nbcompat_cgetmatch
761 #define cgetnext __nbcompat_cgetnext
762 #define cgetnum __nbcompat_cgetnum
763 #define cgetset __nbcompat_cgetset
764 #define cgetstr __nbcompat_cgetstr
765 #define cgetustr __nbcompat_cgetustr
767 char *cgetcap(char *, const char *, int);
769 int cgetent(char **, const char * const *, const char *);
770 int cgetfirst(char **, const char * const *);
771 int cgetmatch(const char *, const char *);
772 int cgetnext(char **, const char * const *);
773 int cgetnum(char *, const char *, long *);
774 int cgetset(const char *);
775 int cgetstr(char *, const char *, char **);
776 int cgetustr(char *, const char *, char **);
781 #if !HAVE_DECL_HTOBE16
782 #define htobe16(x) (x)
784 #if !HAVE_DECL_HTOBE32
785 #define htobe32(x) (x)
787 #if !HAVE_DECL_HTOBE64
788 #define htobe64(x) (x)
790 #if !HAVE_DECL_HTOLE16
791 #define htole16(x) bswap16((u_int16_t)(x))
793 #if !HAVE_DECL_HTOLE32
794 #define htole32(x) bswap32((u_int32_t)(x))
796 #if !HAVE_DECL_HTOLE64
797 #define htole64(x) bswap64((u_int64_t)(x))
800 #if !HAVE_DECL_HTOBE16
801 #define htobe16(x) bswap16((u_int16_t)(x))
803 #if !HAVE_DECL_HTOBE32
804 #define htobe32(x) bswap32((u_int32_t)(x))
806 #if !HAVE_DECL_HTOBE64
807 #define htobe64(x) bswap64((u_int64_t)(x))
809 #if !HAVE_DECL_HTOLE16
810 #define htole16(x) (x)
812 #if !HAVE_DECL_HTOLE32
813 #define htole32(x) (x)
815 #if !HAVE_DECL_HTOLE64
816 #define htole64(x) (x)
819 #if !HAVE_DECL_BE16TOH
820 #define be16toh(x) htobe16(x)
822 #if !HAVE_DECL_BE32TOH
823 #define be32toh(x) htobe32(x)
825 #if !HAVE_DECL_BE64TOH
826 #define be64toh(x) htobe64(x)
828 #if !HAVE_DECL_LE16TOH
829 #define le16toh(x) htole16(x)
831 #if !HAVE_DECL_LE32TOH
832 #define le32toh(x) htole32(x)
834 #if !HAVE_DECL_LE64TOH
835 #define le64toh(x) htole64(x)
838 #define __GEN_ENDIAN_ENC(bits, endian) \
840 endian ## bits ## enc(void *dst, uint ## bits ## _t u) \
842 u = hto ## endian ## bits (u); \
843 memcpy(dst, &u, sizeof(u)); \
845 #if !HAVE_DECL_BE16ENC
846 __GEN_ENDIAN_ENC(16, be
)
848 #if !HAVE_DECL_BE32ENC
849 __GEN_ENDIAN_ENC(32, be
)
851 #if !HAVE_DECL_BE64ENC
852 __GEN_ENDIAN_ENC(64, be
)
854 #if !HAVE_DECL_LE16ENC
855 __GEN_ENDIAN_ENC(16, le
)
857 #if !HAVE_DECL_LE32ENC
858 __GEN_ENDIAN_ENC(32, le
)
860 #if !HAVE_DECL_LE64ENC
861 __GEN_ENDIAN_ENC(64, le
)
863 #undef __GEN_ENDIAN_ENC
865 #define __GEN_ENDIAN_DEC(bits, endian) \
866 static uint ## bits ## _t \
867 endian ## bits ## dec(const void *buf) \
869 uint ## bits ## _t u; \
870 memcpy(&u, buf, sizeof(u)); \
871 return endian ## bits ## toh (u); \
873 #if !HAVE_DECL_BE16DEC
874 __GEN_ENDIAN_DEC(16, be
)
876 #if !HAVE_DECL_BE32DEC
877 __GEN_ENDIAN_DEC(32, be
)
879 #if !HAVE_DECL_BE64DEC
880 __GEN_ENDIAN_DEC(64, be
)
882 #if !HAVE_DECL_LE16DEC
883 __GEN_ENDIAN_DEC(16, le
)
885 #if !HAVE_DECL_LE32DEC
886 __GEN_ENDIAN_DEC(32, le
)
888 #if !HAVE_DECL_LE64DEC
889 __GEN_ENDIAN_DEC(64, le
)
891 #undef __GEN_ENDIAN_DEC
899 /* HP-UX has MAP_ANONYMOUS but not MAP_ANON */
902 #define MAP_ANON MAP_ANONYMOUS
910 #define BIG_ENDIAN 4321
911 #define LITTLE_ENDIAN 1234
915 #define BYTE_ORDER BIG_ENDIAN
917 #define BYTE_ORDER LITTLE_ENDIAN
921 #define DEV_BSIZE (1 << 9)
926 #define MIN(a,b) ((a) < (b) ? (a) : (b))
927 #define MAX(a,b) ((a) > (b) ? (a) : (b))
930 #define MAXBSIZE (64 * 1024)
936 #define MAXPHYS (64 * 1024)
939 /* XXX needed by makefs; this should be done in a better way */
941 #define btodb(x) ((x) << 9)
947 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
948 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
949 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
950 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
953 #define powerof2(x) ((((x)-1)&(x))==0)
957 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
962 #define ALLPERMS (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
965 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
969 #define S_ISTXT S_ISVTX
975 /* Protected by _NETBSD_SOURCE otherwise. */
976 #if HAVE_STRUCT_STAT_ST_FLAGS && defined(__NetBSD__)
977 #define UF_SETTABLE 0x0000ffff
978 #define UF_NODUMP 0x00000001
979 #define UF_IMMUTABLE 0x00000002
980 #define UF_APPEND 0x00000004
981 #define UF_OPAQUE 0x00000008
982 #define SF_SETTABLE 0xffff0000
983 #define SF_ARCHIVED 0x00010000
984 #define SF_IMMUTABLE 0x00020000
985 #define SF_APPEND 0x00040000
988 /* <sys/syslimits.h> */
991 #define LINE_MAX 2048
997 #define timercmp(tvp, uvp, cmp) \
998 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
999 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1000 ((tvp)->tv_sec cmp (uvp)->tv_sec))
1003 #define timeradd(tvp, uvp, vvp) \
1005 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
1006 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
1007 if ((vvp)->tv_usec >= 1000000) { \
1009 (vvp)->tv_usec -= 1000000; \
1011 } while (/* CONSTCOND */ 0)
1014 #define timersub(tvp, uvp, vvp) \
1016 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
1017 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
1018 if ((vvp)->tv_usec < 0) { \
1020 (vvp)->tv_usec += 1000000; \
1022 } while (/* CONSTCOND */ 0)
1030 #define major(x) ((int32_t)((((x) & 0x000fff00) >> 8)))
1035 #define minor(x) ((int32_t)((((x) & 0xfff00000) >> 12) | \
1036 (((x) & 0x000000ff) >> 0)))
1040 #define makedev(x,y) ((dev_t)((((x) << 8) & 0x000fff00) | \
1041 (((y) << 12) & 0xfff00000) | \
1042 (((y) << 0) & 0x000000ff)))
1048 /* #define, not typedef, as quad_t exists as a struct on some systems */
1049 #define quad_t long long
1050 #define u_quad_t unsigned long long
1051 #define strtoq strtoll
1052 #define strtouq strtoull
1055 /* Has quad_t but these prototypes don't get pulled into scope. w/o we lose */
1057 quad_t strtoq
__P((const char *, char **, int));
1058 u_quad_t strtouq
__P((const char *, char **, int));
1061 #endif /* !__NETBSD_COMPAT_DEFS_H__ */