1 /* vi: set sw=4 ts=4: */
3 * wc implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* BB_AUDIT SUSv3 _NOT_ compliant -- option -m is not currently supported. */
24 /* http://www.opengroup.org/onlinepubs/007904975/utilities/wc.html */
26 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
28 * Rewritten to fix a number of problems and do some size optimizations.
29 * Problems in the previous busybox implementation (besides bloat) included:
30 * 1) broken 'wc -c' optimization (read note below)
31 * 2) broken handling of '-' args
32 * 3) no checking of ferror on EOF returns
33 * 4) isprint() wasn't considered when word counting.
37 * When locale support is enabled, count multibyte chars in the '-m' case.
41 * The previous busybox wc attempted an optimization using stat for the
42 * case of counting chars only. I omitted that because it was broken.
43 * It didn't take into account the possibility of input coming from a
44 * pipe, or input from a file with file pointer not at the beginning.
46 * To implement such a speed optimization correctly, not only do you
47 * need the size, but also the file position. Note also that the
48 * file position may be past the end of file. Consider the example
49 * (adapted from example in gnu wc.c)
51 * echo hello > /tmp/testfile &&
52 * (dd ibs=1k skip=1 count=0 &> /dev/null ; wc -c) < /tmp/testfile
54 * for which 'wc -c' should output '0'.
63 #ifdef CONFIG_LOCALE_SUPPORT
66 #define isspace_given_isprint(c) isspace(c)
70 #define isspace(c) ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9))))
71 #define isprint(c) (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20))
72 #define isspace_given_isprint(c) ((c) == ' ')
82 /* Note: If this changes, remember to change the initialization of
83 * 'name' in wc_main. It needs to point to the terminating nul. */
84 static const char wc_opts[] = "lwcL"; /* READ THE WARNING ABOVE! */
87 OP_INC_LINE = 1, /* OP_INC_LINE must be 1. */
94 /* Note: If fmt_str changes, the offsets to 's' in the OUTPUT section
95 * will need to be updated. */
96 static const char fmt_str[] = " %7u\0 %s\n";
97 static const char total_str[] = "total";
99 int wc_main(int argc, char **argv)
103 unsigned int *pcounts;
104 unsigned int counts[4];
105 unsigned int totals[4];
106 unsigned int linepos;
110 char status = EXIT_SUCCESS;
114 print_type = bb_getopt_ulflags(argc, argv, wc_opts);
116 if (print_type == 0) {
117 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
122 *--argv = (char *) bb_msg_standard_input;
125 memset(totals, 0, sizeof(totals));
131 if (!(fp = bb_wfopen_input(*argv))) {
132 status = EXIT_FAILURE;
136 memset(counts, 0, sizeof(counts));
145 if (!isspace_given_isprint(c)) {
149 } else if (((unsigned int)(c - 9)) <= 4) {
157 linepos = (linepos | 7) + 1;
158 } else { /* '\n', '\r', '\f', or '\v' */
160 if (linepos > counts[WC_LENGTH]) {
161 counts[WC_LENGTH] = linepos;
170 } else if (c == EOF) {
172 bb_perror_msg("%s", *argv);
173 status = EXIT_FAILURE;
176 goto DO_EOF; /* Treat an EOF as '\r'. */
181 counts[WC_WORDS] += in_word;
188 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
189 totals[WC_LENGTH] = counts[WC_LENGTH];
191 totals[WC_LENGTH] -= counts[WC_LENGTH];
193 bb_fclose_nonstdin(fp);
196 s = fmt_str + 1; /* Skip the leading space on 1st pass. */
199 if (print_type & (1 << u)) {
200 bb_printf(s, pcounts[u]);
201 s = fmt_str; /* Ok... restore the leading space. */
203 totals[u] += pcounts[u];
206 s += 8; /* Set the format to the empty string. */
208 if (*argv != bb_msg_standard_input) {
209 s -= 3; /* We have a name, so do %s conversion. */
215 /* If more than one file was processed, we want the totals. To save some
216 * space, we set the pcounts ptr to the totals array. This has the side
217 * effect of trashing the totals array after outputting it, but that's
218 * irrelavent since we no longer need it. */
220 num_files = 0; /* Make sure we don't get here again. */
221 *--argv = (char *) total_str;
226 bb_fflush_stdout_and_exit(status);