1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56#include "libbb.h"
57#include "unicode.h"
58
59#if !ENABLE_LOCALE_SUPPORT
60# undef isprint
61# undef isspace
62# define isprint(c) ((unsigned)((c) - 0x20) <= (0x7e - 0x20))
63# define isspace(c) ((c) == ' ')
64#endif
65
66#if ENABLE_FEATURE_WC_LARGE
67# define COUNT_T unsigned long long
68# define COUNT_FMT "llu"
69#else
70# define COUNT_T unsigned
71# define COUNT_FMT "u"
72#endif
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99enum {
100 WC_LINES = 0,
101 WC_WORDS = 1,
102 WC_UNICHARS = 2,
103 WC_BYTES = 3,
104 WC_LENGTH = 4,
105 NUM_WCS = 5,
106};
107
108int wc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
109int wc_main(int argc UNUSED_PARAM, char **argv)
110{
111 const char *arg;
112 const char *start_fmt = " %9"COUNT_FMT + 1;
113 const char *fname_fmt = " %s\n";
114 COUNT_T *pcounts;
115 COUNT_T counts[NUM_WCS];
116 COUNT_T totals[NUM_WCS];
117 int num_files;
118 smallint status = EXIT_SUCCESS;
119 unsigned print_type;
120
121 init_unicode();
122
123 print_type = getopt32(argv, "lwmcL");
124
125 if (print_type == 0) {
126 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_BYTES);
127 }
128
129 argv += optind;
130 if (!argv[0]) {
131 *--argv = (char *) bb_msg_standard_input;
132 fname_fmt = "\n";
133 }
134 if (!argv[1]) {
135 if (!((print_type-1) & print_type))
136 start_fmt = "%"COUNT_FMT;
137 }
138
139 memset(totals, 0, sizeof(totals));
140
141 pcounts = counts;
142
143 num_files = 0;
144 while ((arg = *argv++) != NULL) {
145 FILE *fp;
146 const char *s;
147 unsigned u;
148 unsigned linepos;
149 smallint in_word;
150
151 ++num_files;
152 fp = fopen_or_warn_stdin(arg);
153 if (!fp) {
154 status = EXIT_FAILURE;
155 continue;
156 }
157
158 memset(counts, 0, sizeof(counts));
159 linepos = 0;
160 in_word = 0;
161
162 while (1) {
163 int c;
164
165
166 c = getc(fp);
167 if (c == EOF) {
168 if (ferror(fp)) {
169 bb_simple_perror_msg(arg);
170 status = EXIT_FAILURE;
171 }
172 goto DO_EOF;
173 }
174
175
176 ++counts[WC_BYTES];
177 if (unicode_status != UNICODE_ON
178 || (c & 0xc0) != 0x80
179 ) {
180 ++counts[WC_UNICHARS];
181 }
182
183 if (isprint_asciionly(c)) {
184 ++linepos;
185 if (!isspace(c)) {
186 in_word = 1;
187 continue;
188 }
189 } else if ((unsigned)(c - 9) <= 4) {
190
191
192
193
194
195
196 if (c == '\t') {
197 linepos = (linepos | 7) + 1;
198 } else {
199 DO_EOF:
200 if (linepos > counts[WC_LENGTH]) {
201 counts[WC_LENGTH] = linepos;
202 }
203 if (c == '\n') {
204 ++counts[WC_LINES];
205 }
206 if (c != '\v') {
207 linepos = 0;
208 }
209 }
210 } else {
211 continue;
212 }
213
214 counts[WC_WORDS] += in_word;
215 in_word = 0;
216 if (c == EOF) {
217 break;
218 }
219 }
220
221 fclose_if_not_stdin(fp);
222
223 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
224 totals[WC_LENGTH] = counts[WC_LENGTH];
225 }
226 totals[WC_LENGTH] -= counts[WC_LENGTH];
227
228 OUTPUT:
229
230
231
232 s = start_fmt;
233 u = 0;
234 do {
235 if (print_type & (1 << u)) {
236 printf(s, pcounts[u]);
237 s = " %9"COUNT_FMT;
238 }
239 totals[u] += pcounts[u];
240 } while (++u < NUM_WCS);
241 printf(fname_fmt, arg);
242 }
243
244
245
246
247
248 if (num_files > 1) {
249 num_files = 0;
250 arg = "total";
251 pcounts = totals;
252 --argv;
253 goto OUTPUT;
254 }
255
256 fflush_stdout_and_exit(status);
257}
258