From 510a7bd65c680fcf292493b5c00ce32c90a5155c Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 20 Jul 2018 12:42:31 -0400 Subject: printf: Output ? for errors in %Y --- printf.c | 21 ++++++++++++++++----- tests.sh | 16 ++++++++++++++++ tests/test_printf_Y_error.out | 3 +++ 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 tests/test_printf_Y_error.out diff --git a/printf.c b/printf.c index 86804b8..6a87f99 100644 --- a/printf.c +++ b/printf.c @@ -377,6 +377,8 @@ static int bfs_printf_y(FILE *file, const struct bfs_printf_directive *directive /** %Y: target type */ static int bfs_printf_Y(FILE *file, const struct bfs_printf_directive *directive, const struct BFTW *ftwbuf) { + int error = 0; + if (ftwbuf->typeflag != BFTW_LNK) { return bfs_printf_y(file, directive, ftwbuf); } @@ -395,10 +397,19 @@ static int bfs_printf_Y(FILE *file, const struct bfs_printf_directive *directive case ENOTDIR: type = "N"; break; + default: + type = "?"; + error = errno; + break; } } - return fprintf(file, directive->str, type); + int ret = fprintf(file, directive->str, type); + if (error != 0) { + ret = -1; + errno = error; + } + return ret; } /** @@ -798,16 +809,16 @@ error: } int bfs_printf(FILE *file, const struct bfs_printf *command, const struct BFTW *ftwbuf) { - int ret = -1; + int ret = 0, error = 0; for (struct bfs_printf_directive *directive = command->directives; directive; directive = directive->next) { if (directive->fn(file, directive, ftwbuf) < 0) { - goto done; + ret = -1; + error = errno; } } - ret = 0; -done: + errno = error; return ret; } diff --git a/tests.sh b/tests.sh index 3e47374..2cf8062 100755 --- a/tests.sh +++ b/tests.sh @@ -395,6 +395,7 @@ gnu_tests=( test_printf_times test_printf_leak test_printf_nul + test_printf_Y_error test_quit_after_print test_quit_before_print test_fstype @@ -1400,6 +1401,21 @@ function test_printf_w() { bfs_diff times -false -printf '%w %WY\n' } +function test_printf_Y_error() { + rm -rf scratch/* + mkdir scratch/foo + chmod -x scratch/foo + ln -s foo/bar scratch/bar + + bfs_diff scratch -printf '(%p) (%l) %y %Y\n' 2>/dev/null + local ret=$? + + chmod +x scratch/foo + rm -rf scratch/* + + return $ret +} + function test_fstype() { fstype="$(invoke_bfs basic -maxdepth 0 -printf '%F\n')" bfs_diff basic -fstype "$fstype" diff --git a/tests/test_printf_Y_error.out b/tests/test_printf_Y_error.out new file mode 100644 index 0000000..00d6ee7 --- /dev/null +++ b/tests/test_printf_Y_error.out @@ -0,0 +1,3 @@ +(scratch) () d d +(scratch/foo) () d d +(scratch/bar) (foo/bar) l ? -- cgit v1.2.3