X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;ds=sidebyside;f=wc.c;h=e6f753435610fa68bcd92a622c82bd8fbab83f32;hb=39b727f498aad830780169228995e6e8f34953de;hp=058444d89634f2d2126caa9e2ca717eeb6ec6e63;hpb=bf181b9338152759fd56c8009e9a962a84808e7c;p=oweals%2Fbusybox.git diff --git a/wc.c b/wc.c index 058444d89..e6f753435 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,8 +20,9 @@ * */ -#include "internal.h" +#include "busybox.h" #include +#include static int total_lines, total_words, total_chars, max_length; static int print_lines, print_words, print_chars, print_length; @@ -103,13 +104,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; @@ -130,28 +132,23 @@ int wc_main(int argc, char **argv) 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) { - 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; }