dc5b5ab668798ce05c01c0b7000950cb8acb0286
[pintos-anon] / grading / vm / posix-compat.c
1 #include <dirent.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/mman.h>
6 #include <sys/stat.h>
7 #include <sys/wait.h>
8 #include "posix-compat.h"
9
10 #undef halt
11 #undef exit
12 #undef exec
13 #undef join
14 #undef create
15 #undef remove
16 #undef open
17 #undef filesize
18 #undef read
19 #undef write
20 #undef seek
21 #undef tell
22 #undef close
23 #undef mmap
24 #undef munmap
25 #undef chdir
26 #undef mkdir
27 #undef lsdir
28
29 void
30 pintos_halt (void) 
31 {
32   exit (-1);
33 }
34
35 void
36 pintos_exit (int status) 
37 {
38   if (status < -127 || status > 255)
39     printf ("warning: non-POSIX exit code %d\n", status);
40   exit (status);
41 }
42
43 pid_t
44 pintos_exec (const char *cmd_line) 
45 {
46   pid_t child = fork ();
47   if (child == -1)
48     return child;
49   else if (child == 0) 
50     {
51       char copy[1024];
52       char *args[128];
53       char **argp;
54
55       strcpy (copy, cmd_line);
56
57       argp = args;
58       *argp = strtok (copy, " ");
59       while (*argp++ != NULL) 
60         *argp = strtok (NULL, " ");
61
62       execv (args[0], args);
63       abort ();
64     }
65   else
66     return child;
67 }
68
69 int
70 pintos_join (pid_t child) 
71 {
72   int status = 0;
73   waitpid (child, &status, 0);
74   return WIFEXITED (status) ? WEXITSTATUS (status) : -1;
75 }
76
77 bool
78 pintos_create (const char *file, unsigned initial_size) 
79 {
80   int fd = open (file, O_RDWR | O_CREAT | O_EXCL, 0666);
81   if (fd < 0)
82     return false;
83   ftruncate (fd, initial_size);
84   close (fd);
85   return true;
86 }
87
88 bool
89 pintos_remove (const char *file) 
90 {
91   return unlink (file) == 0;
92 }
93
94 int
95 pintos_open (const char *file) 
96 {
97   return open (file, O_RDWR);
98 }
99
100 int
101 pintos_filesize (int fd) 
102 {
103   off_t old = lseek (fd, 0, SEEK_CUR);
104   off_t size = lseek (fd, 0, SEEK_END);
105   lseek (fd, old, SEEK_SET);
106   return 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 bool
141 pintos_mmap (int fd, void *addr, unsigned length) 
142 {
143   return mmap (addr, length, PROT_READ | PROT_WRITE,
144                MAP_SHARED | MAP_FIXED, fd, 0) == addr;
145 }
146
147 bool
148 pintos_munmap (void *addr, unsigned length) 
149 {
150   return munmap (addr, length) == 0;
151 }
152
153 bool
154 pintos_chdir (const char *dir) 
155 {
156   return chdir (dir) == 0;
157 }
158
159 bool
160 pintos_mkdir (const char *dir) 
161 {
162   return mkdir (dir, 0777) == 0;
163 }
164
165 void
166 pintos_lsdir (void) 
167 {
168   DIR *dir = opendir (".");
169   if (dir != NULL) 
170     {
171       struct dirent *de;
172       while ((de = readdir (dir)) != NULL) 
173         {
174           if (!strcmp (de->d_name, ".") || !strcmp (de->d_name, ".."))
175             continue;
176           printf ("%s\n", de->d_name);
177         }
178     }
179   closedir (dir);
180 }