From 4da8ba603b588bef38f772c18d883fbf5ddc9e65 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 16 Mar 2017 23:30:21 -0400 Subject: Color link targets for -ls Fixes #18. --- color.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'color.c') diff --git a/color.c b/color.c index 18a975f..640cd7e 100644 --- a/color.c +++ b/color.c @@ -11,6 +11,7 @@ #include "color.h" #include "bftw.h" +#include "util.h" #include #include #include @@ -413,6 +414,32 @@ static int print_path(CFILE *cfile, const struct BFTW *ftwbuf) { return 0; } +static int print_link(CFILE *cfile, const struct BFTW *ftwbuf) { + int ret = -1; + + char *target = xreadlinkat(ftwbuf->at_fd, ftwbuf->at_path, 0); + if (!target) { + goto done; + } + + struct BFTW altbuf = *ftwbuf; + altbuf.path = target; + altbuf.nameoff = xbasename(target) - target; + + struct stat statbuf; + if (fstatat(ftwbuf->at_fd, ftwbuf->at_path, &statbuf, 0) == 0) { + altbuf.statbuf = &statbuf; + } else { + altbuf.statbuf = NULL; + } + + ret = print_path(cfile, &altbuf); + +done: + free(target); + return ret; +} + int cfprintf(CFILE *cfile, const char *format, ...) { const struct colors *colors = cfile->colors; FILE *file = cfile->file; @@ -449,6 +476,12 @@ int cfprintf(CFILE *cfile, const char *format, ...) { } break; + case 'L': + if (print_link(cfile, va_arg(args, const struct BFTW *)) != 0) { + goto done; + } + break; + case '{': memcpy(name, i + 1, 2); esc = get_color(colors, name); -- cgit v1.2.3