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