diff options
| author | Thorsten Töpper <atsutane@freethoughts.de> | 2026-02-18 21:51:53 +0100 |
|---|---|---|
| committer | Thorsten Töpper <atsutane@freethoughts.de> | 2026-02-18 21:51:53 +0100 |
| commit | d513977a3566b14d9357906615d045d71741537f (patch) | |
| tree | 3e707d2de9da71d98650fa8bb1b92ed11ab724ba /src/options.c | |
| parent | eed2d1323441861f2d41f0ecc0a72fcc9190fa5f (diff) | |
| download | duplicate_finder-d513977a3566b14d9357906615d045d71741537f.tar.gz duplicate_finder-d513977a3566b14d9357906615d045d71741537f.tar.bz2 | |
squash initial implementation
Diffstat (limited to 'src/options.c')
| -rw-r--r-- | src/options.c | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/options.c b/src/options.c new file mode 100644 index 0000000..214a72c --- /dev/null +++ b/src/options.c @@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +/* Copyright 2026 Thorsten Töpper + * + * vim:ts=4:sw=4:expandtab + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <getopt.h> +#include <stdbool.h> +#include <ctype.h> + +#include "options.h" +#include "trace_macros.h" + + +/* === GLOBAL VARIABLES === */ +struct option long_options[] = { + { "help", no_argument, 0, 0 }, + { "quiet", no_argument, 0, 0 }, + { "show-hidden-entries", no_argument, 0, 0 }, + { 0, 0, 0, 0 } +}; + +bool option_quiet = false; +bool option_show_hidden_entries = false; + +char *exec_name; + +/* === 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", "--help", "-h", "", + "Show this message and exit"); + fprintf(stderr, " %-25s %2s %10s - %s\n", "--quiet", "-q", "", + "Don't print error messages or warnings"); + fprintf(stderr, " %-25s %2s %10s - %s\n", "--show-hidden-entries", "-a", "", + "Show hidden entries in the directory"); +} + + +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("help", option_name) == 0) { + usage(exec_name); + exit(EXIT_SUCCESS); + } + + if (strcmp("quiet", option_name) == 0) { + option_quiet = true; + return; + } + + if (strcmp("show-hidden-entries", option_name) == 0) { + option_show_hidden_entries = 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); + exit(EXIT_FAILURE); + } + + LOGERR("ERROR: Option '%s' not recognized\n.", option_name); +} + + +int parse_arguments(int argc, char **argv) { + int c = 0, index; + /* exec_name is a file internal global variable for --help in set_option() */ + exec_name = argv[0]; + + while(1) { + index = 0; + c = getopt_long(argc, argv, "hqs", long_options, &index); + + if (c == -1) { + break; + } + + switch (c) { + case 0: + set_option(long_options[index].name, optarg); + break; + case 'h': + usage(exec_name); + exit(EXIT_SUCCESS); + case 'q': + option_quiet = true; + break; + case 's': + option_show_hidden_entries = true; + break; + case '?': + break; + default: + LOGERR("ERROR: unrecognized option 0x%02X '%c'\n", c, c); + usage(exec_name); + exit(EXIT_FAILURE); + } + } + + return optind; +} + |
