From 096884c4fe1fb922eab2797d4583ca2ae8ae2600 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 6 Nov 2022 16:22:57 -0500 Subject: config: Split out some of util.h into a new header --- src/bfs.h | 32 ---------- src/color.h | 2 +- src/config.h | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/dir.c | 3 +- src/dstring.h | 2 +- src/eval.c | 1 + src/fsade.h | 2 +- src/main.c | 4 +- src/mtab.c | 5 +- src/parse.c | 4 +- src/stat.c | 5 +- src/stat.h | 2 +- src/trie.c | 2 +- src/util.c | 5 +- src/util.h | 134 ----------------------------------------- src/xspawn.c | 1 + 16 files changed, 205 insertions(+), 188 deletions(-) delete mode 100644 src/bfs.h create mode 100644 src/config.h diff --git a/src/bfs.h b/src/bfs.h deleted file mode 100644 index d611e82..0000000 --- a/src/bfs.h +++ /dev/null @@ -1,32 +0,0 @@ -/**************************************************************************** - * bfs * - * Copyright (C) 2015-2021 Tavian Barnes * - * * - * Permission to use, copy, modify, and/or distribute this software for any * - * purpose with or without fee is hereby granted. * - * * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * - ****************************************************************************/ - -/** - * Constants about the bfs program itself. - */ - -#ifndef BFS_H -#define BFS_H - -#ifndef BFS_VERSION -# define BFS_VERSION "2.6.2" -#endif - -#ifndef BFS_HOMEPAGE -# define BFS_HOMEPAGE "https://tavianator.com/projects/bfs.html" -#endif - -#endif // BFS_H diff --git a/src/color.h b/src/color.h index edf1ef7..1b0cadb 100644 --- a/src/color.h +++ b/src/color.h @@ -21,7 +21,7 @@ #ifndef BFS_COLOR_H #define BFS_COLOR_H -#include "util.h" +#include "config.h" #include #include #include diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..5bd49de --- /dev/null +++ b/src/config.h @@ -0,0 +1,189 @@ +/**************************************************************************** + * bfs * + * Copyright (C) 2015-2022 Tavian Barnes * + * * + * Permission to use, copy, modify, and/or distribute this software for any * + * purpose with or without fee is hereby granted. * + * * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * + ****************************************************************************/ + +/** + * Configuration and feature/platform detection. + */ + +#ifndef BFS_CONFIG_H +#define BFS_CONFIG_H + +#include +#include + +// bfs packaging configuration + +#ifndef BFS_COMMAND +# define BFS_COMMAND "bfs" +#endif +#ifndef BFS_VERSION +# define BFS_VERSION "2.6.2" +#endif +#ifndef BFS_HOMEPAGE +# define BFS_HOMEPAGE "https://tavianator.com/projects/bfs.html" +#endif + +// Check for system headers + +#ifdef __has_include + +#if __has_include() +# define BFS_HAS_MNTENT_H true +#endif +#if __has_include() +# define BFS_HAS_PATHS_H true +#endif +#if __has_include() +# define BFS_HAS_SYS_ACL_H true +#endif +#if __has_include() +# define BFS_HAS_SYS_CAPABILITY_H true +#endif +#if __has_include() +# define BFS_HAS_SYS_EXTATTR_H true +#endif +#if __has_include() +# define BFS_HAS_SYS_MKDEV_H true +#endif +#if __has_include() +# define BFS_HAS_SYS_PARAM_H true +#endif +#if __has_include() +# define BFS_HAS_SYS_SYSMACROS_H true +#endif +#if __has_include() +# define BFS_HAS_SYS_XATTR_H true +#endif +#if __has_include() +# define BFS_HAS_UTIL_H true +#endif + +#else // !__has_include + +#define BFS_HAS_MNTENT_H __GLIBC__ +#define BFS_HAS_PATHS_H true +#define BFS_HAS_SYS_ACL_H true +#define BFS_HAS_SYS_CAPABILITY_H __linux__ +#define BFS_HAS_SYS_EXTATTR_H __FreeBSD__ +#define BFS_HAS_SYS_MKDEV_H false +#define BFS_HAS_SYS_PARAM_H true +#define BFS_HAS_SYS_SYSMACROS_H __GLIBC__ +#define BFS_HAS_SYS_XATTR_H __linux__ +#define BFS_HAS_UTIL_H __NetBSD__ + +#endif // !__has_include + +#ifndef BFS_USE_MNTENT_H +# define BFS_USE_MNTENT_H BFS_HAS_MNTENT_H +#endif +#ifndef BFS_USE_PATHS_H +# define BFS_USE_PATHS_H BFS_HAS_PATHS_H +#endif +#ifndef BFS_USE_SYS_ACL_H +# define BFS_USE_SYS_ACL_H BFS_HAS_SYS_ACL_H +#endif +#ifndef BFS_USE_SYS_CAPABILITY_H +# define BFS_USE_SYS_CAPABILITY_H BFS_HAS_SYS_CAPABILITY_H +#endif +#ifndef BFS_USE_SYS_EXTATTR_H +# define BFS_USE_SYS_EXTATTR_H BFS_HAS_SYS_EXTATTR_H +#endif +#ifndef BFS_USE_SYS_MKDEV_H +# define BFS_USE_SYS_MKDEV_H BFS_HAS_SYS_MKDEV_H +#endif +#ifndef BFS_USE_SYS_PARAM_H +# define BFS_USE_SYS_PARAM_H BFS_HAS_SYS_PARAM_H +#endif +#ifndef BFS_USE_SYS_SYSMACROS_H +# define BFS_USE_SYS_SYSMACROS_H BFS_HAS_SYS_SYSMACROS_H +#endif +#ifndef BFS_USE_SYS_XATTR_H +# define BFS_USE_SYS_XATTR_H BFS_HAS_SYS_XATTR_H +#endif +#ifndef BFS_USE_UTIL_H +# define BFS_USE_UTIL_H BFS_HAS_UTIL_H +#endif + +// Stub out feature detection on old/incompatible compilers + +#ifndef __has_feature +# define __has_feature(feat) false +#endif + +#ifndef __has_c_attribute +# define __has_c_attribute(attr) false +#endif + +#ifndef __has_attribute +# define __has_attribute(attr) false +#endif + +// Platform detection + +// Get the definition of BSD if available +#if BFS_USE_SYS_PARAM_H +# include +#endif + +#ifndef __GLIBC_PREREQ +# define __GLIBC_PREREQ(maj, min) false +#endif + +// Wrappers for fundamental language features/extensions + +/** + * Silence compiler warnings about switch/case fall-throughs. + */ +#if __has_c_attribute(fallthrough) +# define BFS_FALLTHROUGH [[fallthrough]] +#elif __has_attribute(fallthrough) +# define BFS_FALLTHROUGH __attribute__((fallthrough)) +#else +# define BFS_FALLTHROUGH ((void)0) +#endif + +// Lower bound on BFS_FLEX_SIZEOF() +#define BFS_FLEX_LB(type, member, length) (offsetof(type, member) + sizeof(((type *)NULL)->member[0]) * (length)) + +// Maximum macro for BFS_FLEX_SIZE() +#define BFS_FLEX_MAX(a, b) ((a) > (b) ? (a) : (b)) + +/** + * Computes the size of a struct containing a flexible array member of the given + * length. + * + * @param type + * The type of the struct containing the flexible array. + * @param member + * The name of the flexible array member. + * @param length + * The length of the flexible array. + */ +#define BFS_FLEX_SIZEOF(type, member, length) \ + (sizeof(type) <= BFS_FLEX_LB(type, member, 0) \ + ? BFS_FLEX_LB(type, member, length) \ + : BFS_FLEX_MAX(sizeof(type), BFS_FLEX_LB(type, member, length))) + +/** + * Adds compiler warnings for bad printf()-style function calls, if supported. + */ +#if __has_attribute(format) +# define BFS_FORMATTER(fmt, args) __attribute__((format(printf, fmt, args))) +#else +# define BFS_FORMATTER(fmt, args) +#endif + +#endif // BFS_CONFIG_H diff --git a/src/dir.c b/src/dir.c index 6d4a325..1cd2d2c 100644 --- a/src/dir.c +++ b/src/dir.c @@ -15,6 +15,7 @@ ****************************************************************************/ #include "dir.h" +#include "config.h" #include "util.h" #include #include @@ -27,7 +28,7 @@ #if __linux__ # include -#endif // __linux__ +#endif enum bfs_type bfs_mode_to_type(mode_t mode) { switch (mode & S_IFMT) { diff --git a/src/dstring.h b/src/dstring.h index 54106f3..51f1b2f 100644 --- a/src/dstring.h +++ b/src/dstring.h @@ -21,7 +21,7 @@ #ifndef BFS_DSTRING_H #define BFS_DSTRING_H -#include "util.h" +#include "config.h" #include #include diff --git a/src/eval.c b/src/eval.c index d685ab0..dc79188 100644 --- a/src/eval.c +++ b/src/eval.c @@ -22,6 +22,7 @@ #include "bar.h" #include "bftw.h" #include "color.h" +#include "config.h" #include "ctx.h" #include "darray.h" #include "diag.h" diff --git a/src/fsade.h b/src/fsade.h index bd7fc5b..f45c6fd 100644 --- a/src/fsade.h +++ b/src/fsade.h @@ -22,7 +22,7 @@ #ifndef BFS_FSADE_H #define BFS_FSADE_H -#include "util.h" +#include "config.h" #include #define BFS_CAN_CHECK_ACL BFS_USE_SYS_ACL_H diff --git a/src/main.c b/src/main.c index b8ee6d0..6f82976 100644 --- a/src/main.c +++ b/src/main.c @@ -33,9 +33,9 @@ * - bftw.[ch] (an extended version of nftw(3)) * * - Utilities: - * - bfs.h (constants about bfs itself) * - bar.[ch] (a terminal status bar) * - color.[ch] (for pretty terminal colors) + * - config.h (configuration and feature/platform detection) * - darray.[ch] (a dynamic array library) * - diag.[ch] (formats diagnostic messages) * - dir.[ch] (a directory API facade) @@ -46,10 +46,10 @@ * - stat.[ch] (wraps stat(), or statx() on Linux) * - trie.[ch] (a trie set/map implementation) * - typo.[ch] (fuzzy matching for typos) - * - util.[ch] (everything else) * - xregex.[ch] (regular expression support) * - xspawn.[ch] (spawns processes) * - xtime.[ch] (date/time handling utilities) + * - util.[ch] (everything else) */ #include "ctx.h" diff --git a/src/mtab.c b/src/mtab.c index a9f5b41..e6a92e1 100644 --- a/src/mtab.c +++ b/src/mtab.c @@ -15,6 +15,7 @@ ****************************************************************************/ #include "mtab.h" +#include "config.h" #include "darray.h" #include "stat.h" #include "trie.h" @@ -26,10 +27,6 @@ #include #include -#if BFS_USE_SYS_PARAM_H -# include -#endif - #if BFS_USE_MNTENT_H # define BFS_MNTENT 1 #elif BSD diff --git a/src/parse.c b/src/parse.c index 15feac1..2fe3473 100644 --- a/src/parse.c +++ b/src/parse.c @@ -22,9 +22,9 @@ */ #include "parse.h" -#include "bfs.h" #include "bftw.h" #include "color.h" +#include "config.h" #include "ctx.h" #include "darray.h" #include "diag.h" @@ -3852,7 +3852,7 @@ struct bfs_ctx *bfs_parse_cmdline(int argc, char *argv[]) { goto fail; } - static char* default_argv[] = {"bfs", NULL}; + static char* default_argv[] = {BFS_COMMAND, NULL}; if (argc < 1) { argc = 1; argv = default_argv; diff --git a/src/stat.c b/src/stat.c index cc20697..3134b6a 100644 --- a/src/stat.c +++ b/src/stat.c @@ -15,6 +15,7 @@ ****************************************************************************/ #include "stat.h" +#include "config.h" #include "util.h" #include #include @@ -24,10 +25,6 @@ #include #include -#if BFS_USE_SYS_PARAM_H -# include -#endif - #if defined(STATX_BASIC_STATS) && (!__ANDROID__ || __ANDROID_API__ >= 30) # define BFS_LIBC_STATX true #elif __linux__ diff --git a/src/stat.h b/src/stat.h index f45302a..44cbfad 100644 --- a/src/stat.h +++ b/src/stat.h @@ -25,7 +25,7 @@ #ifndef BFS_STAT_H #define BFS_STAT_H -#include "util.h" +#include "config.h" #include #include diff --git a/src/trie.c b/src/trie.c index d7561a4..24cc3a3 100644 --- a/src/trie.c +++ b/src/trie.c @@ -95,7 +95,7 @@ */ #include "trie.h" -#include "util.h" +#include "config.h" #include #include #include diff --git a/src/util.c b/src/util.c index bb2e1cc..da54040 100644 --- a/src/util.c +++ b/src/util.c @@ -15,6 +15,7 @@ ****************************************************************************/ #include "util.h" +#include "config.h" #include "dstring.h" #include "xregex.h" #include @@ -31,10 +32,6 @@ #include #include -#if BFS_USE_SYS_PARAM_H -# include -#endif - #if BFS_USE_SYS_SYSMACROS_H # include #elif BFS_USE_SYS_MKDEV_H diff --git a/src/util.h b/src/util.h index 0fbcdaa..2d99152 100644 --- a/src/util.h +++ b/src/util.h @@ -30,101 +30,6 @@ // Some portability concerns -#ifndef __has_feature -# define __has_feature(feat) false -#endif - -#ifndef __has_c_attribute -# define __has_c_attribute(attr) false -#endif - -#ifndef __has_attribute -# define __has_attribute(attr) false -#endif - -#ifdef __has_include - -#if __has_include() -# define BFS_HAS_MNTENT_H true -#endif -#if __has_include() -# define BFS_HAS_PATHS_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_ACL_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_CAPABILITY_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_EXTATTR_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_MKDEV_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_PARAM_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_SYSMACROS_H true -#endif -#if __has_include() -# define BFS_HAS_SYS_XATTR_H true -#endif -#if __has_include() -# define BFS_HAS_UTIL_H true -#endif - -#else // !__has_include - -#define BFS_HAS_MNTENT_H __GLIBC__ -#define BFS_HAS_PATHS_H true -#define BFS_HAS_SYS_ACL_H true -#define BFS_HAS_SYS_CAPABILITY_H __linux__ -#define BFS_HAS_SYS_EXTATTR_H __FreeBSD__ -#define BFS_HAS_SYS_MKDEV_H false -#define BFS_HAS_SYS_PARAM_H true -#define BFS_HAS_SYS_SYSMACROS_H __GLIBC__ -#define BFS_HAS_SYS_XATTR_H __linux__ -#define BFS_HAS_UTIL_H __NetBSD__ - -#endif // !__has_include - -#ifndef BFS_USE_MNTENT_H -# define BFS_USE_MNTENT_H BFS_HAS_MNTENT_H -#endif -#ifndef BFS_USE_PATHS_H -# define BFS_USE_PATHS_H BFS_HAS_PATHS_H -#endif -#ifndef BFS_USE_SYS_ACL_H -# define BFS_USE_SYS_ACL_H BFS_HAS_SYS_ACL_H -#endif -#ifndef BFS_USE_SYS_CAPABILITY_H -# define BFS_USE_SYS_CAPABILITY_H BFS_HAS_SYS_CAPABILITY_H -#endif -#ifndef BFS_USE_SYS_EXTATTR_H -# define BFS_USE_SYS_EXTATTR_H BFS_HAS_SYS_EXTATTR_H -#endif -#ifndef BFS_USE_SYS_MKDEV_H -# define BFS_USE_SYS_MKDEV_H BFS_HAS_SYS_MKDEV_H -#endif -#ifndef BFS_USE_SYS_PARAM_H -# define BFS_USE_SYS_PARAM_H BFS_HAS_SYS_PARAM_H -#endif -#ifndef BFS_USE_SYS_SYSMACROS_H -# define BFS_USE_SYS_SYSMACROS_H BFS_HAS_SYS_SYSMACROS_H -#endif -#ifndef BFS_USE_SYS_XATTR_H -# define BFS_USE_SYS_XATTR_H BFS_HAS_SYS_XATTR_H -#endif -#ifndef BFS_USE_UTIL_H -# define BFS_USE_UTIL_H BFS_HAS_UTIL_H -#endif - -#ifndef __GLIBC_PREREQ -# define __GLIBC_PREREQ(maj, min) false -#endif - #if !defined(FNM_CASEFOLD) && defined(FNM_IGNORECASE) # define FNM_CASEFOLD FNM_IGNORECASE #endif @@ -133,45 +38,6 @@ # define O_DIRECTORY 0 #endif -#if __has_c_attribute(fallthrough) -# define BFS_FALLTHROUGH [[fallthrough]] -#elif __has_attribute(fallthrough) -# define BFS_FALLTHROUGH __attribute__((fallthrough)) -#else -# define BFS_FALLTHROUGH ((void)0) -#endif - -/** - * Adds compiler warnings for bad printf()-style function calls, if supported. - */ -#if __has_attribute(format) -# define BFS_FORMATTER(fmt, args) __attribute__((format(printf, fmt, args))) -#else -# define BFS_FORMATTER(fmt, args) -#endif - -// Lower bound on BFS_FLEX_SIZEOF() -#define BFS_FLEX_LB(type, member, length) (offsetof(type, member) + sizeof(((type *)NULL)->member[0]) * (length)) - -// Maximum macro for BFS_FLEX_SIZE() -#define BFS_FLEX_MAX(a, b) ((a) > (b) ? (a) : (b)) - -/** - * Computes the size of a struct containing a flexible array member of the given - * length. - * - * @param type - * The type of the struct containing the flexible array. - * @param member - * The name of the flexible array member. - * @param length - * The length of the flexible array. - */ -#define BFS_FLEX_SIZEOF(type, member, length) \ - (sizeof(type) <= BFS_FLEX_LB(type, member, 0) \ - ? BFS_FLEX_LB(type, member, length) \ - : BFS_FLEX_MAX(sizeof(type), BFS_FLEX_LB(type, member, length))) - /** * readlinkat() wrapper that dynamically allocates the result. * diff --git a/src/xspawn.c b/src/xspawn.c index bcaeb35..f9d52b0 100644 --- a/src/xspawn.c +++ b/src/xspawn.c @@ -15,6 +15,7 @@ ****************************************************************************/ #include "xspawn.h" +#include "config.h" #include "util.h" #include #include -- cgit v1.2.3