Skip to content
Snippets Groups Projects
Commit 898ca630 authored by a2-stratford's avatar a2-stratford
Browse files

Merge branch 'josh/22-read-syscall' into 'feature/system-calls'

Implement read() system call

See merge request !18
parents 26a45135 b6f4ad10
Branches
No related tags found
2 merge requests!22Merge Feature/system calls,!18Implement read() system call
...@@ -71,6 +71,7 @@ userprog_SRC += userprog/syscall_create.c ...@@ -71,6 +71,7 @@ userprog_SRC += userprog/syscall_create.c
userprog_SRC += userprog/syscall_remove.c userprog_SRC += userprog/syscall_remove.c
userprog_SRC += userprog/syscall_write.c userprog_SRC += userprog/syscall_write.c
userprog_SRC += userprog/file_descriptors_map.c userprog_SRC += userprog/file_descriptors_map.c
userprog_SRC += userprog/syscall_read.c
# No virtual memory code yet. # No virtual memory code yet.
#vm_SRC = vm/file.c # Some file. #vm_SRC = vm/file.c # Some file.
......
...@@ -79,6 +79,9 @@ syscall_handler (struct intr_frame *f UNUSED) ...@@ -79,6 +79,9 @@ syscall_handler (struct intr_frame *f UNUSED)
case SYSCALL_OPEN: case SYSCALL_OPEN:
syscall_open(f); syscall_open(f);
break; break;
case SYSCALL_READ:
syscall_read(f);
break;
case SYSCALL_FILESIZE: case SYSCALL_FILESIZE:
syscall_filesize(f); syscall_filesize(f);
break; break;
......
/*
* The Read System Call
*
* Authored by Joshua Saxby
*/
#include <stddef.h>
#include "devices/input.h"
#include "filesys/file.h"
#include "system_calls.h"
#include "threads/interrupt.h"
void syscall_read(struct intr_frame *f) {
// first argument is syscall code (already handled)
int fd = *((int*)f->esp + 1); // file descriptor is second argument
char* buffer = (void*)(*((int*)f->esp + 2)); // buffer is third argument
unsigned size = *((unsigned*)((int*)f->esp + 3)); // size to read is fourth
// reading from stdin (keyboard) is a special case
switch (fd) {
case 0: {
// read from keyboard for as many bytes as requested
for (size_t i = 0; i < size; i++) {
*(buffer + i) = input_getc();
}
f->eax = size;
break;
}
case 1: // stdout
case 2: // stderr
f->eax = -1; // it is a mistake to attempt to read from stdout or stderr
break;
default: {
// otherwise, we need to read from a file denoted by fd
struct file* file_to_read = get_associated_file_pointer(fd);
if (file_to_read == NULL) {
f->eax = -1; // invalid file descriptor
break;
}
f->eax = file_read(file_to_read, buffer, size);
break;
}
}
}
...@@ -36,6 +36,13 @@ void syscall_wait(struct intr_frame *f); ...@@ -36,6 +36,13 @@ void syscall_wait(struct intr_frame *f);
*/ */
void syscall_create(struct intr_frame *f); void syscall_create(struct intr_frame *f);
/*
* Reads size bytes from the file open as fd into buffer.
* Returns the number of bytes actually read (0 at end of file), or -1 if the
* file could not be read (due to a condition other than end of file).
*/
void syscall_read(struct intr_frame *f);
/* /*
* Writes size bytes from buffer to the open file fd. * Writes size bytes from buffer to the open file fd.
* Returns the number of bytes actually written, which may be less than size if * Returns the number of bytes actually written, which may be less than size if
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment