]> granicus.if.org Git - p11-kit/commitdiff
path: Fix expanding of paths and tests
authorStef Walter <stef@thewalter.net>
Fri, 14 Jun 2013 10:49:34 +0000 (12:49 +0200)
committerStef Walter <stef@thewalter.net>
Fri, 14 Jun 2013 10:49:34 +0000 (12:49 +0200)
common/path.c
common/tests/test-path.c

index 3f1fccccc756236e728b408f0c0216fc125ebdf1..d3d881d63689e06ea19769215063b5bc40ab308b 100644 (file)
@@ -97,6 +97,9 @@ expand_homedir (const char *remainder)
 {
        const char *env;
 
+       if (remainder[0] == '\0')
+               remainder = NULL;
+
        env = getenv ("HOME");
        if (env && env[0]) {
                return p11_path_build (env, remainder, NULL);
@@ -137,6 +140,9 @@ expand_tempdir (const char *remainder)
 {
        const char *env;
 
+       if (remainder[0] == '\0')
+               remainder = NULL;
+
        env = getenv ("TEMP");
        if (env && env[0]) {
                return p11_path_build (env, remainder, NULL);
@@ -164,10 +170,10 @@ expand_tempdir (const char *remainder)
        }
 }
 
-static bool
+static inline bool
 is_path_component_or_null (char ch)
 {
-       return (ch == '0' || ch == '/'
+       return (ch == '\0' || ch == '/'
 #ifdef OS_WIN32
                        || ch == '\\'
 #endif
@@ -181,15 +187,15 @@ p11_path_expand (const char *path)
 
        if (strncmp (path, "~", 1) == 0 &&
            is_path_component_or_null (path[1])) {
-               return expand_homedir (path + 2);
+               return expand_homedir (path + 1);
 
        } else if (strncmp (path, "$HOME", 5) == 0 &&
            is_path_component_or_null (path[5])) {
-               return expand_homedir (path + 6);
+               return expand_homedir (path + 5);
 
        } else if (strncmp (path, "$TEMP", 5) == 0 &&
            is_path_component_or_null (path[5])) {
-               return expand_tempdir (path + 6);
+               return expand_tempdir (path + 5);
 
        } else {
                return strdup (path);
index 54d6f294adbe7f55d2f47457e268073a3e713d6f..f1bccbd517a657b61b5ebb870adf511bd5573eed 100644 (file)
@@ -75,33 +75,33 @@ test_base (void)
        }
 }
 
-#define check_equals_and_free(tc, ex, ac) \
+#define check_equals_and_free(ex, ac) \
        do { assert_str_eq (ex, ac); free (ac); } while (0)
 
 static void
 test_build (void)
 {
 #ifdef OS_UNIX
-       check_equals_and_free (tc, "/root/second",
+       check_equals_and_free ("/root/second",
                               p11_path_build ("/root", "second", NULL));
-       check_equals_and_free (tc, "/root/second",
+       check_equals_and_free ("/root/second",
                               p11_path_build ("/root", "/second", NULL));
-       check_equals_and_free (tc, "/root/second",
+       check_equals_and_free ("/root/second",
                               p11_path_build ("/root/", "second", NULL));
-       check_equals_and_free (tc, "/root/second/third",
+       check_equals_and_free ("/root/second/third",
                               p11_path_build ("/root", "second", "third", NULL));
-       check_equals_and_free (tc, "/root/second/third",
+       check_equals_and_free ("/root/second/third",
                               p11_path_build ("/root", "/second/third", NULL));
 #else /* OS_WIN32 */
-       check_equals_and_free (tc, "C:\\root\\second",
+       check_equals_and_free ("C:\\root\\second",
                               p11_path_build ("C:\\root", "second", NULL));
-       check_equals_and_free (tc, "C:\\root\\second",
+       check_equals_and_free ("C:\\root\\second",
                               p11_path_build ("C:\\root", "\\second", NULL));
-       check_equals_and_free (tc, "C:\\root\\second",
+       check_equals_and_free ("C:\\root\\second",
                               p11_path_build ("C:\\root\\", "second", NULL));
-       check_equals_and_free (tc, "C:\\root\\second\\third",
+       check_equals_and_free ("C:\\root\\second\\third",
                               p11_path_build ("C:\\root", "second", "third", NULL));
-       check_equals_and_free (tc, "C:\\root\\second/third",
+       check_equals_and_free ("C:\\root\\second/third",
                               p11_path_build ("C:\\root", "second/third", NULL));
 #endif
 }
@@ -113,28 +113,34 @@ test_expand (void)
 
 #ifdef OS_UNIX
        putenv ("HOME=/home/blah");
-       check_equals_and_free (tc, "/home/blah/my/path",
+       check_equals_and_free ("/home/blah/my/path",
                               p11_path_expand ("$HOME/my/path"));
-       check_equals_and_free (tc, "/home/blah/my/path",
+       check_equals_and_free ("/home/blah/my/path",
                               p11_path_expand ("~/my/path"));
+       check_equals_and_free ("/home/blah",
+                              p11_path_expand ("$HOME"));
+       check_equals_and_free ("/home/blah",
+                              p11_path_expand ("~"));
        putenv ("TEMP=/tmpdir");
-       check_equals_and_free (tc, "/tmpdir/my/path",
+       check_equals_and_free ("/tmpdir/my/path",
                               p11_path_expand ("$TEMP/my/path"));
+       check_equals_and_free ("/tmpdir",
+                              p11_path_expand ("$TEMP"));
 #else /* OS_WIN32 */
        putenv ("HOME=C:\\Users\\blah");
-       check_equals_and_free (tc, "C:\\Users\\blah\\path",
+       check_equals_and_free ("C:\\Users\\blah\\path",
                               p11_path_expand ("$HOME/path"));
-       check_equals_and_free (tc, "C:\\Users\\blah\\path",
+       check_equals_and_free ("C:\\Users\\blah\\path",
                               p11_path_expand ("$HOME\\path"));
-       check_equals_and_free (tc, "C:\\Users\\blah\\path",
+       check_equals_and_free ("C:\\Users\\blah\\path",
                               p11_path_expand ("~/path"));
-       check_equals_and_free (tc, "C:\\Users\\blah\\path",
+       check_equals_and_free ("C:\\Users\\blah\\path",
                               p11_path_expand ("~\\path"));
 
        putenv ("TEMP=C:\\Temp Directory");
-       check_equals_and_free (tc, "C:\\Temp Directory\\path",
+       check_equals_and_free ("C:\\Temp Directory\\path",
                               p11_path_expand ("$TEMP/path"));
-       check_equals_and_free (tc, "C:\\Temp Directory\\path",
+       check_equals_and_free ("C:\\Temp Directory\\path",
                               p11_path_expand ("$TEMP\\path"));
 #endif