diff --git a/compat/basename.c b/compat/basename.c index 3ba9227f8f..96bd9533b4 100644 --- a/compat/basename.c +++ b/compat/basename.c @@ -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; }