From 18694d7b629bdb13ced060f78ce897ac614c6ab0 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Fri, 8 Nov 2024 16:25:49 +0000 Subject: [PATCH] Implement file reading syscall and fix fd validation w/ S. --- src/userprog/syscall.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index f0aaf47..9a42214 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -211,7 +211,7 @@ syscall_read (int fd, void *buffer, unsigned size) { /* Only console (fd = 0) or other files, not including STDOUT, (fd > 1) are allowed. */ - if (fd < 0 && fd != STDOUT_FILENO) + if (fd < 0 || fd == STDOUT_FILENO) return -1; validate_user_pointer (buffer, size); @@ -228,7 +228,14 @@ syscall_read (int fd, void *buffer, unsigned size) else { /* Reading from a file. */ - return 0; // TODO: Implement Write to Files + struct open_file *file_info = fd_get_file (fd); + if (file_info == NULL) + return -1; + + lock_acquire (&filesys_lock); + int bytes_written = file_read (file_info->file, buffer, size); + lock_release (&filesys_lock); + return bytes_written; } }