X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=wc.c;h=695e7e7d4e4b765b1954d5ecb5d9ea11f1128a10;hb=4391a16c92cef744fc715cb7347b7b71fc8f312e;hp=d1e05ae37824d009c645a3d80910fe3678d62056;hpb=a16c66335e24009c4cbcd57ce8205b4dfc7b099c;p=oweals%2Fbusybox.git diff --git a/wc.c b/wc.c index d1e05ae37..695e7e7d4 100644 --- a/wc.c +++ b/wc.c @@ -2,7 +2,7 @@ /* * Mini wc implementation for busybox * - * by Edward Betts + * Copyright (C) 2000 Edward Betts * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,14 +20,17 @@ * */ -#include "internal.h" #include +#include +#include +#include +#include "busybox.h" static int total_lines, total_words, total_chars, max_length; static int print_lines, print_words, print_chars, print_length; -void print_counts(int lines, int words, int chars, int length, - const char *name) +static void print_counts(int lines, int words, int chars, int length, + const char *name) { char const *space = ""; @@ -103,13 +106,14 @@ static void wc_file(FILE * file, const char *name) int wc_main(int argc, char **argv) { FILE *file; + unsigned int num_files_counted = 0; + int opt, status = EXIT_SUCCESS; total_lines = total_words = total_chars = max_length = 0; print_lines = print_words = print_chars = print_length = 0; - while (--argc && **(++argv) == '-') { - while (*++(*argv)) - switch (**argv) { + while ((opt = getopt(argc, argv, "clLw")) > 0) { + switch (opt) { case 'c': print_chars = 1; break; @@ -123,33 +127,30 @@ int wc_main(int argc, char **argv) print_words = 1; break; default: - usage(wc_usage); + show_usage(); } } if (!print_lines && !print_words && !print_chars && !print_length) print_lines = print_words = print_chars = 1; - if (argc == 0) { + if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { wc_file(stdin, ""); - exit(TRUE); - } else if (argc == 1) { - file = fopen(*argv, "r"); - if (file == NULL) { - fatalError(*argv); - } - wc_file(file, *argv); + return EXIT_SUCCESS; } else { - while (argc-- > 0) { - file = fopen(*argv, "r"); - if (file == NULL) { - fatalError(*argv); - } - wc_file(file, *argv); - argv++; + while (optind < argc) { + if ((file = wfopen(argv[optind], "r")) != NULL) + wc_file(file, argv[optind]); + else + status = EXIT_FAILURE; + num_files_counted++; + optind++; } + } + + if (num_files_counted > 1) print_counts(total_lines, total_words, total_chars, max_length, "total"); - } - return(TRUE); + + return status; }