aboutsummaryrefslogtreecommitdiffstats
path: root/c/wc.c
diff options
context:
space:
mode:
Diffstat (limited to 'c/wc.c')
-rw-r--r--c/wc.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/c/wc.c b/c/wc.c
new file mode 100644
index 0000000..a957542
--- /dev/null
+++ b/c/wc.c
@@ -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();
+}