diary

Text-based journaling program
git clone https://git.in0rdr.ch/diary.git
Log | Files | Refs | README | LICENSE

commit 876261624fbe9e495ab214b84b702ee935ce76f2
parent 8262253b1418f0824664436231a62b9e2faf517e
Author: Andreas Gruhler <agruhl@gmx.ch>
Date:   Sun,  9 Jan 2022 15:11:54 +0100

fix(#1): read from arg at end

Diffstat:
Msrc/diary.c | 186++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 93 insertions(+), 93 deletions(-)

diff --git a/src/diary.c b/src/diary.c @@ -317,6 +317,7 @@ bool read_config(const char* file_path) { if (sscanf(line, "%s = %s", key_buf, value_buf) == 2) { if (strcmp("dir", key_buf) == 0) { expaned_value = expand_path(value_buf); + CONFIG.dir = (char *) malloc(strlen(expaned_value) + 1 * sizeof(char)); strcpy(CONFIG.dir, expaned_value); free(expaned_value); } else if (strcmp("range", key_buf) == 0) { @@ -404,6 +405,7 @@ int main(int argc, char** argv) { // get diary directory from environment env_var = getenv("DIARY_DIR"); if (env_var != NULL) { + free(CONFIG.dir); // if available, overwrite the diary directory with the environment variable CONFIG.dir = (char *) calloc(strlen(env_var) + 1, sizeof(char)); strcpy(CONFIG.dir, env_var); @@ -454,103 +456,101 @@ int main(int argc, char** argv) { #endif } - // get the diary directory via argument, this takes precedence over env/config - if (argc < 2) { - if (CONFIG.dir == NULL) { - fprintf(stderr, "The diary directory must be provided as (non-option) arg, `--dir` arg,\n" - "or in the DIARY_DIR environment variable, see `diary --help` or DIARY(1)\n"); - return 1; + int option_char; + int option_index = 0; + + // define options, see GETOPT(3) + static const struct option long_options[] = { + { "version", no_argument, 0, 'v' }, + { "help", no_argument, 0, 'h' }, + { "dir", required_argument, 0, 'd' }, + { "editor", required_argument, 0, 'e' }, + { "fmt", required_argument, 0, 'f' }, + { "fmt-cmd", required_argument, 0, 'F' }, + { "no-pty", no_argument, 0, 'p' }, + { "no-mouse", no_argument, 0, 'm' }, + { "range", required_argument, 0, 'r' }, + { "weekday", required_argument, 0, 'w' }, + { 0, 0, 0, 0 } + }; + + // read option characters + while (1) { + option_char = getopt_long(argc, argv, "vhpmd:r:w:f:F:e:", long_options, &option_index); + + if (option_char == -1) { + break; } - } else { - int option_char; - int option_index = 0; - - // define options, see GETOPT(3) - static const struct option long_options[] = { - { "version", no_argument, 0, 'v' }, - { "help", no_argument, 0, 'h' }, - { "dir", required_argument, 0, 'd' }, - { "editor", required_argument, 0, 'e' }, - { "fmt", required_argument, 0, 'f' }, - { "fmt-cmd", required_argument, 0, 'F' }, - { "no-pty", no_argument, 0, 'p' }, - { "no-mouse", no_argument, 0, 'm' }, - { "range", required_argument, 0, 'r' }, - { "weekday", required_argument, 0, 'w' }, - { 0, 0, 0, 0 } - }; - - // read option characters - while (1) { - option_char = getopt_long(argc, argv, "vhpmd:r:w:f:F:e:", long_options, &option_index); - - if (option_char == -1) { - break; - } - switch (option_char) { - case 'v': - // show program version - printf("v%s\n", DIARY_VERSION); - return 0; - break; - case 'h': - // show help text - // printf("see man(1) diary\n"); - usage(); - return 0; - break; - case 'd': - free(CONFIG.dir); - // set diary directory from option character - CONFIG.dir = (char *) calloc(strlen(optarg) + 1, sizeof(char)); - strcpy(CONFIG.dir, optarg); - break; - case 'r': - // set year range from option character - CONFIG.range = atoi(optarg); - break; - case 'w': - // set first week day from option character - fprintf(stderr, "%i\n", atoi(optarg)); - CONFIG.weekday = atoi(optarg); - break; - case 'f': - // set date format from option character - CONFIG.fmt = (char *) calloc(strlen(optarg) + 1, sizeof(char)); - strcpy(CONFIG.fmt, optarg); - break; - case 'F': - free(CONFIG.fmt_cmd); - // set formatting command - CONFIG.fmt_cmd = (char *) calloc(strlen(optarg) + 1, sizeof(char)); - strcpy(CONFIG.fmt_cmd, optarg); - break; - case 'p': - // set no-pty flag - CONFIG.no_pty = true; - break; - case 'm': - // set no-mouse flag - CONFIG.no_mouse = true; - break; - case 'e': - // set default editor from option character - CONFIG.editor = (char *) calloc(strlen(optarg) + 1, sizeof(char)); - strcpy(CONFIG.editor, optarg); - break; - default: - printf("?? getopt returned character code 0%o ??\n", option_char); - } + switch (option_char) { + case 'v': + // show program version + printf("v%s\n", DIARY_VERSION); + return 0; + break; + case 'h': + // show help text + // printf("see man(1) diary\n"); + usage(); + return 0; + break; + case 'd': + free(CONFIG.dir); + // set diary directory from option character + CONFIG.dir = (char *) calloc(strlen(optarg) + 1, sizeof(char)); + strcpy(CONFIG.dir, optarg); + break; + case 'r': + // set year range from option character + CONFIG.range = atoi(optarg); + break; + case 'w': + // set first week day from option character + fprintf(stderr, "%i\n", atoi(optarg)); + CONFIG.weekday = atoi(optarg); + break; + case 'f': + // set date format from option character + CONFIG.fmt = (char *) calloc(strlen(optarg) + 1, sizeof(char)); + strcpy(CONFIG.fmt, optarg); + break; + case 'F': + free(CONFIG.fmt_cmd); + // set formatting command + CONFIG.fmt_cmd = (char *) calloc(strlen(optarg) + 1, sizeof(char)); + strcpy(CONFIG.fmt_cmd, optarg); + break; + case 'p': + // set no-pty flag + CONFIG.no_pty = true; + break; + case 'm': + // set no-mouse flag + CONFIG.no_mouse = true; + break; + case 'e': + // set default editor from option character + CONFIG.editor = (char *) calloc(strlen(optarg) + 1, sizeof(char)); + strcpy(CONFIG.editor, optarg); + break; } + } - if (optind < argc) { - free(CONFIG.dir); - // set diary directory from first non-option argv-element, - // required for backwarad compatibility with diary <= 0.4 - CONFIG.dir = (char *) calloc(strlen(argv[optind]) + 1, sizeof(char)); - strcpy(CONFIG.dir, argv[optind]); - } + // Get the diary directory from argument, this takes precedence over env/config. + // optind is an external variable set by getopt, see man 3 getopt. The system + // initializes this value to 1. If optind >= argc, the DIARY_DIR was not provided. + if (optind < argc) { + free(CONFIG.dir); + // set diary directory from first non-option argv-element, + // required for backwarad compatibility with diary <= 0.4 + CONFIG.dir = (char *) calloc(strlen(argv[optind]) + 1, sizeof(char)); + strcpy(CONFIG.dir, argv[optind]); + } + + if (CONFIG.dir == NULL) { + fprintf(stderr, "The diary directory must be provided as (non-option) arg, `--dir` arg,\n" + "or in the DIARY_DIR environment variable, see `diary --help` or DIARY(1)\n"); + return 1; } // check if that directory exists