diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-04 17:04:20 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-04 17:04:20 +0200 |
commit | 09e7dafbfe358bed5a3bbf45dc2369760c09a47e (patch) | |
tree | 500cf12c1a0dbb245e00aea79be48b6fcc53ddb7 | |
parent | 9a1b2605476c8e6e69a3666e9f538004b6623962 (diff) | |
download | busybox-w32-09e7dafbfe358bed5a3bbf45dc2369760c09a47e.tar.gz busybox-w32-09e7dafbfe358bed5a3bbf45dc2369760c09a47e.tar.bz2 busybox-w32-09e7dafbfe358bed5a3bbf45dc2369760c09a47e.zip |
wc: preparatory patch, no logic changes
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
-rw-r--r-- | coreutils/wc.c | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/coreutils/wc.c b/coreutils/wc.c index 4f14374c3..ae38fd5fe 100644 --- a/coreutils/wc.c +++ b/coreutils/wc.c | |||
@@ -59,10 +59,11 @@ | |||
59 | #endif | 59 | #endif |
60 | 60 | ||
61 | enum { | 61 | enum { |
62 | WC_LINES = 0, | 62 | WC_LINES = 0, |
63 | WC_WORDS = 1, | 63 | WC_WORDS = 1, |
64 | WC_CHARS = 2, | 64 | WC_CHARS = 2, |
65 | WC_LENGTH = 3 | 65 | WC_LENGTH = 3, |
66 | NUM_WCS = 4, | ||
66 | }; | 67 | }; |
67 | 68 | ||
68 | int wc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 69 | int wc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
@@ -72,8 +73,8 @@ int wc_main(int argc UNUSED_PARAM, char **argv) | |||
72 | const char *start_fmt = " %9"COUNT_FMT + 1; | 73 | const char *start_fmt = " %9"COUNT_FMT + 1; |
73 | const char *fname_fmt = " %s\n"; | 74 | const char *fname_fmt = " %s\n"; |
74 | COUNT_T *pcounts; | 75 | COUNT_T *pcounts; |
75 | COUNT_T counts[4]; | 76 | COUNT_T counts[NUM_WCS]; |
76 | COUNT_T totals[4]; | 77 | COUNT_T totals[NUM_WCS]; |
77 | int num_files; | 78 | int num_files; |
78 | smallint status = EXIT_SUCCESS; | 79 | smallint status = EXIT_SUCCESS; |
79 | unsigned print_type; | 80 | unsigned print_type; |
@@ -99,7 +100,7 @@ int wc_main(int argc UNUSED_PARAM, char **argv) | |||
99 | pcounts = counts; | 100 | pcounts = counts; |
100 | 101 | ||
101 | num_files = 0; | 102 | num_files = 0; |
102 | while ((arg = *argv++) != 0) { | 103 | while ((arg = *argv++) != NULL) { |
103 | FILE *fp; | 104 | FILE *fp; |
104 | const char *s; | 105 | const char *s; |
105 | unsigned u; | 106 | unsigned u; |
@@ -117,20 +118,20 @@ int wc_main(int argc UNUSED_PARAM, char **argv) | |||
117 | linepos = 0; | 118 | linepos = 0; |
118 | in_word = 0; | 119 | in_word = 0; |
119 | 120 | ||
120 | do { | 121 | while (1) { |
121 | int c; | 122 | int c; |
122 | /* Our -w doesn't match GNU wc exactly... oh well */ | 123 | /* Our -w doesn't match GNU wc exactly... oh well */ |
123 | 124 | ||
124 | ++counts[WC_CHARS]; | ||
125 | c = getc(fp); | 125 | c = getc(fp); |
126 | if (c == EOF) { | 126 | if (c == EOF) { |
127 | if (ferror(fp)) { | 127 | if (ferror(fp)) { |
128 | bb_simple_perror_msg(arg); | 128 | bb_simple_perror_msg(arg); |
129 | status = EXIT_FAILURE; | 129 | status = EXIT_FAILURE; |
130 | } | 130 | } |
131 | --counts[WC_CHARS]; | ||
132 | goto DO_EOF; /* Treat an EOF as '\r'. */ | 131 | goto DO_EOF; /* Treat an EOF as '\r'. */ |
133 | } | 132 | } |
133 | ++counts[WC_CHARS]; | ||
134 | |||
134 | if (isprint_asciionly(c)) { | 135 | if (isprint_asciionly(c)) { |
135 | ++linepos; | 136 | ++linepos; |
136 | if (!isspace(c)) { | 137 | if (!isspace(c)) { |
@@ -167,18 +168,18 @@ int wc_main(int argc UNUSED_PARAM, char **argv) | |||
167 | if (c == EOF) { | 168 | if (c == EOF) { |
168 | break; | 169 | break; |
169 | } | 170 | } |
170 | } while (1); | 171 | } |
172 | |||
173 | fclose_if_not_stdin(fp); | ||
171 | 174 | ||
172 | if (totals[WC_LENGTH] < counts[WC_LENGTH]) { | 175 | if (totals[WC_LENGTH] < counts[WC_LENGTH]) { |
173 | totals[WC_LENGTH] = counts[WC_LENGTH]; | 176 | totals[WC_LENGTH] = counts[WC_LENGTH]; |
174 | } | 177 | } |
175 | totals[WC_LENGTH] -= counts[WC_LENGTH]; | 178 | totals[WC_LENGTH] -= counts[WC_LENGTH]; |
176 | 179 | ||
177 | fclose_if_not_stdin(fp); | ||
178 | |||
179 | OUTPUT: | 180 | OUTPUT: |
180 | /* coreutils wc tries hard to print pretty columns | 181 | /* coreutils wc tries hard to print pretty columns |
181 | * (saves results for all files, find max col len etc...) | 182 | * (saves results for all files, finds max col len etc...) |
182 | * we won't try that hard, it will bloat us too much */ | 183 | * we won't try that hard, it will bloat us too much */ |
183 | s = start_fmt; | 184 | s = start_fmt; |
184 | u = 0; | 185 | u = 0; |
@@ -188,7 +189,7 @@ int wc_main(int argc UNUSED_PARAM, char **argv) | |||
188 | s = " %9"COUNT_FMT; /* Ok... restore the leading space. */ | 189 | s = " %9"COUNT_FMT; /* Ok... restore the leading space. */ |
189 | } | 190 | } |
190 | totals[u] += pcounts[u]; | 191 | totals[u] += pcounts[u]; |
191 | } while (++u < 4); | 192 | } while (++u < NUM_WCS); |
192 | printf(fname_fmt, arg); | 193 | printf(fname_fmt, arg); |
193 | } | 194 | } |
194 | 195 | ||