From 578e943afcd9c818f969502a94375b1a70548bf9 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 23 Mar 2019 08:38:21 +0000 Subject: win32: share code to find root prefix of path Move unc_root_len() from ash to mingw32.c and use it in the new function root_len(), which can be used in make_directory(). This reduces changes to upstream code and saves a few bytes. --- win32/mingw.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'win32') diff --git a/win32/mingw.c b/win32/mingw.c index 0206f6dca..3ee1a2496 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -1609,3 +1609,38 @@ void hide_console(void) } } #endif + +#define is_path_sep(x) ((x) == '/' || (x) == '\\') +#define is_unc_path(x) (is_path_sep(x[0]) && is_path_sep(x[1])) + +/* Return the length of the root of a UNC path, i.e. the '//host/share' + * component, or 0 if the path doesn't look like that. */ +int unc_root_len(const char *dir) +{ + const char *s = dir + 2; + int len; + + if (!is_unc_path(dir)) + return 0; + len = strcspn(s, "/\\"); + if (len == 0) + return 0; + s += len + 1; + len = strcspn(s, "/\\"); + if (len == 0) + return 0; + s += len; + + return s - dir; +} + +/* Return the length of the root of a path, i.e. either the drive or + * UNC '//host/share', or 0 if the path doesn't look like that. */ +int root_len(const char *path) +{ + if (path == NULL) + return 0; + if (isalpha(*path) && path[1] == ':') + return 2; + return unc_root_len(path); +} -- cgit v1.2.3-55-g6feb