Skip to content

Commit

Permalink
compat/basename: make basename() conform to POSIX
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dscho authored and gitster committed Jan 12, 2016
1 parent 2f36eed commit 61725be
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions compat/basename.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 61725be

Please sign in to comment.