aboutsummaryrefslogtreecommitdiffstats
path: root/usr/ls.c
diff options
context:
space:
mode:
authoruser <user@clank>2026-09-24 12:48:51 +0200
committeruser <user@clank>2026-09-24 12:48:51 +0200
commit65b53de14dc6918db39b6ee6dc032f805182daad (patch)
tree6af7b52262321a3cbe11e1d472ca2938df2fc37e /usr/ls.c
parentsh: run scripts, and >> redirection (diff)
downloadgbos-65b53de14dc6918db39b6ee6dc032f805182daad.tar.gz
gbos-65b53de14dc6918db39b6ee6dc032f805182daad.tar.xz
gbos-65b53de14dc6918db39b6ee6dc032f805182daad.zip
usr: ls -l, df, mv, tail, tee, sleep, help, poweroff
Filling in the core utilities the box was missing: - ls -l: type, links and size, now that stat() exists. The screen is 40 columns, so it is "d 1 256 name", not the GNU sprawl. Sizes were previously unknowable except one file at a time with wc -c. - df: blocks and inodes, used and free, on the 32 KiB cart disk. - mv: no rename syscall, so it is cp + rm - and the copy has to finish before the source goes, or a failure loses the file. - tail [-n N]: no seek syscall, so it streams the input through a ring of N lines rather than jumping to the end. N is capped by that ring. - tee: the missing half of a pipeline - `cat big | tee copy | wc -l`. - sleep: the thing every script needs; the kernel counts 1/64 s ticks. - help: lists every command the shell knows, walked out of the kernel's own name table. The box previously had no way to tell you what it could do. - poweroff: the only safe way to stop a gbos that has data on it. The cart's battery RAM is written when the emulator exits cleanly, so killing it loses the whole filesystem - it comes back "formatted". Found while testing /rc.
Diffstat (limited to 'usr/ls.c')
-rw-r--r--usr/ls.c68
1 files changed, 63 insertions, 5 deletions
diff --git a/usr/ls.c b/usr/ls.c
index 0e2e01f..5a26717 100644
--- a/usr/ls.c
+++ b/usr/ls.c
@@ -1,10 +1,68 @@
-#include "gbos.h"
-/* ls [dir]: list a directory (default: current). */
+#include "stat.h"
+/* ls [-l] [dir]: list a directory (default: current).
+ * -l adds the type, link count and size - the terminal is 40 columns, so the
+ * long form is "d 1 256 name", not the BSD/GNU sprawl. */
+
+static void pad(unsigned int n, unsigned char width) {
+ unsigned char d = 1;
+ unsigned int v = n;
+ while (v >= 10) {
+ v = v / 10;
+ d++;
+ }
+ while (d++ < width) putc(' ');
+ putu(n);
+}
+
void main(void) {
char *argv[4];
char name[16];
+ char path[40];
+ struct gstat st;
unsigned char argc = argv_parse(argv, 4);
- unsigned char i;
- if (opendir(argc > 0 ? argv[0] : ".")) { puts("ls: no such directory"); nl(); return; }
- for (i = 0; flist(i, name); i++) { name[15] = 0; puts(name); nl(); }
+ unsigned char lng = hasflag(argv, argc, 'l');
+ unsigned char i, j, k;
+ char *dir = 0;
+
+ for (i = 0; i < argc; i++)
+ if (argv[i][0] != '-') {
+ dir = argv[i];
+ break;
+ }
+ if (opendir(dir ? dir : ".")) {
+ puts("ls: no such directory");
+ nl();
+ sexit(1);
+ }
+
+ for (i = 0; flist(i, name); i++) {
+ name[15] = 0;
+ if (!lng) {
+ puts(name);
+ nl();
+ continue;
+ }
+ /* stat wants a path, and flist only hands back the leaf name */
+ j = 0;
+ if (dir) {
+ for (k = 0; dir[k] && j < 30; k++) path[j++] = dir[k];
+ if (j && path[j - 1] != '/') path[j++] = '/';
+ }
+ for (k = 0; name[k] && j < 38; k++) path[j++] = name[k];
+ path[j] = 0;
+ if (!gstat(path, &st)) {
+ puts("? ");
+ puts(name);
+ nl();
+ continue;
+ }
+ putc(st.type == ST_DIR ? 'd' : '-');
+ putc(' ');
+ pad(st.nlink, 2);
+ putc(' ');
+ pad(st.size, 5);
+ putc(' ');
+ puts(name);
+ nl();
+ }
}