From 575dcfc9e15cfbd45339fe0e5cb4be6c637248d1 Mon Sep 17 00:00:00 2001 From: Tharre Date: Sun, 15 Feb 2015 01:50:48 +0100 Subject: Replace make_relative() with relpath() Also improve the documentation for this function, and add a few examples to clarify what it does (and what not). --- src/filepath.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'src/filepath.c') diff --git a/src/filepath.c b/src/filepath.c index d0b68de..e4d8e62 100644 --- a/src/filepath.c +++ b/src/filepath.c @@ -53,27 +53,33 @@ char *take_extension(const char *target) { return ""; } -/* Make one path relative to another i.e. some/path to some/path/a/b/c would - yield a/b/c as result. Prints '.' if the 2 paths match. */ -// TODO: nameing, requires absolute paths, doesn't MALLOC -const char *make_relative(const char *target, const char *to) { +/* Returns a pointer to a relative path to `path` from `start`. This pointer may + be pointing to either of it's arguments, or to the statically allocated + string "." should both paths match. Both paths must be canonicalized. + A few examples: + relpath("/abc/de", "/abc/de") => "." + relpath("some/path/a/b/c", "some/path") => "a/b/c" + relpath("some/path", "some/path/a/b/c") => "some/path/a/b/c" + relpath("/", "/") => "." + relpath("/abc", "/") => "abc" + */ +char *relpath(char *path, char *start) { int i; - for (i = 0; target[i] && to[i]; ++i) - if (target[i] != to[i]) { - /* the paths do not match */ - return to; - } - - if (!target[i] && !to[i]) { - /* both paths match completely */ - return "."; + for (i = 0; path[i] && start[i]; ++i) { + if (path[i] != start[i]) + return path; /* paths share nothing */ } - /* skip any leading seperators */ - while (to[i] == '/') + if (!path[i] && start[i]) + return start; /* path is above start */ + + if (!path[i] && !start[i]) + return "."; /* paths match completely */ + + if (path[i] == '/') ++i; - return &to[i]; + return &path[i]; } /* Transforms target into a safe filename, replacing all '/' with '!'. */ -- cgit v1.2.3-70-g09d2