diff options
| author | user <user@clank> | 2026-07-16 07:30:34 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-07-16 07:30:34 +0200 |
| commit | 8682553498d32d3c18dea41c598ab40de18404a3 (patch) | |
| tree | 74096aa1fcefce24f80583ee10f25e5831a3b589 /c/libc.c | |
| parent | libc + CLI tools: a small C userland (diff) | |
| download | gbos-8682553498d32d3c18dea41c598ab40de18404a3.tar.gz gbos-8682553498d32d3c18dea41c598ab40de18404a3.tar.xz gbos-8682553498d32d3c18dea41c598ab40de18404a3.zip | |
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.
Diffstat (limited to '')
| -rw-r--r-- | c/libc.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/c/libc.c b/c/libc.c new file mode 100644 index 0000000..3b9883b --- /dev/null +++ b/c/libc.c @@ -0,0 +1,34 @@ +/* gbos libc C helpers (compiled once, linked into every program). */ +#include "gbos.h" + +/* print an unsigned int in decimal */ +void putu(unsigned int n) { + char buf[5]; + unsigned char i = 0; + if (n == 0) { putc('0'); return; } + while (n) { buf[i++] = '0' + (n % 10); n = n / 10; } + while (i) putc(buf[--i]); +} + +/* parse a decimal number (stops at the first non-digit) */ +unsigned char atou(const char *s) { + unsigned char n = 0; + while (*s >= '0' && *s <= '9') { n = n * 10 + (*s - '0'); s++; } + return n; +} + +/* tokenize the argument string in place into argv[]; returns argc. + (replaces the spaces in the arg string with NULs) */ +unsigned char argv_parse(char **argv, unsigned char maxv) { + char *p = getargs(); + unsigned char argc = 0; + for (;;) { + while (*p == ' ') p++; + if (*p == 0) break; + if (argc < maxv) argv[argc] = p; + argc++; + while (*p != 0 && *p != ' ') p++; + if (*p == ' ') *p++ = 0; + } + return argc; +} |
