-
- if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
- wc_file(stdin, "");
- return EXIT_SUCCESS;
- } else {
- while (optind < argc) {
- file = wfopen(argv[optind], "r");
- if (file != NULL)
- wc_file(file, argv[optind]);
- else
- status = EXIT_FAILURE;
- num_files_counted++;
- optind++;
+
+ argv += optind;
+ if (!*argv) {
+ *--argv = (char *) bb_msg_standard_input;
+ }
+
+ memset(totals, 0, sizeof(totals));
+
+ pcounts = counts;
+
+ do {
+ ++num_files;
+ if (!(fp = bb_wfopen_input(*argv))) {
+ status = EXIT_FAILURE;
+ continue;
+ }
+
+ memset(counts, 0, sizeof(counts));
+ linepos = 0;
+ in_word = 0;
+
+ do {
+ ++counts[WC_CHARS];
+ c = getc(fp);
+ if (isprint(c)) {
+ ++linepos;
+ if (!isspace_given_isprint(c)) {
+ in_word = 1;
+ continue;
+ }
+ } else if (((unsigned int)(c - 9)) <= 4) {
+ /* \t 9
+ * \n 10
+ * \v 11
+ * \f 12
+ * \r 13
+ */
+ if (c == '\t') {
+ linepos = (linepos | 7) + 1;
+ } else { /* '\n', '\r', '\f', or '\v' */
+ DO_EOF:
+ if (linepos > counts[WC_LENGTH]) {
+ counts[WC_LENGTH] = linepos;
+ }
+ if (c == '\n') {
+ ++counts[WC_LINES];
+ }
+ if (c != '\v') {
+ linepos = 0;
+ }
+ }
+ } else if (c == EOF) {
+ if (ferror(fp)) {
+ bb_perror_msg("%s", *argv);
+ status = EXIT_FAILURE;
+ }
+ --counts[WC_CHARS];
+ goto DO_EOF; /* Treat an EOF as '\r'. */
+ } else {
+ continue;
+ }
+
+ counts[WC_WORDS] += in_word;
+ in_word = 0;
+ if (c == EOF) {
+ break;
+ }
+ } while (1);
+
+ if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
+ totals[WC_LENGTH] = counts[WC_LENGTH];
+ }
+ totals[WC_LENGTH] -= counts[WC_LENGTH];
+
+ bb_fclose_nonstdin(fp);
+
+ OUTPUT:
+ s = fmt_str + 1; /* Skip the leading space on 1st pass. */
+ u = 0;
+ do {
+ if (print_type & (1 << u)) {
+ bb_printf(s, pcounts[u]);
+ s = fmt_str; /* Ok... restore the leading space. */
+ }
+ totals[u] += pcounts[u];
+ } while (++u < 4);
+
+ s += 8; /* Set the format to the empty string. */
+
+ if (*argv != bb_msg_standard_input) {
+ s -= 3; /* We have a name, so do %s conversion. */