aboutsummaryrefslogtreecommitdiffstats
path: root/c/head.c
diff options
context:
space:
mode:
authoruser <user@clank>2026-07-16 07:41:04 +0200
committeruser <user@clank>2026-07-16 07:41:04 +0200
commitc303bcde76d0d90a5c6e623466fd3c8d7ff3a072 (patch)
tree2fbaac3afc7c5c1e3212746223753b379cd6a80b /c/head.c
parenttools: wc, head, and an argc/argv demo (args) + libc.c helpers (diff)
downloadgbos-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.c13
1 files changed, 7 insertions, 6 deletions
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;