summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorStef Walter <stef@thewalter.net>2013-07-17 11:58:05 +0200
committerStef Walter <stef@thewalter.net>2013-07-18 13:04:35 +0200
commit52a84b84a924a9f1cd8090b0a47b9f7d00ca69f3 (patch)
tree32428cc8b83eaa360b8b53e92f769cac1cf8a17e /common
parent936e4c229a4ed205e9981fc4f31acea063701b69 (diff)
Support expanding $XDG_CONFIG_HOME in user config paths
If ~/.config is specified as a prefix to a configured path, then it is expanded to the $XDG_CONFIG_HOME if that exists Add --with-user-config ./configure option to configure a different user config directory. Interpolate the right directories into documentation.
Diffstat (limited to 'common')
-rw-r--r--common/path.c37
-rw-r--r--common/tests/test-path.c6
2 files changed, 30 insertions, 13 deletions
diff --git a/common/path.c b/common/path.c
index d807301..a22c2a6 100644
--- a/common/path.c
+++ b/common/path.c
@@ -91,19 +91,40 @@ p11_path_base (const char *path)
return strndup (beg, end - beg);
}
+static inline bool
+is_path_component_or_null (char ch)
+{
+ return (ch == '\0' || ch == '/'
+#ifdef OS_WIN32
+ || ch == '\\'
+#endif
+ );
+}
+
static char *
expand_homedir (const char *remainder)
{
const char *env;
- if (remainder[0] == '\0')
- remainder = NULL;
-
if (getauxval (AT_SECURE)) {
errno = EPERM;
return NULL;
}
+ while (remainder[0] && is_path_component_or_null (remainder[0]))
+ remainder++;
+ if (remainder[0] == '\0')
+ remainder = NULL;
+
+ /* Expand $XDG_CONFIG_HOME */
+ if (remainder != NULL &&
+ strncmp (remainder, ".config", 7) == 0 &&
+ is_path_component_or_null (remainder[7])) {
+ env = getenv ("XDG_CONFIG_HOME");
+ if (env && env[0])
+ return p11_path_build (env, remainder + 8, NULL);
+ }
+
env = getenv ("HOME");
if (env && env[0]) {
return p11_path_build (env, remainder, NULL);
@@ -139,16 +160,6 @@ expand_homedir (const char *remainder)
}
}
-static inline bool
-is_path_component_or_null (char ch)
-{
- return (ch == '\0' || ch == '/'
-#ifdef OS_WIN32
- || ch == '\\'
-#endif
- );
-}
-
char *
p11_path_expand (const char *path)
{
diff --git a/common/tests/test-path.c b/common/tests/test-path.c
index 0077cd0..a6ba54d 100644
--- a/common/tests/test-path.c
+++ b/common/tests/test-path.c
@@ -117,6 +117,12 @@ test_expand (void)
p11_path_expand ("~/my/path"));
check_equals_and_free ("/home/blah",
p11_path_expand ("~"));
+ putenv ("XDG_CONFIG_HOME=/my");
+ check_equals_and_free ("/my/path",
+ p11_path_expand ("~/.config/path"));
+ putenv ("XDG_CONFIG_HOME=");
+ check_equals_and_free ("/home/blah/.config/path",
+ p11_path_expand ("~/.config/path"));
#else /* OS_WIN32 */
putenv ("HOME=C:\\Users\\blah");
check_equals_and_free ("C:\\Users\\blah\\path",