]> git.sur5r.net Git - i3/i3/blob - libi3/get_config_path.c
efece5cde792f44949423897983445a033865dd1
[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
25  * is specified, that path is returned and saved for further calls. Otherwise,
26  * checks the home directory first, then the system directory first, always
27  * taking 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     /* 1: check the traditional path under the home directory */
45     config_path = resolve_tilde("~/.i3/config");
46     if (path_exists(config_path))
47         return config_path;
48     free(config_path);
49
50     /* 2: check for $XDG_CONFIG_HOME/i3/config */
51     if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
52         xdg_config_home = "~/.config";
53
54     xdg_config_home = resolve_tilde(xdg_config_home);
55     sasprintf(&config_path, "%s/i3/config", xdg_config_home);
56     free(xdg_config_home);
57
58     if (path_exists(config_path))
59         return config_path;
60     free(config_path);
61
62     /* The below paths are considered system-level, and can be skipped if the
63      * caller only wants user-level configs. */
64     if (!use_system_paths)
65         return NULL;
66
67     /* 3: check the traditional path under /etc */
68     config_path = SYSCONFDIR "/i3/config";
69     if (path_exists(config_path))
70         return sstrdup(config_path);
71
72     /* 4: 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     char *buf = sstrdup(xdg_config_dirs);
77     char *tok = strtok(buf, ":");
78     while (tok != NULL) {
79         tok = resolve_tilde(tok);
80         sasprintf(&config_path, "%s/i3/config", tok);
81         free(tok);
82         if (path_exists(config_path)) {
83             free(buf);
84             return config_path;
85         }
86         free(config_path);
87         tok = strtok(NULL, ":");
88     }
89     free(buf);
90
91     return NULL;
92 }