Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool create(const char *file, unsigned initial_size){
- return filesys_create(file, (off_t)initial_size);
- }
- int open(const char *file){
- struct file* name = filesys_open(file);
- if (name != NULL){
- for (int i = 0; i < 128; i++) {
- if(thread_current()->files[i] == NULL){
- thread_current()->files[i] = name;
- return i + 2;
- }
- }
- }
- return -1;
- }
- void close(int fd){
- struct file* openFile = thread_current()->files[fd - 2];
- file_close(openFile);
- thread_current()->files[fd - 2] = NULL;
- }
- int read(int fd, void *buffer, unsigned size){
- if(fd == 0){
- for (int i = 0; i < 10; i++){
- *((char*)buffer + i) = input_getc();
- }
- return size;
- }
- if (fd < 2 || fd > 128){
- return -1;
- }
- else {
- struct file* open_file = thread_current()->files[fd - 2];
- if(open_file){
- return file_read(open_file, buffer, size);
- }
- }
- return -1;
- }
- int write(int fd, const void *buffer, unsigned size){
- if (fd == 1){
- putbuf(buffer, size);
- return size;
- }
- struct file* target_file = thread_current()->files[fd - 2];
- if (!target_file){
- return -1;
- }
- return file_write (target_file, buffer, size);
- }
- void exit(int status){
- for (size_t i = 0; i < 128; i++) {
- if(thread_current()->files[i]){
- close(i + 2);
- }
- }
- }
- void* get_arg_v(void* esp, int argnr){
- return esp + (4 * argnr);
- }
- static void
- syscall_handler (struct intr_frame *f)
- {
- int syscall_nr = *((int*)f->esp);
- int* args = (int*)f->esp;
- switch (syscall_nr) {
- case SYS_HALT:
- halt();
- break;
- case SYS_CREATE:
- ; //A label can only be part of a statement and a declaration is not a statement.
- char *file = *(char**)(args + 1);
- unsigned initial_size = *((unsigned*)(args + 2));
- bool success = create(file, initial_size);
- f->eax = success;
- break;
- case SYS_OPEN:
- f->eax = open(*(char**)(args+1));;
- break;
- case SYS_CLOSE:
- close(*(int*)(args + 1));
- break;
- case SYS_READ:
- f->eax = read(*(int*)(args + 1), (void*)(args + 2), *(unsigned*)(args + 3));
- break;
- case SYS_WRITE:
- f->eax = write(*((int*)(args + 1)), *(int*)(args + 2), *((unsigned*)(args + 3)));
- break;
- case SYS_EXIT:
- exit(*(int*)(args + 1));
- break;
- }
- // printf ("system call!\n");
- // thread_exit ();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement