aboutsummaryrefslogtreecommitdiffstats
path: root/usr/uniq.c
diff options
context:
space:
mode:
authoruser <user@clank>2026-09-24 22:45:04 +0200
committeruser <user@clank>2026-09-24 22:45:04 +0200
commit4cd933009dd6ce87fe710acfca6b1f786d51040c (patch)
tree67e50c2ab347ddc2ac3e5b6a5cf5a4cfc44cc8d0 /usr/uniq.c
parentfs: mount the builtins at /bin (diff)
downloadgbos-4cd933009dd6ce87fe710acfca6b1f786d51040c.tar.gz
gbos-4cd933009dd6ce87fe710acfca6b1f786d51040c.tar.xz
gbos-4cd933009dd6ce87fe710acfca6b1f786d51040c.zip
usr: touch, tr, rev, cut, uniq
/# ls -l /bin > b /# cut -d \s -f 1 b | uniq -c 52 - - touch FILE...: gbfs has no timestamps (an MBC5 cart has no RTC), so touch does the half that means something here - make an empty file. O_APPEND is exactly right: it creates, and it does not truncate what is already there. - tr [-d] SET1 [SET2]: ranges (a-z), -d to delete, and a short SET2 padded with its last character, like tr(1). ASCII only - a 128-entry map. - rev, uniq [-c]: one line each, from a file or stdin like the other filters. Two things the tests caught. A bare "-" is a SET, not a flag, so `tr . -` has to skip flag-matching only when a letter follows the dash. And uniq compared an empty "line" against the last run at EOF and flushed a phantom blank one - a stray "1" in uniq -c output. tr and cut also take \s \t \n \r escapes, because the shell has no quoting: `-d ' '` arrives as three tokens, so a space or tab delimiter was simply untypeable. Shell quoting would be the better fix; this makes the tools usable today without touching the parser. Banks 49-53; 10 program slots left in the 1 MB ROM.
Diffstat (limited to '')
-rw-r--r--usr/uniq.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/usr/uniq.c b/usr/uniq.c
new file mode 100644
index 0000000..2c6ebdc
--- /dev/null
+++ b/usr/uniq.c
@@ -0,0 +1,84 @@
+#include "gbos.h"
+/* uniq [-c] [file]: collapse runs of identical ADJACENT lines (like uniq(1) -
+ * it does not sort). -c prefixes each line with how many times it repeated. */
+
+#define MAXLINE 80
+
+static unsigned char lines_eq(const char *a, unsigned char la, const char *b, unsigned char lb) {
+ unsigned char i;
+ if (la != lb) return 0;
+ for (i = 0; i < la; i++)
+ if (a[i] != b[i]) return 0;
+ return 1;
+}
+
+void main(void) {
+ char *argv[4];
+ char cur[MAXLINE], prev[MAXLINE];
+ unsigned char argc = argv_parse(argv, 4);
+ unsigned char cnt = hasflag(argv, argc, 'c');
+ unsigned char clen = 0, plen = 0, have = 0, fd = NOFD, i;
+ unsigned int reps = 0;
+ char *fname = 0;
+ int ch;
+
+ for (i = 0; i < argc; i++)
+ if (argv[i][0] != '-') {
+ fname = argv[i];
+ break;
+ }
+ if (fname) {
+ fd = open(fname, O_READ);
+ if (fd == EISDIR) {
+ puts("uniq: is a directory");
+ nl();
+ sexit(2);
+ }
+ if (fd == NOFD) {
+ puts("uniq: no such file");
+ nl();
+ sexit(2);
+ }
+ }
+ for (;;) {
+ if (fd == NOFD) {
+ char c = readc();
+ ch = (c == EOF) ? -1 : c;
+ } else ch = fgetc(fd);
+ if (ch == '\r') continue;
+ if (ch >= 0 && ch != '\n') {
+ if (clen < MAXLINE) cur[clen++] = (char)ch;
+ continue;
+ }
+ /* EOF with nothing buffered: stop. Falling through would compare an
+ empty "line" against the last run and flush a phantom blank one -
+ which showed up as a stray "1" in uniq -c. */
+ if (ch < 0 && clen == 0) break;
+ if (have && lines_eq(cur, clen, prev, plen)) reps++;
+ else {
+ if (have) { /* flush the run that just ended */
+ if (cnt) {
+ putu(reps);
+ putc(' ');
+ }
+ writes(prev, plen);
+ nl();
+ }
+ for (i = 0; i < clen; i++) prev[i] = cur[i];
+ plen = clen;
+ have = 1;
+ reps = 1;
+ }
+ clen = 0;
+ if (ch < 0) break;
+ }
+ if (have) {
+ if (cnt) {
+ putu(reps);
+ putc(' ');
+ }
+ writes(prev, plen);
+ nl();
+ }
+ if (fd != NOFD) close(fd);
+}