From 5fbd9b519e082fc235a8a57c6cf761e6cefbfb62 Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Sat, 22 Jun 2024 21:41:46 +0200 Subject: LibC: Fix bugs relating to fseek --- userland/libc/stdio/fgetc.c | 1 + userland/libc/stdio/fseek.c | 21 ++++----------------- userland/libc/stdio/ftell.c | 2 +- userland/libc/stdio/stdin.c | 3 +++ userland/libc/stdio/ungetc.c | 1 + 5 files changed, 10 insertions(+), 18 deletions(-) (limited to 'userland/libc/stdio') diff --git a/userland/libc/stdio/fgetc.c b/userland/libc/stdio/fgetc.c index c69211f..afa299f 100644 --- a/userland/libc/stdio/fgetc.c +++ b/userland/libc/stdio/fgetc.c @@ -4,6 +4,7 @@ int fgetc(FILE *stream) { if (stream->has_buffered_char) { stream->has_buffered_char = 0; + fseek(stream, 1, SEEK_CUR); return stream->buffered_char; } char c; diff --git a/userland/libc/stdio/fseek.c b/userland/libc/stdio/fseek.c index fb891ec..cbccbb8 100644 --- a/userland/libc/stdio/fseek.c +++ b/userland/libc/stdio/fseek.c @@ -1,21 +1,8 @@ -#include #include +#include int fseek(FILE *stream, long offset, int whence) { - return stream->seek(stream, offset, whence); - /* - switch (whence) { - case SEEK_SET: - stream->offset_in_file = offset; - break; - case SEEK_CUR: - stream->offset_in_file += offset; - break; - case SEEK_END: - // FIXME - assert(0); - break; - } - // FIXME: Error checking - return 0;*/ + stream->read_buffer_stored = 0; + assert(stream->seek); + return stream->seek(stream, offset, whence); } diff --git a/userland/libc/stdio/ftell.c b/userland/libc/stdio/ftell.c index 35076d0..1729d74 100644 --- a/userland/libc/stdio/ftell.c +++ b/userland/libc/stdio/ftell.c @@ -1,5 +1,5 @@ #include long ftell(FILE *stream) { - return stream->offset_in_file; + return fseek(stream, 0, SEEK_CUR); } diff --git a/userland/libc/stdio/stdin.c b/userland/libc/stdio/stdin.c index fcfc71e..e497167 100644 --- a/userland/libc/stdio/stdin.c +++ b/userland/libc/stdio/stdin.c @@ -71,10 +71,13 @@ size_t read_fd(FILE *f, unsigned char *s, size_t l) { f->read_buffer_has_read += read_len; s += read_len; l -= read_len; + lseek(f->fd, read_len, SEEK_CUR); return read_len + read_fd(f, s, l); } if (0 == f->read_buffer_stored) { + int offset = lseek(f->fd, 0, SEEK_CUR); f->read_buffer_stored = non_cache_read_fd(f, f->read_buffer, 4096); + lseek(f->fd, offset, SEEK_SET); f->read_buffer_has_read = 0; if (0 == f->read_buffer_stored) { return 0; diff --git a/userland/libc/stdio/ungetc.c b/userland/libc/stdio/ungetc.c index 8d649bc..3b93b70 100644 --- a/userland/libc/stdio/ungetc.c +++ b/userland/libc/stdio/ungetc.c @@ -5,5 +5,6 @@ int ungetc(int c, FILE *stream) { return EOF; stream->buffered_char = c; stream->has_buffered_char = 1; + fseek(stream, -1, SEEK_CUR); return c; } -- cgit v1.2.3