4aea7d8260a37252ad91a550ab59c4b47c66a399
[oweals/busybox.git] / coreutils / wc.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * wc implementation for busybox
4  *
5  * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /* BB_AUDIT SUSv3 _NOT_ compliant -- option -m is not currently supported. */
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/wc.html */
12
13 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
14  *
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.
21  *
22  * TODO:
23  *
24  * When locale support is enabled, count multibyte chars in the '-m' case.
25  *
26  * NOTES:
27  *
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.
32  *
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)
37  *
38  *      echo hello > /tmp/testfile &&
39  *      (dd ibs=1k skip=1 count=0 &> /dev/null ; wc -c) < /tmp/testfile
40  *
41  * for which 'wc -c' should output '0'.
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include "busybox.h"
49
50 #ifdef CONFIG_LOCALE_SUPPORT
51 #include <locale.h>
52 #include <ctype.h>
53 #define isspace_given_isprint(c) isspace(c)
54 #else
55 #undef isspace
56 #undef isprint
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) == ' ')
60 #endif
61
62 enum {
63         WC_LINES        = 0,
64         WC_WORDS        = 1,
65         WC_CHARS        = 2,
66         WC_LENGTH       = 3
67 };
68
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! */
72
73 enum {
74         OP_INC_LINE     = 1, /* OP_INC_LINE must be 1. */
75         OP_SPACE        = 2,
76         OP_NEWLINE      = 4,
77         OP_TAB          = 8,
78         OP_NUL          = 16,
79 };
80
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";
85
86 int wc_main(int argc, char **argv)
87 {
88         FILE *fp;
89         const char *s;
90         unsigned int *pcounts;
91         unsigned int counts[4];
92         unsigned int totals[4];
93         unsigned int linepos;
94         unsigned int u;
95         int num_files = 0;
96         int c;
97         char status = EXIT_SUCCESS;
98         char in_word;
99         char print_type;
100
101         print_type = bb_getopt_ulflags(argc, argv, wc_opts);
102
103         if (print_type == 0) {
104                 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
105         }
106
107         argv += optind;
108         if (!*argv) {
109                 *--argv = (char *) bb_msg_standard_input;
110         }
111
112         memset(totals, 0, sizeof(totals));
113
114         pcounts = counts;
115
116         do {
117                 ++num_files;
118                 if (!(fp = bb_wfopen_input(*argv))) {
119                         status = EXIT_FAILURE;
120                         continue;
121                 }
122
123                 memset(counts, 0, sizeof(counts));
124                 linepos = 0;
125                 in_word = 0;
126
127                 do {
128                         ++counts[WC_CHARS];
129                         c = getc(fp);
130                         if (isprint(c)) {
131                                 ++linepos;
132                                 if (!isspace_given_isprint(c)) {
133                                         in_word = 1;
134                                         continue;
135                                 }
136                         } else if (((unsigned int)(c - 9)) <= 4) {
137                                 /* \t  9
138                                  * \n 10
139                                  * \v 11
140                                  * \f 12
141                                  * \r 13
142                                  */
143                                 if (c == '\t') {
144                                         linepos = (linepos | 7) + 1;
145                                 } else {                        /* '\n', '\r', '\f', or '\v' */
146                                 DO_EOF:
147                                         if (linepos > counts[WC_LENGTH]) {
148                                                 counts[WC_LENGTH] = linepos;
149                                         }
150                                         if (c == '\n') {
151                                                 ++counts[WC_LINES];
152                                         }
153                                         if (c != '\v') {
154                                                 linepos = 0;
155                                         }
156                                 }
157                         } else if (c == EOF) {
158                                 if (ferror(fp)) {
159                                         bb_perror_msg("%s", *argv);
160                                         status = EXIT_FAILURE;
161                                 }
162                                 --counts[WC_CHARS];
163                                 goto DO_EOF;            /* Treat an EOF as '\r'. */
164                         } else {
165                                 continue;
166                         }
167
168                         counts[WC_WORDS] += in_word;
169                         in_word = 0;
170                         if (c == EOF) {
171                                 break;
172                         }
173                 } while (1);
174
175                 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
176                         totals[WC_LENGTH] = counts[WC_LENGTH];
177                 }
178                 totals[WC_LENGTH] -= counts[WC_LENGTH];
179
180                 bb_fclose_nonstdin(fp);
181
182         OUTPUT:
183                 s = fmt_str + 1;                        /* Skip the leading space on 1st pass. */
184                 u = 0;
185                 do {
186                         if (print_type & (1 << u)) {
187                                 bb_printf(s, pcounts[u]);
188                                 s = fmt_str;            /* Ok... restore the leading space. */
189                         }
190                         totals[u] += pcounts[u];
191                 } while (++u < 4);
192
193                 s += 8;                                         /* Set the format to the empty string. */
194
195                 if (*argv != bb_msg_standard_input) {
196                         s -= 3;                                 /* We have a name, so do %s conversion. */
197                 }
198                 bb_printf(s, *argv);
199
200         } while (*++argv);
201
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. */
206         if (num_files > 1) {
207                 num_files = 0;                          /* Make sure we don't get here again. */
208                 *--argv = (char *) total_str;
209                 pcounts = totals;
210                 goto OUTPUT;
211         }
212
213         bb_fflush_stdout_and_exit(status);
214 }