aboutsummaryrefslogtreecommitdiff
path: root/common.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2015-07-02 01:02:06 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2015-07-02 01:05:56 +0200
commitb4869652775416dde95cfc103d4b03b04828bf60 (patch)
tree6238e6f01a02a8e649cfaad85e4f9cbc7febe777 /common.c
parent51bf132c6ba429be8778c485076e017d98e2876c (diff)
downloadxK-b4869652775416dde95cfc103d4b03b04828bf60.tar.gz
xK-b4869652775416dde95cfc103d4b03b04828bf60.tar.xz
xK-b4869652775416dde95cfc103d4b03b04828bf60.zip
kike: resolve the path to PID files better
Diffstat (limited to 'common.c')
-rw-r--r--common.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/common.c b/common.c
index cc6221e..7b845d8 100644
--- a/common.c
+++ b/common.c
@@ -90,6 +90,49 @@ strncasecmp_ascii (const char *a, const char *b, size_t n)
return 0;
}
+static char *
+resolve_relative_runtime_filename (const char *filename)
+{
+ struct str path;
+ str_init (&path);
+
+ const char *runtime_dir = getenv ("XDG_RUNTIME_DIR");
+ if (runtime_dir && *runtime_dir == '/')
+ str_append (&path, runtime_dir);
+ else
+ get_xdg_home_dir (&path, "XDG_DATA_HOME", ".local/share");
+ str_append_printf (&path, "/%s/%s", PROGRAM_NAME, filename);
+
+ // Try to create the file's ancestors
+ const char *last_slash = strrchr (path.str, '/');
+ if (last_slash && last_slash != path.str)
+ {
+ char *copy = xstrndup (path.str, last_slash - path.str);
+ (void) mkdir_with_parents (copy, NULL);
+ free (copy);
+ }
+ return str_steal (&path);
+}
+
+static char *
+resolve_filename (const char *filename, char *(*relative_cb) (const char *))
+{
+ // Absolute path is absolute
+ if (*filename == '/')
+ return xstrdup (filename);
+
+ // We don't want to use wordexp() for this as it may execute /bin/sh
+ if (*filename == '~')
+ {
+ // Paths to home directories ought to be absolute
+ char *expanded = try_expand_tilde (filename + 1);
+ if (expanded)
+ return expanded;
+ print_debug ("failed to expand the home directory in `%s'", filename);
+ }
+ return relative_cb (filename);
+}
+
// --- Logging -----------------------------------------------------------------
static void