Documentation: fix a bunch of typos, both old and new
[git.git] / compat / basename.c
blob96bd9533b448e56176a335a41e32f9bb2dd24d03
1 #include "../git-compat-util.h"
2 #include "../strbuf.h"
4 /* Adapted from libiberty's basename.c. */
5 char *gitbasename (char *path)
7 const char *base;
9 if (path)
10 skip_dos_drive_prefix(&path);
12 if (!path || !*path)
13 return ".";
15 for (base = path; *path; path++) {
16 if (!is_dir_sep(*path))
17 continue;
18 do {
19 path++;
20 } while (is_dir_sep(*path));
21 if (*path)
22 base = path;
23 else
24 while (--path != base && is_dir_sep(*path))
25 *path = '\0';
27 return (char *)base;
30 char *gitdirname(char *path)
32 static struct strbuf buf = STRBUF_INIT;
33 char *p = path, *slash = NULL, c;
34 int dos_drive_prefix;
36 if (!p)
37 return ".";
39 if ((dos_drive_prefix = skip_dos_drive_prefix(&p)) && !*p)
40 goto dot;
43 * POSIX.1-2001 says dirname("/") should return "/", and dirname("//")
44 * should return "//", but dirname("///") should return "/" again.
46 if (is_dir_sep(*p)) {
47 if (!p[1] || (is_dir_sep(p[1]) && !p[2]))
48 return path;
49 slash = ++p;
51 while ((c = *(p++)))
52 if (is_dir_sep(c)) {
53 char *tentative = p - 1;
55 /* POSIX.1-2001 says to ignore trailing slashes */
56 while (is_dir_sep(*p))
57 p++;
58 if (*p)
59 slash = tentative;
62 if (slash) {
63 *slash = '\0';
64 return path;
67 dot:
68 strbuf_reset(&buf);
69 strbuf_addf(&buf, "%.*s.", dos_drive_prefix, path);
70 return buf.buf;