diff options
author | dzwdz | 2022-07-27 12:27:48 +0200 |
---|---|---|
committer | dzwdz | 2022-07-27 12:27:48 +0200 |
commit | 55b3a04e97b7ddd0db17a4fc3ba35461b106b92f (patch) | |
tree | ef05f83045eee6628385daa46ef7f15d5ff48032 /src/user/lib/file.c | |
parent | 734da5ae528b1e61e9701d70bfe7034a20bb61f2 (diff) |
user/libc: fseek()
Diffstat (limited to 'src/user/lib/file.c')
-rw-r--r-- | src/user/lib/file.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/user/lib/file.c b/src/user/lib/file.c index 44fa194..bd6bc56 100644 --- a/src/user/lib/file.c +++ b/src/user/lib/file.c @@ -143,13 +143,48 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restri return nitems; } +int fseek(FILE *f, long offset, int whence) { + if (fflush(f)) + return -1; + + switch (whence) { + case SEEK_SET: + f->pos = 0; + break; + case SEEK_CUR: + break; + case SEEK_END: + f->pos = -1; + // TODO doesn't -1 put the cursor before the last byte? i need to fix up the drivers + break; + default: + errno = EINVAL; + return -1; + } + + bool pos_neg = f->pos < 0; + f->pos += offset; + if (pos_neg && f->pos >= 0) { + errno = ENOSYS; // TODO + return -1; + } + f->eof = false; + return 0; +} + int fclose(FILE *f) { + fflush(f); if (f->fd > 0) close(f->fd); if (f != &_stdin_null && f != &_stdout_null) free(f); return 0; } +int fflush(FILE *f) { + (void)f; + return 0; +} + int feof(FILE *f) { return f->eof; } |