From e95ec269efdfbd97b5d0ee85dda38e7bae498181 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 17 Dec 2018 17:10:18 -0500 Subject: bftw: Move bftw_typeflag conversion out of util Turns out incomplete enum types are a GNU C extension. --- bftw.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- bftw.h | 5 ++++ eval.c | 2 +- printf.c | 2 +- util.c | 99 +------------------------------------------------------------ util.h | 10 ------- 6 files changed, 107 insertions(+), 112 deletions(-) diff --git a/bftw.c b/bftw.c index 295c09f..118a441 100644 --- a/bftw.c +++ b/bftw.c @@ -652,12 +652,109 @@ static struct bftw_dir *bftw_queue_pop(struct bftw_queue *queue) { return dir; } +enum bftw_typeflag bftw_mode_typeflag(mode_t mode) { + switch (mode & S_IFMT) { +#ifdef S_IFBLK + case S_IFBLK: + return BFTW_BLK; +#endif +#ifdef S_IFCHR + case S_IFCHR: + return BFTW_CHR; +#endif +#ifdef S_IFDIR + case S_IFDIR: + return BFTW_DIR; +#endif +#ifdef S_IFDOOR + case S_IFDOOR: + return BFTW_DOOR; +#endif +#ifdef S_IFIFO + case S_IFIFO: + return BFTW_FIFO; +#endif +#ifdef S_IFLNK + case S_IFLNK: + return BFTW_LNK; +#endif +#ifdef S_IFPORT + case S_IFPORT: + return BFTW_PORT; +#endif +#ifdef S_IFREG + case S_IFREG: + return BFTW_REG; +#endif +#ifdef S_IFSOCK + case S_IFSOCK: + return BFTW_SOCK; +#endif +#ifdef S_IFWHT + case S_IFWHT: + return BFTW_WHT; +#endif + + default: + return BFTW_UNKNOWN; + } +} + +static enum bftw_typeflag bftw_dirent_typeflag(const struct dirent *de) { +#if defined(_DIRENT_HAVE_D_TYPE) || defined(DT_UNKNOWN) + switch (de->d_type) { +#ifdef DT_BLK + case DT_BLK: + return BFTW_BLK; +#endif +#ifdef DT_CHR + case DT_CHR: + return BFTW_CHR; +#endif +#ifdef DT_DIR + case DT_DIR: + return BFTW_DIR; +#endif +#ifdef DT_DOOR + case DT_DOOR: + return BFTW_DOOR; +#endif +#ifdef DT_FIFO + case DT_FIFO: + return BFTW_FIFO; +#endif +#ifdef DT_LNK + case DT_LNK: + return BFTW_LNK; +#endif +#ifdef DT_PORT + case DT_PORT: + return BFTW_PORT; +#endif +#ifdef DT_REG + case DT_REG: + return BFTW_REG; +#endif +#ifdef DT_SOCK + case DT_SOCK: + return BFTW_SOCK; +#endif +#ifdef DT_WHT + case DT_WHT: + return BFTW_WHT; +#endif + } +#endif + + return BFTW_UNKNOWN; +} + /** Call stat() and use the results. */ static int bftw_stat(struct BFTW *ftwbuf, struct bfs_stat *sb) { int ret = bfs_stat(ftwbuf->at_fd, ftwbuf->at_path, ftwbuf->at_flags, BFS_STAT_BROKEN_OK, sb); if (ret == 0) { ftwbuf->statbuf = sb; - ftwbuf->typeflag = mode_to_typeflag(sb->mode); + ftwbuf->typeflag = bftw_mode_typeflag(sb->mode); } return ret; } @@ -809,7 +906,7 @@ static void bftw_prepare_visit(struct bftw_state *state) { ftwbuf->typeflag = BFTW_ERROR; return; } else if (de) { - ftwbuf->typeflag = dirent_to_typeflag(de); + ftwbuf->typeflag = bftw_dirent_typeflag(de); } else if (ftwbuf->depth > 0) { ftwbuf->typeflag = BFTW_DIR; } else { diff --git a/bftw.h b/bftw.h index c874188..eff24bc 100644 --- a/bftw.h +++ b/bftw.h @@ -50,6 +50,11 @@ enum bftw_typeflag { BFTW_ERROR = 1 << 10, }; +/** + * Convert a bfs_stat() mode to a bftw() typeflag. + */ +enum bftw_typeflag bftw_mode_typeflag(mode_t mode); + /** * Possible visit occurrences. */ diff --git a/eval.c b/eval.c index b3d85f2..a170967 100644 --- a/eval.c +++ b/eval.c @@ -919,7 +919,7 @@ bool eval_xtype(const struct expr *expr, struct eval_state *state) { const struct bfs_stat *statbuf = eval_xstat(state); if (statbuf) { - return mode_to_typeflag(statbuf->mode) & expr->idata; + return bftw_mode_typeflag(statbuf->mode) & expr->idata; } else if (!follow && is_nonexistence_error(errno)) { // Broken symlink return eval_type(expr, state); diff --git a/printf.c b/printf.c index 8643f68..1da0de7 100644 --- a/printf.c +++ b/printf.c @@ -405,7 +405,7 @@ static int bfs_printf_Y(FILE *file, const struct bfs_printf_directive *directive struct bfs_stat sb; if (bfs_stat(ftwbuf->at_fd, ftwbuf->at_path, 0, 0, &sb) == 0) { - type = bfs_printf_type(mode_to_typeflag(sb.mode)); + type = bfs_printf_type(bftw_mode_typeflag(sb.mode)); } else { switch (errno) { case ELOOP: diff --git a/util.c b/util.c index 93f86ca..b708527 100644 --- a/util.c +++ b/util.c @@ -173,7 +173,7 @@ int xlocaltime(const time_t *timep, struct tm *result) { void format_mode(mode_t mode, char str[11]) { strcpy(str, "----------"); - switch (mode_to_typeflag(mode)) { + switch (bftw_mode_typeflag(mode)) { case BFTW_BLK: str[0] = 'b'; break; @@ -275,103 +275,6 @@ bool is_nonexistence_error(int error) { return error == ENOENT || errno == ENOTDIR; } -enum bftw_typeflag mode_to_typeflag(mode_t mode) { - switch (mode & S_IFMT) { -#ifdef S_IFBLK - case S_IFBLK: - return BFTW_BLK; -#endif -#ifdef S_IFCHR - case S_IFCHR: - return BFTW_CHR; -#endif -#ifdef S_IFDIR - case S_IFDIR: - return BFTW_DIR; -#endif -#ifdef S_IFDOOR - case S_IFDOOR: - return BFTW_DOOR; -#endif -#ifdef S_IFIFO - case S_IFIFO: - return BFTW_FIFO; -#endif -#ifdef S_IFLNK - case S_IFLNK: - return BFTW_LNK; -#endif -#ifdef S_IFPORT - case S_IFPORT: - return BFTW_PORT; -#endif -#ifdef S_IFREG - case S_IFREG: - return BFTW_REG; -#endif -#ifdef S_IFSOCK - case S_IFSOCK: - return BFTW_SOCK; -#endif -#ifdef S_IFWHT - case S_IFWHT: - return BFTW_WHT; -#endif - - default: - return BFTW_UNKNOWN; - } -} - -enum bftw_typeflag dirent_to_typeflag(const struct dirent *de) { -#if defined(_DIRENT_HAVE_D_TYPE) || defined(DT_UNKNOWN) - switch (de->d_type) { -#ifdef DT_BLK - case DT_BLK: - return BFTW_BLK; -#endif -#ifdef DT_CHR - case DT_CHR: - return BFTW_CHR; -#endif -#ifdef DT_DIR - case DT_DIR: - return BFTW_DIR; -#endif -#ifdef DT_DOOR - case DT_DOOR: - return BFTW_DOOR; -#endif -#ifdef DT_FIFO - case DT_FIFO: - return BFTW_FIFO; -#endif -#ifdef DT_LNK - case DT_LNK: - return BFTW_LNK; -#endif -#ifdef DT_PORT - case DT_PORT: - return BFTW_PORT; -#endif -#ifdef DT_REG - case DT_REG: - return BFTW_REG; -#endif -#ifdef DT_SOCK - case DT_SOCK: - return BFTW_SOCK; -#endif -#ifdef DT_WHT - case DT_WHT: - return BFTW_WHT; -#endif - } -#endif - - return BFTW_UNKNOWN; -} - /** Read a line from standard input. */ static char *xgetline() { char *line = dstralloc(0); diff --git a/util.h b/util.h index 413d289..e95b67d 100644 --- a/util.h +++ b/util.h @@ -165,16 +165,6 @@ int xfaccessat(int fd, const char *path, int amode); */ bool is_nonexistence_error(int error); -/** - * Convert a bfs_stat() mode to a bftw() typeflag. - */ -enum bftw_typeflag mode_to_typeflag(mode_t mode); - -/** - * Convert a directory entry to a bftw() typeflag. - */ -enum bftw_typeflag dirent_to_typeflag(const struct dirent *de); - /** * Process a yes/no prompt. * -- cgit v1.2.3