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>
This commit is contained in:
Johannes Schindelin 2016-01-12 08:57:30 +01:00 committed by Junio C Hamano
parent 2f36eed936
commit 61725be349

View File

@ -4,10 +4,24 @@
char *gitbasename (char *path) char *gitbasename (char *path)
{ {
const char *base; const char *base;
if (path)
skip_dos_drive_prefix(&path); skip_dos_drive_prefix(&path);
if (!path || !*path)
return ".";
for (base = path; *path; path++) { for (base = path; *path; path++) {
if (is_dir_sep(*path)) if (!is_dir_sep(*path))
base = path + 1; 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; return (char *)base;
} }