diff options
| author | Jake Mannens <jakem_5@hotmail.com> | 2020-02-24 17:39:40 +1100 |
|---|---|---|
| committer | Jake Mannens <jakem_5@hotmail.com> | 2020-02-24 17:39:40 +1100 |
| commit | 07c004bf3cf7fcb6e875bddb1a7fb0793377ebfb (patch) | |
| tree | 6135939912e3ba14cebd47c7fd0d64522c3ed92e | |
| parent | a77b79c1959a134764b88cfe70411d109c6c0354 (diff) | |
Changed read() and write() calls to now accept an integer file
descriptor as their first parameter. read()/write() calls to descriptors
other than stdin/stdout respectively, are currently discared as the file
table has yet to be fully implemented.
| -rw-r--r-- | include/stdio.h | 4 | ||||
| -rw-r--r-- | include/unistd.h | 15 | ||||
| -rw-r--r-- | kernel/fs/fs.c | 16 | ||||
| -rw-r--r-- | lib/stdio.c | 6 | ||||
| -rw-r--r-- | usrbin/main.c | 2 |
5 files changed, 33 insertions, 10 deletions
diff --git a/include/stdio.h b/include/stdio.h index 5387db0..9a599fe 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -14,8 +14,8 @@ int sprintf(char*, char*, ...); int vsprintf(char*, char*, va_list); -ssize_t read(void*, size_t); -ssize_t write(void*, size_t); +ssize_t read(int, void*, size_t); +ssize_t write(int, void*, size_t); int ctty(int); diff --git a/include/unistd.h b/include/unistd.h index 47e1621..0b3c666 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -8,6 +8,10 @@ typedef int32_t ssize_t; #endif +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 + typedef int16_t pid_t; #define __SYS_alarm 0 @@ -55,6 +59,17 @@ typedef int16_t pid_t; return __res; \ } +#define _syscall3(type, name, atype, a, btype, b, ctype, c) \ + type name(atype a, btype b, ctype c) { \ + type __res; \ + __asm__ volatile ( \ + "int $0x80" \ + : "=a" (__res) \ + : "a" (__SYS_##name), "b" (a), "c" (b), "d" (c) \ + ); \ + return __res; \ + } + pid_t getpid(void); void *getpdir(void); diff --git a/kernel/fs/fs.c b/kernel/fs/fs.c index e0d9205..381f20a 100644 --- a/kernel/fs/fs.c +++ b/kernel/fs/fs.c @@ -1,3 +1,4 @@ +#include <errno.h> #include <kernel/con.h> #include <kernel/fs.h> #include <kernel/kernel.h> @@ -5,16 +6,23 @@ #include <kernel/tty.h> #include <string.h> #include <sys/types.h> +#include <unistd.h> /* master file table */ static struct file ftable[NRFILE]; -ssize_t sys_read(void *buf, size_t len) { - return tty_read(ctask->ctty, buf, len); +ssize_t sys_read(int fd, void *buf, size_t len) { + if(fd == STDIN_FILENO) + return tty_read(ctask->ctty, buf, len); + + return -EINVAL; } -ssize_t sys_write(void *buf, size_t len) { - return tty_write(ctask->ctty, buf, len); +ssize_t sys_write(int fd, void *buf, size_t len) { + if(fd == STDOUT_FILENO || fd == STDERR_FILENO) + return tty_write(ctask->ctty, buf, len); + + return -EINVAL; } void fs_init(void) { diff --git a/lib/stdio.c b/lib/stdio.c index b1c00ba..9fbf7fe 100644 --- a/lib/stdio.c +++ b/lib/stdio.c @@ -5,14 +5,14 @@ #include <unistd.h> _syscall1(int, ctty, int, ctty); -_syscall2(ssize_t, read, void*, buf, size_t, len); -_syscall2(ssize_t, write, void*, buf, size_t, len); +_syscall3(ssize_t, read, int, fd, void*, buf, size_t, len); +_syscall3(ssize_t, write, int, fd, void*, buf, size_t, len); int puts(char *s) { size_t len = strlen(s); ssize_t ret; - ret = write(s, len); + ret = write(STDOUT_FILENO, s, len); if(ret < 0) return EOF; diff --git a/usrbin/main.c b/usrbin/main.c index 6acf5fe..c4a257c 100644 --- a/usrbin/main.c +++ b/usrbin/main.c @@ -17,7 +17,7 @@ void pid1(void) { signal(1, &sighandler); while(1) { - in = read(buf, 1); + in = read(STDIN_FILENO, buf, 1); if(in < 1) { if(in == -EINTR) { printf("Read call interrupted!\n"); |
