7f0137aa94086782bb37e39eba4afd4499812df2
[pintos-anon] / grading / filesys / child-syn-rw.c
1 #include <random.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <syscall.h>
6 #include "fslib.h"
7 #include "syn-rw.h"
8
9 const char test_name[128] = "child-syn-rw";
10
11 static char buf1[BUF_SIZE];
12 static char buf2[BUF_SIZE];
13
14 int
15 main (int argc, const char *argv[]) 
16 {
17   int child_idx;
18   int fd;
19   size_t ofs;
20
21   quiet = true;
22   
23   CHECK (argc == 2, "argc must be 2, actually %d", argc);
24   child_idx = atoi (argv[1]);
25
26   random_init (0);
27   random_bytes (buf1, sizeof buf1);
28
29   CHECK ((fd = open (filename)) > 1, "open \"%s\"", filename);
30   ofs = 0;
31   while (ofs < sizeof buf2)
32     {
33       int bytes_read = read (fd, buf2 + ofs, sizeof buf2 - ofs);
34       CHECK (bytes_read >= -1 && bytes_read <= (int) (sizeof buf2 - ofs),
35              "%zu-byte read on \"%s\" returned invalid value of %d",
36              sizeof buf2 - ofs, filename, bytes_read);
37       if (bytes_read > 0) 
38         {
39           compare_bytes (buf2 + ofs, buf1 + ofs, bytes_read, ofs, filename);
40           ofs += bytes_read;
41         }
42     }
43   close (fd);
44
45   return child_idx;
46 }