Move problem 1-2 (join) into project 2 as the "wait" system call.
[pintos-anon] / grading / vm / posix-compat.c
1 #include <dirent.h>
2 #include <round.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/mman.h>
7 #include <sys/stat.h>
8 #include <sys/wait.h>
9 #include "posix-compat.h"
10
11 #undef halt
12 #undef exit
13 #undef exec
14 #undef wait
15 #undef create
16 #undef remove
17 #undef open
18 #undef filesize
19 #undef read
20 #undef write
21 #undef seek
22 #undef tell
23 #undef close
24 #undef mmap
25 #undef munmap
26 #undef chdir
27 #undef mkdir
28 #undef lsdir
29
30 void
31 pintos_halt (void) 
32 {
33   exit (-1);
34 }
35
36 void
37 pintos_exit (int status) 
38 {
39   if (status < -127 || status > 255)
40     printf ("warning: non-POSIX exit code %d\n", status);
41   exit (status);
42 }
43
44 pid_t
45 pintos_exec (const char *cmd_line) 
46 {
47   pid_t child = fork ();
48   if (child == -1)
49     return child;
50   else if (child == 0) 
51     {
52       char copy[1024];
53       char *args[128];
54       char **argp;
55
56       strcpy (copy, cmd_line);
57
58       argp = args;
59       *argp = strtok (copy, " ");
60       while (*argp++ != NULL) 
61         *argp = strtok (NULL, " ");
62
63       execv (args[0], args);
64       abort ();
65     }
66   else
67     return child;
68 }
69
70 int
71 pintos_wait (pid_t child) 
72 {
73   int status = 0;
74   waitpid (child, &status, 0);
75   return WIFEXITED (status) ? WEXITSTATUS (status) : -1;
76 }
77
78 bool
79 pintos_create (const char *file, unsigned initial_size) 
80 {
81   int fd = open (file, O_RDWR | O_CREAT | O_EXCL, 0666);
82   if (fd < 0)
83     return false;
84   ftruncate (fd, initial_size);
85   close (fd);
86   return true;
87 }
88
89 bool
90 pintos_remove (const char *file) 
91 {
92   return unlink (file) == 0;
93 }
94
95 int
96 pintos_open (const char *file) 
97 {
98   return open (file, O_RDWR);
99 }
100
101 int
102 pintos_filesize (int fd) 
103 {
104   struct stat s;
105   fstat (fd, &s);
106   return s.st_size;
107 }
108
109 int
110 pintos_read (int fd, void *buffer, unsigned length) 
111 {
112   return read (fd, buffer, length);
113 }
114
115 int
116 pintos_write (int fd, const void *buffer, unsigned length) 
117 {
118   return write (fd, buffer, length);
119 }
120
121 void
122 pintos_seek (int fd, unsigned position) 
123 {
124   lseek (fd, position, SEEK_SET);
125 }
126
127
128 unsigned
129 pintos_tell (int fd) 
130 {
131   return lseek (fd, 0, SEEK_CUR);
132 }
133
134 void
135 pintos_close (int fd) 
136 {
137   close (fd);
138 }
139
140 mapid_t
141 pintos_mmap (int fd, void *addr) 
142 {
143   size_t page_size = getpagesize ();
144   int length = pintos_filesize (fd);
145   uintptr_t ptr = (uintptr_t) mmap (addr, length, PROT_READ | PROT_WRITE,
146                                     MAP_SHARED | MAP_FIXED, fd, 0);
147   return ptr | DIV_ROUND_UP (size, page_size);
148 }
149
150 bool
151 pintos_munmap (void *addr, unsigned length) 
152 {
153   size_t page_size = getpagesize ();
154   uintptr_t page_mask = page_size - 1;
155   void *base = (void *) ((uintptr_t) addr & ~page_mask);
156   size_t length = ((uintptr_t) addr & page_mask) * page_size;
157   return munmap (base, length);
158 }
159
160 bool
161 pintos_chdir (const char *dir) 
162 {
163   return chdir (dir) == 0;
164 }
165
166 bool
167 pintos_mkdir (const char *dir) 
168 {
169   return mkdir (dir, 0777) == 0;
170 }
171
172 void
173 pintos_lsdir (void) 
174 {
175   DIR *dir = opendir (".");
176   if (dir != NULL) 
177     {
178       struct dirent *de;
179       while ((de = readdir (dir)) != NULL) 
180         {
181           if (!strcmp (de->d_name, ".") || !strcmp (de->d_name, ".."))
182             continue;
183           printf ("%s\n", de->d_name);
184         }
185     }
186   closedir (dir);
187 }