Merge branch 'vs/complete-stash-show-p-fix'
[git] / compat / win32 / path-utils.c
1 #include "../../git-compat-util.h"
2
3 int win32_has_dos_drive_prefix(const char *path)
4 {
5         int i;
6
7         /*
8          * Does it start with an ASCII letter (i.e. highest bit not set),
9          * followed by a colon?
10          */
11         if (!(0x80 & (unsigned char)*path))
12                 return *path && path[1] == ':' ? 2 : 0;
13
14         /*
15          * While drive letters must be letters of the English alphabet, it is
16          * possible to assign virtually _any_ Unicode character via `subst` as
17          * a drive letter to "virtual drives". Even `1`, or `ä`. Or fun stuff
18          * like this:
19          *
20          *      subst ֍: %USERPROFILE%\Desktop
21          */
22         for (i = 1; i < 4 && (0x80 & (unsigned char)path[i]); i++)
23                 ; /* skip first UTF-8 character */
24         return path[i] == ':' ? i + 1 : 0;
25 }
26
27 int win32_skip_dos_drive_prefix(char **path)
28 {
29         int ret = has_dos_drive_prefix(*path);
30         *path += ret;
31         return ret;
32 }
33
34 int win32_offset_1st_component(const char *path)
35 {
36         char *pos = (char *)path;
37
38         /* unc paths */
39         if (!skip_dos_drive_prefix(&pos) &&
40                         is_dir_sep(pos[0]) && is_dir_sep(pos[1])) {
41                 /* skip server name */
42                 pos = strpbrk(pos + 2, "\\/");
43                 if (!pos)
44                         return 0; /* Error: malformed unc path */
45
46                 do {
47                         pos++;
48                 } while (*pos && !is_dir_sep(*pos));
49         }
50
51         return pos + is_dir_sep(*pos) - path;
52 }