From 88ab5a5273e13066ce3f496a690f10d20a278bb4 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 22 Jun 2020 19:13:19 +0200 Subject: Added better option handling --- src/args.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/args.c (limited to 'src/args.c') diff --git a/src/args.c b/src/args.c new file mode 100644 index 0000000..1600e0e --- /dev/null +++ b/src/args.c @@ -0,0 +1,45 @@ +#include "tar.h" + +bool args_parse(int argc, char **argv, t_args *args) +{ + int opt; + int action_counter = 0; + + args->archive_name = NULL; + args->files = NULL; + while ((opt = getopt(argc, argv, "Acdtruxvf:")) != -1) + { + switch (opt) + { + case 'A': args->action = ACTION_CONCAT; break; + case 'c': args->action = ACTION_CREATE; break; + case 'd': args->action = ACTION_DIFF; break; + case 't': args->action = ACTION_LIST; break; + case 'r': args->action = ACTION_APPEND; break; + case 'u': args->action = ACTION_UPDATE; break; + case 'x': args->action = ACTION_EXTRACT; break; + + case 'v': args->flags |= FLAG_VERBOSE; break; + case 'f': + args->flags |= FLAG_FILE; + args->archive_name = optarg; + break; + default: + return false; + } + if (strchr("Acdtrux", opt) != NULL) + action_counter++; + } + if (action_counter != 1) + { + fprintf(stderr, "%s: You way not specify more than one '-Acdtrux' option\n", argv[0]); + return false; + } + if (!(args->flags & FLAG_FILE) && isatty(STDOUT_FILENO)) + { + fprintf(stderr, "%s: Refusing to write archive contents to terminal (missing -f option?)\n", argv[0]); + return false; + } + args->files = argv + optind; + return true; +} -- cgit