From 5b343f3e2515eb1077792564735f7e7c9e4c65e7 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 8 Apr 2017 07:47:37 -0600 Subject: Move bftw_typeflag converters to util.c --- bftw.c | 110 ++------------------------------------------------------------- bftw.h | 5 --- eval.c | 2 +- printf.c | 2 +- util.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- util.h | 10 ++++++ 6 files changed, 112 insertions(+), 116 deletions(-) diff --git a/bftw.c b/bftw.c index 0dba4b2..05abaf6 100644 --- a/bftw.c +++ b/bftw.c @@ -445,112 +445,6 @@ static void dirqueue_free(struct dirqueue *queue) { free(queue->entries); } -/** Fill in ftwbuf fields with information from a struct dirent. */ -static void ftwbuf_use_dirent(struct BFTW *ftwbuf, const struct dirent *de) { -#if defined(_DIRENT_HAVE_D_TYPE) || defined(DT_UNKNOWN) - switch (de->d_type) { -#ifdef DT_BLK - case DT_BLK: - ftwbuf->typeflag = BFTW_BLK; - break; -#endif -#ifdef DT_CHR - case DT_CHR: - ftwbuf->typeflag = BFTW_CHR; - break; -#endif -#ifdef DT_DIR - case DT_DIR: - ftwbuf->typeflag = BFTW_DIR; - break; -#endif -#ifdef DT_DOOR - case DT_DOOR: - ftwbuf->typeflag = BFTW_DOOR; - break; -#endif -#ifdef DT_FIFO - case DT_FIFO: - ftwbuf->typeflag = BFTW_FIFO; - break; -#endif -#ifdef DT_LNK - case DT_LNK: - ftwbuf->typeflag = BFTW_LNK; - break; -#endif -#ifdef DT_PORT - case DT_PORT: - ftwbuf->typeflag = BFTW_PORT; - break; -#endif -#ifdef DT_REG - case DT_REG: - ftwbuf->typeflag = BFTW_REG; - break; -#endif -#ifdef DT_SOCK - case DT_SOCK: - ftwbuf->typeflag = BFTW_SOCK; - break; -#endif -#ifdef DT_WHT - case DT_WHT: - ftwbuf->typeflag = BFTW_WHT; - break; -#endif - } -#endif -} - -enum bftw_typeflag bftw_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; - } -} - /** Call stat() and use the results. */ static int ftwbuf_stat(struct BFTW *ftwbuf, struct stat *sb) { int ret = fstatat(ftwbuf->at_fd, ftwbuf->at_path, sb, ftwbuf->at_flags); @@ -559,7 +453,7 @@ static int ftwbuf_stat(struct BFTW *ftwbuf, struct stat *sb) { } ftwbuf->statbuf = sb; - ftwbuf->typeflag = bftw_mode_to_typeflag(sb->st_mode); + ftwbuf->typeflag = mode_to_typeflag(sb->st_mode); return 0; } @@ -818,7 +712,7 @@ static void bftw_init_buffers(struct bftw_state *state, const struct dirent *de) ftwbuf->typeflag = BFTW_UNKNOWN; if (de) { - ftwbuf_use_dirent(ftwbuf, de); + ftwbuf->typeflag = dirent_to_typeflag(de); } else if (state->status != BFTW_CHILD) { ftwbuf->typeflag = BFTW_DIR; } diff --git a/bftw.h b/bftw.h index 3bfe2a2..1dd2766 100644 --- a/bftw.h +++ b/bftw.h @@ -45,11 +45,6 @@ enum bftw_typeflag { BFTW_ERROR = 1 << 10, }; -/** - * Convert a stat() st_mode to a bftw() typeflag. - */ -enum bftw_typeflag bftw_mode_to_typeflag(mode_t mode); - /** * Possible visit occurrences. */ diff --git a/eval.c b/eval.c index ded73cb..8763a6f 100644 --- a/eval.c +++ b/eval.c @@ -961,7 +961,7 @@ bool eval_xtype(const struct expr *expr, struct eval_state *state) { } } - return bftw_mode_to_typeflag(sb.st_mode) & expr->idata; + return mode_to_typeflag(sb.st_mode) & expr->idata; } #if _POSIX_MONOTONIC_CLOCK > 0 diff --git a/printf.c b/printf.c index 7999010..cc8989f 100644 --- a/printf.c +++ b/printf.c @@ -350,7 +350,7 @@ static int bfs_printf_Y(FILE *file, const struct bfs_printf_directive *directive struct stat sb; if (fstatat(ftwbuf->at_fd, ftwbuf->at_path, &sb, 0) == 0) { - type = bfs_printf_type(bftw_mode_to_typeflag(sb.st_mode)); + type = bfs_printf_type(mode_to_typeflag(sb.st_mode)); } else { switch (errno) { case ELOOP: diff --git a/util.c b/util.c index 55cf2a4..61a23f3 100644 --- a/util.c +++ b/util.c @@ -134,7 +134,7 @@ int xlocaltime(const time_t *timep, struct tm *result) { void format_mode(mode_t mode, char str[11]) { strcpy(str, "----------"); - switch (bftw_mode_to_typeflag(mode)) { + switch (mode_to_typeflag(mode)) { case BFTW_BLK: str[0] = 'b'; break; @@ -217,3 +217,100 @@ const char *xbasename(const char *path) { return i; } + +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; +} diff --git a/util.h b/util.h index 971860b..ddc01e5 100644 --- a/util.h +++ b/util.h @@ -132,4 +132,14 @@ void format_mode(mode_t mode, char str[11]); */ const char *xbasename(const char *path); +/** + * Convert a stat() st_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); + #endif // BFS_UTIL_H -- cgit v1.2.3