2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
15 * Checks if the given path exists by calling stat().
18 static bool path_exists(const char *path) {
20 return (stat(path, &buf) == 0);
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,
31 char *get_config_path(const char *override_configpath, bool use_system_paths) {
32 char *xdg_config_home, *xdg_config_dirs, *config_path;
34 static const char *saved_configpath = NULL;
36 if (override_configpath != NULL) {
37 saved_configpath = override_configpath;
38 return sstrdup(saved_configpath);
41 if (saved_configpath != NULL) {
42 return sstrdup(saved_configpath);
45 /* 1: check for $XDG_CONFIG_HOME/i3/config */
46 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) {
47 xdg_config_home = "~/.config";
50 xdg_config_home = resolve_tilde(xdg_config_home);
51 sasprintf(&config_path, "%s/i3/config", xdg_config_home);
52 free(xdg_config_home);
54 if (path_exists(config_path)) {
59 /* 2: check the traditional path under the home directory */
60 config_path = resolve_tilde("~/.i3/config");
61 if (path_exists(config_path)) {
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) {
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";
77 char *buf = sstrdup(xdg_config_dirs);
78 char *tok = strtok(buf, ":");
80 tok = resolve_tilde(tok);
81 sasprintf(&config_path, "%s/i3/config", tok);
83 if (path_exists(config_path)) {
88 tok = strtok(NULL, ":");
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);