blob: 0eb795c4b2f7be1716559477dadda33fce410ae1 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen31638212000-01-15 22:28:50 +00002/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * wc implementation for busybox
Erik Andersen31638212000-01-15 22:28:50 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
Erik Andersen31638212000-01-15 22:28:50 +00006 *
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.
11 *
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.
16 *
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
20 *
21 */
22
Manuel Novoa III cad53642003-03-19 09:13:01 +000023/* BB_AUDIT SUSv3 _NOT_ compliant -- option -m is not currently supported. */
24/* http://www.opengroup.org/onlinepubs/007904975/utilities/wc.html */
25
26/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
27 *
28 * Rewritten to fix a number of problems and do some size optimizations.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000029 * Problems in the previous busybox implementation (besides bloat) included:
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 * 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.
34 *
35 * TODO:
36 *
37 * When locale support is enabled, count multibyte chars in the '-m' case.
38 *
39 * NOTES:
40 *
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.
45 *
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)
50 *
51 * echo hello > /tmp/testfile &&
52 * (dd ibs=1k skip=1 count=0 &> /dev/null ; wc -c) < /tmp/testfile
53 *
54 * for which 'wc -c' should output '0'.
55 */
56
Erik Andersen31638212000-01-15 22:28:50 +000057#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000058#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000059#include <string.h>
60#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000061#include "busybox.h"
Erik Andersen31638212000-01-15 22:28:50 +000062
Manuel Novoa III cad53642003-03-19 09:13:01 +000063#ifdef CONFIG_LOCALE_SUPPORT
64#include <locale.h>
65#include <ctype.h>
66#define isspace_given_isprint(c) isspace(c)
67#else
68#undef isspace
69#undef isprint
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) == ' ')
73#endif
74
75enum {
76 WC_LINES = 0,
77 WC_WORDS = 1,
78 WC_CHARS = 2,
79 WC_LENGTH = 3
Glenn L McGrath02d090d2001-11-21 09:17:00 +000080};
Erik Andersen31638212000-01-15 22:28:50 +000081
Manuel Novoa III cad53642003-03-19 09:13:01 +000082/* Note: If this changes, remember to change the initialization of
83 * 'name' in wc_main. It needs to point to the terminating nul. */
84static const char wc_opts[] = "lwcL"; /* READ THE WARNING ABOVE! */
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +000085
Manuel Novoa III cad53642003-03-19 09:13:01 +000086enum {
87 OP_INC_LINE = 1, /* OP_INC_LINE must be 1. */
88 OP_SPACE = 2,
89 OP_NEWLINE = 4,
90 OP_TAB = 8,
91 OP_NUL = 16,
92};
Matt Kraai38c15be2001-12-20 21:11:59 +000093
Manuel Novoa III cad53642003-03-19 09:13:01 +000094/* Note: If fmt_str changes, the offsets to 's' in the OUTPUT section
95 * will need to be updated. */
96static const char fmt_str[] = " %7u\0 %s\n";
97static const char total_str[] = "total";
Erik Andersen31638212000-01-15 22:28:50 +000098
Erik Andersene49d5ec2000-02-08 19:58:47 +000099int wc_main(int argc, char **argv)
100{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 FILE *fp;
102 const char *s;
103 unsigned int *pcounts;
104 unsigned int counts[4];
105 unsigned int totals[4];
106 unsigned int linepos;
107 unsigned int u;
108 int num_files = 0;
109 int c;
110 char status = EXIT_SUCCESS;
111 char in_word;
112 char print_type;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000113
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 print_type = bb_getopt_ulflags(argc, argv, wc_opts);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000115
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000116 if (print_type == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000118 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000119
Manuel Novoa III cad53642003-03-19 09:13:01 +0000120 argv += optind;
121 if (!*argv) {
122 *--argv = (char *) bb_msg_standard_input;
123 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000124
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 memset(totals, 0, sizeof(totals));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000126
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 pcounts = counts;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000128
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 do {
130 ++num_files;
131 if (!(fp = bb_wfopen_input(*argv))) {
132 status = EXIT_FAILURE;
133 continue;
134 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000135
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 memset(counts, 0, sizeof(counts));
137 linepos = 0;
138 in_word = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000139
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 do {
141 ++counts[WC_CHARS];
142 c = getc(fp);
143 if (isprint(c)) {
144 ++linepos;
145 if (!isspace_given_isprint(c)) {
146 in_word = 1;
147 continue;
148 }
149 } else if (((unsigned int)(c - 9)) <= 4) {
150 /* \t 9
151 * \n 10
152 * \v 11
153 * \f 12
154 * \r 13
155 */
156 if (c == '\t') {
157 linepos = (linepos | 7) + 1;
158 } else { /* '\n', '\r', '\f', or '\v' */
159 DO_EOF:
160 if (linepos > counts[WC_LENGTH]) {
161 counts[WC_LENGTH] = linepos;
162 }
163 if (c == '\n') {
164 ++counts[WC_LINES];
165 }
166 if (c != '\v') {
167 linepos = 0;
168 }
169 }
170 } else if (c == EOF) {
171 if (ferror(fp)) {
172 bb_perror_msg("%s", *argv);
173 status = EXIT_FAILURE;
174 }
175 --counts[WC_CHARS];
176 goto DO_EOF; /* Treat an EOF as '\r'. */
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000177 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 continue;
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000179 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000180
Manuel Novoa III cad53642003-03-19 09:13:01 +0000181 counts[WC_WORDS] += in_word;
182 in_word = 0;
183 if (c == EOF) {
184 break;
185 }
186 } while (1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000187
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
189 totals[WC_LENGTH] = counts[WC_LENGTH];
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000190 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191 totals[WC_LENGTH] -= counts[WC_LENGTH];
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000192
Manuel Novoa III cad53642003-03-19 09:13:01 +0000193 bb_fclose_nonstdin(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000194
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 OUTPUT:
196 s = fmt_str + 1; /* Skip the leading space on 1st pass. */
197 u = 0;
198 do {
199 if (print_type & (1 << u)) {
200 bb_printf(s, pcounts[u]);
201 s = fmt_str; /* Ok... restore the leading space. */
202 }
203 totals[u] += pcounts[u];
204 } while (++u < 4);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000205
Manuel Novoa III cad53642003-03-19 09:13:01 +0000206 s += 8; /* Set the format to the empty string. */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000207
Manuel Novoa III cad53642003-03-19 09:13:01 +0000208 if (*argv != bb_msg_standard_input) {
209 s -= 3; /* We have a name, so do %s conversion. */
Erik Andersen31638212000-01-15 22:28:50 +0000210 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000211 bb_printf(s, *argv);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000212
Manuel Novoa III cad53642003-03-19 09:13:01 +0000213 } while (*++argv);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000214
Manuel Novoa III cad53642003-03-19 09:13:01 +0000215 /* 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. */
219 if (num_files > 1) {
220 num_files = 0; /* Make sure we don't get here again. */
221 *--argv = (char *) total_str;
222 pcounts = totals;
223 goto OUTPUT;
Mark Whitley39505962000-07-20 00:08:10 +0000224 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000225
Manuel Novoa III cad53642003-03-19 09:13:01 +0000226 bb_fflush_stdout_and_exit(status);
Erik Andersen31638212000-01-15 22:28:50 +0000227}