123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- #include "libbb.h"
- #include "unicode.h"
- #if !ENABLE_LOCALE_SUPPORT
- # undef isprint
- # undef isspace
- # define isprint(c) ((unsigned)((c) - 0x20) <= (0x7e - 0x20))
- # define isspace(c) ((c) == ' ')
- #endif
- #if ENABLE_FEATURE_WC_LARGE
- # define COUNT_T unsigned long long
- # define COUNT_FMT "llu"
- #else
- # define COUNT_T unsigned
- # define COUNT_FMT "u"
- #endif
- enum {
- WC_LINES = 0,
- WC_WORDS = 1,
- WC_UNICHARS = 2,
- WC_BYTES = 3,
- WC_LENGTH = 4,
- NUM_WCS = 5,
- };
- int wc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int wc_main(int argc UNUSED_PARAM, char **argv)
- {
- const char *arg;
- const char *start_fmt = " %9"COUNT_FMT + 1;
- const char *fname_fmt = " %s\n";
- COUNT_T *pcounts;
- COUNT_T counts[NUM_WCS];
- COUNT_T totals[NUM_WCS];
- int num_files;
- smallint status = EXIT_SUCCESS;
- unsigned print_type;
- init_unicode();
- print_type = getopt32(argv, "lwmcL");
- if (print_type == 0) {
- print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_BYTES);
- }
- argv += optind;
- if (!argv[0]) {
- *--argv = (char *) bb_msg_standard_input;
- fname_fmt = "\n";
- }
- if (!argv[1]) {
- if (!((print_type-1) & print_type))
- start_fmt = "%"COUNT_FMT;
- }
- memset(totals, 0, sizeof(totals));
- pcounts = counts;
- num_files = 0;
- while ((arg = *argv++) != NULL) {
- FILE *fp;
- const char *s;
- unsigned u;
- unsigned linepos;
- smallint in_word;
- ++num_files;
- fp = fopen_or_warn_stdin(arg);
- if (!fp) {
- status = EXIT_FAILURE;
- continue;
- }
- memset(counts, 0, sizeof(counts));
- linepos = 0;
- in_word = 0;
- while (1) {
- int c;
-
- c = getc(fp);
- if (c == EOF) {
- if (ferror(fp)) {
- bb_simple_perror_msg(arg);
- status = EXIT_FAILURE;
- }
- goto DO_EOF;
- }
-
- ++counts[WC_BYTES];
- if (unicode_status != UNICODE_ON
- || (c & 0xc0) != 0x80
- ) {
- ++counts[WC_UNICHARS];
- }
- if (isprint_asciionly(c)) {
- ++linepos;
- if (!isspace(c)) {
- in_word = 1;
- continue;
- }
- } else if ((unsigned)(c - 9) <= 4) {
-
- if (c == '\t') {
- linepos = (linepos | 7) + 1;
- } else {
- DO_EOF:
- if (linepos > counts[WC_LENGTH]) {
- counts[WC_LENGTH] = linepos;
- }
- if (c == '\n') {
- ++counts[WC_LINES];
- }
- if (c != '\v') {
- linepos = 0;
- }
- }
- } else {
- continue;
- }
- counts[WC_WORDS] += in_word;
- in_word = 0;
- if (c == EOF) {
- break;
- }
- }
- fclose_if_not_stdin(fp);
- if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
- totals[WC_LENGTH] = counts[WC_LENGTH];
- }
- totals[WC_LENGTH] -= counts[WC_LENGTH];
- OUTPUT:
-
- s = start_fmt;
- u = 0;
- do {
- if (print_type & (1 << u)) {
- printf(s, pcounts[u]);
- s = " %9"COUNT_FMT;
- }
- totals[u] += pcounts[u];
- } while (++u < NUM_WCS);
- printf(fname_fmt, arg);
- }
-
- if (num_files > 1) {
- num_files = 0;
- arg = "total";
- pcounts = totals;
- --argv;
- goto OUTPUT;
- }
- fflush_stdout_and_exit(status);
- }
|