Browse Source

Avoid duplicated call to xasprintf

Refactor the previous patch to call xasprintf at only one place
when making the pathname to the configuration file.
master
Damien Goutte-Gattat 5 years ago
parent
commit
17279743d5
  1. 35
      src/gfsec-split.c

35
src/gfsec-split.c

@ -204,6 +204,23 @@ interactive_loop(gfsec_secret_t *cfg)
errx(EXIT_FAILURE, "Split cancelled");
}
static void
transform_config_pathname(char **cfg, const char *secret)
{
char *base = NULL;
if ( ! *cfg )
base = get_file_basename(secret);
else if ( ! strchr(*cfg, '/') && ! strchr(*cfg, '.') )
base = xstrdup(*cfg);
if ( base ) {
xasprintf(cfg, "%s/gfsecret/%s.conf",
getenv("XDG_CONFIG_HOME"), base);
free(base);
}
}
static unsigned
get_uinteger_or_die(const char *arg)
{
@ -319,23 +336,7 @@ main(int argc, char **argv)
errx(EXIT_FAILURE, "Missing secret file");
secret_file = argv[optind++];
if ( ! config_path ) {
char *base;
base = get_file_basename(secret_file);
xasprintf(&config_path, "%s/gfsecret/%s.conf",
getenv("XDG_CONFIG_HOME"), base);
free(base);
}
else if ( ! strchr(config_path, '/') && ! strchr(config_path, '.') ) {
char *tmp;
tmp = xstrdup(config_path);
xasprintf(&config_path, "%s/gfsecret/%s.conf",
getenv("XDG_CONFIG_HOME"), tmp);
free(tmp);
}
transform_config_pathname(&config_path, secret_file);
if ( (c = gfsec_secret_set_secret_file(cfg, secret_file)) != 0 )
errx(EXIT_FAILURE, "Cannot set secret: %s", gfsec_error_string(c));

Loading…
Cancel
Save