Devices: Return EOF after first read for some devices

Still not optimal.
This commit is contained in:
apio 2022-10-26 20:54:47 +02:00
parent d5a64319f9
commit 41f7232b77
4 changed files with 4 additions and 20 deletions

View File

@ -8,22 +8,6 @@
void cat(FILE* stream) 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]; char buf[BUFSIZ];
do { do {
fgets(buf, BUFSIZ, stream); fgets(buf, BUFSIZ, stream);

View File

@ -16,9 +16,10 @@ VFS::Node* UptimeDevice::create_new(const char* devname)
return dev; 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 (!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 snprintf(buffer, size + 1, "%ld", PIT::ms_since_boot); // FIXME: Support offseting this read
return (ssize_t)size; return (ssize_t)size;
} }

View File

@ -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) ssize_t VersionDevice::read(VFS::Node* node, size_t offset, size_t size, char* buffer)
{ {
if (!node) return -1; if (!node) return -1;
if (offset > node->length) return -1; if (offset > 0) return 0; // EOF after first read (FIXME: Should be only if everything was read)
if (offset + size > node->length) { size = node->length - offset; }
snprintf(buffer, size + 1, "moon %s", moon_version()); // FIXME: Support offseting this read snprintf(buffer, size + 1, "moon %s", moon_version()); // FIXME: Support offseting this read
return (ssize_t)size; return (ssize_t)size;
} }

View File

@ -8,7 +8,7 @@
#include <bits/seek.h> #include <bits/seek.h>
#define FOPEN_MAX 32 // Maximum number of files that can be simultaneously opened with fopen(). #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. #define FILENAME_MAX 1024 // Dummy value, we don't have a limit for filenames right now.
/* A stream representing a file. */ /* A stream representing a file. */