Search XDG_CONFIG_HOME for user-specific config

This commit is contained in:
ksqsf
2018-02-11 00:30:09 +08:00
parent 23a258147d
commit 3c3c50b4f8
7 changed files with 34 additions and 6 deletions
-2
View File
@@ -22,8 +22,6 @@
#ifndef _COMMON_H
#define _COMMON_H
#define DEFAULT_CONF_PATH "/etc/shadowsocks-libev/config.json"
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif
+1 -1
View File
@@ -1424,7 +1424,7 @@ main(int argc, char **argv)
if (argc == 1) {
if (conf_path == NULL) {
conf_path = DEFAULT_CONF_PATH;
conf_path = get_default_conf();
}
}
if (conf_path != NULL) {
+1 -1
View File
@@ -989,7 +989,7 @@ main(int argc, char **argv)
if (argc == 1) {
if (conf_path == NULL) {
conf_path = DEFAULT_CONF_PATH;
conf_path = get_default_conf();
}
}
+1 -1
View File
@@ -1636,7 +1636,7 @@ main(int argc, char **argv)
if (argc == 1) {
if (conf_path == NULL) {
conf_path = DEFAULT_CONF_PATH;
conf_path = get_default_conf();
}
}
+1 -1
View File
@@ -921,7 +921,7 @@ main(int argc, char **argv)
if (argc == 1) {
if (conf_path == NULL) {
conf_path = DEFAULT_CONF_PATH;
conf_path = get_default_conf();
}
}
+28
View File
@@ -474,3 +474,31 @@ set_nofile(int nofile)
}
#endif
char *
get_default_conf(void)
{
static char sysconf[] = "/etc/shadowsocks-libev/config.json";
static char userconf[PATH_MAX] = { 0 };
char *conf_home;
if (userconf[0] != '\0')
return userconf;
conf_home = getenv("XDG_CONFIG_HOME");
if (!conf_home) {
strcpy(userconf, getenv("HOME"));
strcat(userconf, "/.config/shadowsocks-libev/config.json");
} else {
strcpy(userconf, conf_home);
strcat(userconf, "/shadowsocks-libev/config.json");
}
// Check if the user-specific config exists.
if (access(userconf, F_OK) != -1)
return userconf;
// If not, fall back to the system-wide config.
return sysconf;
}
+2
View File
@@ -173,4 +173,6 @@ void *ss_realloc(void *ptr, size_t new_size);
ptr = NULL; \
} while (0)
char *get_default_conf(void);
#endif // _UTILS_H