Some initial VM tests.
[pintos-anon] / grading / vm / child-sort.c
1 #include <stdio.h>
2 #ifdef PINTOS
3 #include <syscall.h>
4 #else
5 #include <unistd.h>
6 #include <fcntl.h>
7 #endif
8
9 unsigned char buf[65536];
10 size_t histogram[256];
11
12 int
13 main (void) 
14 {
15   int fd;
16   unsigned char *p;
17   size_t size;
18   size_t i;
19   
20 #ifdef PINTOS
21   fd = open ("buffer");
22 #else
23   fd = open ("buffer", O_RDWR);
24 #endif
25   if (fd < 0) 
26     {
27       printf ("(child-sort) open() failed\n");
28       return 1;
29     }
30
31   size = read (fd, buf, sizeof buf);
32   for (i = 0; i < size; i++)
33     histogram[buf[i]]++;
34   p = buf;
35   for (i = 0; i < sizeof histogram / sizeof *histogram; i++) 
36     {
37       size_t j = histogram[i];
38       while (j-- > 0)
39         *p++ = i;
40     }
41 #ifdef PINTOS
42   seek (fd, 0);
43 #else
44   lseek (fd, 0, SEEK_SET);
45 #endif
46   write (fd, buf, size);
47   close (fd);
48   
49   return 0x123;
50 }