diff --git a/userprog/syscall.c b/userprog/syscall.c index 210d7a8f536d405afada6e553ce469d5647a2a0e..19322a7d4075e65361c2b1570a568746849e883f 100644 --- a/userprog/syscall.c +++ b/userprog/syscall.c @@ -58,15 +58,15 @@ syscall_handler (struct intr_frame *f UNUSED) case SYSCALL_HALT: syscall_halt(f); break; - case SYSCALL_EXIT: - syscall_exit(f); - break; case SYSCALL_EXEC: syscall_exec(f); break; case SYSCALL_WAIT: syscall_exec(f); break; + case SYSCALL_OPEN: + syscall_open(f); + break; default: printf ("WARNING: Invalid Syscall (%d)\n", syscall_number); } diff --git a/userprog/syscall_exit.c b/userprog/syscall_exit.c deleted file mode 100644 index 4441a2715b4c4757fe6a9bb8ac41be89ca85c839..0000000000000000000000000000000000000000 --- a/userprog/syscall_exit.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "system_calls.h" -#include "threads/thread.h" - -void syscall_exit(struct intr_frame *status) { - struct thread *current_thread; // creates thread struct from thread.h - current_thread = thread_current(); // Sets current thread - - current_thread->status = status; // Returning status to kernel - thread_exit(); // Exiting current thread - return -1; // Returning -1 for success -} diff --git a/userprog/syscall_open.c b/userprog/syscall_open.c new file mode 100644 index 0000000000000000000000000000000000000000..dcec481c0362416c23e88a15a9bfde5121464af0 --- /dev/null +++ b/userprog/syscall_open.c @@ -0,0 +1,21 @@ +/* + * Opens the file called file. Returns a nonnegative integer handle called a + * "file descriptor" (fd), or -1 if the file could not be opened. + * + * Authored by Alex Stratford + */ + + +#include "system_calls.h" +#include "threads/interrupt.h" // Dependency for intr_frame struct +#include "filesys/file.h" // Dependency for file_open and file struct + +void syscall_open(struct intr_frame *f) { + // pop off first int argument from interrupt frame + struct file_map *f_map->file_descriptor = *((int*)f->esp + 1); + // Described in system_calls.h, opens the file + f_map->file = file_open(file_descriptor); + if (f_map->file == NULL) // Checking if file is empty or non-existent + f->eax = -1; // Returning a failure state + f->eax = f_map->file_descriptor; // Returning the file descriptor +} \ No newline at end of file diff --git a/userprog/system_calls.h b/userprog/system_calls.h index 7d31857b1f87c8bf0cdad8df434716acae9ec434..6246058bb5069bdd9f7df19555a1d8b58afcf2a6 100644 --- a/userprog/system_calls.h +++ b/userprog/system_calls.h @@ -1,4 +1,16 @@ #include "threads/interrupt.h" +#include "lib/kernel/list.h" // Added due to dependency for list_elem +#include "filesys/file.h" // Added due to dependency for file + + +// Maps file descriptions to the associated file structure +struct file_map +{ + struct list_elem list_element; // Defined in list.h + int file_descriptor; + struct file *file; // Defined in file.c +}; + /* * Terminates Pintos by calling shutdown_power_off() @@ -6,14 +18,6 @@ */ void syscall_halt(struct intr_frame *f); -/* - * Terminates the current user program, returning status to the kernel. If the - * process's parent waits for it (see below), this is the status that will be - * returned. Conventionally, a status of 0 indicates success and nonzero - * values indicate errors. - */ -void syscall_exit(struct intr_frame *status); - /* * Runs the executable whose name is given in cmd_line, passing any given * arguments, and returns the new process's program id (pid). Must return pid @@ -30,6 +34,7 @@ void syscall_exec(struct intr_frame *f); void syscall_wait(struct intr_frame *f); /* - * NOTE: There are more system calls implemented by Pintos but we are not - * implementing them because the assignment brief does not ask of it. - */ \ No newline at end of file + * Opens the file called file. Returns a nonnegative integer handle called a + * "file descriptor" (fd), or -1 if the file could not be opened. + */ +void syscall_open(struct intr_frame *f); \ No newline at end of file