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
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,
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);
44 /* 1: check the traditional path under the home directory */
45 config_path = resolve_tilde("~/.i3/config");
46 if (path_exists(config_path))
50 /* 2: check for $XDG_CONFIG_HOME/i3/config */
51 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
52 xdg_config_home = "~/.config";
54 xdg_config_home = resolve_tilde(xdg_config_home);
55 sasprintf(&config_path, "%s/i3/config", xdg_config_home);
56 free(xdg_config_home);
58 if (path_exists(config_path))
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)
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);
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";
76 char *buf = sstrdup(xdg_config_dirs);
77 char *tok = strtok(buf, ":");
79 tok = resolve_tilde(tok);
80 sasprintf(&config_path, "%s/i3/config", tok);
82 if (path_exists(config_path)) {
87 tok = strtok(NULL, ":");