From c85f4e85a1906d94b0b4b2e212c11b7b42f6afc0 Mon Sep 17 00:00:00 2001 From: Renge Date: Sat, 12 Feb 2022 16:47:38 -0500 Subject: [PATCH] feat: implemented validargs.c --- hw1/src/validargs.c | 105 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 4 deletions(-) diff --git a/hw1/src/validargs.c b/hw1/src/validargs.c index c218d35..6cf49d6 100644 --- a/hw1/src/validargs.c +++ b/hw1/src/validargs.c @@ -20,7 +20,104 @@ * of the selected program options. */ -int validargs(int argc, char **argv) { - // TO BE IMPLEMENTED - abort(); -} +int validargs(int argc, char **argv) +{ + char **ptr = argv; + ptr++; + for (int i = 1; i < argc; i++, ptr++) + { + if (**ptr == '-') + { + char *arg = *ptr; + arg++; + switch (*arg) + { + case 'h': + if (i == 1 && *(++arg) == '\0') + { + global_options = HELP_OPTION; + return 0; + } + else + { + return -1; + } + break; + case 'v': + if (i == 1 && *(++arg) == '\0' && argc == 2) + { + global_options = VALIDATE_OPTION; + return 0; + } + else + { + return -1; + } + break; + case 'c': + if (i == 1 && *(++arg) == '\0') + { + if (argc == 2) + { + global_options = CANONICALIZE_OPTION; + return 0; + } + else + { + ptr++; + if (**ptr != '-') + { + return -1; + } + arg = *ptr; + if (*(++arg) != 'p') + { + return -1; + } + if (*(++arg) != '\0') + { + return -1; + } + global_options = PRETTY_PRINT_OPTION; + if (argc == 3) + { + return 0; + } + else if (argc == 4) + { + int indent_value = 0; + arg = *(++ptr); + while (*arg != '\0') + { + if (*arg <= '9' && *arg >= '0') + { + indent_value = indent_value * 10 + (*arg - '0'); + arg++; + } + else + { + return 1; + } + } + global_options += indent_value; + return 0; + } + else + { + return -1; + } + } + } + else + { + return -1; + } + break; + default: + return -1; + break; + } + } + } + return -1; +} \ No newline at end of file