i was writing a script when i ran across this.

$ echo "hello world" | wc
       1       2      12
$ echo -n "hello world" | wc
       0       2      11

the following patch corrects this:

--- usr.bin/wc/wc.c.orig        Fri Oct  9 02:43:08 2015
+++ usr.bin/wc/wc.c     Tue Nov  3 22:52:46 2015
@@ -148,6 +148,8 @@ cnt(char *file)
                                warn("%s", file);
                                rval = 1;
                        }
+                       if (charct && *--C != '\n')
+                               ++linect;
                }
                /*
                 * If all we need is the number of characters and
@@ -212,6 +214,8 @@ cnt(char *file)
                        warn("%s", file);
                        rval = 1;
                }
+               if (charct && *--C != '\n')
+                       ++linect;
        }
 
        print_counts(linect, wordct, charct, file);


outside of the while(read()) loop, C should always point to the byte
after last, provided anything has been read (hence checking charct).
i used '*--C' instead of '*(C-1)' because it looks a bit better to me,
and C is no longer used. not sure if there are some style issues with
that.

i also rely on the fact that the buffer is not overwritten if read()
returns 0 or -1, something i am not sure of. (would complicate things
slightly otherwise).

Reply via email to