]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
utils: Support Windows path separators in path_basename/dirname
authorMartin Willi <martin@revosec.ch>
Fri, 7 Mar 2014 11:12:55 +0000 (12:12 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 4 Jun 2014 13:53:08 +0000 (15:53 +0200)
src/libstrongswan/tests/suites/test_utils.c
src/libstrongswan/utils/utils.c

index 0260726b21923d551db6b8f978254c45c4678f64..04f0c46402c503473f553dcb1fca8b9f1a6e0dfc 100644 (file)
@@ -520,6 +520,24 @@ static struct {
        {"", ".", "."},
        {".", ".", "."},
        {"..", ".", ".."},
+#ifdef WIN32
+       {"C:\\", "C:\\", "C:\\"},
+       {"C:\\\\", "C:\\", "C:\\"},
+       {"foo", ".", "foo"},
+       {"f\\", ".", "f"},
+       {"foo\\", ".", "foo"},
+       {"foo\\\\", ".", "foo"},
+       {"C:\\f", "C:\\", "f"},
+       {"C:\\f\\", "\\", "f"},
+       {"C:\\foo", "C:\\", "foo"},
+       {"C:\\foo\\", "C:\\", "foo"},
+       {"foo\\bar", "foo", "bar"},
+       {"foo\\\\bar", "foo", "bar"},
+       {"C:\\foo\\bar", "C:\\foo", "bar"},
+       {"C:\\foo\\bar\\", "C:\\foo", "bar"},
+       {"C:\\foo\\bar\\baz", "C:\\foo\\bar", "baz"},
+       {"\\foo\\bar", "\\foo", "bar"},
+#else /* !WIN32 */
        {"/", "/", "/"},
        {"//", "/", "/"},
        {"foo", ".", "foo"},
@@ -536,6 +554,7 @@ static struct {
        {"/foo/bar", "/foo", "bar"},
        {"/foo/bar/", "/foo", "bar"},
        {"/foo/bar/baz", "/foo/bar", "baz"},
+#endif
 };
 
 START_TEST(test_path_dirname)
index dc0608627a5b6693d01f08fb3e812d18f70cb571..1b3765a6937604a7d99be49bf8a5f387c8132aab 100644 (file)
@@ -229,21 +229,21 @@ char* path_dirname(const char *path)
 {
        char *pos;
 
-       pos = path ? strrchr(path, '/') : NULL;
+       pos = path ? strrchr(path, DIRECTORY_SEPARATOR[0]) : NULL;
 
        if (pos && !pos[1])
        {       /* if path ends with slashes we have to look beyond them */
-               while (pos > path && *pos == '/')
+               while (pos > path && *pos == DIRECTORY_SEPARATOR[0])
                {       /* skip trailing slashes */
                        pos--;
                }
-               pos = memrchr(path, '/', pos - path + 1);
+               pos = memrchr(path, DIRECTORY_SEPARATOR[0], pos - path + 1);
        }
        if (!pos)
        {
                return strdup(".");
        }
-       while (pos > path && *pos == '/')
+       while (pos > path && *pos == DIRECTORY_SEPARATOR[0])
        {       /* skip superfluous slashes */
                pos--;
        }
@@ -261,19 +261,19 @@ char* path_basename(const char *path)
        {
                return strdup(".");
        }
-       pos = strrchr(path, '/');
+       pos = strrchr(path, DIRECTORY_SEPARATOR[0]);
        if (pos && !pos[1])
        {       /* if path ends with slashes we have to look beyond them */
-               while (pos > path && *pos == '/')
+               while (pos > path && *pos == DIRECTORY_SEPARATOR[0])
                {       /* skip trailing slashes */
                        pos--;
                }
-               if (pos == path && *pos == '/')
+               if (pos == path && *pos == DIRECTORY_SEPARATOR[0])
                {       /* contains only slashes */
-                       return strdup("/");
+                       return strdup(DIRECTORY_SEPARATOR);
                }
                trail = pos + 1;
-               pos = memrchr(path, '/', trail - path);
+               pos = memrchr(path, DIRECTORY_SEPARATOR[0], trail - path);
        }
        pos = pos ? pos + 1 : (char*)path;
        return trail ? strndup(pos, trail - pos) : strdup(pos);