diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-11-25 14:06:27 -0500 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-11-25 14:06:27 -0500 |
commit | dafc84daccf46427778fb9f47b0f53536f8e5015 (patch) | |
tree | bc2d22cfbc0a31a9db88311b293409852218d783 | |
parent | 358b9cc367a3a39d00da70fab36e06613dc7f1b1 (diff) |
consider start of input as space to eliminate off-by-one word counts when the first character is a space
-rw-r--r-- | wc.c | 17 |
1 files changed, 10 insertions, 7 deletions
@@ -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 { |