diff options
-rw-r--r-- | common/path.c | 16 | ||||
-rw-r--r-- | common/tests/test-path.c | 46 |
2 files changed, 37 insertions, 25 deletions
diff --git a/common/path.c b/common/path.c index 3f1fccc..d3d881d 100644 --- a/common/path.c +++ b/common/path.c @@ -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); diff --git a/common/tests/test-path.c b/common/tests/test-path.c index 54d6f29..f1bccbd 100644 --- a/common/tests/test-path.c +++ b/common/tests/test-path.c @@ -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 |