diff options
author | taiyu <taiyu.len@gmail.com> | 2015-09-20 10:56:22 -0700 |
---|---|---|
committer | taiyu <taiyu.len@gmail.com> | 2015-09-20 10:56:22 -0700 |
commit | 72aaffcf5b96f7d20438191e17c1df7174e52e66 (patch) | |
tree | e8064bd9dfe9db50ff78c465e7ff7c38edb60ce7 | |
parent | container_move check (diff) | |
download | sway-72aaffcf5b96f7d20438191e17c1df7174e52e66.tar.gz sway-72aaffcf5b96f7d20438191e17c1df7174e52e66.tar.zst sway-72aaffcf5b96f7d20438191e17c1df7174e52e66.zip |
fix config search paths
-rw-r--r-- | sway/config.c | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/sway/config.c b/sway/config.c index 0026e0af..46a26424 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -110,47 +110,49 @@ static void config_defaults(struct sway_config *config) { | |||
110 | 110 | ||
111 | static char *get_config_path(void) { | 111 | static char *get_config_path(void) { |
112 | char *config_path = NULL; | 112 | char *config_path = NULL; |
113 | char *paths[3] = {getenv("HOME"), getenv("XDG_CONFIG_HOME"), ""}; | 113 | char *paths[3] = { getenv("HOME"), getenv("XDG_CONFIG_HOME"), "" }; |
114 | int pathlen[3] = {0, 0, 0}; | 114 | int pathlen[3] = { 0, 0, 0 }; |
115 | int i; | 115 | int i; |
116 | #define home paths[0] | 116 | #define home paths[0] |
117 | #define conf paths[1] | 117 | #define conf paths[1] |
118 | // Get home and config directories | 118 | // Get home and config directories |
119 | conf = conf ? strdup(conf) : NULL; | ||
119 | home = home ? strdup(home) : NULL; | 120 | home = home ? strdup(home) : NULL; |
120 | if (conf) { | 121 | // If config folder is unset, set it to $HOME/.config |
121 | conf = strdup(conf); | 122 | if (!conf && home) { |
122 | } else if (home) { | ||
123 | const char *def = "/.config"; | 123 | const char *def = "/.config"; |
124 | conf = malloc(strlen(home) + strlen(def) + 1); | 124 | conf = malloc(strlen(home) + strlen(def) + 1); |
125 | strcpy(conf, home); | 125 | strcpy(conf, home); |
126 | strcat(conf, def); | 126 | strcat(conf, def); |
127 | } else { | ||
128 | home = strdup(""); | ||
129 | conf = strdup(""); | ||
130 | } | 127 | } |
131 | pathlen[0] = strlen(home); | 128 | // Get path lengths |
132 | pathlen[1] = strlen(conf); | 129 | pathlen[0] = home ? strlen(home) : 0; |
130 | pathlen[1] = conf ? strlen(conf) : 0; | ||
133 | #undef home | 131 | #undef home |
134 | #undef conf | 132 | #undef conf |
133 | |||
135 | // Search for config file from search paths | 134 | // Search for config file from search paths |
136 | static const char *search_paths[] = { | 135 | static const char *search_paths[] = { |
137 | "/.sway/config", // Prepend with $home | 136 | "/.sway/config", // Prepend with $home |
138 | "/sway/config", // Prepend with $config | 137 | "/sway/config", // Prepend with $config |
139 | "/etc/sway/config", | 138 | "/etc/sway/config", |
140 | "/.i3/config", // $home | 139 | "/.i3/config", // $home |
141 | "/.i3/config", // $config | 140 | "/i3/config", // $config |
142 | "/etc/i3/config" | 141 | "/etc/i3/config" |
143 | }; | 142 | }; |
144 | for (i = 0; i < (int)(sizeof(search_paths) / sizeof(char *)); ++i) { | 143 | for (i = 0; i < (int)(sizeof(search_paths) / sizeof(char *)); ++i) { |
145 | char *test = malloc(pathlen[i%3] + strlen(search_paths[i]) + 1); | 144 | // Only try path if it is set by enviroment variables |
146 | strcpy(test, paths[i%3]); | 145 | if (paths[i%3]) { |
147 | strcat(test, search_paths[i]); | 146 | char *test = malloc(pathlen[i%3] + strlen(search_paths[i]) + 1); |
148 | sway_log(L_DEBUG, "Checking for config at %s", test); | 147 | strcpy(test, paths[i%3]); |
149 | if (file_exists(test)) { | 148 | strcpy(test + pathlen[i%3], search_paths[i]); |
150 | config_path = test; | 149 | sway_log(L_DEBUG, "Checking for config at %s", test); |
151 | goto cleanup; | 150 | if (file_exists(test)) { |
151 | config_path = test; | ||
152 | goto cleanup; | ||
153 | } | ||
154 | free(test); | ||
152 | } | 155 | } |
153 | free(test); | ||
154 | } | 156 | } |
155 | 157 | ||
156 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); | 158 | sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS"); |