]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(wc): Handle separately the cases in which words need
authorJim Meyering <jim@meyering.net>
Fri, 10 Feb 1995 05:34:27 +0000 (05:34 +0000)
committerJim Meyering <jim@meyering.net>
Fri, 10 Feb 1995 05:34:27 +0000 (05:34 +0000)
not be counted.  Suggested by Karl Heuer.
(wc): Use memchr.c instead.

src/wc.c

index 7dca0f37d93f19447c2d58459b2c91b804b5564c..34b3e0872e722e59379f5e1b3e86cbfa641f961a 100644 (file)
--- 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)
        {