summaryrefslogtreecommitdiffstats
path: root/src/printf.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-04-24 12:47:12 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-04-24 12:48:05 -0400
commit3a72b93d26ad2bb06ea0b7d39257130084b1ee1e (patch)
treeb476e317185f9d26038007507dc4a1fbd7f5070b /src/printf.c
parent98a026cf8b9d5da7730d5d4d1c88ba35d94771b2 (diff)
downloadbfs-3a72b93d26ad2bb06ea0b7d39257130084b1ee1e.tar.xz
printf: Refactor %y/%Y implementation
Diffstat (limited to 'src/printf.c')
-rw-r--r--src/printf.c40
1 files changed, 17 insertions, 23 deletions
diff --git a/src/printf.c b/src/printf.c
index 4df399b..e39d756 100644
--- a/src/printf.c
+++ b/src/printf.c
@@ -520,10 +520,14 @@ static const char *bfs_printf_type(enum bfs_type type) {
return "p";
case BFS_LNK:
return "l";
+ case BFS_PORT:
+ return "P";
case BFS_REG:
return "f";
case BFS_SOCK:
return "s";
+ case BFS_WHT:
+ return "w";
default:
return "U";
}
@@ -537,34 +541,24 @@ static int bfs_printf_y(CFILE *cfile, const struct bfs_fmt *fmt, const struct BF
/** %Y: target type */
static int bfs_printf_Y(CFILE *cfile, const struct bfs_fmt *fmt, const struct BFTW *ftwbuf) {
- int error = 0;
-
- if (ftwbuf->type != BFS_LNK) {
- return bfs_printf_y(cfile, fmt, ftwbuf);
- }
-
- const char *type = "U";
+ enum bfs_type type = bftw_type(ftwbuf, BFS_STAT_FOLLOW);
+ const char *str;
- const struct bfs_stat *statbuf = bftw_stat(ftwbuf, BFS_STAT_FOLLOW);
- if (statbuf) {
- type = bfs_printf_type(bfs_mode_to_type(statbuf->mode));
- } else {
- switch (errno) {
- case ELOOP:
- type = "L";
- break;
- case ENOENT:
- case ENOTDIR:
- type = "N";
- break;
- default:
- type = "?";
+ int error = 0;
+ if (type == BFS_ERROR) {
+ if (errno_is_like(ELOOP)) {
+ str = "L";
+ } else if (errno_is_like(ENOENT)) {
+ str = "N";
+ } else {
+ str = "?";
error = errno;
- break;
}
+ } else {
+ str = bfs_printf_type(type);
}
- int ret = dyn_fprintf(cfile->file, fmt, type);
+ int ret = dyn_fprintf(cfile->file, fmt, str);
if (error != 0) {
ret = -1;
errno = error;