compat/basename: make basename() conform to POSIX

According to POSIX, basename("/path/") should return "path", not "path/". Likewise, basename(NULL) and basename("") should both return "." to conform. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jan 12, 2016 at 08:57 UTC 61725be349b44f15b0239182c859553d5c547ba0
1 file changed +17 -3
compat/basename.c
+17 -3
@@ -4,10 +4,24 @@
4 char *gitbasename (char *path)
5 {
6 const char *base;
7 - skip_dos_drive_prefix(&path);
7 +
8 + if (path)
9 + skip_dos_drive_prefix(&path);
10 +
11 + if (!path || !*path)
12 + return ".";
13 +
14 for (base = path; *path; path++) {
9 - if (is_dir_sep(*path))
10 - base = path + 1;
15 + if (!is_dir_sep(*path))
16 + continue;
17 + do {
18 + path++;
19 + } while (is_dir_sep(*path));
20 + if (*path)
21 + base = path;
22 + else
23 + while (--path != base && is_dir_sep(*path))
24 + *path = '\0';
25 }
26 return (char *)base;
27 }