summaryrefslogtreecommitdiffstats
path: root/color.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2017-03-16 23:30:21 -0400
committerTavian Barnes <tavianator@tavianator.com>2017-03-16 23:30:21 -0400
commit4da8ba603b588bef38f772c18d883fbf5ddc9e65 (patch)
tree583b6182ebc22baf1c1ce39a4686da0375ff0f04 /color.c
parentc85f569daf3e0e99c9e8941d0234711afdb58cd7 (diff)
downloadbfs-4da8ba603b588bef38f772c18d883fbf5ddc9e65.tar.xz
Color link targets for -ls
Fixes #18.
Diffstat (limited to 'color.c')
-rw-r--r--color.c33
1 files changed, 33 insertions, 0 deletions
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 <errno.h>
#include <stdarg.h>
#include <stdbool.h>
@@ -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);