diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/include/camellia/errno.h | 1 | ||||
-rw-r--r-- | src/user/lib/elfload.c | 2 | ||||
-rw-r--r-- | src/user/lib/file.c | 35 | ||||
-rw-r--r-- | src/user/lib/include/stdio.h | 6 |
4 files changed, 43 insertions, 1 deletions
diff --git a/src/shared/include/camellia/errno.h b/src/shared/include/camellia/errno.h index 0c3afb3..3ff735a 100644 --- a/src/shared/include/camellia/errno.h +++ b/src/shared/include/camellia/errno.h @@ -3,3 +3,4 @@ #define EFAULT 2 #define EBADF 3 /* Invalid file descriptor. */ #define EINVAL 4 +#define ENOSYS 5 /* Unsupported. */ diff --git a/src/user/lib/elfload.c b/src/user/lib/elfload.c index a471513..5ba596f 100644 --- a/src/user/lib/elfload.c +++ b/src/user/lib/elfload.c @@ -12,9 +12,9 @@ void elf_execf(FILE *f) { const size_t cap = 0x60000; size_t pos = 0; void *buf = malloc(cap); // TODO a way to get file size + fseek(f, 0, SEEK_SET); if (buf && fread(buf, 1, cap - pos, f)) elf_exec(buf); - free(buf); } 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; } diff --git a/src/user/lib/include/stdio.h b/src/user/lib/include/stdio.h index b8de85d..eb59793 100644 --- a/src/user/lib/include/stdio.h +++ b/src/user/lib/include/stdio.h @@ -4,6 +4,10 @@ #define EOF (-1) +#define SEEK_SET 1 +#define SEEK_CUR 2 +#define SEEK_END 3 + int printf(const char *fmt, ...); int snprintf(char *str, size_t len, const char *fmt, ...); @@ -17,9 +21,11 @@ FILE *freopen(const char *path, const char *mode, FILE *); FILE *fdopen(int fd, const char *mode); FILE *file_clone(const FILE *); int fclose(FILE *); +int fflush(FILE *f); size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict); size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict); +int fseek(FILE *f, long offset, int whence); int feof(FILE *); int ferror(FILE *); |