blob: 96bd9533b448e56176a335a41e32f9bb2dd24d03 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#include "../git-compat-util.h"
#include "../strbuf.h"
/* Adapted from libiberty's basename.c. */
char *gitbasename (char *path)
{
const char *base;
if (path)
skip_dos_drive_prefix(&path);
if (!path || !*path)
return ".";
for (base = path; *path; path++) {
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;
}
char *gitdirname(char *path)
{
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)
goto dot;
/*
* POSIX.1-2001 says dirname("/") should return "/", and dirname("//")
* should return "//", but dirname("///") should return "/" again.
*/
if (is_dir_sep(*p)) {
if (!p[1] || (is_dir_sep(p[1]) && !p[2]))
return path;
slash = ++p;
}
while ((c = *(p++)))
if (is_dir_sep(c)) {
char *tentative = p - 1;
/* POSIX.1-2001 says to ignore trailing slashes */
while (is_dir_sep(*p))
p++;
if (*p)
slash = tentative;
}
if (slash) {
*slash = '\0';
return path;
}
dot:
strbuf_reset(&buf);
strbuf_addf(&buf, "%.*s.", dos_drive_prefix, path);
return buf.buf;
}
|