diff options
author | izabera <izaberina@gmail.com> | 2016-02-06 15:41:59 +0100 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2016-02-10 20:40:07 -0600 |
commit | 3684510034450f5f50d1ad9b5acca327a5c484dd (patch) | |
tree | e7ea250ce1cad4f46347180a14e0d38c5410b457 | |
parent | e8427bfd008be233aadea49e89075451c8a9ceee (diff) | |
download | toybox-3684510034450f5f50d1ad9b5acca327a5c484dd.tar.gz |
make wc -c faster
-rw-r--r-- | toys/posix/wc.c | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/toys/posix/wc.c b/toys/posix/wc.c index b7f09b4e..62d1f7a0 100644 --- a/toys/posix/wc.c +++ b/toys/posix/wc.c @@ -4,7 +4,7 @@ * * See http://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" @@ -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; @@ -78,6 +92,7 @@ static void do_wc(int fd, char *name) } } +show: show_lengths(lengths, name); } |