summaryrefslogtreecommitdiff
path: root/src/wc.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1995-02-10 05:34:27 +0000
committerJim Meyering <jim@meyering.net>1995-02-10 05:34:27 +0000
commit15d9f70d1a5af7d27ac66de79be0514e8d09e5f6 (patch)
tree3b1ab71f649b1348326e85d32e1ac96465369099 /src/wc.c
parentd131ddcf1fd7b00accdf61a35e8dcd212cb19387 (diff)
downloadcoreutils-15d9f70d1a5af7d27ac66de79be0514e8d09e5f6.tar.xz
(wc): Handle separately the cases in which words need
not be counted. Suggested by Karl Heuer. (wc): Use memchr.c instead.
Diffstat (limited to 'src/wc.c')
-rw-r--r--src/wc.c13
1 files changed, 8 insertions, 5 deletions
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)
{