mirror of
https://github.com/git-for-windows/git.git
synced 2026-03-25 01:21:26 -05:00
compat/basename: make basename() conform to POSIX
According to POSIX, basename("/path/") should return "path", not
"path/". Likewise, basename(NULL) and basename("abc") should both
return ".".
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
@@ -4,10 +4,24 @@
|
||||
char *gitbasename (char *path)
|
||||
{
|
||||
const char *base;
|
||||
skip_dos_drive_prefix(&path);
|
||||
|
||||
if (path)
|
||||
skip_dos_drive_prefix(&path);
|
||||
|
||||
if (!path || !*path)
|
||||
return ".";
|
||||
|
||||
for (base = path; *path; path++) {
|
||||
if (is_dir_sep(*path))
|
||||
base = path + 1;
|
||||
if (!is_dir_sep(*path))
|
||||
continue;
|
||||
do {
|
||||
path++;
|
||||
} while (is_dir_sep(*path));
|
||||
if (*path)
|
||||
base = path;
|
||||
else
|
||||
while (--path != base && is_dir_sep(*path))
|
||||
*path = '\0';
|
||||
}
|
||||
return (char *)base;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user