From 15d9f70d1a5af7d27ac66de79be0514e8d09e5f6 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Fri, 10 Feb 1995 05:34:27 +0000 Subject: (wc): Handle separately the cases in which words need not be counted. Suggested by Karl Heuer. (wc): Use memchr.c instead. --- src/wc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/wc.c') diff --git a/src/wc.c b/src/wc.c index 7dca0f37d..34b3e0872 100644 --- a/src/wc.c +++ b/src/wc.c @@ -197,7 +197,7 @@ wc (fd, file) int fd; char *file; { - char buf[BUFFER_SIZE]; + char buf[BUFFER_SIZE + 1]; register int bytes_read; register int in_word = 0; register unsigned long lines, words, chars; @@ -210,7 +210,7 @@ wc (fd, file) bytes at a time until EOF. NOTE: using fstat and stats.st_size (and omitting the lseek calls) - over counts when the file is not positioned at the beginning. + overcounts when the file is not positioned at the beginning. For example the command `(dd skip=9999; wc -c) < /etc/group' should make wc report `0' bytes. */ @@ -246,13 +246,16 @@ wc (fd, file) { register char *p = buf; + buf[bytes_read] = '\n'; /* End of buffer sentinel. */ chars += bytes_read; + --p; do { - if (*p++ == '\n') - lines++; + p = memchr (p + 1, '\n', bytes_read); + ++lines; } - while (--bytes_read); + while (p != buf + bytes_read); + --lines; } if (bytes_read < 0) { -- cgit v1.2.3-54-g00ecf