diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 15721383..644bf9b8 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -8,10 +8,14 @@ #define SEEK_CUR 1 #define SEEK_END 2 -typedef struct +struct _IO_FILE { - int unused; -} FILE; + size_t offset; + + void *KernelPrivate; +}; + +typedef struct _IO_FILE FILE; #ifdef __cplusplus extern "C" @@ -22,15 +26,15 @@ extern "C" extern FILE *stdout; extern FILE *stderr; - int fclose(FILE *stream); - int fflush(FILE *stream); FILE *fopen(const char *filename, const char *mode); - int fprintf(FILE *stream, const char *format, ...); - int printf(const char *format, ...); size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); + size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); int fseek(FILE *stream, long offset, int whence); long ftell(FILE *stream); - size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); + int fclose(FILE *fp); + int fflush(FILE *stream); + int fprintf(FILE *stream, const char *format, ...); + int printf(const char *format, ...); void setbuf(FILE *stream, char *buf); int vfprintf(FILE *stream, const char *format, va_list arg); int vsscanf(const char *s, const char *format, va_list arg); diff --git a/libc/src/std/io.c b/libc/src/std/io/file.c similarity index 52% rename from libc/src/std/io.c rename to libc/src/std/io/file.c index 9b769a73..562618ee 100644 --- a/libc/src/std/io.c +++ b/libc/src/std/io/file.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -6,9 +7,52 @@ FILE *stdin; FILE *stdout; FILE *stderr; -int fclose(FILE *stream) +FILE *fopen(const char *filename, const char *mode) { - return 0; + FILE *FilePtr = malloc(sizeof(FILE)); + FilePtr->KernelPrivate = (void *)syscall2(_FileOpen, (uint64_t)filename, (uint64_t)mode); + return FilePtr; +} + +size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + return syscall4(_FileRead, (uint64_t)stream->KernelPrivate, stream->offset, (uint64_t)ptr, size * nmemb); +} + +size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + return syscall4(_FileWrite, (uint64_t)stream->KernelPrivate, stream->offset, (uint64_t)ptr, size * nmemb); +} + +int fseek(FILE *stream, long offset, int whence) +{ + switch (whence) + { + case SEEK_SET: + stream->offset = offset; + break; + case SEEK_CUR: + stream->offset += offset; + break; + case SEEK_END: + // stream->offset = syscall1(_FileLength, (uint64_t)File->KernelPrivate) + offset; + break; + default: + return -1; + } + return stream->offset; +} + +long ftell(FILE *stream) +{ + return stream->offset; +} + +int fclose(FILE *fp) +{ + void *KP = fp->KernelPrivate; + free(fp); + return syscall1(_FileClose, (uint64_t)KP); } int fflush(FILE *stream) @@ -16,11 +60,6 @@ int fflush(FILE *stream) return 0; } -FILE *fopen(const char *filename, const char *mode) -{ - return 0; -} - int fprintf(FILE *stream, const char *format, ...) { return 0; // sprintf(char *s, const char *format, ...) @@ -31,26 +70,6 @@ int fprintf(FILE *stream, const char *format, ...) // return 0; // } -size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) -{ - return 0; -} - -int fseek(FILE *stream, long offset, int whence) -{ - return 0; -} - -long ftell(FILE *stream) -{ - return 0; -} - -size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) -{ - return 0; -} - void setbuf(FILE *stream, char *buf) { } @@ -72,30 +91,3 @@ int sscanf(const char *s, const char *format, ...) va_end(args); return ret; } - -int fputc(int c, FILE *stream) -{ - syscall1(_Print, c); -} - -int putc(int c, FILE *stream) -{ - syscall1(_Print, c); -} - -int fputs(const char *s, FILE *stream) -{ - for (int i = 0; s[i] != '\0'; i++) - fputc(s[i], stream); -} - -int putchar(int c) -{ - syscall1(_Print, c); -} - -int puts(const char *s) -{ - for (int i = 0; s[i] != '\0'; i++) - fputc(s[i], stdout); -} diff --git a/libc/src/std/io/put.c b/libc/src/std/io/put.c new file mode 100644 index 00000000..fb7203ce --- /dev/null +++ b/libc/src/std/io/put.c @@ -0,0 +1,30 @@ +#include +#include +#include + +int fputc(int c, FILE *stream) +{ + syscall1(_Print, c); +} + +int putc(int c, FILE *stream) +{ + syscall1(_Print, c); +} + +int fputs(const char *s, FILE *stream) +{ + for (int i = 0; s[i] != '\0'; i++) + fputc(s[i], stream); +} + +int putchar(int c) +{ + syscall1(_Print, c); +} + +int puts(const char *s) +{ + for (int i = 0; s[i] != '\0'; i++) + fputc(s[i], stdout); +}