From 01933cb5de852bcb14b1b225c7512eb0aa8c2255 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Tue, 5 Nov 2024 23:07:07 +0000 Subject: [PATCH] Implement the write system call, w/ E --- src/userprog/syscall.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index 6eb1bce..dbcd3b8 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -29,7 +29,7 @@ static void syscall_seek (int fd, unsigned position); static unsigned syscall_tell (int fd); static void syscall_close (int fd); -static void *validate_user_pointer (void *ptr, size_t size); +static void *validate_user_pointer (const void *ptr, size_t size); /* A struct defining a syscall_function pointer along with its arity. */ typedef struct @@ -157,10 +157,26 @@ syscall_read (int fd UNUSED, void *buffer UNUSED, unsigned size UNUSED) } static int -syscall_write (int fd UNUSED, const void *buffer UNUSED, unsigned size UNUSED) +syscall_write (int fd, const void *buffer, unsigned size) { - //TODO - return 0; + /* Only console (fd = 1) or other files, not including STDIN, (fd > 1) are + allowed. */ + if (fd <= 0) + return 0; + + validate_user_pointer (buffer, size); + + if (fd == STDOUT_FILENO) + { + /* Writing to the console. */ + putbuf (buffer, size); + return size; + } + else + { + /* Writing to a file. */ + return 0; // TODO: Implement Write to Files + } } static void @@ -187,7 +203,7 @@ syscall_close (int fd UNUSED) thread_exit) if the memory is invalid. Otherwise, returns the PTR given. If the size is 0, the function does no checks and returns PTR.*/ static void * -validate_user_pointer (void *ptr, size_t size) +validate_user_pointer (const void *ptr, size_t size) { if (size > 0 && (ptr == NULL || !is_user_vaddr (ptr) ||