wc: preparatory patch, no logic changes

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
This commit is contained in:
Denys Vlasenko 2010-10-04 17:04:20 +02:00
parent 9a1b260547
commit 09e7dafbfe

View File

@ -59,10 +59,11 @@
#endif #endif
enum { enum {
WC_LINES = 0, WC_LINES = 0,
WC_WORDS = 1, WC_WORDS = 1,
WC_CHARS = 2, WC_CHARS = 2,
WC_LENGTH = 3 WC_LENGTH = 3,
NUM_WCS = 4,
}; };
int wc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int wc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@ -72,8 +73,8 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
const char *start_fmt = " %9"COUNT_FMT + 1; const char *start_fmt = " %9"COUNT_FMT + 1;
const char *fname_fmt = " %s\n"; const char *fname_fmt = " %s\n";
COUNT_T *pcounts; COUNT_T *pcounts;
COUNT_T counts[4]; COUNT_T counts[NUM_WCS];
COUNT_T totals[4]; COUNT_T totals[NUM_WCS];
int num_files; int num_files;
smallint status = EXIT_SUCCESS; smallint status = EXIT_SUCCESS;
unsigned print_type; unsigned print_type;
@ -99,7 +100,7 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
pcounts = counts; pcounts = counts;
num_files = 0; num_files = 0;
while ((arg = *argv++) != 0) { while ((arg = *argv++) != NULL) {
FILE *fp; FILE *fp;
const char *s; const char *s;
unsigned u; unsigned u;
@ -117,20 +118,20 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
linepos = 0; linepos = 0;
in_word = 0; in_word = 0;
do { while (1) {
int c; int c;
/* Our -w doesn't match GNU wc exactly... oh well */ /* Our -w doesn't match GNU wc exactly... oh well */
++counts[WC_CHARS];
c = getc(fp); c = getc(fp);
if (c == EOF) { if (c == EOF) {
if (ferror(fp)) { if (ferror(fp)) {
bb_simple_perror_msg(arg); bb_simple_perror_msg(arg);
status = EXIT_FAILURE; status = EXIT_FAILURE;
} }
--counts[WC_CHARS];
goto DO_EOF; /* Treat an EOF as '\r'. */ goto DO_EOF; /* Treat an EOF as '\r'. */
} }
++counts[WC_CHARS];
if (isprint_asciionly(c)) { if (isprint_asciionly(c)) {
++linepos; ++linepos;
if (!isspace(c)) { if (!isspace(c)) {
@ -167,18 +168,18 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
if (c == EOF) { if (c == EOF) {
break; break;
} }
} while (1); }
fclose_if_not_stdin(fp);
if (totals[WC_LENGTH] < counts[WC_LENGTH]) { if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
totals[WC_LENGTH] = counts[WC_LENGTH]; totals[WC_LENGTH] = counts[WC_LENGTH];
} }
totals[WC_LENGTH] -= counts[WC_LENGTH]; totals[WC_LENGTH] -= counts[WC_LENGTH];
fclose_if_not_stdin(fp);
OUTPUT: OUTPUT:
/* coreutils wc tries hard to print pretty columns /* coreutils wc tries hard to print pretty columns
* (saves results for all files, find max col len etc...) * (saves results for all files, finds max col len etc...)
* we won't try that hard, it will bloat us too much */ * we won't try that hard, it will bloat us too much */
s = start_fmt; s = start_fmt;
u = 0; u = 0;
@ -188,7 +189,7 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
s = " %9"COUNT_FMT; /* Ok... restore the leading space. */ s = " %9"COUNT_FMT; /* Ok... restore the leading space. */
} }
totals[u] += pcounts[u]; totals[u] += pcounts[u];
} while (++u < 4); } while (++u < NUM_WCS);
printf(fname_fmt, arg); printf(fname_fmt, arg);
} }