9 #include "posix-compat.h"
37 pintos_exit (int status)
39 if (status < -127 || status > 255)
40 printf ("warning: non-POSIX exit code %d\n", status);
45 pintos_exec (const char *cmd_line)
47 pid_t child = fork ();
56 strcpy (copy, cmd_line);
59 *argp = strtok (copy, " ");
60 while (*argp++ != NULL)
61 *argp = strtok (NULL, " ");
63 execv (args[0], args);
71 pintos_wait (pid_t child)
74 waitpid (child, &status, 0);
75 return WIFEXITED (status) ? WEXITSTATUS (status) : -1;
79 pintos_create (const char *file, unsigned initial_size)
81 int fd = open (file, O_RDWR | O_CREAT | O_EXCL, 0666);
84 ftruncate (fd, initial_size);
90 pintos_remove (const char *file)
92 return unlink (file) == 0;
96 pintos_open (const char *file)
98 return open (file, O_RDWR);
102 pintos_filesize (int fd)
110 pintos_read (int fd, void *buffer, unsigned length)
112 return read (fd, buffer, length);
116 pintos_write (int fd, const void *buffer, unsigned length)
118 return write (fd, buffer, length);
122 pintos_seek (int fd, unsigned position)
124 lseek (fd, position, SEEK_SET);
131 return lseek (fd, 0, SEEK_CUR);
135 pintos_close (int fd)
141 pintos_mmap (int fd, void *addr)
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);
151 pintos_munmap (void *addr, unsigned length)
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);
161 pintos_chdir (const char *dir)
163 return chdir (dir) == 0;
167 pintos_mkdir (const char *dir)
169 return mkdir (dir, 0777) == 0;
175 DIR *dir = opendir (".");
179 while ((de = readdir (dir)) != NULL)
181 if (!strcmp (de->d_name, ".") || !strcmp (de->d_name, ".."))
183 printf ("%s\n", de->d_name);