diff options
| author | Mark Whitley <markw@lineo.com> | 2000-07-20 00:08:10 +0000 |
|---|---|---|
| committer | Mark Whitley <markw@lineo.com> | 2000-07-20 00:08:10 +0000 |
| commit | 3950596e1e13d3593d06ab3cf1e48a07d5bd80c9 (patch) | |
| tree | 56b1e31b4ab5faddc6e67f19e8e1be1e6565cf28 /coreutils | |
| parent | 99e370f0c655b24a44c7ebba7c35a36e6b6bd285 (diff) | |
| download | busybox-w32-3950596e1e13d3593d06ab3cf1e48a07d5bd80c9.tar.gz busybox-w32-3950596e1e13d3593d06ab3cf1e48a07d5bd80c9.tar.bz2 busybox-w32-3950596e1e13d3593d06ab3cf1e48a07d5bd80c9.zip | |
Converted option parsing to using getopt(). Also managed to remove an
extraneous logic loop that existed only for the purpose of the special case of
only one file. I replaced it with a variable to keep track of the number of
files read.
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/wc.c | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/coreutils/wc.c b/coreutils/wc.c index d1e05ae37..02e2b2aa6 100644 --- a/coreutils/wc.c +++ b/coreutils/wc.c | |||
| @@ -22,6 +22,7 @@ | |||
| 22 | 22 | ||
| 23 | #include "internal.h" | 23 | #include "internal.h" |
| 24 | #include <stdio.h> | 24 | #include <stdio.h> |
| 25 | #include <getopt.h> | ||
| 25 | 26 | ||
| 26 | static int total_lines, total_words, total_chars, max_length; | 27 | static int total_lines, total_words, total_chars, max_length; |
| 27 | static int print_lines, print_words, print_chars, print_length; | 28 | static int print_lines, print_words, print_chars, print_length; |
| @@ -103,13 +104,14 @@ static void wc_file(FILE * file, const char *name) | |||
| 103 | int wc_main(int argc, char **argv) | 104 | int wc_main(int argc, char **argv) |
| 104 | { | 105 | { |
| 105 | FILE *file; | 106 | FILE *file; |
| 107 | unsigned int num_files_counted = 0; | ||
| 108 | int opt; | ||
| 106 | 109 | ||
| 107 | total_lines = total_words = total_chars = max_length = 0; | 110 | total_lines = total_words = total_chars = max_length = 0; |
| 108 | print_lines = print_words = print_chars = print_length = 0; | 111 | print_lines = print_words = print_chars = print_length = 0; |
| 109 | 112 | ||
| 110 | while (--argc && **(++argv) == '-') { | 113 | while ((opt = getopt(argc, argv, "clLw")) > 0) { |
| 111 | while (*++(*argv)) | 114 | switch (opt) { |
| 112 | switch (**argv) { | ||
| 113 | case 'c': | 115 | case 'c': |
| 114 | print_chars = 1; | 116 | print_chars = 1; |
| 115 | break; | 117 | break; |
| @@ -130,26 +132,24 @@ int wc_main(int argc, char **argv) | |||
| 130 | if (!print_lines && !print_words && !print_chars && !print_length) | 132 | if (!print_lines && !print_words && !print_chars && !print_length) |
| 131 | print_lines = print_words = print_chars = 1; | 133 | print_lines = print_words = print_chars = 1; |
| 132 | 134 | ||
| 133 | if (argc == 0) { | 135 | if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { |
| 134 | wc_file(stdin, ""); | 136 | wc_file(stdin, ""); |
| 135 | exit(TRUE); | 137 | exit(TRUE); |
| 136 | } else if (argc == 1) { | ||
| 137 | file = fopen(*argv, "r"); | ||
| 138 | if (file == NULL) { | ||
| 139 | fatalError(*argv); | ||
| 140 | } | ||
| 141 | wc_file(file, *argv); | ||
| 142 | } else { | 138 | } else { |
| 143 | while (argc-- > 0) { | 139 | while (optind < argc) { |
| 144 | file = fopen(*argv, "r"); | 140 | file = fopen(argv[optind], "r"); |
| 145 | if (file == NULL) { | 141 | if (file == NULL) { |
| 146 | fatalError(*argv); | 142 | fatalError(argv[optind]); |
| 147 | } | 143 | } |
| 148 | wc_file(file, *argv); | 144 | wc_file(file, argv[optind]); |
| 149 | argv++; | 145 | num_files_counted++; |
| 146 | optind++; | ||
| 150 | } | 147 | } |
| 148 | } | ||
| 149 | |||
| 150 | if (num_files_counted > 1) | ||
| 151 | print_counts(total_lines, total_words, total_chars, | 151 | print_counts(total_lines, total_words, total_chars, |
| 152 | max_length, "total"); | 152 | max_length, "total"); |
| 153 | } | 153 | |
| 154 | return(TRUE); | 154 | return 0 ; |
| 155 | } | 155 | } |
