CS318 - Pintos
Pintos source browser for JHU CS318 course
mcp.c
Go to the documentation of this file.
1 /** mcp.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 EXIT_FAILURE;
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 EXIT_FAILURE;
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 EXIT_FAILURE;
38  }
39  out_fd = open (argv[2]);
40  if (out_fd < 0)
41  {
42  printf ("%s: open failed\n", argv[2]);
43  return EXIT_FAILURE;
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 EXIT_FAILURE;
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 EXIT_FAILURE;
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 EXIT_SUCCESS;
68 }
string.h
memcpy
void * memcpy(void *dst_, const void *src_, size_t size)
Copies SIZE bytes from SRC to DST, which must not overlap.
Definition: string.c:7
EXIT_FAILURE
#define EXIT_FAILURE
Unsuccessful execution.
Definition: syscall.h:20
MAP_FAILED
#define MAP_FAILED
Definition: syscall.h:13
printf
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
open
int open(const char *file)
Definition: syscall.c:103
EXIT_SUCCESS
#define EXIT_SUCCESS
Typical return values from main() and arguments to exit().
Definition: syscall.h:19
filesize
int filesize(int fd)
Definition: syscall.c:109
main
int main(int argc, char *argv[])
mcp.c
Definition: mcp.c:10
mmap
mapid_t mmap(int fd, void *addr)
Project 3 and optionally project 4.
Definition: syscall.c:145
munmap
void munmap(mapid_t mapid)
Definition: syscall.c:151
mapid_t
int mapid_t
Map region identifier.
Definition: syscall.h:12
create
bool create(const char *file, unsigned initial_size)
Definition: syscall.c:91