1 /* wc.c - Word count
2  *
3  * Copyright 2011 Rob Landley <rob@landley.net>
4  *
5  * See http://opengroup.org/onlinepubs/9699919799/utilities/wc.html
6 
7 USE_WC(NEWTOY(wc, USE_TOYBOX_I18N("m")"cwl[!cm]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE))
8 
9 config WC
10   bool "wc"
11   default y
12   help
13     usage: wc -lwcm [FILE...]
14 
15     Count lines, words, and characters in input.
16 
17     -l	show lines
18     -w	show words
19     -c	show bytes
20     -m	show characters
21 
22     By default outputs lines, words, bytes, and filename for each
23     argument (or from stdin if none). Displays only either bytes
24     or characters.
25 */
26 
27 #define FOR_wc
28 #include "toys.h"
29 
GLOBALS(unsigned long totals[3];)30 GLOBALS(
31   unsigned long totals[3];
32 )
33 
34 static void show_lengths(unsigned long *lengths, char *name)
35 {
36   int i, nospace = 1;
37   for (i=0; i<3; i++) {
38     if (!toys.optflags || (toys.optflags&(1<<i))) {
39       xprintf(" %ld"+nospace, lengths[i]);
40       nospace = 0;
41     }
42     TT.totals[i] += lengths[i];
43   }
44   if (*toys.optargs) xprintf(" %s", name);
45   xputc('\n');
46 }
47 
do_wc(int fd,char * name)48 static void do_wc(int fd, char *name)
49 {
50   int i, len, clen=1, space;
51   unsigned long word=0, lengths[]={0,0,0};
52 
53   if (toys.optflags == FLAG_c) {
54     struct stat st;
55 
56     // On Linux, files in /proc often report their size as 0.
57     if (!fstat(fd, &st) && S_ISREG(st.st_mode) && st.st_size > 0) {
58       lengths[2] = st.st_size;
59       goto show;
60     }
61   }
62 
63   for (;;) {
64     len = read(fd, toybuf, sizeof(toybuf));
65     if (len<0) perror_msg_raw(name);
66     if (len<1) break;
67     if (toys.optflags == FLAG_c) {
68       lengths[2] += len;
69       continue;
70     }
71     for (i=0; i<len; i+=clen) {
72       wchar_t wchar;
73 
74       if (CFG_TOYBOX_I18N && (toys.optflags&FLAG_m)) {
75         clen = mbrtowc(&wchar, toybuf+i, len-i, 0);
76         if (clen == -1) {
77           clen = 1;
78           continue;
79         }
80         if (clen == -2) break;
81         if (clen == 0) clen=1;
82         space = iswspace(wchar);
83       } else space = isspace(toybuf[i]);
84 
85       if (toybuf[i]==10) lengths[0]++;
86       if (space) word=0;
87       else {
88         if (!word) lengths[1]++;
89         word=1;
90       }
91       lengths[2]++;
92     }
93   }
94 
95 show:
96   show_lengths(lengths, name);
97 }
98 
wc_main(void)99 void wc_main(void)
100 {
101   toys.optflags |= (toys.optflags&8)>>1;
102   loopfiles(toys.optargs, do_wc);
103   if (toys.optc>1) show_lengths(TT.totals, "total");
104 }
105