From 3684510034450f5f50d1ad9b5acca327a5c484dd Mon Sep 17 00:00:00 2001 From: izabera Date: Sat, 6 Feb 2016 15:41:59 +0100 Subject: make wc -c faster --- toys/posix/wc.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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