diff options
Diffstat (limited to 'src/userprog')
| -rw-r--r-- | src/userprog/syscall.c | 57 |
1 files changed, 21 insertions, 36 deletions
diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index dc1f067..921ed44 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -37,7 +37,7 @@ open (const char *filename) struct thread *t = thread_current (); if (!t->fds) { - t->fds = (struct fd *) calloc (MAX_FDS, sizeof (struct fd)); + t->fds = (struct file **) calloc (MAX_FDS, sizeof (struct file *)); } struct file *file = filesys_open (filename); @@ -46,10 +46,9 @@ open (const char *filename) } for (int i = 0; i < MAX_FDS; i++) { - struct fd *fd = t->fds + i; - if (!fd->active) { - fd->active = true; - fd->file = file; + struct file **fd = t->fds + i; + if (*fd == NULL) { + *fd = file; return i + 2; // 0 and 1 are reserved for stdin and stdout } } @@ -57,7 +56,7 @@ open (const char *filename) return -1; } -static struct fd * +static struct file ** get_fd (struct thread *thread, int fd_i) { if (!thread->fds) { @@ -67,18 +66,6 @@ get_fd (struct thread *thread, int fd_i) return thread->fds + fd_i - 2; // -2 since 0 and 1 are reserved and not present in the array } -static struct file * -get_file (struct thread *thread, int fd_i) -{ - struct fd *fd = get_fd (thread, fd_i); - - if (!fd || !fd->active) { - return NULL; - } - - return fd->file; -} - static void exit (int status) { @@ -86,10 +73,10 @@ exit (int status) if (thread->fds) { for (int i = 0; i < MAX_FDS; i++) { - struct fd *fd = thread->fds + i; - if (fd && fd->active) { - file_close (fd->file); - fd->active = false; + struct file **fd = thread->fds + i; + if (fd && *fd) { + file_close (*fd); + *fd = NULL; } } free (thread->fds); @@ -113,12 +100,12 @@ read (int fd_i, void *buf, unsigned size) return -1; } - struct file *file = get_file (thread, fd_i); - if (!file) { + struct file **fd = get_fd (thread, fd_i); + if (fd && *fd) { + return file_read (*fd, buf, size); + } else { return -1; } - - return file_read (file, buf, size); } static int @@ -135,12 +122,12 @@ write (int fd_i, const void *buf, unsigned size) return -1; } - struct file *file = get_file (thread, fd_i); - if (!file) { + struct file **fd = get_fd (thread, fd_i); + if (fd && *fd) { + return file_write (*fd, buf, size); + } else { return -1; } - - return file_write (file, buf, size); } static void @@ -148,13 +135,11 @@ close (int fd_i) { struct thread *thread = thread_current (); - struct fd *fd = get_fd (thread, fd_i); - if (!fd || !fd->active) { - return; + struct file **fd = get_fd (thread, fd_i); + if (fd && *fd) { + file_close (*fd); + *fd = NULL; } - - file_close (fd->file); - fd->active = false; } // cast to TYPE and deref argument N from f->esp |
