diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-27 22:54:44 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-27 22:54:44 +0200 |
commit | ba4811b1d2ac5a9d363e6000c83098f2eb885119 (patch) | |
tree | 776184456b7a9181cd29bf23e0e214e5a67dd462 /include | |
parent | 2f1b5d44343aac079af2671e97aa3ffd5c7e4b66 (diff) |
Add a header for platform compatibillity fluff
- We don't have "endian.h" everywhere. On some BSDs its in sys and
on some BSDs the macros have different names.
- We definitely don't have sysmacros.h on non-Unix-like systems.
- Likewise for sys/types.h, sys/stat.h and their contents.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'include')
-rw-r--r-- | include/compat.h | 95 | ||||
-rw-r--r-- | include/fstree.h | 3 | ||||
-rw-r--r-- | include/highlevel.h | 3 | ||||
-rw-r--r-- | include/sqfs/predef.h | 1 | ||||
-rw-r--r-- | include/tar.h | 1 | ||||
-rw-r--r-- | include/util.h | 3 |
6 files changed, 99 insertions, 7 deletions
diff --git a/include/compat.h b/include/compat.h new file mode 100644 index 0000000..74d0b6f --- /dev/null +++ b/include/compat.h @@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * util.h + * + * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> + */ +#ifndef COMPAT_H +#define COMPAT_H + +#if defined(__APPLE__) +#include <libkern/OSByteOrder.h> + +#define htole16(x) OSSwapHostToLittleInt16(x) +#define htole32(x) OSSwapHostToLittleInt32(x) +#define htole64(x) OSSwapHostToLittleInt64(x) + +#define le32toh(x) OSSwapLittleToHostInt32(x) +#define le16toh(x) OSSwapLittleToHostInt16(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) +#elif defined(__OpenBSD__) +#include <sys/endian.h> +#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) +#include <sys/endian.h> + +#define le16toh(x) letoh16(x) +#define le32toh(x) letoh32(x) +#define le64toh(x) letoh64(x) +#elif defined(_WIN32) || defined(__WINDOWS__) +#define htole16(x) (x) +#define htole32(x) (x) +#define htole64(x) (x) + +#define le16toh(x) (x) +#define le32toh(x) (x) +#define le64toh(x) (x) +#else +#include <endian.h> +#endif + +#if defined(_WIN32) || defined(__WINDOWS__) +#define S_IFSOCK SQFS_INODE_MODE_SOCK +#define S_IFLNK SQFS_INODE_MODE_LNK +#define S_IFREG SQFS_INODE_MODE_REG +#define S_IFBLK SQFS_INODE_MODE_BLK +#define S_IFDIR SQFS_INODE_MODE_DIR +#define S_IFCHR SQFS_INODE_MODE_CHR +#define S_IFIFO SQFS_INODE_MODE_FIFO +#define S_IFMT SQFS_INODE_MODE_MASK + +#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) +#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) +#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) +#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) +#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) + +#define S_ISUID SQFS_INODE_SET_UID +#define S_ISGID SQFS_INODE_SET_GID +#define S_ISVTX SQFS_INODE_STICKY + +#define S_IRWXU SQFS_INODE_OWNER_MASK +#define S_IRUSR SQFS_INODE_OWNER_R +#define S_IWUSR SQFS_INODE_OWNER_W +#define S_IXUSR SQFS_INODE_OWNER_X + +#define S_IRWXG SQFS_INODE_GROUP_MASK +#define S_IRGRP SQFS_INODE_GROUP_R +#define S_IWGRP SQFS_INODE_GROUP_W +#define S_IXGRP SQFS_INODE_GROUP_X + +#define S_IRWXO SQFS_INODE_OTHERS_MASK +#define S_IROTH SQFS_INODE_OTHERS_R +#define S_IWOTH SQFS_INODE_OTHERS_W +#define S_IXOTH SQFS_INODE_OTHERS_X + +/* lifted from musl libc */ +#define major(x) \ + ((unsigned)( (((x)>>31>>1) & 0xfffff000) | (((x)>>8) & 0x00000fff) )) + +#define minor(x) \ + ((unsigned)( (((x)>>12) & 0xffffff00) | ((x) & 0x000000ff) )) + +#define makedev(x,y) ( \ + (((x)&0xfffff000ULL) << 32) | \ + (((x)&0x00000fffULL) << 8) | \ + (((y)&0xffffff00ULL) << 12) | \ + (((y)&0x000000ffULL)) ) +#else +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/sysmacros.h> +#endif + +#endif /* COMPAT_H */ diff --git a/include/fstree.h b/include/fstree.h index dbd2cd6..337b598 100644 --- a/include/fstree.h +++ b/include/fstree.h @@ -9,14 +9,13 @@ #include "config.h" -#include <sys/types.h> -#include <sys/stat.h> #include <stdbool.h> #include <stdint.h> #include <stddef.h> #include <stdio.h> #include "str_table.h" +#include "compat.h" #define FSTREE_XATTR_KEY_BUCKETS 31 #define FSTREE_XATTR_VALUE_BUCKETS 511 diff --git a/include/highlevel.h b/include/highlevel.h index 13c85a7..6e4ad5e 100644 --- a/include/highlevel.h +++ b/include/highlevel.h @@ -22,12 +22,11 @@ #include "sqfs/xattr.h" #include "sqfs/dir.h" #include "sqfs/io.h" +#include "compat.h" #include "fstree.h" #include "util.h" #include "tar.h" -#include <sys/stat.h> -#include <stdint.h> #include <stddef.h> typedef struct { diff --git a/include/sqfs/predef.h b/include/sqfs/predef.h index 2e835f3..13e49f3 100644 --- a/include/sqfs/predef.h +++ b/include/sqfs/predef.h @@ -27,7 +27,6 @@ * macros and integer types. */ -#include <sys/types.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> diff --git a/include/tar.h b/include/tar.h index d4e72ec..d0a483c 100644 --- a/include/tar.h +++ b/include/tar.h @@ -10,7 +10,6 @@ #include "config.h" #include "util.h" -#include <sys/stat.h> #include <stdbool.h> #include <stdint.h> diff --git a/include/util.h b/include/util.h index 61754ed..e7bc431 100644 --- a/include/util.h +++ b/include/util.h @@ -10,10 +10,11 @@ #include "config.h" #include "sqfs/predef.h" -#include <sys/types.h> #include <stdint.h> #include <stddef.h> +#include "compat.h" + #if defined(__GNUC__) || defined(__clang__) #define UI_ADD_OV __builtin_uadd_overflow #define UL_ADD_OV __builtin_uaddl_overflow |