aboutsummaryrefslogtreecommitdiff
path: root/coreutils/wc.c
diff options
context:
space:
mode:
authorManuel Novoa III <mjn3@codepoet.org>2003-03-19 09:13:01 +0000
committerManuel Novoa III <mjn3@codepoet.org>2003-03-19 09:13:01 +0000
commitcad5364599eb5062d59e0c397ed638ddd61a8d5d (patch)
treea318d0f03aa076c74b576ea45dc543a5669e8e91 /coreutils/wc.c
parente01f9662a5bd5d91be4f6b3941b57fff73cd5af1 (diff)
downloadbusybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.gz
busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.bz2
busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.zip
Major coreutils update.
Diffstat (limited to 'coreutils/wc.c')
-rw-r--r--coreutils/wc.c325
1 files changed, 187 insertions, 138 deletions
diff --git a/coreutils/wc.c b/coreutils/wc.c
index 8e3b5bbf4..779901524 100644
--- a/coreutils/wc.c
+++ b/coreutils/wc.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini wc implementation for busybox 3 * wc implementation for busybox
4 * 4 *
5 * Copyright (C) 2000 Edward Betts <edward@debian.org> 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 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 8 * it under the terms of the GNU General Public License as published by
@@ -20,159 +20,208 @@
20 * 20 *
21 */ 21 */
22 22
23/* 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.
29 * Problems in the previous busybox implementation (besides bloat) included:
30 * 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
23#include <stdio.h> 57#include <stdio.h>
24#include <getopt.h>
25#include <string.h>
26#include <stdlib.h> 58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
27#include "busybox.h" 61#include "busybox.h"
28 62
29enum print_e { 63#ifdef CONFIG_LOCALE_SUPPORT
30 print_lines = 1, 64#include <locale.h>
31 print_words = 2, 65#include <ctype.h>
32 print_chars = 4, 66#define isspace_given_isprint(c) isspace(c)
33 print_length = 8 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
34}; 80};
35 81
36static unsigned int total_lines = 0; 82/* Note: If this changes, remember to change the initialization of
37static unsigned int total_words = 0; 83 * 'name' in wc_main. It needs to point to the terminating nul. */
38static unsigned int total_chars = 0; 84static const char wc_opts[] = "lwcL"; /* READ THE WARNING ABOVE! */
39static unsigned int max_length = 0;
40static char print_type = 0;
41 85
42static void print_counts(const unsigned int lines, const unsigned int words, 86enum {
43 const unsigned int chars, const unsigned int length, const char *name) 87 OP_INC_LINE = 1, /* OP_INC_LINE must be 1. */
44{ 88 OP_SPACE = 2,
45 int output = 0; 89 OP_NEWLINE = 4,
90 OP_TAB = 8,
91 OP_NUL = 16,
92};
46 93
47 if (print_type & print_lines) { 94/* Note: If fmt_str changes, the offsets to 's' in the OUTPUT section
48 printf("%7d", lines); 95 * will need to be updated. */
49 output++; 96static const char fmt_str[] = " %7u\0 %s\n";
50 } 97static const char total_str[] = "total";
51 if (print_type & print_words) {
52 if (output++)
53 putchar(' ');
54 printf("%7d", words);
55 }
56 if (print_type & print_chars) {
57 if (output++)
58 putchar(' ');
59 printf("%7d", chars);
60 }
61 if (print_type & print_length) {
62 if (output++)
63 putchar(' ');
64 printf("%7d", length);
65 }
66 if (*name) {
67 printf(" %s", name);
68 }
69 putchar('\n');
70}
71 98
72static void wc_file(FILE * file, const char *name) 99int wc_main(int argc, char **argv)
73{ 100{
74 unsigned int lines = 0; 101 FILE *fp;
75 unsigned int words = 0; 102 const char *s;
76 unsigned int chars = 0; 103 unsigned int *pcounts;
77 unsigned int length = 0; 104 unsigned int counts[4];
78 unsigned int linepos = 0; 105 unsigned int totals[4];
79 char in_word = 0; 106 unsigned int linepos;
107 unsigned int u;
108 int num_files = 0;
80 int c; 109 int c;
81 110 char status = EXIT_SUCCESS;
82 while ((c = getc(file)) != EOF) { 111 char in_word;
83 chars++; 112 char print_type;
84 switch (c) { 113
85 case '\n': 114 print_type = bb_getopt_ulflags(argc, argv, wc_opts);
86 lines++; 115
87 case '\r': 116 if (print_type == 0) {
88 case '\f': 117 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
89 if (linepos > length)
90 length = linepos;
91 linepos = 0;
92 goto word_separator;
93 case '\t':
94 linepos += 8 - (linepos % 8);
95 goto word_separator;
96 case ' ':
97 linepos++;
98 case '\v':
99word_separator:
100 if (in_word) {
101 in_word = 0;
102 words++;
103 }
104 break;
105 default:
106 linepos++;
107 in_word = 1;
108 break;
109 }
110 }
111 if (linepos > length) {
112 length = linepos;
113 }
114 if (in_word) {
115 words++;
116 } 118 }
117 print_counts(lines, words, chars, length, name); 119
118 total_lines += lines; 120 argv += optind;
119 total_words += words; 121 if (!*argv) {
120 total_chars += chars; 122 *--argv = (char *) bb_msg_standard_input;
121 if (length > max_length) {
122 max_length = length;
123 } 123 }
124} 124
125 125 memset(totals, 0, sizeof(totals));
126int wc_main(int argc, char **argv) 126
127{ 127 pcounts = counts;
128 int opt; 128
129 129 do {
130 while ((opt = getopt(argc, argv, "clLw")) > 0) { 130 ++num_files;
131 switch (opt) { 131 if (!(fp = bb_wfopen_input(*argv))) {
132 case 'c': 132 status = EXIT_FAILURE;
133 print_type |= print_chars; 133 continue;
134 break;
135 case 'l':
136 print_type |= print_lines;
137 break;
138 case 'L':
139 print_type |= print_length;
140 break;
141 case 'w':
142 print_type |= print_words;
143 break;
144 default:
145 show_usage();
146 } 134 }
147 } 135
148 136 memset(counts, 0, sizeof(counts));
149 if (print_type == 0) { 137 linepos = 0;
150 print_type = print_lines | print_words | print_chars; 138 in_word = 0;
151 } 139
152 140 do {
153 if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { 141 ++counts[WC_CHARS];
154 wc_file(stdin, ""); 142 c = getc(fp);
155 } else { 143 if (isprint(c)) {
156 unsigned short num_files_counted = 0; 144 ++linepos;
157 while (optind < argc) { 145 if (!isspace_given_isprint(c)) {
158 if (print_type == print_chars) { 146 in_word = 1;
159 struct stat statbuf; 147 continue;
160 stat(argv[optind], &statbuf); 148 }
161 print_counts(0, 0, statbuf.st_size, 0, argv[optind]); 149 } else if (((unsigned int)(c - 9)) <= 4) {
162 total_chars += statbuf.st_size; 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'. */
163 } else { 177 } else {
164 FILE *file; 178 continue;
165 file = xfopen(argv[optind], "r"); 179 }
166 wc_file(file, argv[optind]); 180
167 fclose(file); 181 counts[WC_WORDS] += in_word;
182 in_word = 0;
183 if (c == EOF) {
184 break;
168 } 185 }
169 optind++; 186 } while (1);
170 num_files_counted++; 187
188 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
189 totals[WC_LENGTH] = counts[WC_LENGTH];
171 } 190 }
172 if (num_files_counted > 1) { 191 totals[WC_LENGTH] -= counts[WC_LENGTH];
173 print_counts(total_lines, total_words, total_chars, max_length, "total"); 192
193 bb_fclose_nonstdin(fp);
194
195 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);
205
206 s += 8; /* Set the format to the empty string. */
207
208 if (*argv != bb_msg_standard_input) {
209 s -= 3; /* We have a name, so do %s conversion. */
174 } 210 }
211 bb_printf(s, *argv);
212
213 } while (*++argv);
214
215 /* 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;
175 } 224 }
176 225
177 return(EXIT_SUCCESS); 226 bb_fflush_stdout_and_exit(status);
178} 227}