From 41f7232b7702fc9d22e72124e86b0d0327390ef5 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 26 Oct 2022 20:54:47 +0200 Subject: [PATCH] Devices: Return EOF after first read for some devices Still not optimal. --- apps/src/cat.c | 16 ---------------- kernel/src/fs/devices/Uptime.cpp | 3 ++- kernel/src/fs/devices/Version.cpp | 3 +-- libs/libc/include/stdio.h | 2 +- 4 files changed, 4 insertions(+), 20 deletions(-) diff --git a/apps/src/cat.c b/apps/src/cat.c index 273b8d6a..077d4925 100644 --- a/apps/src/cat.c +++ b/apps/src/cat.c @@ -8,22 +8,6 @@ void cat(FILE* stream) { - struct stat statbuf; - fstat(fileno(stream), &statbuf); - if (statbuf.st_mode == __VFS_DEVICE) // FIXME: Add S_ISDEV. - { - char buf[4096]; // FIXME: Do not limit ourselves to 4096 bytes. Devices which can't seek and are constant should - // return EOF after the first read. - fgets(buf, 4096, stream); - if (ferror(stream)) - { - perror(program_invocation_name); - exit(EXIT_FAILURE); - } - if (feof(stream)) return; - fputs(buf, stdout); - return; - } char buf[BUFSIZ]; do { fgets(buf, BUFSIZ, stream); diff --git a/kernel/src/fs/devices/Uptime.cpp b/kernel/src/fs/devices/Uptime.cpp index a80cd09d..f536a479 100644 --- a/kernel/src/fs/devices/Uptime.cpp +++ b/kernel/src/fs/devices/Uptime.cpp @@ -16,9 +16,10 @@ VFS::Node* UptimeDevice::create_new(const char* devname) return dev; } -ssize_t UptimeDevice::read(VFS::Node* node, size_t, size_t size, char* buffer) +ssize_t UptimeDevice::read(VFS::Node* node, size_t offset, size_t size, char* buffer) { if (!node) return -1; + if (offset > 0) return 0; // EOF after first read (FIXME: Should be only if everything was read) snprintf(buffer, size + 1, "%ld", PIT::ms_since_boot); // FIXME: Support offseting this read return (ssize_t)size; } \ No newline at end of file diff --git a/kernel/src/fs/devices/Version.cpp b/kernel/src/fs/devices/Version.cpp index 164a6f8f..86a4962a 100644 --- a/kernel/src/fs/devices/Version.cpp +++ b/kernel/src/fs/devices/Version.cpp @@ -19,8 +19,7 @@ VFS::Node* VersionDevice::create_new(const char* devname) ssize_t VersionDevice::read(VFS::Node* node, size_t offset, size_t size, char* buffer) { if (!node) return -1; - if (offset > node->length) return -1; - if (offset + size > node->length) { size = node->length - offset; } + if (offset > 0) return 0; // EOF after first read (FIXME: Should be only if everything was read) snprintf(buffer, size + 1, "moon %s", moon_version()); // FIXME: Support offseting this read return (ssize_t)size; } \ No newline at end of file diff --git a/libs/libc/include/stdio.h b/libs/libc/include/stdio.h index 15beb1d0..0ceec611 100644 --- a/libs/libc/include/stdio.h +++ b/libs/libc/include/stdio.h @@ -8,7 +8,7 @@ #include #define FOPEN_MAX 32 // Maximum number of files that can be simultaneously opened with fopen(). -#define BUFSIZ 32 // Standard buffer size. +#define BUFSIZ 1024 // Standard buffer size. #define FILENAME_MAX 1024 // Dummy value, we don't have a limit for filenames right now. /* A stream representing a file. */