diff options
Diffstat (limited to 'c/wc.c')
| -rw-r--r-- | c/wc.c | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -0,0 +1,20 @@ +#include "gbos.h" +/* wc: count lines, words, chars of stdin (like `wc`). */ +void main(void) { + unsigned int lines = 0, words = 0, chars = 0; + unsigned char inword = 0; + char c; + for (;;) { + c = readc(); + if (c == EOF) break; + chars++; + if (c == '\n') lines++; + if (c == ' ' || c == '\n' || c == '\r' || c == '\t') { + inword = 0; + } else if (!inword) { + inword = 1; + words++; + } + } + putu(lines); putc(' '); putu(words); putc(' '); putu(chars); nl(); +} |
