git
1#include "../../git-compat-util.h"2#include "../../environment.h"3
4int win32_has_dos_drive_prefix(const char *path)5{
6int i;7
8/*9* Does it start with an ASCII letter (i.e. highest bit not set),
10* followed by a colon?
11*/
12if (!(0x80 & (unsigned char)*path))13return *path && path[1] == ':' ? 2 : 0;14
15/*16* While drive letters must be letters of the English alphabet, it is
17* possible to assign virtually _any_ Unicode character via `subst` as
18* a drive letter to "virtual drives". Even `1`, or `ä`. Or fun stuff
19* like this:
20*
21* subst ֍: %USERPROFILE%\Desktop
22*/
23for (i = 1; i < 4 && (0x80 & (unsigned char)path[i]); i++)24; /* skip first UTF-8 character */25return path[i] == ':' ? i + 1 : 0;26}
27
28int win32_skip_dos_drive_prefix(char **path)29{
30int ret = has_dos_drive_prefix(*path);31*path += ret;32return ret;33}
34
35int win32_offset_1st_component(const char *path)36{
37char *pos = (char *)path;38
39/* unc paths */40if (!skip_dos_drive_prefix(&pos) &&41is_dir_sep(pos[0]) && is_dir_sep(pos[1])) {42/* skip server name */43pos = strpbrk(pos + 2, "\\/");44if (!pos)45return 0; /* Error: malformed unc path */46
47do {48pos++;49} while (*pos && !is_dir_sep(*pos));50}51
52return pos + is_dir_sep(*pos) - path;53}
54
55int win32_fspathncmp(const char *a, const char *b, size_t count)56{
57int diff;58
59for (;;) {60if (!count--)61return 0;62if (!*a)63return *b ? -1 : 0;64if (!*b)65return +1;66
67if (is_dir_sep(*a)) {68if (!is_dir_sep(*b))69return -1;70a++;71b++;72continue;73} else if (is_dir_sep(*b))74return +1;75
76diff = ignore_case ?77(unsigned char)tolower(*a) - (int)(unsigned char)tolower(*b) :78(unsigned char)*a - (int)(unsigned char)*b;79if (diff)80return diff;81a++;82b++;83}84}
85
86int win32_fspathcmp(const char *a, const char *b)87{
88return win32_fspathncmp(a, b, (size_t)-1);89}
90