From 52dab7fc85d877e80e3032f0afabae906adebad8 Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 4 Oct 2026 16:03:42 +0200 Subject: feat: fs tree and free list alloc --- user/calc.c | 6 ++++++ user/hexview.c | 32 +++++++++++++++++++++++++++----- user/save.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ user/user.h | 17 ++++++++++++++++- 4 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 user/save.c (limited to 'user') diff --git a/user/calc.c b/user/calc.c index 2d0e3f8..cf5b1d2 100644 --- a/user/calc.c +++ b/user/calc.c @@ -23,6 +23,7 @@ void _start(void) int valid = 1; int saw_number = 0; int position = 0; + while (position < length) { char character = line[position]; @@ -31,11 +32,13 @@ void _start(void) position++; continue; } + if (is_digit(character)) { long value = 0; while (position < length && is_digit(line[position])) value = value * 10 + (line[position++] - '0'); + if (operator == '+') accumulator += value; else if (operator == '-') @@ -52,6 +55,7 @@ void _start(void) } accumulator = (operator == '/') ? accumulator / value : accumulator % value; } + saw_number = 1; } else if (character == '+' || character == '-' || character == '*' @@ -67,6 +71,7 @@ void _start(void) break; } } + if (valid && saw_number) { put("= "); @@ -74,5 +79,6 @@ void _start(void) put("\n"); } } + sys_exit(0); } diff --git a/user/hexview.c b/user/hexview.c index 6354338..c211b44 100644 --- a/user/hexview.c +++ b/user/hexview.c @@ -19,12 +19,22 @@ static void put_hex32(unsigned long value) #define PAGE 256 #define COLS 16 -void _start(void) +void _start(const char* arg, unsigned long arglen) { - put("hexview, type a filename\nfile: "); char name[64]; - read_line(name, 64); - put("\n"); + + if (arglen > 0 && arglen < 64) + { + for (unsigned long i = 0; i < arglen; i++) + name[i] = arg[i]; + name[arglen] = 0; + } + else + { + put("hexview, type a filename\nfile: "); + read_line(name, 64); + put("\n"); + } long size = sys_fs_size(name); if (size < 0) @@ -35,6 +45,7 @@ void _start(void) unsigned long offset = 0; char buffer[PAGE]; + for (;;) { long count = sys_fs_read(name, offset, buffer, PAGE); @@ -61,6 +72,7 @@ void _start(void) else put(" "); } + put(" "); for (int column = 0; column < COLS && row + column < count; column++) { @@ -73,8 +85,18 @@ void _start(void) put("\n"); } - put("[n]ext [p]rev [q]uit > "); + put("[n]ext [p]rev [q]uit (or up/down arrows) > "); char key = (char)sys_read(); + if (key == 27) // ESC + { + sys_read(); + char final = (char)sys_read(); + if (final == 'A') + key = 'p'; + else if (final == 'B') + key = 'n'; + } + put("\n"); if (key == 'q') break; diff --git a/user/save.c b/user/save.c new file mode 100644 index 0000000..b3664f4 --- /dev/null +++ b/user/save.c @@ -0,0 +1,45 @@ +#include "user.h" + +void _start(const char* arg, unsigned long arglen) +{ + if (arglen == 0 || arglen >= 64) + { + put("usage: run save \n"); + sys_exit(1); + } + + char name[64]; + for (unsigned long i = 0; i < arglen; i++) + name[i] = arg[i]; + name[arglen] = 0; + + put("saving to "); + put(name); + put(" -- type lines, a single . saves\n"); + + char buffer[4096]; + int length = 0; + for (;;) + { + char line[256]; + int n = read_line(line, 256); + put("\n"); + if (n == 1 && line[0] == '.') + break; + for (int i = 0; i < n && length < 4000; i++) + buffer[length++] = line[i]; + if (length < 4000) + buffer[length++] = '\n'; + } + + if (sys_fs_write(name, buffer, (unsigned long)length)) + { + put("saved "); + put(name); + put("\n"); + } + else + put("write failed\n"); + + sys_exit(0); +} diff --git a/user/user.h b/user/user.h index 676326b..635dc86 100644 --- a/user/user.h +++ b/user/user.h @@ -1,4 +1,4 @@ -// shared syscall stubs + tiny helpers for revel ring 3 programs +// shared syscall stubs static long sys_write(const char* buffer, unsigned long length) { @@ -37,6 +37,16 @@ static long sys_fs_read(const char* name, unsigned long offset, char* buffer, un return result; } +static long sys_fs_write(const char* name, const char* buffer, unsigned long length) +{ + long result; + __asm__ volatile("int $0x80" + : "=a"(result) + : "a"(6L), "D"(name), "S"(buffer), "d"(length) + : "memory"); + return result; +} + static void put(const char* string) { unsigned long length = 0; @@ -78,20 +88,25 @@ static void put_int(long value) int length = 0; int negative = value < 0; unsigned long magnitude = negative ? (unsigned long)(-value) : (unsigned long)value; + if (magnitude == 0) buffer[length++] = '0'; + while (magnitude > 0) { buffer[length++] = (char)('0' + magnitude % 10); magnitude /= 10; } + if (negative) buffer[length++] = '-'; + for (int left = 0, right = length - 1; left < right; left++, right--) { char swap = buffer[left]; buffer[left] = buffer[right]; buffer[right] = swap; } + sys_write(buffer, length); } -- cgit v1.3