Skip to content

Commit

Permalink
Add /graftcp/graftcp.conf for graftcp default config
Browse files Browse the repository at this point in the history
  • Loading branch information
hmgle committed Mar 3, 2023
1 parent 44fe1a3 commit 066451a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ Usage: graftcp [options] prog [prog-args]

Options:
-c --conf-file=<config-file-path>
Specify configuration file
Specify configuration file.
Default: $XDG_CONFIG_HOME/graftcp/graftcp.conf
-a --local-addr=<graftcp-local-IP-addr>
graftcp-local's IP address. Default: localhost
-p --local-port=<graftcp-local-port>
Expand Down
3 changes: 2 additions & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ Usage: graftcp [options] prog [prog-args]

Options:
-c --conf-file=<config-file-path>
Specify configuration file
Specify configuration file.
Default: $XDG_CONFIG_HOME/graftcp/graftcp.conf
-a --local-addr=<graftcp-local-IP-addr>
graftcp-local's IP address. Default: localhost
-p --local-port=<graftcp-local-port>
Expand Down
44 changes: 44 additions & 0 deletions conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>

#include "conf.h"

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions graftcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ static void usage(char **argv)
fprintf(stderr, "Usage: %s [options] prog [prog-args]\n\n"
"Options:\n"
" -c --conf-file=<config-file-path>\n"
" Specify configuration file\n"
" Specify configuration file.\n"
" Default: $XDG_CONFIG_HOME/graftcp/graftcp.conf\n"
" -a --local-addr=<graftcp-local-IP-addr>\n"
" graftcp-local's IP address. Default: localhost\n"
" -p --local-port=<graftcp-local-port>\n"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit 066451a

Please sign in to comment.