blob: a410e407a0cb155d00f57509cfb5abf8315aad3a [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen31638212000-01-15 22:28:50 +00008 */
9
Denys Vlasenkoafc7b4c2010-10-04 17:08:14 +020010/* BB_AUDIT SUSv3 compliant. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* 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 *
Manuel Novoa III cad53642003-03-19 09:13:01 +000022 * NOTES:
23 *
24 * The previous busybox wc attempted an optimization using stat for the
25 * case of counting chars only. I omitted that because it was broken.
26 * It didn't take into account the possibility of input coming from a
27 * pipe, or input from a file with file pointer not at the beginning.
28 *
29 * To implement such a speed optimization correctly, not only do you
30 * need the size, but also the file position. Note also that the
31 * file position may be past the end of file. Consider the example
32 * (adapted from example in gnu wc.c)
33 *
34 * echo hello > /tmp/testfile &&
Denis Vlasenkob71c6682007-07-21 15:08:09 +000035 * (dd ibs=1k skip=1 count=0 &> /dev/null; wc -c) < /tmp/testfile
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 *
37 * for which 'wc -c' should output '0'.
38 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000039#include "libbb.h"
Denys Vlasenkoafc7b4c2010-10-04 17:08:14 +020040#include "unicode.h"
Erik Andersen31638212000-01-15 22:28:50 +000041
Denys Vlasenkoc0dab372009-10-22 22:28:08 +020042#if !ENABLE_LOCALE_SUPPORT
43# undef isprint
44# undef isspace
45# define isprint(c) ((unsigned)((c) - 0x20) <= (0x7e - 0x20))
46# define isspace(c) ((c) == ' ')
Manuel Novoa III cad53642003-03-19 09:13:01 +000047#endif
48
Denis Vlasenko70210162006-09-29 23:41:59 +000049#if ENABLE_FEATURE_WC_LARGE
Denys Vlasenkoc0dab372009-10-22 22:28:08 +020050# define COUNT_T unsigned long long
51# define COUNT_FMT "llu"
Denis Vlasenko70210162006-09-29 23:41:59 +000052#else
Denys Vlasenkoc0dab372009-10-22 22:28:08 +020053# define COUNT_T unsigned
54# define COUNT_FMT "u"
Denis Vlasenko70210162006-09-29 23:41:59 +000055#endif
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000056
Denys Vlasenkoafc7b4c2010-10-04 17:08:14 +020057/* We support -m even when UNICODE_SUPPORT is off,
58 * we just don't advertise it in help text,
59 * since it is the same as -c in this case.
60 */
61
62//usage:#define wc_trivial_usage
63//usage: "[-c"IF_UNICODE_SUPPORT("m")"lwL] [FILE]..."
64//usage:
65//usage:#define wc_full_usage "\n\n"
66//usage: "Count lines, words, and bytes for each FILE (or stdin)\n"
Denys Vlasenkoafc7b4c2010-10-04 17:08:14 +020067//usage: "\n -c Count bytes"
68//usage: IF_UNICODE_SUPPORT(
69//usage: "\n -m Count characters"
70//usage: )
71//usage: "\n -l Count newlines"
72//usage: "\n -w Count words"
73//usage: "\n -L Print longest line length"
74//usage:
75//usage:#define wc_example_usage
76//usage: "$ wc /etc/passwd\n"
77//usage: " 31 46 1365 /etc/passwd\n"
78
79/* Order is important if we want to be compatible with
80 * column order in "wc -cmlwL" output:
81 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000082enum {
Denys Vlasenko1336f892011-01-22 17:57:01 +010083 WC_LINES = 0, /* -l */
84 WC_WORDS = 1, /* -w */
85 WC_UNICHARS = 2, /* -m */
86 WC_BYTES = 3, /* -c */
87 WC_LENGTH = 4, /* -L */
Denys Vlasenkoafc7b4c2010-10-04 17:08:14 +020088 NUM_WCS = 5,
Glenn L McGrath02d090d2001-11-21 09:17:00 +000089};
Erik Andersen31638212000-01-15 22:28:50 +000090
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000091int wc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000092int wc_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000093{
Dan Fandrich5b0a7f12009-11-18 10:48:09 +010094 const char *arg;
Denis Vlasenkoe8419c92008-02-19 00:38:10 +000095 const char *start_fmt = " %9"COUNT_FMT + 1;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +000096 const char *fname_fmt = " %s\n";
97 COUNT_T *pcounts;
Denys Vlasenko09e7daf2010-10-04 17:04:20 +020098 COUNT_T counts[NUM_WCS];
99 COUNT_T totals[NUM_WCS];
Dan Fandrich5b0a7f12009-11-18 10:48:09 +0100100 int num_files;
Bernhard Reutner-Fischerd58c1942007-01-20 21:28:36 +0000101 smallint status = EXIT_SUCCESS;
Denis Vlasenko70210162006-09-29 23:41:59 +0000102 unsigned print_type;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000103
Denys Vlasenkoafc7b4c2010-10-04 17:08:14 +0200104 init_unicode();
105
Denys Vlasenko1336f892011-01-22 17:57:01 +0100106 print_type = getopt32(argv, "lwmcL");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000107
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000108 if (print_type == 0) {
Denys Vlasenko1336f892011-01-22 17:57:01 +0100109 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_BYTES);
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000110 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000111
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 argv += optind;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000113 if (!argv[0]) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 *--argv = (char *) bb_msg_standard_input;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000115 fname_fmt = "\n";
Denys Vlasenko79950a62010-03-08 22:03:24 +0100116 }
117 if (!argv[1]) { /* zero or one filename? */
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000118 if (!((print_type-1) & print_type)) /* exactly one option? */
119 start_fmt = "%"COUNT_FMT;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000120 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000121
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 memset(totals, 0, sizeof(totals));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000123
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 pcounts = counts;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000125
Dan Fandrich5b0a7f12009-11-18 10:48:09 +0100126 num_files = 0;
Denys Vlasenko09e7daf2010-10-04 17:04:20 +0200127 while ((arg = *argv++) != NULL) {
Dan Fandrich5b0a7f12009-11-18 10:48:09 +0100128 FILE *fp;
129 const char *s;
130 unsigned u;
131 unsigned linepos;
132 smallint in_word;
133
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 ++num_files;
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000135 fp = fopen_or_warn_stdin(arg);
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000136 if (!fp) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 status = EXIT_FAILURE;
138 continue;
139 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000140
Manuel Novoa III cad53642003-03-19 09:13:01 +0000141 memset(counts, 0, sizeof(counts));
142 linepos = 0;
143 in_word = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000144
Denys Vlasenko09e7daf2010-10-04 17:04:20 +0200145 while (1) {
Dan Fandrich5b0a7f12009-11-18 10:48:09 +0100146 int c;
Denis Vlasenko70210162006-09-29 23:41:59 +0000147 /* Our -w doesn't match GNU wc exactly... oh well */
148
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 c = getc(fp);
Dan Fandrich5b0a7f12009-11-18 10:48:09 +0100150 if (c == EOF) {
151 if (ferror(fp)) {
152 bb_simple_perror_msg(arg);
153 status = EXIT_FAILURE;
154 }
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200155 goto DO_EOF; /* Treat an EOF as '\r'. */
Dan Fandrich5b0a7f12009-11-18 10:48:09 +0100156 }
Denys Vlasenko09e7daf2010-10-04 17:04:20 +0200157
Denys Vlasenkoafc7b4c2010-10-04 17:08:14 +0200158 /* Cater for -c and -m */
Denys Vlasenko1336f892011-01-22 17:57:01 +0100159 ++counts[WC_BYTES];
Denys Vlasenkoafc7b4c2010-10-04 17:08:14 +0200160 if (unicode_status != UNICODE_ON /* every byte is a new char */
161 || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
162 ) {
163 ++counts[WC_UNICHARS];
164 }
165
166 if (isprint_asciionly(c)) { /* FIXME: not unicode-aware */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 ++linepos;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +0200168 if (!isspace(c)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 in_word = 1;
170 continue;
171 }
Denys Vlasenkoc0dab372009-10-22 22:28:08 +0200172 } else if ((unsigned)(c - 9) <= 4) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000173 /* \t 9
174 * \n 10
175 * \v 11
176 * \f 12
177 * \r 13
178 */
179 if (c == '\t') {
180 linepos = (linepos | 7) + 1;
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200181 } else { /* '\n', '\r', '\f', or '\v' */
Dan Fandrich5b0a7f12009-11-18 10:48:09 +0100182 DO_EOF:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 if (linepos > counts[WC_LENGTH]) {
184 counts[WC_LENGTH] = linepos;
185 }
186 if (c == '\n') {
187 ++counts[WC_LINES];
188 }
189 if (c != '\v') {
190 linepos = 0;
191 }
192 }
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000193 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000194 continue;
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000195 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000196
Manuel Novoa III cad53642003-03-19 09:13:01 +0000197 counts[WC_WORDS] += in_word;
198 in_word = 0;
199 if (c == EOF) {
200 break;
201 }
Denys Vlasenko09e7daf2010-10-04 17:04:20 +0200202 }
203
204 fclose_if_not_stdin(fp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000205
Manuel Novoa III cad53642003-03-19 09:13:01 +0000206 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
207 totals[WC_LENGTH] = counts[WC_LENGTH];
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000208 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000209 totals[WC_LENGTH] -= counts[WC_LENGTH];
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000210
Dan Fandrich5b0a7f12009-11-18 10:48:09 +0100211 OUTPUT:
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000212 /* coreutils wc tries hard to print pretty columns
Denys Vlasenko09e7daf2010-10-04 17:04:20 +0200213 * (saves results for all files, finds max col len etc...)
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000214 * we won't try that hard, it will bloat us too much */
215 s = start_fmt;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000216 u = 0;
217 do {
218 if (print_type & (1 << u)) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000219 printf(s, pcounts[u]);
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000220 s = " %9"COUNT_FMT; /* Ok... restore the leading space. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 }
222 totals[u] += pcounts[u];
Denys Vlasenko09e7daf2010-10-04 17:04:20 +0200223 } while (++u < NUM_WCS);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000224 printf(fname_fmt, arg);
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000225 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000226
Manuel Novoa III cad53642003-03-19 09:13:01 +0000227 /* If more than one file was processed, we want the totals. To save some
228 * space, we set the pcounts ptr to the totals array. This has the side
229 * effect of trashing the totals array after outputting it, but that's
230 * irrelavent since we no longer need it. */
231 if (num_files > 1) {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200232 num_files = 0; /* Make sure we don't get here again. */
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000233 arg = "total";
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234 pcounts = totals;
Denis Vlasenko3ed001f2006-09-29 23:41:04 +0000235 --argv;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000236 goto OUTPUT;
Mark Whitley39505962000-07-20 00:08:10 +0000237 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000238
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000239 fflush_stdout_and_exit(status);
Erik Andersen31638212000-01-15 22:28:50 +0000240}