From 9ad37f5ddea118ed12974156cd3cc46998267970 Mon Sep 17 00:00:00 2001 From: Alex Stratford <alexander3.stratford@live.uwe.ac.uk> Date: Wed, 4 Dec 2019 00:59:03 +0000 Subject: [PATCH] Improved file_search function line 80..106: Added comments and improved functionality --- userprog/syscall.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/userprog/syscall.c b/userprog/syscall.c index b2a467f..98fc8eb 100644 --- a/userprog/syscall.c +++ b/userprog/syscall.c @@ -4,7 +4,8 @@ #include "system_calls.h" #include "threads/interrupt.h" #include "threads/thread.h" -#include "lib/kernel/list.h" // Dependencies for +// Dependency for list_elem struct and list_next / is_tail functions +#include "lib/kernel/list.h" /* System call numbers. */ @@ -88,15 +89,30 @@ syscall_handler (struct intr_frame *f UNUSED) static struct list *file_list; -// Goes through all the files in the file_list and looks for the +/* Goes through all the files in the file_list and looks for the given file + * Descriptor. + */ void file_search(struct file_map *f) { + // Creates a struct to hold the currently checked file struct file_map *curr_file; + // Creates the list element to test for end of list struct list_elem *list_element; *list_element = *list_begin(file_list); + + // Loop to check each file in the linked list in turn while ((curr_file->file_descriptor != f->file_descriptor) && - (is_tail(curr_file->list_element) == false) + (is_tail(list_element) == false) ) { - *list_element = curr_file->list_element; + // Swaps the list_element for the next one in place list_next(list_element); } + // Copies list_element to current file + curr_file->list_element = *list_element; + // Checks if file has been found returning NULL if not or the file if it has + if (curr_file->file_descriptor != f->file_descriptor) { + f->file = NULL; + } + else { + f = curr_file; + } } \ No newline at end of file -- GitLab