From c9f37f069c6e294e58c64c9f5ad94f0bd3e2fd02 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 15 Jun 2023 17:11:42 +0200 Subject: Cleanup: bring some semblance of structure into compat.h Signed-off-by: David Oberhollenzer --- include/compat.h | 66 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 28 deletions(-) (limited to 'include') diff --git a/include/compat.h b/include/compat.h index 7d0a646..7c9f937 100644 --- a/include/compat.h +++ b/include/compat.h @@ -13,12 +13,7 @@ #include #include -#if defined(__GNUC__) || defined(__clang__) -# define PRINTF_ATTRIB(fmt, elipsis) \ - __attribute__ ((format (printf, fmt, elipsis))) -#else -# define PRINTF_ATTRIB(fmt, elipsis) -#endif +/****************************** safe arithmetic ******************************/ #if defined(__GNUC__) && __GNUC__ >= 5 # define SZ_ADD_OV __builtin_add_overflow @@ -52,6 +47,15 @@ static inline int _sz_mul_overflow(size_t a, size_t b, size_t *res) # define SZ_MUL_OV _sz_mul_overflow #endif +/********************* printf macros & format specifiers *********************/ + +#if defined(__GNUC__) || defined(__clang__) +# define PRINTF_ATTRIB(fmt, elipsis) \ + __attribute__ ((format (printf, fmt, elipsis))) +#else +# define PRINTF_ATTRIB(fmt, elipsis) +#endif + #if defined(_WIN32) || defined(__WINDOWS__) # define PRI_U64 "%I64u" # define PRI_U32 "%I32u" @@ -71,6 +75,8 @@ static inline int _sz_mul_overflow(size_t a, size_t b, size_t *res) # error Cannot figure out propper printf specifier for size_t #endif +/***************************** endian conversion *****************************/ + #if defined(__APPLE__) #include @@ -91,13 +97,12 @@ static inline int _sz_mul_overflow(size_t a, size_t b, size_t *res) #define le16toh(x) (x) #define le32toh(x) (x) #define le64toh(x) (x) - -#define WIN32_LEAN_AND_MEAN -#include #else #include #endif +/**************************** struct stat related ****************************/ + #if defined(_WIN32) || defined(__WINDOWS__) #include "sqfs/inode.h" @@ -165,17 +170,6 @@ struct stat { (((x)&0x00000fffULL) << 8) | \ (((y)&0xffffff00ULL) << 12) | \ (((y)&0x000000ffULL)) ) - -#define AT_FDCWD ((int)0xDEADBEEF) -#define AT_SYMLINK_NOFOLLOW (0x01) - -int fchownat(int dirfd, const char *path, int uid, int gid, int flags); - -int fchmodat(int dirfd, const char *path, int mode, int flags); - -int chdir(const char *path); - -void w32_perror(const char *str); #else #include #include @@ -187,6 +181,8 @@ void w32_perror(const char *str); #endif #endif +/***************************** missing functions *****************************/ + #ifndef HAVE_STRNDUP char *strndup(const char *str, size_t max_len); #endif @@ -220,10 +216,6 @@ int getopt_long(int, char *const *, const char *, int getsubopt(char **opt, char *const *keys, char **val); #endif -#if defined(_WIN32) || defined(__WINDOWS__) -WCHAR *path_to_windows(const char *input); -#endif - #ifdef HAVE_FNMATCH #include #else @@ -235,7 +227,29 @@ WCHAR *path_to_windows(const char *input); int fnmatch(const char *, const char *, int); #endif +#ifndef HAVE_STRCHRNUL +char *strchrnul(const char *s, int c); +#endif + +/************************** Windows related madness **************************/ + #if defined(_WIN32) || defined(__WINDOWS__) +#define WIN32_LEAN_AND_MEAN +#include + +#define AT_FDCWD ((int)0xDEADBEEF) +#define AT_SYMLINK_NOFOLLOW (0x01) + +int fchownat(int dirfd, const char *path, int uid, int gid, int flags); + +int fchmodat(int dirfd, const char *path, int mode, int flags); + +int chdir(const char *path); + +void w32_perror(const char *str); + +WCHAR *path_to_windows(const char *input); + extern int sqfs_tools_main(int argc, char **argv); int sqfs_tools_fputc(int c, FILE *strm); @@ -251,8 +265,4 @@ int sqfs_tools_fprintf(FILE *strm, const char *fmt, ...) PRINTF_ATTRIB(2, 3); #define putc sqfs_tools_fputc #endif -#ifndef HAVE_STRCHRNUL -char *strchrnul(const char *s, int c); -#endif - #endif /* COMPAT_H */ -- cgit v1.2.3