Subject: Re: [PATCH v3 2/3] relative_path should honor DOS and UNC paths

From: Torsten Bögershausen <[email-masked]>
Date: Wed, 18 Sep 2013 17:09:59 +0200

On 2013-09-18 16.37, Torsten Bögershausen wrote:
> On 2013-09-17 18.12, Junio C Hamano wrote:
>> Jiang Xin <[email-masked]> writes:
>>
>>> diff --git a/compat/mingw.h b/compat/mingw.h
>>> index bd0a88b..06e9f49 100644
>>> --- a/compat/mingw.h
>>> +++ b/compat/mingw.h
>>> @@ -311,6 +311,15 @@ int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format
>>>  
>>>  #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
>>>  #define is_dir_sep(c) ((c) == '/' || (c) == '\\')
>>> +static inline int is_unc_path(const char *path)
>>> +{
>>> +	if (!is_dir_sep(*path) || !is_dir_sep(*(path+1)) || is_dir_sep(*(path+2)))
>>> +		return 0;
>>
>> If path[1] == '\0', it would be !is_dir_sep() and we end up
>> inspecting past the end of the string?
Yes
(If there was a previous mail, it was incomplete, sorry)

I think we want to catch "2 (back)slashes followed by a letter"
<http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx>

#define is_unc_path(path) ((is_dir_sep(path)[0]) && is_dir_sep((path)[1]) && isalpha((path[2])))

Then we need 
#define is_relative_path(path)  (((path)[0]) && !is_dir_sep((path)[1]))

And may be like this:

static int have_same_root(const char *path1, const char *path2)
{
	int is_abs1, is_abs2;

	is_abs1 = is_absolute_path(path1);
	is_abs2 = is_absolute_path(path2);
	if (is_abs1 && is_abs2) {
		if (is_unc_path(path1) && !is_relative_path(path2))
			return 0;
		if (!is_relative_path(path1) && is_unc_path(path2))
			return 0;
		return tolower(path1[0]) == tolower(path2[0]);
	} else {
		return !is_abs1 && !is_abs2;
	}
}

Could that work?