aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDan Fandrich <dan@coneharvesters.com>2009-11-18 10:48:09 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2009-11-18 10:48:09 +0100
commit5b0a7f1a6e66af3f1ff4159d4eb96c30517782b8 (patch)
treea80cc18d00c88ad9c2211939e7fc1b2d9a13cfe6 /coreutils
parent995f15452a265190c222442758a21fed2233ba14 (diff)
downloadbusybox-w32-5b0a7f1a6e66af3f1ff4159d4eb96c30517782b8.tar.gz
busybox-w32-5b0a7f1a6e66af3f1ff4159d4eb96c30517782b8.tar.bz2
busybox-w32-5b0a7f1a6e66af3f1ff4159d4eb96c30517782b8.zip
wc: fix a hang gue to isprint(EOF)
The new isprint replacement macro returns TRUE for isprint(EOF), so the read loop never returns. Moved the check for EOF immediately after the read. Signed-off-by: Dan Fandrich <dan@coneharvesters.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/wc.c36
1 files changed, 20 insertions, 16 deletions
diff --git a/coreutils/wc.c b/coreutils/wc.c
index 3e32e3d6d..43e8ed492 100644
--- a/coreutils/wc.c
+++ b/coreutils/wc.c
@@ -68,19 +68,14 @@ enum {
68int wc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 68int wc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
69int wc_main(int argc UNUSED_PARAM, char **argv) 69int wc_main(int argc UNUSED_PARAM, char **argv)
70{ 70{
71 FILE *fp; 71 const char *arg;
72 const char *s, *arg;
73 const char *start_fmt = " %9"COUNT_FMT + 1; 72 const char *start_fmt = " %9"COUNT_FMT + 1;
74 const char *fname_fmt = " %s\n"; 73 const char *fname_fmt = " %s\n";
75 COUNT_T *pcounts; 74 COUNT_T *pcounts;
76 COUNT_T counts[4]; 75 COUNT_T counts[4];
77 COUNT_T totals[4]; 76 COUNT_T totals[4];
78 unsigned linepos; 77 int num_files;
79 unsigned u;
80 int num_files = 0;
81 int c;
82 smallint status = EXIT_SUCCESS; 78 smallint status = EXIT_SUCCESS;
83 smallint in_word;
84 unsigned print_type; 79 unsigned print_type;
85 80
86 print_type = getopt32(argv, "lwcL"); 81 print_type = getopt32(argv, "lwcL");
@@ -101,7 +96,14 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
101 96
102 pcounts = counts; 97 pcounts = counts;
103 98
99 num_files = 0;
104 while ((arg = *argv++) != 0) { 100 while ((arg = *argv++) != 0) {
101 FILE *fp;
102 const char *s;
103 unsigned u;
104 unsigned linepos;
105 smallint in_word;
106
105 ++num_files; 107 ++num_files;
106 fp = fopen_or_warn_stdin(arg); 108 fp = fopen_or_warn_stdin(arg);
107 if (!fp) { 109 if (!fp) {
@@ -114,10 +116,19 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
114 in_word = 0; 116 in_word = 0;
115 117
116 do { 118 do {
119 int c;
117 /* Our -w doesn't match GNU wc exactly... oh well */ 120 /* Our -w doesn't match GNU wc exactly... oh well */
118 121
119 ++counts[WC_CHARS]; 122 ++counts[WC_CHARS];
120 c = getc(fp); 123 c = getc(fp);
124 if (c == EOF) {
125 if (ferror(fp)) {
126 bb_simple_perror_msg(arg);
127 status = EXIT_FAILURE;
128 }
129 --counts[WC_CHARS];
130 goto DO_EOF; /* Treat an EOF as '\r'. */
131 }
121 if (isprint(c)) { 132 if (isprint(c)) {
122 ++linepos; 133 ++linepos;
123 if (!isspace(c)) { 134 if (!isspace(c)) {
@@ -134,7 +145,7 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
134 if (c == '\t') { 145 if (c == '\t') {
135 linepos = (linepos | 7) + 1; 146 linepos = (linepos | 7) + 1;
136 } else { /* '\n', '\r', '\f', or '\v' */ 147 } else { /* '\n', '\r', '\f', or '\v' */
137 DO_EOF: 148 DO_EOF:
138 if (linepos > counts[WC_LENGTH]) { 149 if (linepos > counts[WC_LENGTH]) {
139 counts[WC_LENGTH] = linepos; 150 counts[WC_LENGTH] = linepos;
140 } 151 }
@@ -145,13 +156,6 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
145 linepos = 0; 156 linepos = 0;
146 } 157 }
147 } 158 }
148 } else if (c == EOF) {
149 if (ferror(fp)) {
150 bb_simple_perror_msg(arg);
151 status = EXIT_FAILURE;
152 }
153 --counts[WC_CHARS];
154 goto DO_EOF; /* Treat an EOF as '\r'. */
155 } else { 159 } else {
156 continue; 160 continue;
157 } 161 }
@@ -170,7 +174,7 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
170 174
171 fclose_if_not_stdin(fp); 175 fclose_if_not_stdin(fp);
172 176
173 OUTPUT: 177 OUTPUT:
174 /* coreutils wc tries hard to print pretty columns 178 /* coreutils wc tries hard to print pretty columns
175 * (saves results for all files, find max col len etc...) 179 * (saves results for all files, find max col len etc...)
176 * we won't try that hard, it will bloat us too much */ 180 * we won't try that hard, it will bloat us too much */