remove bb_printf and the like
[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 "busybox.h"
45
46 #ifdef CONFIG_LOCALE_SUPPORT
47 #include <locale.h>
48 #include <ctype.h>
49 #define isspace_given_isprint(c) isspace(c)
50 #else
51 #undef isspace
52 #undef isprint
53 #define isspace(c) ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9))))
54 #define isprint(c) (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20))
55 #define isspace_given_isprint(c) ((c) == ' ')
56 #endif
57
58 #if ENABLE_FEATURE_WC_LARGE
59 #define COUNT_T unsigned long long
60 #define COUNT_FMT "llu"
61 #else
62 #define COUNT_T unsigned
63 #define COUNT_FMT "u"
64 #endif
65
66 enum {
67         WC_LINES        = 0,
68         WC_WORDS        = 1,
69         WC_CHARS        = 2,
70         WC_LENGTH       = 3
71 };
72
73 int wc_main(int argc, char **argv)
74 {
75         FILE *fp;
76         const char *s, *arg;
77         const char *start_fmt = "%9"COUNT_FMT;
78         const char *fname_fmt = " %s\n";
79         COUNT_T *pcounts;
80         COUNT_T counts[4];
81         COUNT_T totals[4];
82         unsigned linepos;
83         unsigned u;
84         int num_files = 0;
85         int c;
86         char status = EXIT_SUCCESS;
87         char in_word;
88         unsigned print_type;
89
90         print_type = getopt32(argc, argv, "lwcL");
91
92         if (print_type == 0) {
93                 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
94         }
95
96         argv += optind;
97         if (!argv[0]) {
98                 *--argv = (char *) bb_msg_standard_input;
99                 fname_fmt = "\n";
100                 if (!((print_type-1) & print_type)) /* exactly one option? */
101                         start_fmt = "%"COUNT_FMT;
102         }
103
104         memset(totals, 0, sizeof(totals));
105
106         pcounts = counts;
107
108         while ((arg = *argv++) != 0) {
109                 ++num_files;
110                 fp = bb_wfopen_input(arg);
111                 if (!fp) {
112                         status = EXIT_FAILURE;
113                         continue;
114                 }
115
116                 memset(counts, 0, sizeof(counts));
117                 linepos = 0;
118                 in_word = 0;
119
120                 do {
121                         /* Our -w doesn't match GNU wc exactly... oh well */
122
123                         ++counts[WC_CHARS];
124                         c = getc(fp);
125                         if (isprint(c)) {
126                                 ++linepos;
127                                 if (!isspace_given_isprint(c)) {
128                                         in_word = 1;
129                                         continue;
130                                 }
131                         } else if (((unsigned int)(c - 9)) <= 4) {
132                                 /* \t  9
133                                  * \n 10
134                                  * \v 11
135                                  * \f 12
136                                  * \r 13
137                                  */
138                                 if (c == '\t') {
139                                         linepos = (linepos | 7) + 1;
140                                 } else {                        /* '\n', '\r', '\f', or '\v' */
141                                 DO_EOF:
142                                         if (linepos > counts[WC_LENGTH]) {
143                                                 counts[WC_LENGTH] = linepos;
144                                         }
145                                         if (c == '\n') {
146                                                 ++counts[WC_LINES];
147                                         }
148                                         if (c != '\v') {
149                                                 linepos = 0;
150                                         }
151                                 }
152                         } else if (c == EOF) {
153                                 if (ferror(fp)) {
154                                         bb_perror_msg("%s", arg);
155                                         status = EXIT_FAILURE;
156                                 }
157                                 --counts[WC_CHARS];
158                                 goto DO_EOF;            /* Treat an EOF as '\r'. */
159                         } else {
160                                 continue;
161                         }
162
163                         counts[WC_WORDS] += in_word;
164                         in_word = 0;
165                         if (c == EOF) {
166                                 break;
167                         }
168                 } while (1);
169
170                 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
171                         totals[WC_LENGTH] = counts[WC_LENGTH];
172                 }
173                 totals[WC_LENGTH] -= counts[WC_LENGTH];
174
175                 bb_fclose_nonstdin(fp);
176
177         OUTPUT:
178                 /* coreutils wc tries hard to print pretty columns
179                  * (saves results for all files, find max col len etc...)
180                  * we won't try that hard, it will bloat us too much */
181                 s = start_fmt;
182                 u = 0;
183                 do {
184                         if (print_type & (1 << u)) {
185                                 printf(s, pcounts[u]);
186                                 s = " %9"COUNT_FMT; /* Ok... restore the leading space. */
187                         }
188                         totals[u] += pcounts[u];
189                 } while (++u < 4);
190                 printf(fname_fmt, arg);
191         }
192
193         /* If more than one file was processed, we want the totals.  To save some
194          * space, we set the pcounts ptr to the totals array.  This has the side
195          * effect of trashing the totals array after outputting it, but that's
196          * irrelavent since we no longer need it. */
197         if (num_files > 1) {
198                 num_files = 0;                          /* Make sure we don't get here again. */
199                 arg = "total";
200                 pcounts = totals;
201                 --argv;
202                 goto OUTPUT;
203         }
204
205         fflush_stdout_and_exit(status);
206 }