blob: 4aea7d8260a37252ad91a550ab59c4b47c66a399 [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 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersen31638212000-01-15 22:28:50 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* 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.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000016 * Problems in the previous busybox implementation (besides bloat) included:
Manuel Novoa III cad53642003-03-19 09:13:01 +000017 * 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
Erik Andersen31638212000-01-15 22:28:50 +000044#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000045#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000046#include <string.h>
47#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000048#include "busybox.h"
Erik Andersen31638212000-01-15 22:28:50 +000049
Manuel Novoa III cad53642003-03-19 09:13:01 +000050#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
62enum {
63 WC_LINES = 0,
64 WC_WORDS = 1,
65 WC_CHARS = 2,
66 WC_LENGTH = 3
Glenn L McGrath02d090d2001-11-21 09:17:00 +000067};
Erik Andersen31638212000-01-15 22:28:50 +000068
Manuel Novoa III cad53642003-03-19 09:13:01 +000069/* Note: If this changes, remember to change the initialization of
70 * 'name' in wc_main. It needs to point to the terminating nul. */
71static const char wc_opts[] = "lwcL"; /* READ THE WARNING ABOVE! */
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +000072
Manuel Novoa III cad53642003-03-19 09:13:01 +000073enum {
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};
Matt Kraai38c15be2001-12-20 21:11:59 +000080
Manuel Novoa III cad53642003-03-19 09:13:01 +000081/* Note: If fmt_str changes, the offsets to 's' in the OUTPUT section
82 * will need to be updated. */
83static const char fmt_str[] = " %7u\0 %s\n";
84static const char total_str[] = "total";
Erik Andersen31638212000-01-15 22:28:50 +000085
Erik Andersene49d5ec2000-02-08 19:58:47 +000086int wc_main(int argc, char **argv)
87{
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 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;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000100
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 print_type = bb_getopt_ulflags(argc, argv, wc_opts);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000102
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000103 if (print_type == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000105 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000106
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107 argv += optind;
108 if (!*argv) {
109 *--argv = (char *) bb_msg_standard_input;
110 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000111
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 memset(totals, 0, sizeof(totals));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000113
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 pcounts = counts;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000115
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 do {
117 ++num_files;
118 if (!(fp = bb_wfopen_input(*argv))) {
119 status = EXIT_FAILURE;
120 continue;
121 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000122
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 memset(counts, 0, sizeof(counts));
124 linepos = 0;
125 in_word = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000126
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 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'. */
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000164 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000165 continue;
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000166 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000167
Manuel Novoa III cad53642003-03-19 09:13:01 +0000168 counts[WC_WORDS] += in_word;
169 in_word = 0;
170 if (c == EOF) {
171 break;
172 }
173 } while (1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000174
Manuel Novoa III cad53642003-03-19 09:13:01 +0000175 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
176 totals[WC_LENGTH] = counts[WC_LENGTH];
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000177 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 totals[WC_LENGTH] -= counts[WC_LENGTH];
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000179
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 bb_fclose_nonstdin(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000181
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 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);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000192
Manuel Novoa III cad53642003-03-19 09:13:01 +0000193 s += 8; /* Set the format to the empty string. */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000194
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 if (*argv != bb_msg_standard_input) {
196 s -= 3; /* We have a name, so do %s conversion. */
Erik Andersen31638212000-01-15 22:28:50 +0000197 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000198 bb_printf(s, *argv);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000199
Manuel Novoa III cad53642003-03-19 09:13:01 +0000200 } while (*++argv);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000201
Manuel Novoa III cad53642003-03-19 09:13:01 +0000202 /* 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;
Mark Whitley39505962000-07-20 00:08:10 +0000211 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000212
Manuel Novoa III cad53642003-03-19 09:13:01 +0000213 bb_fflush_stdout_and_exit(status);
Erik Andersen31638212000-01-15 22:28:50 +0000214}