diff options
author | Jim Meyering <jim@meyering.net> | 1995-02-10 05:34:27 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1995-02-10 05:34:27 +0000 |
commit | 15d9f70d1a5af7d27ac66de79be0514e8d09e5f6 (patch) | |
tree | 3b1ab71f649b1348326e85d32e1ac96465369099 | |
parent | d131ddcf1fd7b00accdf61a35e8dcd212cb19387 (diff) | |
download | coreutils-15d9f70d1a5af7d27ac66de79be0514e8d09e5f6.tar.xz |
(wc): Handle separately the cases in which words need
not be counted. Suggested by Karl Heuer.
(wc): Use memchr.c instead.
-rw-r--r-- | src/wc.c | 13 |
1 files changed, 8 insertions, 5 deletions
@@ -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) { |