2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
14 * Checks if the given path exists by calling stat().
17 static bool path_exists(const char *path) {
19 return (stat(path, &buf) == 0);
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,
30 char *get_config_path(const char *override_configpath, bool use_system_paths) {
31 char *xdg_config_home, *xdg_config_dirs, *config_path;
33 static const char *saved_configpath = NULL;
35 if (override_configpath != NULL) {
36 saved_configpath = override_configpath;
37 return sstrdup(saved_configpath);
40 if (saved_configpath != NULL)
41 return sstrdup(saved_configpath);
43 /* 1: check the traditional path under the home directory */
44 config_path = resolve_tilde("~/.i3/config");
45 if (path_exists(config_path))
49 /* 2: check for $XDG_CONFIG_HOME/i3/config */
50 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
51 xdg_config_home = "~/.config";
53 xdg_config_home = resolve_tilde(xdg_config_home);
54 sasprintf(&config_path, "%s/i3/config", xdg_config_home);
55 free(xdg_config_home);
57 if (path_exists(config_path))
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)
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);
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";
75 char *buf = sstrdup(xdg_config_dirs);
76 char *tok = strtok(buf, ":");
78 tok = resolve_tilde(tok);
79 sasprintf(&config_path, "%s/i3/config", tok);
81 if (path_exists(config_path)) {
86 tok = strtok(NULL, ":");