Skip to content
Snippets Groups Projects
Verified Commit cb1f903f authored by ja3-saxby's avatar ja3-saxby
Browse files

Implemented read()

Tested this at least works on the console by running the shell program
it works
parent 0e54903f
Branches
No related tags found
2 merge requests!22Merge Feature/system calls,!18Implement read() system call
...@@ -3,10 +3,40 @@ ...@@ -3,10 +3,40 @@
* *
* Authored by Joshua Saxby * Authored by Joshua Saxby
*/ */
#include "filesys/filesys.h" #include <stddef.h>
#include "devices/input.h"
#include "filesys/file.h"
#include "system_calls.h" #include "system_calls.h"
#include "threads/interrupt.h" #include "threads/interrupt.h"
void syscall_read(struct intr_frame *f) { void syscall_read(struct intr_frame *f) {
(void*)0; // 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;
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment