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'.
46 #if ENABLE_LOCALE_SUPPORT
47 #define isspace_given_isprint(c) isspace(c)
51 #define isspace(c) ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9))))
52 #define isprint(c) (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20))
53 #define isspace_given_isprint(c) ((c) == ' ')
56 #if ENABLE_FEATURE_WC_LARGE
57 #define COUNT_T unsigned long long
58 #define COUNT_FMT "llu"
60 #define COUNT_T unsigned
71 int wc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
72 int wc_main(int argc UNUSED_PARAM, char **argv)
76 const char *start_fmt = " %9"COUNT_FMT + 1;
77 const char *fname_fmt = " %s\n";
85 smallint status = EXIT_SUCCESS;
89 print_type = getopt32(argv, "lwcL");
91 if (print_type == 0) {
92 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
97 *--argv = (char *) bb_msg_standard_input;
99 if (!((print_type-1) & print_type)) /* exactly one option? */
100 start_fmt = "%"COUNT_FMT;
103 memset(totals, 0, sizeof(totals));
107 while ((arg = *argv++) != 0) {
109 fp = fopen_or_warn_stdin(arg);
111 status = EXIT_FAILURE;
115 memset(counts, 0, sizeof(counts));
120 /* Our -w doesn't match GNU wc exactly... oh well */
126 if (!isspace_given_isprint(c)) {
130 } else if (((unsigned int)(c - 9)) <= 4) {
138 linepos = (linepos | 7) + 1;
139 } else { /* '\n', '\r', '\f', or '\v' */
141 if (linepos > counts[WC_LENGTH]) {
142 counts[WC_LENGTH] = linepos;
151 } else if (c == EOF) {
153 bb_simple_perror_msg(arg);
154 status = EXIT_FAILURE;
157 goto DO_EOF; /* Treat an EOF as '\r'. */
162 counts[WC_WORDS] += in_word;
169 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
170 totals[WC_LENGTH] = counts[WC_LENGTH];
172 totals[WC_LENGTH] -= counts[WC_LENGTH];
174 fclose_if_not_stdin(fp);
177 /* coreutils wc tries hard to print pretty columns
178 * (saves results for all files, find max col len etc...)
179 * we won't try that hard, it will bloat us too much */
183 if (print_type & (1 << u)) {
184 printf(s, pcounts[u]);
185 s = " %9"COUNT_FMT; /* Ok... restore the leading space. */
187 totals[u] += pcounts[u];
189 printf(fname_fmt, arg);
192 /* If more than one file was processed, we want the totals. To save some
193 * space, we set the pcounts ptr to the totals array. This has the side
194 * effect of trashing the totals array after outputting it, but that's
195 * irrelavent since we no longer need it. */
197 num_files = 0; /* Make sure we don't get here again. */
204 fflush_stdout_and_exit(status);