diff --git a/userprog/syscall.c b/userprog/syscall.c index a104087f96233896df559ed9c72e37c0c3ea2b14..e6abc56803da28b385bb69fa7b493d30496ddcc9 100644 --- a/userprog/syscall.c +++ b/userprog/syscall.c @@ -79,6 +79,9 @@ syscall_handler (struct intr_frame *f UNUSED) case SYSCALL_OPEN: syscall_open(f); break; + case SYSCALL_FILESIZE: + syscall_filesize(f); + break; default: printf ("WARNING: Invalid Syscall (%d)\n", syscall_number); thread_exit (); diff --git a/userprog/syscall_filesize.c b/userprog/syscall_filesize.c new file mode 100644 index 0000000000000000000000000000000000000000..ae8e45a80bf0e1ed618a6d54c5c416deacae49dd --- /dev/null +++ b/userprog/syscall_filesize.c @@ -0,0 +1,20 @@ +/* + * Returns the size, in bytes, of the file open as fd. + * + * Authored by Alex Stratford + */ + +#include "system_calls.h" +#include "filesys/file.h" + +void syscall_filesize(struct intr_frame *f) { + // pop off first int argument from interrupt frame + int file_descriptor = *((int*)f->esp + 1); + struct file *file = get_associated_file_pointer(file_descriptor); + if (file == NULL) { // Checking if file is empty or non-existent + f->eax -1; // Returning failure state + } + // Using the file_length function in file.h to get the length and store it + int size = file_length(file); + f->eax = size; +} \ No newline at end of file diff --git a/userprog/system_calls.h b/userprog/system_calls.h index 8dc87c9d325a52ee224a1597b3bfd7877553cf5a..ce7ae18823f34dd39916c550db655291e0f7c6d3 100644 --- a/userprog/system_calls.h +++ b/userprog/system_calls.h @@ -49,6 +49,11 @@ void syscall_write(struct intr_frame *f); */ void syscall_open(struct intr_frame *f); +/* + * Returns the size, in bytes, of the file open as fd. + */ +void syscall_filesize(struct intr_frame *f); + /* * Deletes the file called file. Returns true if successful, false otherwise. * A file may be removed regardless of whether it is open or closed, and