X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=wc.c;h=695e7e7d4e4b765b1954d5ecb5d9ea11f1128a10;hb=4391a16c92cef744fc715cb7347b7b71fc8f312e;hp=bad03f79152fafb2be918ee223ccbf3eed4d67c5;hpb=b610615be9aedfac07d1e01f12575707fa3a227c;p=oweals%2Fbusybox.git diff --git a/wc.c b/wc.c index bad03f791..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,27 +20,17 @@ * */ -#include "internal.h" #include - -static const char wc_usage[] = "wc [OPTION]... [FILE]...\n" -#ifndef BB_FEATURE_TRIVIAL_HELP - "\nPrint line, word, and byte counts for each FILE, and a total line if\n" - "more than one FILE is specified. With no FILE, read standard input.\n\n" - "Options:\n" - "\t-c\tprint the byte counts\n" - "\t-l\tprint the newline counts\n" - - "\t-L\tprint the length of the longest line\n" - "\t-w\tprint the word counts\n" -#endif - ; +#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 = ""; @@ -116,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; @@ -136,35 +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) { - perror(*argv); - exit(FALSE); - } - wc_file(file, *argv); + return EXIT_SUCCESS; } else { - while (argc-- > 0 && *argv != '\0' && strlen(*argv)) { - file = fopen(*argv, "r"); - if (file == NULL) { - perror(*argv); - exit(FALSE); - } - 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; }