summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--printf.c19
-rwxr-xr-xtests.sh5
2 files changed, 15 insertions, 9 deletions
diff --git a/printf.c b/printf.c
index 6eb59dd..a8cb8f0 100644
--- a/printf.c
+++ b/printf.c
@@ -306,20 +306,21 @@ static int bfs_printf_k(FILE *file, const struct bfs_printf *directive, const st
/** %l: link target */
static int bfs_printf_l(FILE *file, const struct bfs_printf *directive, const struct BFTW *ftwbuf) {
- if (ftwbuf->type != BFTW_LNK) {
- return 0;
- }
+ char *buf = NULL;
+ const char *target = "";
- const struct bfs_stat *statbuf = bftw_cached_stat(ftwbuf, BFS_STAT_NOFOLLOW);
- size_t len = statbuf ? statbuf->size : 0;
+ if (ftwbuf->type == BFTW_LNK) {
+ const struct bfs_stat *statbuf = bftw_cached_stat(ftwbuf, BFS_STAT_NOFOLLOW);
+ size_t len = statbuf ? statbuf->size : 0;
- char *target = xreadlinkat(ftwbuf->at_fd, ftwbuf->at_path, len);
- if (!target) {
- return -1;
+ target = buf = xreadlinkat(ftwbuf->at_fd, ftwbuf->at_path, len);
+ if (!target) {
+ return -1;
+ }
}
int ret = fprintf(file, directive->str, target);
- free(target);
+ free(buf);
return ret;
}
diff --git a/tests.sh b/tests.sh
index bbc823d..7276672 100755
--- a/tests.sh
+++ b/tests.sh
@@ -528,6 +528,7 @@ gnu_tests=(
test_printf_Y_error
test_printf_H
test_printf_u_g_ulimit
+ test_printf_l_nonlink
test_quit
test_quit_child
@@ -1990,6 +1991,10 @@ function test_printf_u_g_ulimit() {
[ "$(invoke_bfs deep -printf '%u %g\n' | uniq)" = "$(id -un) $(id -gn)" ]
}
+function test_printf_l_nonlink() {
+ bfs_diff links -printf '| %24p -> %-24l |\n'
+}
+
function test_fstype() {
fstype="$(invoke_bfs basic -maxdepth 0 -printf '%F\n')"
bfs_diff basic -fstype "$fstype"