Skip to content

Commit 78ce6bc

Browse files
committed
fixup! Provide a dirname() function when NO_LIBGEN_H=YesPlease
1 parent 8a71fd7 commit 78ce6bc

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

compat/basename.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,15 @@ char *gitbasename (char *path)
2929

3030
char *gitdirname(char *path)
3131
{
32-
char *p = path, *after_slash = NULL, c;
32+
static struct strbuf buf = STRBUF_INIT;
33+
char *p = path, *slash = NULL, c;
3334
int dos_drive_prefix;
3435

3536
if (!p)
3637
return ".";
3738

38-
if ((dos_drive_prefix = skip_dos_drive_prefix(&p)) && !*p) {
39-
static struct strbuf buf = STRBUF_INIT;
40-
41-
dot:
42-
strbuf_reset(&buf);
43-
strbuf_addf(&buf, "%.*s.", dos_drive_prefix, path);
44-
return buf.buf;
45-
}
39+
if ((dos_drive_prefix = skip_dos_drive_prefix(&p)) && !*p)
40+
goto dot;
4641

4742
/*
4843
* POSIX.1-2001 says dirname("/") should return "/", and dirname("//")
@@ -51,7 +46,7 @@ char *gitdirname(char *path)
5146
if (is_dir_sep(*p)) {
5247
if (!p[1] || (is_dir_sep(p[1]) && !p[2]))
5348
return path;
54-
after_slash = ++p;
49+
slash = ++p;
5550
}
5651
while ((c = *(p++)))
5752
if (is_dir_sep(c)) {
@@ -61,11 +56,16 @@ char *gitdirname(char *path)
6156
while (is_dir_sep(*p))
6257
p++;
6358
if (*p)
64-
after_slash = tentative;
59+
slash = tentative;
6560
}
6661

67-
if (!after_slash)
68-
goto dot;
69-
*after_slash = '\0';
70-
return path;
62+
if (slash) {
63+
*slash = '\0';
64+
return path;
65+
}
66+
67+
dot:
68+
strbuf_reset(&buf);
69+
strbuf_addf(&buf, "%.*s.", dos_drive_prefix, path);
70+
return buf.buf;
7171
}

0 commit comments

Comments
 (0)