Wording.
[pintos.git] / src / examples / cp.c
blob0c8c1a5d673c98fba8af1e9a87dccfc2c71c9150
1 /* cat.c
3 Copies one file to another. */
5 #include <stdio.h>
6 #include <syscall.h>
8 int
9 main (int argc, char *argv[])
11 int in_fd, out_fd;
13 if (argc != 3)
15 printf ("usage: cp OLD NEW\n");
16 return 1;
19 /* Open input file. */
20 in_fd = open (argv[1]);
21 if (in_fd < 0)
23 printf ("%s: open failed\n", argv[1]);
24 return 1;
27 /* Create and open output file. */
28 if (!create (argv[2], filesize (in_fd)))
30 printf ("%s: create failed\n", argv[2]);
31 return 1;
33 out_fd = open (argv[2]);
34 if (out_fd < 0)
36 printf ("%s: open failed\n", argv[2]);
37 return 1;
40 /* Copy data. */
41 for (;;)
43 char buffer[1024];
44 int bytes_read = read (in_fd, buffer, sizeof buffer);
45 if (bytes_read == 0)
46 break;
47 if (write (out_fd, buffer, bytes_read) != bytes_read)
49 printf ("%s: write failed\n", argv[2]);
50 return 1;
54 return 0;