Skip to content

Commit 61725be

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2f36eed commit 61725be

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

compat/basename.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,24 @@
44
char *gitbasename (char *path)
55
{
66
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+
814
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';
1125
}
1226
return (char *)base;
1327
}

0 commit comments

Comments
 (0)