Add some more examples.
[pintos-anon] / src / examples / mcp.c
1 /* cat.c
2
3    Copies one file to another, using mmap. */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <syscall.h>
8
9 int
10 main (int argc, char *argv[]) 
11 {
12   int in_fd, out_fd;
13   mapid_t in_map, out_map;
14   void *in_data = (void *) 0x10000000;
15   void *out_data = (void *) 0x20000000;
16   int size;
17
18   if (argc != 3) 
19     {
20       printf ("usage: cp OLD NEW\n");
21       return 1;
22     }
23
24   /* Open input file. */
25   in_fd = open (argv[1]);
26   if (in_fd < 0) 
27     {
28       printf ("%s: open failed\n", argv[1]);
29       return 1;
30     }
31   size = filesize (in_fd);
32
33   /* Create and open output file. */
34   if (!create (argv[2], size)) 
35     {
36       printf ("%s: create failed\n", argv[2]);
37       return 1;
38     }
39   out_fd = open (argv[2]);
40   if (out_fd < 0) 
41     {
42       printf ("%s: open failed\n", argv[2]);
43       return 1;
44     }
45
46   /* Map files. */
47   in_map = mmap (in_fd, in_data);
48   if (in_map == MAP_FAILED) 
49     {
50       printf ("%s: mmap failed\n", argv[1]);
51       return 1;
52     }
53   out_map = mmap (out_fd, out_data);
54   if (out_map == MAP_FAILED)
55     {
56       printf ("%s: mmap failed\n", argv[2]);
57       return 1;
58     }
59
60   /* Copy files. */
61   memcpy (out_data, in_data, size);
62
63   /* Unmap files (optional). */
64   munmap (in_map);
65   munmap (out_map);
66
67   return 0;
68 }