From: Michael Haggerty Date: Sat, 29 Sep 2012 06:15:59 GMT Subject: [PATCH v2 6/9] longest_ancestor_length(): always add a slash to the end of prefixes Message-ID: <1348899362-4057-7-git-send-email-mhagger@alum.mit.edu> In-Reply-To: <1348899362-4057-1-git-send-email-mhagger@alum.mit.edu> Previously we stripped off any slashes that were present. Instead, add a slash if it is missing. This removes the need for an extra check that path has a slash following the prefix and makes the handling of the root directory more natural, making the way clear to use string_list_longest_prefix(). Signed-off-by: Michael Haggerty --- path.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/path.c b/path.c index b255a74..a56c64a 100644 --- a/path.c +++ b/path.c @@ -570,7 +570,7 @@ int normalize_path_copy(char *dst, const char *src) static int normalize_path_callback(struct string_list_item *item, void *cb_data) { - char buf[PATH_MAX+1]; + char buf[PATH_MAX+2]; const char *ceil = item->string; int len = strlen(ceil); @@ -579,8 +579,10 @@ static int normalize_path_callback(struct string_list_item *item, void *cb_data) if (normalize_path_copy(buf, ceil) < 0) return 0; len = strlen(buf); - if (len > 0 && buf[len-1] == '/') - buf[--len] = '\0'; + if (len == 0 || buf[len-1] != '/') { + buf[len++] = '/'; + buf[len++] = '\0'; + } free(item->string); item->string = xstrdup(buf); return 1; @@ -615,9 +617,8 @@ int longest_ancestor_length(const char *path, const char *prefix_list) int len = strlen(ceil); if (!strncmp(path, ceil, len) && - path[len] == '/' && - len > max_len) { - max_len = len; + len - 1 > max_len) { + max_len = len - 1; } } -- 1.7.11.3