summaryrefslogtreecommitdiff
path: root/compat/basename.c
diff options
context:
space:
mode:
authorLibravatar Johannes Schindelin <johannes.schindelin@gmx.de>2016-01-12 08:57:30 +0100
committerLibravatar Junio C Hamano <gitster@pobox.com>2016-01-12 10:40:27 -0800
commit61725be349b44f15b0239182c859553d5c547ba0 (patch)
treef85a67129159f2f0288ab7d6c21594ee6a9a91be /compat/basename.c
parentRefactor skipping DOS drive prefixes (diff)
downloadtgif-61725be349b44f15b0239182c859553d5c547ba0.tar.xz
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>
Diffstat (limited to 'compat/basename.c')
-rw-r--r--compat/basename.c20
1 files changed, 17 insertions, 3 deletions
diff --git a/compat/basename.c b/compat/basename.c
index 9f00421a26..0f1b0b0930 100644
--- a/compat/basename.c
+++ b/compat/basename.c
@@ -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;
}