Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. bool create(const char *file, unsigned initial_size){
  2.     return filesys_create(file, (off_t)initial_size);
  3. }
  4.  
  5. int open(const char *file){
  6.     struct file* name = filesys_open(file);
  7.     if (name != NULL){
  8.         for (int i = 0; i < 128; i++) {
  9.           if(thread_current()->files[i] == NULL){
  10.             thread_current()->files[i] = name;
  11.             return i + 2;
  12.           }
  13.         }
  14.     }
  15.     return -1;
  16. }
  17.  
  18. void close(int fd){
  19.     struct file* openFile = thread_current()->files[fd - 2];
  20.     file_close(openFile);
  21.     thread_current()->files[fd - 2] = NULL;
  22. }
  23.  
  24. int read(int fd, void *buffer, unsigned size){
  25.     if(fd == 0){
  26.         for (int i = 0; i < 10; i++){
  27.             *((char*)buffer + i) = input_getc();
  28.         }
  29.         return size;
  30.     }
  31.     if (fd < 2 || fd > 128){
  32.         return -1;
  33.     }
  34.     else {
  35.         struct file* open_file = thread_current()->files[fd - 2];
  36.         if(open_file){
  37.             return file_read(open_file, buffer, size);
  38.         }
  39.     }
  40.     return -1;
  41. }
  42.  
  43. int write(int fd, const void *buffer, unsigned size){
  44.     if (fd == 1){
  45.         putbuf(buffer, size);
  46.         return size;
  47.     }
  48.     struct file* target_file = thread_current()->files[fd - 2];
  49.     if (!target_file){
  50.       return -1;
  51.     }
  52.     return file_write (target_file, buffer, size);
  53. }
  54.  
  55. void exit(int status){
  56.     for (size_t i = 0; i < 128; i++) {
  57.       if(thread_current()->files[i]){
  58.         close(i + 2);
  59.       }
  60.     }
  61. }
  62.  
  63. void* get_arg_v(void* esp, int argnr){
  64.     return esp + (4 * argnr);
  65. }
  66.  
  67.  
  68. static void
  69. syscall_handler (struct intr_frame *f)
  70. {
  71.     int syscall_nr = *((int*)f->esp);
  72.     int* args = (int*)f->esp;
  73.     switch (syscall_nr) {
  74.  
  75.       case SYS_HALT:
  76.           halt();
  77.           break;
  78.  
  79.       case SYS_CREATE:
  80.           ; //A label can only be part of a statement and a declaration is not a statement.
  81.           char *file = *(char**)(args + 1);
  82.           unsigned initial_size = *((unsigned*)(args + 2));
  83.           bool success = create(file, initial_size);
  84.           f->eax = success;
  85.           break;
  86.  
  87.       case SYS_OPEN:
  88.           f->eax = open(*(char**)(args+1));;
  89.           break;
  90.  
  91.       case SYS_CLOSE:
  92.           close(*(int*)(args + 1));
  93.           break;
  94.  
  95.       case SYS_READ:
  96.           f->eax = read(*(int*)(args + 1), (void*)(args + 2), *(unsigned*)(args + 3));
  97.           break;
  98.  
  99.       case SYS_WRITE:
  100.           f->eax = write(*((int*)(args + 1)), *(int*)(args + 2), *((unsigned*)(args + 3)));
  101.           break;
  102.  
  103.       case SYS_EXIT:
  104.           exit(*(int*)(args + 1));
  105.           break;
  106.  
  107.  
  108.     }
  109.   // printf ("system call!\n");
  110.   // thread_exit ();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement