From 71b95f7ae8ae5cef495cc481bdea7da42c524e13 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 5 Feb 2023 08:08:40 -0600 Subject: [PATCH] Fix leading / on realpath --relative-to $PWD subdir and add some TODO comment tests to realpath.tests. --- lib/lib.c | 6 ++++-- tests/realpath.test | 10 ++++++++++ toys/other/readlink.c | 16 ---------------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 1d620b38..9c5327d2 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -1107,8 +1107,10 @@ char *relative_path(char *from, char *to, int abs) // count remaining destination directories for (i = j, k = 0; from[i]; i++) if (from[i] == '/') k++; - if (!k) ret = xstrdup(to[j] ? to+j : "."); - else { + if (!k) { + if (to[j]=='/') j++; + ret = xstrdup(to[j] ? to+j : "."); + } else { s = ret = xmprintf("%*c%s", 3*k-!!k, ' ', to+j); for (i = 0; i/dev/null || echo fail' 'fail\n' '' '' +testcmd 'relative-to missing -m' \ + '-m --relative-to nothing/potato .' '../..\n' '' '' testcmd '--relative-base' 'one one/two one/two/three --relative-base=one/two' \ "$TOP/one\n.\nthree\n" '' '' testcmd '--relative-base stomps --relative-to' \ @@ -37,6 +42,11 @@ testcmd 'missing defaults to -m' 'missing' "$TOP/missing\n" '' '' testcmd 'missing -e' '-e missing 2>/dev/null || echo ok' 'ok\n' '' '' testcmd '-L' '-L dos/../one' "$TOP/one\n" '' '' +# -s vs -L +ln -s .. parent +testcmd "-s isn't L" '-s --relative-to=. parent' 'parent\n' '' '' +testcmd "-L isn't s" '-L --relative-to=. parent' '..\n' '' '' + # The -s tests use $PWD instead of $TOP because symlinks in path _to_ here # should not be resolved either. The shell exports $PWD: use it. testcmd '-s' '-s uno/two' "$PWD/uno/two\n" '' '' diff --git a/toys/other/readlink.c b/toys/other/readlink.c index 4fc3f976..2acdd558 100644 --- a/toys/other/readlink.c +++ b/toys/other/readlink.c @@ -41,22 +41,6 @@ config REALPATH --relative-base If path under DIR trim off prefix */ -/* TODO -# relative-to is affected by flags -$ realpath --relative-to=nothing/potato . -realpath: nothing/potato: No such file or directory -$ realpath -m --relative-to=nothing/potato . -../.. - -# -L and -s are similar but not the same -$ realpath -s --relative-to=. ccc -ccc -$ realpath -L --relative-to=. ccc -../../mcm/ccc -*/ - - - #define FOR_realpath #define FORCE_FLAGS #define TT this.readlink // workaround: first FOR_ doesn't match filename -- 2.39.2