aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2019-03-23 08:38:21 +0000
committerRon Yorston <rmy@pobox.com>2019-03-23 08:38:21 +0000
commit578e943afcd9c818f969502a94375b1a70548bf9 (patch)
treef202f7ae8d81109e6c87ab4422eb5c6ece3f2ed3
parenta8c63f25b3a8d4b8c9e12b8f6db65c61596da602 (diff)
downloadbusybox-w32-578e943afcd9c818f969502a94375b1a70548bf9.tar.gz
busybox-w32-578e943afcd9c818f969502a94375b1a70548bf9.tar.bz2
busybox-w32-578e943afcd9c818f969502a94375b1a70548bf9.zip
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.
-rw-r--r--include/mingw.h3
-rw-r--r--libbb/make_directory.c25
-rw-r--r--shell/ash.c30
-rw-r--r--win32/mingw.c35
4 files changed, 42 insertions, 51 deletions
diff --git a/include/mingw.h b/include/mingw.h
index 36c2f6805..7c9423cad 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -522,3 +522,6 @@ ULONGLONG CompatGetTickCount64(void);
522ssize_t get_random_bytes(void *buf, ssize_t count); 522ssize_t get_random_bytes(void *buf, ssize_t count);
523int enumerate_links(const char *file, char *name); 523int enumerate_links(const char *file, char *name);
524void hide_console(void); 524void hide_console(void);
525
526int unc_root_len(const char *dir);
527int root_len(const char *path);
diff --git a/libbb/make_directory.c b/libbb/make_directory.c
index 9af5552d5..e0fd486d8 100644
--- a/libbb/make_directory.c
+++ b/libbb/make_directory.c
@@ -59,29 +59,8 @@ int FAST_FUNC bb_make_directory(char *path, long mode, int flags)
59 59
60 if (flags & FILEUTILS_RECUR) { /* Get the parent */ 60 if (flags & FILEUTILS_RECUR) { /* Get the parent */
61#if ENABLE_PLATFORM_MINGW32 61#if ENABLE_PLATFORM_MINGW32
62 if (s == path && *s && s[1] == ':') { 62 if (s == path)
63 /* skip drive letter */ 63 s += root_len(path);
64 s += 2;
65 }
66 else if (s == path && s[0] == '/' && s[1] == '/' ) {
67 /* skip UNC server and share */
68 int count = 0;
69 s += 2;
70 while (*s) {
71 if (*s == '/') {
72 do {
73 ++s;
74 } while (*s == '/');
75 if (++count == 2) {
76 --s;
77 break;
78 }
79 }
80 else {
81 ++s;
82 }
83 }
84 }
85#endif 64#endif
86 /* Bypass leading non-'/'s and then subsequent '/'s */ 65 /* Bypass leading non-'/'s and then subsequent '/'s */
87 while (*s) { 66 while (*s) {
diff --git a/shell/ash.c b/shell/ash.c
index cd6196640..34ddc14f1 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -2860,34 +2860,6 @@ cdopt(void)
2860 return flags; 2860 return flags;
2861} 2861}
2862 2862
2863#if ENABLE_PLATFORM_MINGW32
2864#define is_path_sep(x) ((x) == '/' || (x) == '\\')
2865#define is_unc_path(x) (is_path_sep(x[0]) && is_path_sep(x[1]))
2866#define is_root(x) (is_path_sep(x[0]) && x[1] == '\0')
2867
2868/* Return the length of the root of a UNC path, i.e. the '//host/share'
2869 * component, or 0 if the path doesn't look like that. */
2870static int
2871unc_root_len(const char *dir)
2872{
2873 const char *s = dir + 2;
2874 int len;
2875
2876 if (!is_unc_path(dir))
2877 return 0;
2878 len = strcspn(s, "/\\");
2879 if (len == 0)
2880 return 0;
2881 s += len + 1;
2882 len = strcspn(s, "/\\");
2883 if (len == 0)
2884 return 0;
2885 s += len;
2886
2887 return s - dir;
2888}
2889#endif
2890
2891/* 2863/*
2892 * Update curdir (the name of the current directory) in response to a 2864 * Update curdir (the name of the current directory) in response to a
2893 * cd command. 2865 * cd command.
@@ -2896,6 +2868,8 @@ static const char *
2896updatepwd(const char *dir) 2868updatepwd(const char *dir)
2897{ 2869{
2898#if ENABLE_PLATFORM_MINGW32 2870#if ENABLE_PLATFORM_MINGW32
2871# define is_path_sep(x) ((x) == '/' || (x) == '\\')
2872# define is_root(x) (is_path_sep(x[0]) && x[1] == '\0')
2899 /* 2873 /*
2900 * Due to Windows drive notion, getting pwd is a completely 2874 * Due to Windows drive notion, getting pwd is a completely
2901 * different thing. Handle it in a separate routine 2875 * different thing. Handle it in a separate routine
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)
1609 } 1609 }
1610} 1610}
1611#endif 1611#endif
1612
1613#define is_path_sep(x) ((x) == '/' || (x) == '\\')
1614#define is_unc_path(x) (is_path_sep(x[0]) && is_path_sep(x[1]))
1615
1616/* Return the length of the root of a UNC path, i.e. the '//host/share'
1617 * component, or 0 if the path doesn't look like that. */
1618int unc_root_len(const char *dir)
1619{
1620 const char *s = dir + 2;
1621 int len;
1622
1623 if (!is_unc_path(dir))
1624 return 0;
1625 len = strcspn(s, "/\\");
1626 if (len == 0)
1627 return 0;
1628 s += len + 1;
1629 len = strcspn(s, "/\\");
1630 if (len == 0)
1631 return 0;
1632 s += len;
1633
1634 return s - dir;
1635}
1636
1637/* Return the length of the root of a path, i.e. either the drive or
1638 * UNC '//host/share', or 0 if the path doesn't look like that. */
1639int root_len(const char *path)
1640{
1641 if (path == NULL)
1642 return 0;
1643 if (isalpha(*path) && path[1] == ':')
1644 return 2;
1645 return unc_root_len(path);
1646}