fixup! Provide a dirname() function when NO_LIBGEN_H=YesPlease

This commit is contained in:
Johannes Schindelin
2016-01-12 08:54:19 +01:00
parent 8a71fd7ab0
commit 78ce6bc19e

View File

@@ -29,20 +29,15 @@ char *gitbasename (char *path)
char *gitdirname(char *path)
{
char *p = path, *after_slash = NULL, c;
static struct strbuf buf = STRBUF_INIT;
char *p = path, *slash = NULL, c;
int dos_drive_prefix;
if (!p)
return ".";
if ((dos_drive_prefix = skip_dos_drive_prefix(&p)) && !*p) {
static struct strbuf buf = STRBUF_INIT;
dot:
strbuf_reset(&buf);
strbuf_addf(&buf, "%.*s.", dos_drive_prefix, path);
return buf.buf;
}
if ((dos_drive_prefix = skip_dos_drive_prefix(&p)) && !*p)
goto dot;
/*
* POSIX.1-2001 says dirname("/") should return "/", and dirname("//")
@@ -51,7 +46,7 @@ dot:
if (is_dir_sep(*p)) {
if (!p[1] || (is_dir_sep(p[1]) && !p[2]))
return path;
after_slash = ++p;
slash = ++p;
}
while ((c = *(p++)))
if (is_dir_sep(c)) {
@@ -61,11 +56,16 @@ dot:
while (is_dir_sep(*p))
p++;
if (*p)
after_slash = tentative;
slash = tentative;
}
if (!after_slash)
goto dot;
*after_slash = '\0';
return path;
if (slash) {
*slash = '\0';
return path;
}
dot:
strbuf_reset(&buf);
strbuf_addf(&buf, "%.*s.", dos_drive_prefix, path);
return buf.buf;
}