Skip to content

Commit c11c24b

Browse files
committed
Improve config file search order
Previously the search order is: ~/.config/compton/compton.conf /etc/xdg/compton/compton.conf ~/.config/compton.conf /etc/xdg/compton.conf ... Now the search order is: ~/.config/compton/compton.conf ~/.config/compton.conf ~/.compton.conf /etc/xdg/compton/compton.conf ... In other word, compton will now search all possible user config file path first before searching for a system config file. Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
1 parent f7d4dff commit c11c24b

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

src/config_libconfig.c

+48-21
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,35 @@ lcfg_lookup_bool(const config_t *config, const char *path, bool *value) {
3737
return ret;
3838
}
3939

40+
/// Search for config file under a base directory
41+
FILE *
42+
open_config_file_at(const char *base, char **out_path) {
43+
static const char *config_paths[] = {
44+
"/compton.conf",
45+
"/compton/compton.conf"
46+
};
47+
for (size_t i = 0; i < ARR_SIZE(config_paths); i++) {
48+
char *path = mstrjoin(base, config_paths[i]);
49+
FILE *ret = fopen(path, "r");
50+
if (ret && out_path) {
51+
*out_path = path;
52+
} else {
53+
free(path);
54+
}
55+
if (ret) {
56+
return ret;
57+
}
58+
}
59+
return NULL;
60+
}
61+
4062
/**
4163
* Get a file stream of the configuration file to read.
4264
*
4365
* Follows the XDG specification to search for the configuration file.
4466
*/
4567
FILE *
4668
open_config_file(const char *cpath, char **ppath) {
47-
static const char *config_paths[] = {
48-
"/compton.conf",
49-
"/compton/compton.conf"
50-
};
5169
static const char config_filename_legacy[] = "/.compton.conf";
5270

5371
if (cpath) {
@@ -57,31 +75,40 @@ open_config_file(const char *cpath, char **ppath) {
5775
return ret;
5876
}
5977

60-
for (size_t i = 0; i < ARR_SIZE(config_paths); i++) {
61-
char *path = xdgConfigFind(config_paths[i], NULL);
62-
FILE *ret = fopen(path, "r");
78+
// First search for config file in user config directory
79+
auto config_home = xdgConfigHome(NULL);
80+
auto ret = open_config_file_at(config_home, ppath);
81+
free((void *)config_home);
82+
if (ret) {
83+
return ret;
84+
}
85+
86+
// Fall back to legacy config file in user home directory
87+
const char *home = getenv("HOME");
88+
if (home && strlen(home)) {
89+
auto path = mstrjoin(home, config_filename_legacy);
90+
ret = fopen(path, "r");
6391
if (ret && ppath) {
64-
*ppath = strdup(path);
92+
*ppath = path;
93+
} else {
94+
free(path);
6595
}
66-
free(path);
6796
if (ret) {
6897
return ret;
6998
}
7099
}
71100

72-
// Fall back to legacy config file names
73-
const char *home = getenv("HOME");
74-
if (home && strlen(home)) {
75-
auto path = ccalloc(strlen(home)+strlen(config_filename_legacy)+1, char);
76-
strcpy(path, home);
77-
strcpy(path+strlen(home), config_filename_legacy);
78-
FILE *ret = fopen(path, "r");
79-
if (ret && ppath)
80-
*ppath = path;
81-
else
82-
free(path);
83-
return ret;
101+
// Fall back to config file in system config directory
102+
auto config_dirs = xdgConfigDirectories(NULL);
103+
for (int i = 0; config_dirs[i]; i++) {
104+
ret = open_config_file_at(config_dirs[i], ppath);
105+
if (ret) {
106+
free((void *)config_dirs);
107+
return ret;
108+
}
84109
}
110+
free((void *)config_dirs);
111+
85112
return NULL;
86113
}
87114

0 commit comments

Comments
 (0)