From 8682553498d32d3c18dea41c598ab40de18404a3 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 16 Jul 2026 07:30:34 +0200 Subject: tools: wc, head, and an argc/argv demo (args) + libc.c helpers - c/libc.c: shared C helpers linked into every program - putu (print decimal), atou (parse decimal), argv_parse (tokenize getargs() into argc/argv). - wc: count lines/words/chars of stdin. head [n]: first n lines (drains rest to EOF). args: argc/argv demo. - pid now uses libc putu; build.sh links libc.c; Makefile CBLOBS + libc dep. - README: document the new tools + argv_parse. - verified: 'args one two three' -> argc=3/argv[..]; wc '2 3 16'; head 2. --- c/head.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 c/head.c (limited to 'c/head.c') diff --git a/c/head.c b/c/head.c new file mode 100644 index 0000000..9363b1c --- /dev/null +++ b/c/head.c @@ -0,0 +1,16 @@ +#include "gbos.h" +/* head [n]: print the first n lines of stdin (default 10). Drains the rest to + EOF so the shell sees a clean end of input. */ +void main(void) { + char *a = getargs(); + unsigned char n = 10; + unsigned char lines = 0; + char c; + if (*a) n = atou(a); + for (;;) { + c = readc(); + if (c == EOF) break; + if (lines < n) putc(c); + if (c == '\n') lines++; + } +} -- cgit v1.3.1-sl0p