From ad239cf0bd9ebc8e206ce196dc93eb15b6056706 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Sun, 3 Jan 2021 22:03:16 +0100 Subject: rwc: internally use and print absolute paths Else it's not possible to deal with calls like "rwc ./x" in a proper way. --- README | 7 ++++--- rwc.1 | 4 ++-- rwc.c | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/README b/README index 345cb99..690c33c 100644 --- a/README +++ b/README @@ -8,8 +8,9 @@ SYNOPSIS DESCRIPTION rwc takes a list of files or directories, watches them using inotify(7), - and prints each file name when it changed. If path is a single dash - (‘-’) or absent, rwc reads file names from the standard input. + and prints each file name as absolute path when it changed. If path is a + single dash (‘-’) or absent, rwc reads file names from the standard + input. Watching a directory will result in watching all changes to files which resides directly in that directory. @@ -72,4 +73,4 @@ LICENSE http://creativecommons.org/publicdomain/zero/1.0/ -Void Linux March 3, 2019 Void Linux +Void Linux January 3, 2021 Void Linux diff --git a/rwc.1 b/rwc.1 index 6a84dee..2bce619 100644 --- a/rwc.1 +++ b/rwc.1 @@ -1,4 +1,4 @@ -.Dd March 3, 2019 +.Dd January 3, 2021 .Dt RWC 1 .Os .Sh NAME @@ -12,7 +12,7 @@ .Nm takes a list of files or directories, watches them using .Xr inotify 7 , -and prints each file name when it changed. +and prints each file name as absolute path when it changed. If .Ar path is a single dash diff --git a/rwc.c b/rwc.c index 43ea95a..a316999 100644 --- a/rwc.c +++ b/rwc.c @@ -63,12 +63,45 @@ wdorder(const void *a, const void *b) return 1; } +static char * +realpath_nx(char *path) +{ + char *r = realpath(path, 0); + + if (!r && errno == ENOENT) { + // resolve dirname and append basename + + char *path2 = strdup(path); + if (!path2) + return 0; + char *d = realpath(dirname(path2), 0); + free(path2); + if (!d) + return 0; + char *b = basename(path); + size_t l = strlen(d) + 1 + strlen(b) + 1; + r = malloc(l); + if (!r) + return 0; + snprintf(r, l, "%s/%s", d, b); + } + + return r; +} + static void -add(char *file) +add(char *path) { struct stat st; int wd; + char *file = realpath_nx(path); + if (!file) { + fprintf(stderr, "%s: realpath: %s: %s\n", + argv0, path, strerror(errno)); + return; + } + char *dir = file; tsearch(strdup(file), &root, order); -- cgit 1.4.1