From b4869652775416dde95cfc103d4b03b04828bf60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Thu, 2 Jul 2015 01:02:06 +0200 Subject: kike: resolve the path to PID files better --- common.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'common.c') 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 -- cgit v1.2.3