blob: 3e32e3d6db7acef599dece39c54ebcfc7e06ed5d [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 &&
Denis Vlasenkob71c6682007-07-21 15:08:09 +000039 * (dd ibs=1k skip=1 count=0 &> /dev/null; wc -c) < /tmp/testfile
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 *
41 * for which 'wc -c' should output '0'.
42 */
43
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000044#include "libbb.h"
Erik Andersen31638212000-01-15 22:28:50 +000045
Denys Vlasenkoc0dab372009-10-22 22:28:08 +020046#if !ENABLE_LOCALE_SUPPORT
47# undef isprint
48# undef isspace
49# define isprint(c) ((unsigned)((c) - 0x20) <= (0x7e - 0x20))
50# define isspace(c) ((c) == ' ')
Manuel Novoa III cad53642003-03-19 09:13:01 +000051#endif
52
Denis Vlasenko70210162006-09-29 23:41:59 +000053#if ENABLE_FEATURE_WC_LARGE
Denys Vlasenkoc0dab372009-10-22 22:28:08 +020054# define COUNT_T unsigned long long
55# define COUNT_FMT "llu"
Denis Vlasenko70210162006-09-29 23:41:59 +000056#else
Denys Vlasenkoc0dab372009-10-22 22:28:08 +020057# define COUNT_T unsigned
58# define COUNT_FMT "u"
Denis Vlasenko70210162006-09-29 23:41:59 +000059#endif
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000060
Manuel Novoa III cad53642003-03-19 09:13:01 +000061enum {
62 WC_LINES = 0,
63 WC_WORDS = 1,
64 WC_CHARS = 2,
65 WC_LENGTH = 3
Glenn L McGrath02d090d2001-11-21 09:17:00 +000066};
Erik Andersen31638212000-01-15 22:28:50 +000067
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000068int wc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000069int wc_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000070{
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 FILE *fp;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000072 const char *s, *arg;
Denis Vlasenkoe8419c92008-02-19 00:38:10 +000073 const char *start_fmt = " %9"COUNT_FMT + 1;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000074 const char *fname_fmt = " %s\n";
75 COUNT_T *pcounts;
76 COUNT_T counts[4];
77 COUNT_T totals[4];
78 unsigned linepos;
79 unsigned u;
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 int num_files = 0;
81 int c;
Bernhard Reutner-Fischerd58c1942007-01-20 21:28:36 +000082 smallint status = EXIT_SUCCESS;
83 smallint in_word;
Denis Vlasenko70210162006-09-29 23:41:59 +000084 unsigned print_type;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000085
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000086 print_type = getopt32(argv, "lwcL");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087
Glenn L McGrath02d090d2001-11-21 09:17:00 +000088 if (print_type == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
Glenn L McGrath02d090d2001-11-21 09:17:00 +000090 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000091
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 argv += optind;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000093 if (!argv[0]) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 *--argv = (char *) bb_msg_standard_input;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000095 fname_fmt = "\n";
96 if (!((print_type-1) & print_type)) /* exactly one option? */
97 start_fmt = "%"COUNT_FMT;
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000099
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 memset(totals, 0, sizeof(totals));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000101
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 pcounts = counts;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000103
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000104 while ((arg = *argv++) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 ++num_files;
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000106 fp = fopen_or_warn_stdin(arg);
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000107 if (!fp) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 status = EXIT_FAILURE;
109 continue;
110 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000111
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 memset(counts, 0, sizeof(counts));
113 linepos = 0;
114 in_word = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000115
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 do {
Denis Vlasenko70210162006-09-29 23:41:59 +0000117 /* Our -w doesn't match GNU wc exactly... oh well */
118
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 ++counts[WC_CHARS];
120 c = getc(fp);
121 if (isprint(c)) {
122 ++linepos;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +0200123 if (!isspace(c)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 in_word = 1;
125 continue;
126 }
Denys Vlasenkoc0dab372009-10-22 22:28:08 +0200127 } else if ((unsigned)(c - 9) <= 4) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000128 /* \t 9
129 * \n 10
130 * \v 11
131 * \f 12
132 * \r 13
133 */
134 if (c == '\t') {
135 linepos = (linepos | 7) + 1;
136 } else { /* '\n', '\r', '\f', or '\v' */
137 DO_EOF:
138 if (linepos > counts[WC_LENGTH]) {
139 counts[WC_LENGTH] = linepos;
140 }
141 if (c == '\n') {
142 ++counts[WC_LINES];
143 }
144 if (c != '\v') {
145 linepos = 0;
146 }
147 }
148 } else if (c == EOF) {
149 if (ferror(fp)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000150 bb_simple_perror_msg(arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151 status = EXIT_FAILURE;
152 }
153 --counts[WC_CHARS];
154 goto DO_EOF; /* Treat an EOF as '\r'. */
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000155 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000156 continue;
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000157 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000158
Manuel Novoa III cad53642003-03-19 09:13:01 +0000159 counts[WC_WORDS] += in_word;
160 in_word = 0;
161 if (c == EOF) {
162 break;
163 }
164 } while (1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000165
Manuel Novoa III cad53642003-03-19 09:13:01 +0000166 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
167 totals[WC_LENGTH] = counts[WC_LENGTH];
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000168 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 totals[WC_LENGTH] -= counts[WC_LENGTH];
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000170
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000171 fclose_if_not_stdin(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000172
Manuel Novoa III cad53642003-03-19 09:13:01 +0000173 OUTPUT:
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000174 /* coreutils wc tries hard to print pretty columns
175 * (saves results for all files, find max col len etc...)
176 * we won't try that hard, it will bloat us too much */
177 s = start_fmt;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 u = 0;
179 do {
180 if (print_type & (1 << u)) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000181 printf(s, pcounts[u]);
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000182 s = " %9"COUNT_FMT; /* Ok... restore the leading space. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 }
184 totals[u] += pcounts[u];
185 } while (++u < 4);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000186 printf(fname_fmt, arg);
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000187 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000188
Manuel Novoa III cad53642003-03-19 09:13:01 +0000189 /* If more than one file was processed, we want the totals. To save some
190 * space, we set the pcounts ptr to the totals array. This has the side
191 * effect of trashing the totals array after outputting it, but that's
192 * irrelavent since we no longer need it. */
193 if (num_files > 1) {
194 num_files = 0; /* Make sure we don't get here again. */
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000195 arg = "total";
Manuel Novoa III cad53642003-03-19 09:13:01 +0000196 pcounts = totals;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000197 --argv;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000198 goto OUTPUT;
Mark Whitley39505962000-07-20 00:08:10 +0000199 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000200
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000201 fflush_stdout_and_exit(status);
Erik Andersen31638212000-01-15 22:28:50 +0000202}