diff options
author | markw <markw@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2000-07-20 00:08:10 +0000 |
---|---|---|
committer | markw <markw@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2000-07-20 00:08:10 +0000 |
commit | 29bd4a2ec7b899bdcad396030af33294d2987289 (patch) | |
tree | 56b1e31b4ab5faddc6e67f19e8e1be1e6565cf28 /wc.c | |
parent | 1388a13481bd107d9e15db91213e682917c07682 (diff) | |
download | busybox-w32-29bd4a2ec7b899bdcad396030af33294d2987289.tar.gz busybox-w32-29bd4a2ec7b899bdcad396030af33294d2987289.tar.bz2 busybox-w32-29bd4a2ec7b899bdcad396030af33294d2987289.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.
git-svn-id: svn://busybox.net/trunk/busybox@886 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'wc.c')
-rw-r--r-- | wc.c | 34 |
1 files changed, 17 insertions, 17 deletions
@@ -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 | } |