1 /* vi: set sw=4 ts=4: */
3 * wc implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 /* BB_AUDIT SUSv3 _NOT_ compliant -- option -m is not currently supported. */
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/wc.html */
13 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
15 * Rewritten to fix a number of problems and do some size optimizations.
16 * Problems in the previous busybox implementation (besides bloat) included:
17 * 1) broken 'wc -c' optimization (read note below)
18 * 2) broken handling of '-' args
19 * 3) no checking of ferror on EOF returns
20 * 4) isprint() wasn't considered when word counting.
24 * When locale support is enabled, count multibyte chars in the '-m' case.
28 * The previous busybox wc attempted an optimization using stat for the
29 * case of counting chars only. I omitted that because it was broken.
30 * It didn't take into account the possibility of input coming from a
31 * pipe, or input from a file with file pointer not at the beginning.
33 * To implement such a speed optimization correctly, not only do you
34 * need the size, but also the file position. Note also that the
35 * file position may be past the end of file. Consider the example
36 * (adapted from example in gnu wc.c)
38 * echo hello > /tmp/testfile &&
39 * (dd ibs=1k skip=1 count=0 &> /dev/null ; wc -c) < /tmp/testfile
41 * for which 'wc -c' should output '0'.
50 #ifdef CONFIG_LOCALE_SUPPORT
53 #define isspace_given_isprint(c) isspace(c)
57 #define isspace(c) ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9))))
58 #define isprint(c) (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20))
59 #define isspace_given_isprint(c) ((c) == ' ')
69 /* Note: If this changes, remember to change the initialization of
70 * 'name' in wc_main. It needs to point to the terminating nul. */
71 static const char wc_opts[] = "lwcL"; /* READ THE WARNING ABOVE! */
74 OP_INC_LINE = 1, /* OP_INC_LINE must be 1. */
81 /* Note: If fmt_str changes, the offsets to 's' in the OUTPUT section
82 * will need to be updated. */
83 static const char fmt_str[] = " %7u\0 %s\n";
84 static const char total_str[] = "total";
86 int wc_main(int argc, char **argv)
90 unsigned int *pcounts;
91 unsigned int counts[4];
92 unsigned int totals[4];
97 char status = EXIT_SUCCESS;
101 print_type = bb_getopt_ulflags(argc, argv, wc_opts);
103 if (print_type == 0) {
104 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
109 *--argv = (char *) bb_msg_standard_input;
112 memset(totals, 0, sizeof(totals));
118 if (!(fp = bb_wfopen_input(*argv))) {
119 status = EXIT_FAILURE;
123 memset(counts, 0, sizeof(counts));
132 if (!isspace_given_isprint(c)) {
136 } else if (((unsigned int)(c - 9)) <= 4) {
144 linepos = (linepos | 7) + 1;
145 } else { /* '\n', '\r', '\f', or '\v' */
147 if (linepos > counts[WC_LENGTH]) {
148 counts[WC_LENGTH] = linepos;
157 } else if (c == EOF) {
159 bb_perror_msg("%s", *argv);
160 status = EXIT_FAILURE;
163 goto DO_EOF; /* Treat an EOF as '\r'. */
168 counts[WC_WORDS] += in_word;
175 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
176 totals[WC_LENGTH] = counts[WC_LENGTH];
178 totals[WC_LENGTH] -= counts[WC_LENGTH];
180 bb_fclose_nonstdin(fp);
183 s = fmt_str + 1; /* Skip the leading space on 1st pass. */
186 if (print_type & (1 << u)) {
187 bb_printf(s, pcounts[u]);
188 s = fmt_str; /* Ok... restore the leading space. */
190 totals[u] += pcounts[u];
193 s += 8; /* Set the format to the empty string. */
195 if (*argv != bb_msg_standard_input) {
196 s -= 3; /* We have a name, so do %s conversion. */
202 /* If more than one file was processed, we want the totals. To save some
203 * space, we set the pcounts ptr to the totals array. This has the side
204 * effect of trashing the totals array after outputting it, but that's
205 * irrelavent since we no longer need it. */
207 num_files = 0; /* Make sure we don't get here again. */
208 *--argv = (char *) total_str;
213 bb_fflush_stdout_and_exit(status);