blob: 973929ebeb765caec76eedfb272bd616b1ee3e91 [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
Eric Andersencbe31da2001-02-20 06:14:08 +000044#include "busybox.h"
Erik Andersen31638212000-01-15 22:28:50 +000045
Manuel Novoa III cad53642003-03-19 09:13:01 +000046#ifdef CONFIG_LOCALE_SUPPORT
Manuel Novoa III cad53642003-03-19 09:13:01 +000047#define isspace_given_isprint(c) isspace(c)
48#else
49#undef isspace
50#undef isprint
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) == ' ')
54#endif
55
Denis Vlasenko70210162006-09-29 23:41:59 +000056#if ENABLE_FEATURE_WC_LARGE
57#define COUNT_T unsigned long long
58#define COUNT_FMT "llu"
59#else
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000060#define COUNT_T unsigned
61#define COUNT_FMT "u"
Denis Vlasenko70210162006-09-29 23:41:59 +000062#endif
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000063
Manuel Novoa III cad53642003-03-19 09:13:01 +000064enum {
65 WC_LINES = 0,
66 WC_WORDS = 1,
67 WC_CHARS = 2,
68 WC_LENGTH = 3
Glenn L McGrath02d090d2001-11-21 09:17:00 +000069};
Erik Andersen31638212000-01-15 22:28:50 +000070
Erik Andersene49d5ec2000-02-08 19:58:47 +000071int wc_main(int argc, char **argv)
72{
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 FILE *fp;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000074 const char *s, *arg;
75 const char *start_fmt = "%9"COUNT_FMT;
76 const char *fname_fmt = " %s\n";
77 COUNT_T *pcounts;
78 COUNT_T counts[4];
79 COUNT_T totals[4];
80 unsigned linepos;
81 unsigned u;
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 int num_files = 0;
83 int c;
84 char status = EXIT_SUCCESS;
85 char in_word;
Denis Vlasenko70210162006-09-29 23:41:59 +000086 unsigned print_type;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087
Denis Vlasenko67b23e62006-10-03 21:00:06 +000088 print_type = getopt32(argc, argv, "lwcL");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000089
Glenn L McGrath02d090d2001-11-21 09:17:00 +000090 if (print_type == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
Glenn L McGrath02d090d2001-11-21 09:17:00 +000092 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000093
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 argv += optind;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000095 if (!argv[0]) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 *--argv = (char *) bb_msg_standard_input;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000097 fname_fmt = "\n";
98 if (!((print_type-1) & print_type)) /* exactly one option? */
99 start_fmt = "%"COUNT_FMT;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000101
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 memset(totals, 0, sizeof(totals));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000103
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 pcounts = counts;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000105
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000106 while ((arg = *argv++) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107 ++num_files;
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000108 fp = fopen_or_warn_stdin(arg);
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000109 if (!fp) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 status = EXIT_FAILURE;
111 continue;
112 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000113
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 memset(counts, 0, sizeof(counts));
115 linepos = 0;
116 in_word = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000117
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 do {
Denis Vlasenko70210162006-09-29 23:41:59 +0000119 /* Our -w doesn't match GNU wc exactly... oh well */
120
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 ++counts[WC_CHARS];
122 c = getc(fp);
123 if (isprint(c)) {
124 ++linepos;
125 if (!isspace_given_isprint(c)) {
126 in_word = 1;
127 continue;
128 }
129 } else if (((unsigned int)(c - 9)) <= 4) {
130 /* \t 9
131 * \n 10
132 * \v 11
133 * \f 12
134 * \r 13
135 */
136 if (c == '\t') {
137 linepos = (linepos | 7) + 1;
138 } else { /* '\n', '\r', '\f', or '\v' */
139 DO_EOF:
140 if (linepos > counts[WC_LENGTH]) {
141 counts[WC_LENGTH] = linepos;
142 }
143 if (c == '\n') {
144 ++counts[WC_LINES];
145 }
146 if (c != '\v') {
147 linepos = 0;
148 }
149 }
150 } else if (c == EOF) {
151 if (ferror(fp)) {
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000152 bb_perror_msg("%s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000153 status = EXIT_FAILURE;
154 }
155 --counts[WC_CHARS];
156 goto DO_EOF; /* Treat an EOF as '\r'. */
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000157 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 continue;
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000159 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000160
Manuel Novoa III cad53642003-03-19 09:13:01 +0000161 counts[WC_WORDS] += in_word;
162 in_word = 0;
163 if (c == EOF) {
164 break;
165 }
166 } while (1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000167
Manuel Novoa III cad53642003-03-19 09:13:01 +0000168 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
169 totals[WC_LENGTH] = counts[WC_LENGTH];
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000170 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 totals[WC_LENGTH] -= counts[WC_LENGTH];
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000172
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000173 fclose_if_not_stdin(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000174
Manuel Novoa III cad53642003-03-19 09:13:01 +0000175 OUTPUT:
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000176 /* coreutils wc tries hard to print pretty columns
177 * (saves results for all files, find max col len etc...)
178 * we won't try that hard, it will bloat us too much */
179 s = start_fmt;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 u = 0;
181 do {
182 if (print_type & (1 << u)) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000183 printf(s, pcounts[u]);
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000184 s = " %9"COUNT_FMT; /* Ok... restore the leading space. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000185 }
186 totals[u] += pcounts[u];
187 } while (++u < 4);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000188 printf(fname_fmt, arg);
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000189 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000190
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191 /* If more than one file was processed, we want the totals. To save some
192 * space, we set the pcounts ptr to the totals array. This has the side
193 * effect of trashing the totals array after outputting it, but that's
194 * irrelavent since we no longer need it. */
195 if (num_files > 1) {
196 num_files = 0; /* Make sure we don't get here again. */
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000197 arg = "total";
Manuel Novoa III cad53642003-03-19 09:13:01 +0000198 pcounts = totals;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000199 --argv;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000200 goto OUTPUT;
Mark Whitley39505962000-07-20 00:08:10 +0000201 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000202
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000203 fflush_stdout_and_exit(status);
Erik Andersen31638212000-01-15 22:28:50 +0000204}