diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2022-11-06 16:22:57 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2022-11-06 16:37:14 -0500 |
commit | 096884c4fe1fb922eab2797d4583ca2ae8ae2600 (patch) | |
tree | 850e214301a5fd94b7f2a3e94bd90a390739581f | |
parent | 46387a7dcda93e7df9f5baa3ead753c0feeff122 (diff) | |
download | bfs-096884c4fe1fb922eab2797d4583ca2ae8ae2600.tar.xz |
config: Split out some of util.h into a new header
-rw-r--r-- | src/bfs.h | 32 | ||||
-rw-r--r-- | src/color.h | 2 | ||||
-rw-r--r-- | src/config.h | 189 | ||||
-rw-r--r-- | src/dir.c | 3 | ||||
-rw-r--r-- | src/dstring.h | 2 | ||||
-rw-r--r-- | src/eval.c | 1 | ||||
-rw-r--r-- | src/fsade.h | 2 | ||||
-rw-r--r-- | src/main.c | 4 | ||||
-rw-r--r-- | src/mtab.c | 5 | ||||
-rw-r--r-- | src/parse.c | 4 | ||||
-rw-r--r-- | src/stat.c | 5 | ||||
-rw-r--r-- | src/stat.h | 2 | ||||
-rw-r--r-- | src/trie.c | 2 | ||||
-rw-r--r-- | src/util.c | 5 | ||||
-rw-r--r-- | src/util.h | 134 | ||||
-rw-r--r-- | src/xspawn.c | 1 |
16 files changed, 205 insertions, 188 deletions
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 <tavianator@tavianator.com> * - * * - * 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 <stdarg.h> #include <stdbool.h> #include <stdio.h> 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 <tavianator@tavianator.com> * + * * + * 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 <stdbool.h> +#include <stddef.h> + +// 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(<mntent.h>) +# define BFS_HAS_MNTENT_H true +#endif +#if __has_include(<paths.h>) +# define BFS_HAS_PATHS_H true +#endif +#if __has_include(<sys/acl.h>) +# define BFS_HAS_SYS_ACL_H true +#endif +#if __has_include(<sys/capability.h>) +# define BFS_HAS_SYS_CAPABILITY_H true +#endif +#if __has_include(<sys/extattr.h>) +# define BFS_HAS_SYS_EXTATTR_H true +#endif +#if __has_include(<sys/mkdev.h>) +# define BFS_HAS_SYS_MKDEV_H true +#endif +#if __has_include(<sys/param.h>) +# define BFS_HAS_SYS_PARAM_H true +#endif +#if __has_include(<sys/sysmacros.h>) +# define BFS_HAS_SYS_SYSMACROS_H true +#endif +#if __has_include(<sys/xattr.h>) +# define BFS_HAS_SYS_XATTR_H true +#endif +#if __has_include(<util.h>) +# 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 <sys/param.h> +#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 @@ -15,6 +15,7 @@ ****************************************************************************/ #include "dir.h" +#include "config.h" #include "util.h" #include <dirent.h> #include <errno.h> @@ -27,7 +28,7 @@ #if __linux__ # include <sys/syscall.h> -#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 <stdarg.h> #include <stddef.h> @@ -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 <stdbool.h> #define BFS_CAN_CHECK_ACL BFS_USE_SYS_ACL_H @@ -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" @@ -15,6 +15,7 @@ ****************************************************************************/ #include "mtab.h" +#include "config.h" #include "darray.h" #include "stat.h" #include "trie.h" @@ -26,10 +27,6 @@ #include <string.h> #include <sys/types.h> -#if BFS_USE_SYS_PARAM_H -# include <sys/param.h> -#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; @@ -15,6 +15,7 @@ ****************************************************************************/ #include "stat.h" +#include "config.h" #include "util.h" #include <assert.h> #include <errno.h> @@ -24,10 +25,6 @@ #include <sys/types.h> #include <sys/stat.h> -#if BFS_USE_SYS_PARAM_H -# include <sys/param.h> -#endif - #if defined(STATX_BASIC_STATS) && (!__ANDROID__ || __ANDROID_API__ >= 30) # define BFS_LIBC_STATX true #elif __linux__ @@ -25,7 +25,7 @@ #ifndef BFS_STAT_H #define BFS_STAT_H -#include "util.h" +#include "config.h" #include <sys/types.h> #include <time.h> @@ -95,7 +95,7 @@ */ #include "trie.h" -#include "util.h" +#include "config.h" #include <assert.h> #include <limits.h> #include <stdbool.h> @@ -15,6 +15,7 @@ ****************************************************************************/ #include "util.h" +#include "config.h" #include "dstring.h" #include "xregex.h" #include <assert.h> @@ -31,10 +32,6 @@ #include <unistd.h> #include <wchar.h> -#if BFS_USE_SYS_PARAM_H -# include <sys/param.h> -#endif - #if BFS_USE_SYS_SYSMACROS_H # include <sys/sysmacros.h> #elif BFS_USE_SYS_MKDEV_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(<mntent.h>) -# define BFS_HAS_MNTENT_H true -#endif -#if __has_include(<paths.h>) -# define BFS_HAS_PATHS_H true -#endif -#if __has_include(<sys/acl.h>) -# define BFS_HAS_SYS_ACL_H true -#endif -#if __has_include(<sys/capability.h>) -# define BFS_HAS_SYS_CAPABILITY_H true -#endif -#if __has_include(<sys/extattr.h>) -# define BFS_HAS_SYS_EXTATTR_H true -#endif -#if __has_include(<sys/mkdev.h>) -# define BFS_HAS_SYS_MKDEV_H true -#endif -#if __has_include(<sys/param.h>) -# define BFS_HAS_SYS_PARAM_H true -#endif -#if __has_include(<sys/sysmacros.h>) -# define BFS_HAS_SYS_SYSMACROS_H true -#endif -#if __has_include(<sys/xattr.h>) -# define BFS_HAS_SYS_XATTR_H true -#endif -#if __has_include(<util.h>) -# 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 <errno.h> #include <fcntl.h> |