aboutsummaryrefslogtreecommitdiffstats
path: root/usr/cmp.c
diff options
context:
space:
mode:
authoruser <user@clank>2026-09-24 23:08:46 +0200
committeruser <user@clank>2026-09-24 23:08:46 +0200
commit4bfe20f88b93645f9a277c9903620d75f0ddc179 (patch)
treeba8af4184f10645fecc59adc9b0b0454658ad278 /usr/cmp.c
parentterm: ESC[2J, so clear(1) can actually clear (diff)
downloadgbos-4bfe20f88b93645f9a277c9903620d75f0ddc179.tar.gz
gbos-4bfe20f88b93645f9a277c9903620d75f0ddc179.tar.xz
gbos-4bfe20f88b93645f9a277c9903620d75f0ddc179.zip
usr: sort, cmp, strings, du, time
- sort [-r] [-n]: everything in RAM, which bounds it - a task has 8 KiB and a file caps at 3 KiB, so 96 lines of 2400 bytes with room for the stack. Insertion sort over an index; at that size the simple thing is fast and costs far less ROM. `uniq -c f | sort -n -r` finally works. NB the key needs a scratch SLOT of its own: gt() compares table entries, so reusing slot i as scratch overwrites the entries being shifted into it. - cmp: reports the first differing byte, silent when the files match. There was no way to check a copy on the machine - you had to haul both to a host. - strings [-n N]: pairs with xxd now that /bin/<prog> is readable. It finds "gbos sm83 (Game Boy Color)" inside /bin/uname. - du [dir]: 256-byte blocks, the unit df and the filesystem use. Bytes overflow - /bin alone is 52 x 16 KiB, which wrapped a 16-bit counter and printed 32768. Recurses by re-opendir'ing, since the kernel keeps only one "directory being listed" cursor. - time CMD: 1/64 s resolution from the tick counter. It rebuilds the child's command line at $A000 - which is its own argument area, so everything is copied to the stack first.
Diffstat (limited to 'usr/cmp.c')
-rw-r--r--usr/cmp.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/usr/cmp.c b/usr/cmp.c
new file mode 100644
index 0000000..5a4a718
--- /dev/null
+++ b/usr/cmp.c
@@ -0,0 +1,61 @@
+#include "gbos.h"
+/* cmp FILE1 FILE2: report the first byte where two files differ, or say
+ * nothing when they match (like cmp(1), silence means identical).
+ *
+ * After cp/mv/tee/nc there was still no way to check a copy ON the machine -
+ * you had to haul both files to a host. */
+void main(void) {
+ char *argv[4];
+ unsigned char argc = argv_parse(argv, 4);
+ unsigned char a, b;
+ unsigned int off = 0;
+ int ca, cb;
+
+ if (argc < 2) {
+ puts("usage: cmp FILE1 FILE2");
+ nl();
+ sexit(2);
+ }
+ a = open(argv[0], O_READ);
+ if (a > 3) {
+ puts("cmp: cannot open ");
+ puts(argv[0]);
+ nl();
+ sexit(2);
+ }
+ b = open(argv[1], O_READ);
+ if (b > 3) {
+ puts("cmp: cannot open ");
+ puts(argv[1]);
+ nl();
+ close(a);
+ sexit(2);
+ }
+
+ for (;;) {
+ ca = fgetc(a);
+ cb = fgetc(b);
+ if (ca < 0 && cb < 0) break; /* same length, same bytes */
+ if (ca < 0 || cb < 0) {
+ puts("cmp: EOF on ");
+ puts(ca < 0 ? argv[0] : argv[1]);
+ puts(" after ");
+ putu(off);
+ nl();
+ close(a);
+ close(b);
+ sexit(1);
+ }
+ if (ca != cb) {
+ puts("differ: byte ");
+ putu(off);
+ nl();
+ close(a);
+ close(b);
+ sexit(1);
+ }
+ off++;
+ }
+ close(a);
+ close(b);
+}