From 066451a8ad9c293dd0c454a38cbed253cdd78625 Mon Sep 17 00:00:00 2001 From: "mingang.he" Date: Fri, 3 Mar 2023 15:09:44 +0000 Subject: [PATCH] Add /graftcp/graftcp.conf for graftcp default config --- README.md | 3 ++- README.zh-CN.md | 3 ++- conf.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ graftcp.c | 7 +++++-- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e521cb7..e1f8e08 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,8 @@ Usage: graftcp [options] prog [prog-args] Options: -c --conf-file= - Specify configuration file + Specify configuration file. + Default: $XDG_CONFIG_HOME/graftcp/graftcp.conf -a --local-addr= graftcp-local's IP address. Default: localhost -p --local-port= diff --git a/README.zh-CN.md b/README.zh-CN.md index e5df399..5f778cc 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -72,7 +72,8 @@ Usage: graftcp [options] prog [prog-args] Options: -c --conf-file= - Specify configuration file + Specify configuration file. + Default: $XDG_CONFIG_HOME/graftcp/graftcp.conf -a --local-addr= graftcp-local's IP address. Default: localhost -p --local-port= diff --git a/conf.c b/conf.c index 0fccdf8..5d538f9 100644 --- a/conf.c +++ b/conf.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "conf.h" @@ -197,13 +198,56 @@ void conf_free(struct graftcp_conf *conf) } } +static char *xdg_config_path_dup(void) +{ + const char *home, *config_home; + char *path = NULL; + const char *dotconf = ".config"; + const char *subdir = "graftcp"; + const char *confname = "graftcp.conf"; + + config_home = getenv("XDG_CONFIG_HOME"); + if (config_home && *config_home) { + size_t size = 3 + strlen(config_home) + strlen(subdir) + strlen(confname); + path = calloc(size, sizeof(char)); + snprintf(path, size, "%s/%s/%s", config_home, subdir, confname); + return path; + } + + home = getenv("HOME"); + if (home) { + size_t size = 4 + strlen(home) + strlen(dotconf) + strlen(subdir) + strlen(confname); + path = calloc(size, sizeof(char)); + snprintf(path, size, "%s/%s/%s/%s", home, dotconf, subdir, confname); + return path; + } + + return NULL; +} + int conf_read(const char *path, struct graftcp_conf *conf) { FILE *f; + __defer_free char *xdg_config = NULL; __defer_free char *line = NULL; size_t len = 0; int err = 0; + if (path == NULL) { + xdg_config = xdg_config_path_dup(); + if (xdg_config == NULL) + return 0; + + struct stat st; + if (stat(xdg_config, &st)) + return 0; + if (S_ISDIR(st.st_mode)) { + fprintf(stderr, "%s is a directory not a config file\n", xdg_config); + return -1; + } + path = xdg_config; + } + f = fopen(path, "r"); if (!f) { fprintf(stderr, "Failed to open %s\n", path); diff --git a/graftcp.c b/graftcp.c index 22def87..86b1a57 100644 --- a/graftcp.c +++ b/graftcp.c @@ -367,7 +367,8 @@ static void usage(char **argv) fprintf(stderr, "Usage: %s [options] prog [prog-args]\n\n" "Options:\n" " -c --conf-file=\n" - " Specify configuration file\n" + " Specify configuration file.\n" + " Default: $XDG_CONFIG_HOME/graftcp/graftcp.conf\n" " -a --local-addr=\n" " graftcp-local's IP address. Default: localhost\n" " -p --local-port=\n" @@ -415,6 +416,7 @@ int client_main(int argc, char **argv) .ignore_local = &DEFAULT_IGNORE_LOCAL, }; + __defer_free char *conf_file_path = NULL; __defer_conf_free struct graftcp_conf file_conf; __defer_conf_free struct graftcp_conf cmd_conf; conf_init(&file_conf); @@ -444,7 +446,7 @@ int client_main(int argc, char **argv) *cmd_conf.ignore_local = false; break; case 'c': - conf_read(optarg, &file_conf); + conf_file_path = strdup(optarg); break; case 'V': fprintf(stderr, "graftcp %s\n", VERSION); @@ -456,6 +458,7 @@ int client_main(int argc, char **argv) exit(0); } } + conf_read(conf_file_path, &file_conf); conf_override(&conf, &file_conf); conf_override(&conf, &cmd_conf);