diff --git a/libc/include/stdio.h b/libc/include/stdio.h index f9dfa42a..3ad89399 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -16,9 +16,31 @@ */ #define EOF (-1) +struct _IO_marker +{ + struct _IO_marker *_next; + struct _IO_FILE *_sbuf; + int _pos; +}; + struct _IO_FILE { - size_t offset; + int _flags; + + char *_IO_read_ptr; + char *_IO_read_end; + char *_IO_read_base; + char *_IO_write_base; + char *_IO_write_ptr; + char *_IO_write_end; + char *_IO_buf_base; + char *_IO_buf_end; + + __off_t _offset; + + struct _IO_marker *_markers; + struct _IO_FILE *_chain; + int _fileno; void *KernelPrivate; }; diff --git a/libc/include/sys/stat.h b/libc/include/sys/stat.h index 6dbd35c9..61e31c01 100644 --- a/libc/include/sys/stat.h +++ b/libc/include/sys/stat.h @@ -1,15 +1,15 @@ #ifndef _SYS_STAT_H #define _SYS_STAT_H -typedef unsigned int _dev_t; -typedef unsigned short _ino_t; -typedef unsigned short _mode_t; -typedef long _off_t; +typedef unsigned int __dev_t; +typedef unsigned short __ino_t; +typedef unsigned short __mode_t; +typedef long __off_t; -#define dev_t _dev_t -#define ino_t _ino_t -#define mode_t _mode_t -#define off_t _off_t +#define dev_t __dev_t +#define ino_t __ino_t +#define mode_t __mode_t +#define off_t __off_t int mkdir(const char *path, mode_t mode); int remove(const char *pathname); diff --git a/libc/src/Runtime.c b/libc/src/Runtime.c index fd54f1ce..ba855b4e 100644 --- a/libc/src/Runtime.c +++ b/libc/src/Runtime.c @@ -2,13 +2,18 @@ extern void __libc_init_array(void); extern void __libc_fini_array(void); +extern void __libc_init_std(void); +extern void __libc_fini_std(void); + void __libc_init(void) { __libc_init_array(); + __libc_init_std(); } void _exit(int Code) { + __libc_fini_std(); __libc_fini_array(); __asm__ __volatile__("syscall" : diff --git a/libc/src/std/io/file.c b/libc/src/std/io/file.c index 702a5e31..592603db 100644 --- a/libc/src/std/io/file.c +++ b/libc/src/std/io/file.c @@ -27,7 +27,7 @@ size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) return 0; } - return syscall4(_FileRead, (uint64_t)stream->KernelPrivate, stream->offset, (uint64_t)ptr, size * nmemb); + 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) @@ -38,7 +38,7 @@ size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) return 0; } - return syscall4(_FileWrite, (uint64_t)stream->KernelPrivate, stream->offset, (uint64_t)ptr, size * nmemb); + return syscall4(_FileWrite, (uint64_t)stream->KernelPrivate, stream->_offset, (uint64_t)ptr, size * nmemb); } int fclose(FILE *fp) @@ -56,7 +56,7 @@ int fclose(FILE *fp) off_t fseek(FILE *stream, long offset, int whence) { - if (stream == NULL) + if (stream == NULL || whence < 0 || whence > 2) { errno = EINVAL; return -1; @@ -65,23 +65,40 @@ off_t fseek(FILE *stream, long offset, int whence) off_t new_offset = syscall3(_FileSeek, stream->KernelPrivate, offset, whence); if (IsSyscallError(new_offset)) return -1; - stream->offset = new_offset; + stream->_offset = new_offset; return new_offset; } long ftell(FILE *stream) { - return stream->offset; + return stream->_offset; } int fflush(FILE *stream) { - return 0; + if (stream == NULL) + { + errno = EINVAL; + return EOF; + } + + errno = ENOSYS; + return EOF; } int fprintf(FILE *stream, const char *format, ...) { - return 0; // sprintf(char *s, const char *format, ...) + if (stream == NULL || format == NULL) + { + errno = EINVAL; + return -1; + } + + va_list args; + va_start(args, format); + const int ret = vfprintf(stream, format, args); + va_end(args); + return ret; } void setbuf(FILE *stream, char *buf) diff --git a/libc/src/std/io/put.c b/libc/src/std/io/put.c index c7332ecc..f37c650f 100644 --- a/libc/src/std/io/put.c +++ b/libc/src/std/io/put.c @@ -4,13 +4,17 @@ int fputc(int c, FILE *stream) { + // FIXME + // if (stream == NULL) + // { + // errno = EBADF; + // return EOF; + // } + return syscall2(_Print, c, 0); } -int putc(int c, FILE *stream) -{ - return syscall2(_Print, c, 0); -} +int putc(int c, FILE *stream) { return fputc(c, stream); } int fputs(const char *s, FILE *stream) { @@ -24,12 +28,5 @@ int puts(const char *s) fputc(s[i], stdout); } -int putchar(int c) -{ - return putc(c, stdout); -} - -void perror(const char *s) -{ - fputs(s, stderr); -} \ No newline at end of file +int putchar(int c) { return fputc(c, stdout); } +void perror(const char *s) { fputs(s, stderr); } diff --git a/libc/src/std/std_init.c b/libc/src/std/std_init.c new file mode 100644 index 00000000..aae8d0f5 --- /dev/null +++ b/libc/src/std/std_init.c @@ -0,0 +1,13 @@ +#include + +#include "../../../Kernel/syscalls.h" + +void __libc_init_std(void) +{ + /* FIXME: Temporal workaround */ + // int IsCritical = syscall1(_KernelCTL, 6 /* KCTL_IS_CRITICAL */); +} + +void __libc_fini_std(void) +{ +} diff --git a/libc/src/std/uni/exe.c b/libc/src/std/uni/exe.c index 7fd31f56..728dae02 100644 --- a/libc/src/std/uni/exe.c +++ b/libc/src/std/uni/exe.c @@ -5,36 +5,43 @@ int execl(const char *pathname, const char *arg, ...) { + errno = ENOSYS; return -1; } int execlp(const char *file, const char *arg, ...) { + errno = ENOSYS; return -1; } int execle(const char *pathname, const char *arg, ...) { + errno = ENOSYS; return -1; } int execv(const char *pathname, char *const argv[]) { + errno = ENOSYS; return -1; } int execvp(const char *file, char *const argv[]) { + errno = ENOSYS; return -1; } int execvpe(const char *file, char *const argv[], char *const envp[]) { + errno = ENOSYS; return -1; } int execve(const char *pathname, char *const argv[], char *const envp[]) { + errno = ENOSYS; return -1; }