diff options
Diffstat (limited to 'src/options.c')
| -rw-r--r-- | src/options.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/options.c b/src/options.c new file mode 100644 index 0000000..848eaf5 --- /dev/null +++ b/src/options.c @@ -0,0 +1,131 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +/* Copyright 2025 Thorsten Töpper + * + * options - + * + * vim:ts=4:sw=4:expandtab + */ +#include <stdio.h> +#include <string.h> +#include <getopt.h> +#include <stdbool.h> + + +#include "output.h" +#include "options.h" + + + + + +/* === GLOBAL VARIABLES === */ +struct option long_options[] = { + { "reverse-sort", no_argument, 0, 0 }, + { "show-hidden-entries", no_argument, 0, 0 }, + { "sort-by", required_argument, 0, 0 }, + { "long-timestamp", no_argument, 0, 0 }, + { 0, 0, 0, 0 } +}; +bool option_sort_reverse_order = false; +enum esort_type option_sort_type = SORT_BY_SIZE; +bool option_show_hidden_entries = false; +bool option_timestamp_long = false; + + + +/* === IMPLEMENTATION === */ + +void usage(char *executable) { + fprintf(stderr, "Call: %s OPTIONS path_to_open\n", executable); + fprintf(stderr, "\nOPTIONS are\n"); + /* long name, short name, optional argument, explanation */ + fprintf(stderr, " %-25s %2s %10s %s\n", "--long-timestamp", "-t", "", + "Print timestamp in long form yyyymmdd HH:MM:SS ZONE"); + fprintf(stderr, " %-25s %2s %10s %s\n", "--reverse-sort", "", "", + "Sort reversed"); + fprintf(stderr, " %-25s %2s %10s %s\n", "--show-hidden-entries", "-v", "", + "Show hidden entries in the directory"); + fprintf(stderr, " %-25s %2s %10s %s\n", "--sort-by", "", "size|time", + "Sort either by time or size"); +} + + +void set_option(const char *option_name, char *option_argument) { + + DBGTRC("DEBUG: called with option_name '%s' and option_argument '%s'\n", + option_name, option_argument); + + if (option_name == NULL) + return; + + /* options WITHOUT arguments */ + if (strcmp(option_name, "show-hidden-entries") == 0) { + option_show_hidden_entries = true; + return; + } + + if (strcmp(option_name, "long-timestamp") == 0) { + option_timestamp_long = true; + return; + } + + if (strcmp("reverse-sort", option_name) == 0) { + option_sort_reverse_order = true; + return; + } + + /* options WITH arguments */ + if (option_argument == NULL || option_argument[0] == '\0') { + LOGERR("ERROR: option_name %s with missing option_argument\n", + option_name); + return; + } + + if (strcmp(option_name, "sort-by") == 0) { + if (strncmp("size", option_argument, 4) == 0) { + option_sort_type = SORT_BY_SIZE; + } else if (strncmp("time", option_argument, 4) == 0) { + option_sort_type = SORT_BY_TIME; + } else { + LOGERR("WARNING: '%s' is an invalig argument for %s\n", + option_argument, option_name); + } + return; + } + + LOGERR("ERROR: Option '%s' not recognized\n.", option_name); +} + +int parse_arguments(int argc, char **argv) { + int c = 0, index; + + while(1) { + index = 0; + c = getopt_long(argc, argv, "tv", long_options, &index); + + if (c == -1) { + break; + } + + switch (c) { + case 0: + set_option(long_options[index].name, optarg); + break; + case 't': + option_timestamp_long = true; + break; + case 'v': + option_show_hidden_entries = true; + break; + case '?': + break; + default: + LOGERR("ERROR: unrecognized option 0x%02X '%c'\n", c, c); + break; + } + } + + return optind; +} + |
