Skip to content

Commit

Permalink
make wc -c faster
Browse files Browse the repository at this point in the history
  • Loading branch information
izabera authored and landley committed Feb 11, 2016
1 parent e8427bf commit 3684510
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion toys/posix/wc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* See http:https://opengroup.org/onlinepubs/9699919799/utilities/wc.html
USE_WC(NEWTOY(wc, USE_TOYBOX_I18N("m")"cwl", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE))
USE_WC(NEWTOY(wc, USE_TOYBOX_I18N("m")"cwl[!cm]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE))
config WC
bool "wc"
Expand Down Expand Up @@ -50,10 +50,24 @@ static void do_wc(int fd, char *name)
int i, len, clen=1, space;
unsigned long word=0, lengths[]={0,0,0};

if (toys.optflags == FLAG_c) {
struct stat st;

fstat(fd, &st);
if (S_ISREG(st.st_mode)) {
lengths[2] = st.st_size;
goto show;
}
}

for (;;) {
len = read(fd, toybuf, sizeof(toybuf));
if (len<0) perror_msg_raw(name);
if (len<1) break;
if (toys.optflags == FLAG_c) {
lengths[2] += len;
continue;
}
for (i=0; i<len; i+=clen) {
wchar_t wchar;

Expand All @@ -78,6 +92,7 @@ static void do_wc(int fd, char *name)
}
}

show:
show_lengths(lengths, name);
}

Expand Down

0 comments on commit 3684510

Please sign in to comment.