From d148ca3e192c6738c5d540cb66feef63e5c1a5ab Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Sun, 25 Dec 2022 12:27:20 +0300 Subject: [PATCH] perf(cli): add current database location output in help message --- src/cli.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/cli.c b/src/cli.c index 7762f8c..7a463c2 100644 --- a/src/cli.c +++ b/src/cli.c @@ -269,6 +269,18 @@ int cli(int argc, char **argv) { _Bool opt_t = 0; _Bool opt_k = 0; _Bool opt_w = 0; + FILE *config = fopen(config_path, "r"); + if (config == NULL) { + fprintf(stderr, ERROR("Config file not found\n")); + return 1; + } + fseek(config, 0L, SEEK_END); + char *tanabata_path = malloc(ftell(config) + 1); + rewind(config); + if (fgets(tanabata_path, INT32_MAX, config) == NULL) { + fprintf(stderr, ERROR("Failed to read config file\n")); + return 1; + } while ((opt = getopt(argc, argv, shortopts)) != -1) { switch (opt) { case 'h': @@ -289,6 +301,11 @@ int cli(int argc, char **argv) { HIGHLIGHT("-w")" Weed (defragment) database\n" HIGHLIGHT("-V")" Print version and exit\n" ); + if (tanabata_path != NULL) { + printf(HIGHLIGHT("\nCurrent database location: %s\n"), tanabata_path); + } else { + printf(HIGHLIGHT("\nNo database connected\n")); + } return 0; case 'V': printf("0.1.0-dev\n"); @@ -302,7 +319,7 @@ int cli(int argc, char **argv) { status |= tanabata_init(&tanabata); status |= tanabata_dump(&tanabata, abspath); if (status == 0) { - FILE *config = fopen(config_path, "w"); + config = freopen(NULL, "w", config); if (config == NULL) { fprintf(stderr, ERROR("Failed to write to config file\n")); return 1; @@ -321,7 +338,7 @@ int cli(int argc, char **argv) { return 1; } if (tanabata_open(&tanabata, abspath) == 0) { - FILE *config = fopen(config_path, "w"); + config = freopen(NULL, "w", config); if (config == NULL) { fprintf(stderr, ERROR("Failed to write to config file\n")); return 1; @@ -369,14 +386,6 @@ int cli(int argc, char **argv) { fprintf(stderr, ERROR("Config file not found\n")); return 1; } - fseek(config, 0L, SEEK_END); - char *tanabata_path = malloc(ftell(config) + 1); - rewind(config); - if (fgets(tanabata_path, INT32_MAX, config) == NULL) { - fprintf(stderr, ERROR("Failed to read config file\n")); - return 1; - } - tanabata_open(&tanabata, tanabata_path); free(tanabata_path); fclose(config); if (opt_w) {