From 03c5cb09b570a88405a1a10f91f8470e4d42e1ca Mon Sep 17 00:00:00 2001 From: IonutParau Date: Sun, 6 Jul 2025 00:01:33 +0200 Subject: [PATCH] filesystem seek --- src/components/filesystem.c | 3 ++- src/emulator.c | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/components/filesystem.c b/src/components/filesystem.c index 15f10ba..6f4a606 100644 --- a/src/components/filesystem.c +++ b/src/components/filesystem.c @@ -396,12 +396,13 @@ void nn_fs_seek(nn_filesystem *fs, void *_, nn_component *component, nn_computer // size_t capacity = fs->spaceTotal(component, fs->userdata); int moved = 0; - /* size_t pos = */ fs->seek(component, fs->userdata, fd, whence, off, &moved); + size_t pos = fs->seek(component, fs->userdata, fd, whence, off, &moved); if(moved < 0) moved = -moved; // do not ask where it comes from, balance is hard nn_fs_readCost(fs, 1, component, computer); nn_fs_seekCost(fs, nn_fs_countChunks(fs, moved, component), component, computer); + nn_return_integer(computer, pos); } void nn_loadFilesystemTable(nn_universe *universe) { diff --git a/src/emulator.c b/src/emulator.c index f00fbfc..777f34a 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -181,6 +181,20 @@ size_t ne_fs_read(nn_component *component, ne_fs *fs, int fd, char *buf, size_t return fread(buf, sizeof(char), required, f); } +size_t ne_fs_seek(nn_component *component, ne_fs *fs, int fd, const char *whence, int off, int *moved) { + FILE *f = fs->files[fd]; + *moved = 0; + int w = SEEK_SET; + if(strcmp(whence, "cur") == 0) { + w = SEEK_CUR; + } + if(strcmp(whence, "end") == 0) { + w = SEEK_END; + } + fseek(f, w, off); + return ftell(f); +} + char **ne_fs_list(nn_Alloc *alloc, nn_component *component, ne_fs *fs, const char *path, size_t *len) { const char *p = ne_fs_diskPath(component, path); if(p[0] == '/') p++; @@ -628,7 +642,7 @@ int main() { .close = (void *)ne_fs_close, .write = (void *)ne_fs_write, .read = (void *)ne_fs_read, - .seek = NULL, + .seek = (void *)ne_fs_seek, }; nn_addFileSystem(computer, "OpenOS", 1, &genericFS);