diff options
author | Pádraig Brady <P@draigBrady.com> | 2012-01-25 16:42:42 +0000 |
---|---|---|
committer | Pádraig Brady <P@draigBrady.com> | 2012-01-25 18:01:52 +0000 |
commit | 11af4b31b61302188a65e521921c42ddb45c4fb9 (patch) | |
tree | 8cab26a184bf4f9776b25b61f7bdd6292645cac1 /src | |
parent | e9a2555826767baae96a3eabca1f9d91bddca619 (diff) | |
download | coreutils-11af4b31b61302188a65e521921c42ddb45c4fb9.tar.xz |
realpath: remove extraneous '/' for --relative-to edge cases
* src/realpath.c (path_common_prefix): Be consistent and
always include a leading '/' in the count returned.
(relpath): Account for the change in path_common_prefix()
and avoid outputting extra '/' chars in relative paths that
span the root dir.
* tests/misc/realpath: Add the two reported cases.
Reported by Mike Frysinger
Diffstat (limited to 'src')
-rw-r--r-- | src/realpath.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/realpath.c b/src/realpath.c index b39f7bfb0..b03f37505 100644 --- a/src/realpath.c +++ b/src/realpath.c @@ -136,7 +136,7 @@ path_common_prefix (const char *path1, const char *path2) if (*path1 != *path2) break; if (*path1 == '/') - ret = i; + ret = i + 1; path1++; path2++; i++; @@ -171,11 +171,16 @@ relpath (const char *can_fname) const char *relto_suffix = can_relative_to + common_index; const char *fname_suffix = can_fname + common_index; + /* skip over extraneous '/'. */ + if (*relto_suffix == '/') + relto_suffix++; + if (*fname_suffix == '/') + fname_suffix++; + /* Replace remaining components of --relative-to with '..', to get to a common directory. Then output the remainder of fname. */ if (*relto_suffix) { - ++relto_suffix; printf ("%s", ".."); for (; *relto_suffix; ++relto_suffix) { @@ -183,12 +188,13 @@ relpath (const char *can_fname) printf ("%s", "/.."); } - printf ("%s", fname_suffix); + if (*fname_suffix) + printf ("/%s", fname_suffix); } else { if (*fname_suffix) - printf ("%s", ++fname_suffix); + printf ("%s", fname_suffix); else printf ("%c", '.'); } |