]> git.sur5r.net Git - i3/i3/blob - libi3/get_config_path.c
Move resolve_tilde and get_config_path into libi3
[i3/i3] / libi3 / get_config_path.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2015 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include "libi3.h"
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/stat.h>
12
13 /*
14  * Checks if the given path exists by calling stat().
15  *
16  */
17 static bool path_exists(const char *path) {
18     struct stat buf;
19     return (stat(path, &buf) == 0);
20 }
21
22 /*
23  * Get the path of the first configuration file found. If override_configpath
24  * is specified, that path is returned and saved for further calls. Otherwise,
25  * checks the home directory first, then the system directory first, always
26  * taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
27  * $XDG_CONFIG_DIRS)
28  *
29  */
30 char *get_config_path(const char *override_configpath, bool use_system_paths) {
31     char *xdg_config_home, *xdg_config_dirs, *config_path;
32
33     static const char *saved_configpath = NULL;
34
35     if (override_configpath != NULL) {
36         saved_configpath = override_configpath;
37         return sstrdup(saved_configpath);
38     }
39
40     if (saved_configpath != NULL)
41         return sstrdup(saved_configpath);
42
43     /* 1: check the traditional path under the home directory */
44     config_path = resolve_tilde("~/.i3/config");
45     if (path_exists(config_path))
46         return config_path;
47     free(config_path);
48
49     /* 2: check for $XDG_CONFIG_HOME/i3/config */
50     if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
51         xdg_config_home = "~/.config";
52
53     xdg_config_home = resolve_tilde(xdg_config_home);
54     sasprintf(&config_path, "%s/i3/config", xdg_config_home);
55     free(xdg_config_home);
56
57     if (path_exists(config_path))
58         return config_path;
59     free(config_path);
60
61     /* The below paths are considered system-level, and can be skipped if the
62      * caller only wants user-level configs. */
63     if (!use_system_paths)
64         return NULL;
65
66     /* 3: check the traditional path under /etc */
67     config_path = SYSCONFDIR "/i3/config";
68     if (path_exists(config_path))
69         return sstrdup(config_path);
70
71     /* 4: check for $XDG_CONFIG_DIRS/i3/config */
72     if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
73         xdg_config_dirs = "/etc/xdg";
74
75     char *buf = sstrdup(xdg_config_dirs);
76     char *tok = strtok(buf, ":");
77     while (tok != NULL) {
78         tok = resolve_tilde(tok);
79         sasprintf(&config_path, "%s/i3/config", tok);
80         free(tok);
81         if (path_exists(config_path)) {
82             free(buf);
83             return config_path;
84         }
85         free(config_path);
86         tok = strtok(NULL, ":");
87     }
88     free(buf);
89
90     return NULL;
91 }