longest_ancestor_length(): use string_list_split()

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Jeff King <peff@peff.net>

Michael Haggerty committed Oct 28, 2012 at 17:16 UTC a5ccdbe416081d5749f286c193b70400e48ad03f
1 file changed +11 -7
path.c
+11 -7
@@ -12,6 +12,7 @@
12 */
13 #include "cache.h"
14 #include "strbuf.h"
15 +#include "string-list.h"
16
17 static char bad_path[] = "/bad-path/";
18
@@ -582,20 +583,22 @@ int normalize_path_copy(char *dst, const char *src)
583 */
584 int longest_ancestor_length(const char *path, const char *prefix_list)
585 {
586 + struct string_list prefixes = STRING_LIST_INIT_DUP;
587 char buf[PATH_MAX+1];
586 - const char *ceil, *colon;
587 - int len, max_len = -1;
588 + int i, max_len = -1;
589
590 if (prefix_list == NULL || !strcmp(path, "/"))
591 return -1;
592
592 - for (colon = ceil = prefix_list; *colon; ceil = colon+1) {
593 - for (colon = ceil; *colon && *colon != PATH_SEP; colon++);
594 - len = colon - ceil;
593 + string_list_split(&prefixes, prefix_list, PATH_SEP, -1);
594 +
595 + for (i = 0; i < prefixes.nr; i++) {
596 + const char *ceil = prefixes.items[i].string;
597 + int len = strlen(ceil);
598 +
599 if (len == 0 || len > PATH_MAX || !is_absolute_path(ceil))
600 continue;
597 - strlcpy(buf, ceil, len+1);
598 - if (normalize_path_copy(buf, buf) < 0)
601 + if (normalize_path_copy(buf, ceil) < 0)
602 continue;
603 len = strlen(buf);
604 if (len > 0 && buf[len-1] == '/')
@@ -608,6 +611,7 @@ int longest_ancestor_length(const char *path, const char *prefix_list)
611 }
612 }
613
614 + string_list_clear(&prefixes, 0);
615 return max_len;
616 }
617