From c303bcde76d0d90a5c6e623466fd3c8d7ff3a072 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 16 Jul 2026 07:41:04 +0200 Subject: 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) --- c/head.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'c/head.c') diff --git a/c/head.c b/c/head.c index 9363b1c..952d4c5 100644 --- a/c/head.c +++ b/c/head.c @@ -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; -- cgit v1.3.1-sl0p