diff options
| author | user <user@clank> | 2026-07-16 07:41:04 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-07-16 07:41:04 +0200 |
| commit | c303bcde76d0d90a5c6e623466fd3c8d7ff3a072 (patch) | |
| tree | 2fbaac3afc7c5c1e3212746223753b379cd6a80b /c/head.c | |
| parent | tools: wc, head, and an argc/argv demo (args) + libc.c helpers (diff) | |
| download | gbos-c303bcde76d0d90a5c6e623466fd3c8d7ff3a072.tar.gz gbos-c303bcde76d0d90a5c6e623466fd3c8d7ff3a072.tar.xz gbos-c303bcde76d0d90a5c6e623466fd3c8d7ff3a072.zip | |
libc: option parsing (hasflag/optval); wc -l/-w/-c, head -n N
- hasflag(argv,argc,f): single-char flag present (-f or within -abc)
- optval(argv,argc,opt): value for -opt (-n 5 or -n5)
- wc supports -l/-w/-c (default all); head supports -n N (and bare N)
Diffstat (limited to '')
| -rw-r--r-- | c/head.c | 13 |
1 files changed, 7 insertions, 6 deletions
@@ -1,12 +1,13 @@ #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. */ +/* head [-n N] [N]: print the first N lines of stdin (default 10). */ void main(void) { - char *a = getargs(); - unsigned char n = 10; - unsigned char lines = 0; + char *argv[8]; + unsigned char argc = argv_parse(argv, 8); + char *nv = optval(argv, argc, 'n'); + unsigned char n = 10, lines = 0; char c; - if (*a) n = atou(a); + if (nv) n = atou(nv); + else if (argc > 0 && argv[0][0] >= '0' && argv[0][0] <= '9') n = atou(argv[0]); for (;;) { c = readc(); if (c == EOF) break; |
