summaryrefslogtreecommitdiff
path: root/src/wc.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1995-02-09 17:15:17 +0000
committerJim Meyering <jim@meyering.net>1995-02-09 17:15:17 +0000
commitd131ddcf1fd7b00accdf61a35e8dcd212cb19387 (patch)
tree7c1d6c91c081b6beb2e042802aabcfa912c62a81 /src/wc.c
parent05d1bec3e5a34449ffde7f71648084069b1319f5 (diff)
downloadcoreutils-d131ddcf1fd7b00accdf61a35e8dcd212cb19387.tar.xz
(wc): Add a separate loop for counting only lines or lines and bytes.
From Karl Heuer.
Diffstat (limited to 'src/wc.c')
-rw-r--r--src/wc.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/wc.c b/src/wc.c
index 34706ae67..7dca0f37d 100644
--- a/src/wc.c
+++ b/src/wc.c
@@ -238,6 +238,28 @@ wc (fd, file)
}
}
}
+ else if (!print_words)
+ {
+ /* Use a separate loop when counting only lines or lines and bytes --
+ but not words. */
+ while ((bytes_read = safe_read (fd, buf, BUFFER_SIZE)) > 0)
+ {
+ register char *p = buf;
+
+ chars += bytes_read;
+ do
+ {
+ if (*p++ == '\n')
+ lines++;
+ }
+ while (--bytes_read);
+ }
+ if (bytes_read < 0)
+ {
+ error (0, errno, "%s", file);
+ exit_status = 1;
+ }
+ }
else
{
while ((bytes_read = safe_read (fd, buf, BUFFER_SIZE)) > 0)