From dafc84daccf46427778fb9f47b0f53536f8e5015 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 25 Nov 2019 14:06:27 -0500 Subject: consider start of input as space to eliminate off-by-one word counts when the first character is a space --- wc.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/wc.c b/wc.c index de48ccf..18cb161 100644 --- a/wc.c +++ b/wc.c @@ -39,6 +39,9 @@ static uintmax_t total_c = 0; static wint_t wc_eof = EOF; static wint_t wc_newline = '\n'; +static wint_t (*wc_get)(FILE *); +static int (*wc_isspace)(wint_t); + static void flagprint(uintmax_t n, uintmax_t w, uintmax_t c, char *f, int flags) { if (flags == 0) { @@ -74,15 +77,12 @@ static int wc_isspace_(wint_t c) return isspace((int)c); } -static wint_t (*wc_get)(FILE *) = wc_get_; -static int (*wc_isspace)(wint_t) = wc_isspace_; - static int wc(char *path, int flags) { uintmax_t newlines = 0; uintmax_t words = 0; uintmax_t charbytes = 0; - int wasword = 0; + int wasspace = 1; FILE *f = stdin; @@ -100,10 +100,10 @@ static int wc(char *path, int flags) if (wc_isspace(c)) { newlines += (c == wc_newline); - words += !wasword; - wasword = 1; + words += !wasspace; + wasspace = 1; } else { - wasword = 0; + wasspace = 0; } } @@ -162,6 +162,9 @@ int main(int argc, char *argv[]) wc_get = fgetwc; wc_newline = L'\n'; wc_isspace = iswspace; + } else { + wc_get = wc_get_; + wc_isspace = wc_isspace_; } do { -- cgit v1.2.1