diff options
author | Anton Kling <anton@kling.gg> | 2024-06-22 21:41:46 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-06-22 21:41:46 +0200 |
commit | 5fbd9b519e082fc235a8a57c6cf761e6cefbfb62 (patch) | |
tree | d9df62f8bf6ed6b17a7b61aa77f61da9d097c5ff /userland/libc/string/sscanf.c | |
parent | 5e10cdd5b4aa7486208dd14ebef4254ec5b5d03a (diff) |
LibC: Fix bugs relating to fseek
Diffstat (limited to 'userland/libc/string/sscanf.c')
-rw-r--r-- | userland/libc/string/sscanf.c | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/userland/libc/string/sscanf.c b/userland/libc/string/sscanf.c index 28e1ce1..e1ede63 100644 --- a/userland/libc/string/sscanf.c +++ b/userland/libc/string/sscanf.c @@ -150,20 +150,21 @@ int vfscanf(FILE *stream, const char *format, va_list ap) { struct sscanf_cookie { const char *s; + unsigned long offset; }; size_t sscanf_read(FILE *f, unsigned char *s, size_t l) { struct sscanf_cookie *c = f->cookie; - if (!*(c->s)) { + if (!*(c->s + c->offset)) { return 0; } size_t r = 0; - for (; l && *(c->s); l--, c->s += 1) { - *s = *(c->s); + for (; l && *(c->s); l--, c->offset += 1) { + *s = *(c->s + c->offset); s++; r++; } - if (!(*(c->s))) + if (!(*(c->s + c->offset))) f->is_eof = 1; /* memcpy(s, c->s, l); @@ -171,10 +172,35 @@ size_t sscanf_read(FILE *f, unsigned char *s, size_t l) { return r; } +int sscanf_seek(FILE *stream, long offset, int whence) { + struct sscanf_cookie *c = stream->cookie; + off_t ret_offset = c->offset; + switch (whence) { + case SEEK_SET: + // TODO: Avoid running past the pointer + // Should that even be checked? + ret_offset = offset; + break; + case SEEK_CUR: + ret_offset += offset; + break; + case SEEK_END: + for (; *(c->s + ret_offset); ret_offset++) + ; + break; + default: + return -EINVAL; + break; + } + c->offset = ret_offset; + return ret_offset; +} + int vsscanf(const char *s, const char *restrict format, va_list ap) { - struct sscanf_cookie c = {.s = s}; + struct sscanf_cookie c = {.s = s, .offset = 0}; FILE f = { .read = sscanf_read, + .seek = sscanf_seek, .cookie = &c, .has_buffered_char = 0, .is_eof = 0, |