diff options
Diffstat (limited to 'src/commands/wc.c')
-rw-r--r-- | src/commands/wc.c | 90 |
1 files changed, 43 insertions, 47 deletions
diff --git a/src/commands/wc.c b/src/commands/wc.c index 892ef2e..9acbf5c 100644 --- a/src/commands/wc.c +++ b/src/commands/wc.c @@ -2,13 +2,14 @@ #include <ctype.h> -struct Flags { +static struct { bool newlines; bool words; bool characters; bool bytes; bool longest_line; -}; + bool has_flags; +} flags; static int lines = 0; static int words = 0; @@ -16,20 +17,20 @@ static int chars = 0; static int bytes = 0; static int logst = 0; -static void list(int l, int w, int c, int b, int lg, struct Flags* flags) { - if (flags->newlines) { +static void list(int l, int w, int c, int b, int lg) { + if (flags.newlines) { printf("\t%d", l); } - if (flags->words) { + if (flags.words) { printf("\t%d", w); } - if (flags->characters) { + if (flags.characters) { printf("\t%d", c); } - if (flags->bytes) { + if (flags.bytes) { printf("\t%d", b); } - if (flags->longest_line) { + if (flags.longest_line) { printf("\t%d", lg); } } @@ -40,7 +41,7 @@ static bool is_delimiter(char c) { return c == ' ' || c == '\t' || c == '\n'; } -static void run_wc(FILE* file, struct Flags* flags) { +static void run_wc(FILE* file) { int l = 0, w = 0, c = 0, b = 0, lg = 0; bool in_word = false; @@ -70,7 +71,7 @@ static void run_wc(FILE* file, struct Flags* flags) { } if (in_word) w++; lg = lg > current_length ? lg : current_length; - list(l, w, c, b, lg, flags); + list(l, w, c, b, lg); lines += l; words += w; chars += c; @@ -80,7 +81,7 @@ static void run_wc(FILE* file, struct Flags* flags) { fclose(file); } -static void help() { +static void help(void) { printf("Usage: wc [-cmlwL] [FILE]...\n\n"); printf("Count lines, words, and bytes for FILEs (or stdin)\n\n"); printf("\t-c Count bytes\n"); @@ -91,67 +92,62 @@ static void help() { exit(EXIT_SUCCESS); } +static int short_arg(char c, char* next) { + UNUSED(next); + switch (c) { + case 'c': + flags.bytes = true; + break; + case 'm': + flags.characters = true; + break; + case 'l': + flags.newlines = true; + break; + case 'w': + flags.words = true; + break; + case 'L': + flags.longest_line = true; + break; + default: + error("error: invald option -%c", c); + } + flags.has_flags = true; + return ARG_UNUSED; +} + COMMAND(wc) { - struct Flags flags; flags.newlines = false; flags.words = false; flags.characters = false; flags.bytes = false; flags.longest_line = false; + flags.has_flags = false; - bool has_flags = false; - - int start = 0; - for (int i = 0; i < argc; i++) { - if (!prefix("-", argv[i])) break; - if (streql("--help", argv[i])) help(); - start++; - for (size_t j = 1; j < strlen(argv[i]); j++) { - char c = argv[i][j]; - switch (c) { - case 'c': - flags.bytes = true; - break; - case 'm': - flags.characters = true; - break; - case 'l': - flags.newlines = true; - break; - case 'w': - flags.words = true; - break; - case 'L': - flags.longest_line = true; - break; - default: - error("error: invald option -%c", c); - } - has_flags = true; - } - } + int start = parse_args(argc, argv, help, short_arg, NULL); - if (!has_flags) { + if (!flags.has_flags) { flags.newlines = true; flags.words = true; flags.characters = true; } if (argc - start < 1) { - run_wc(stdin, &flags); + run_wc(stdin); printf("\n"); return EXIT_SUCCESS; } for (int i = start; i < argc; i++) { FILE* file = get_file(argv[i], "r"); - run_wc(file, &flags); + run_wc(file); printf("\t%s\n", argv[i]); } if (argc - start > 1) { - list(lines, words, chars, bytes, logst, &flags); + list(lines, words, chars, bytes, logst); printf("\ttotal\n"); } |