PKUOS - Pintos
Pintos source browser for PKU Operating System course
cp.c
Go to the documentation of this file.
1/** cat.c
2
3Copies one file to another. */
4
5#include <stdio.h>
6#include <syscall.h>
7
8int
9main (int argc, char *argv[])
10{
11 int in_fd, out_fd;
12
13 if (argc != 3)
14 {
15 printf ("usage: cp OLD NEW\n");
16 return EXIT_FAILURE;
17 }
18
19 /* Open input file. */
20 in_fd = open (argv[1]);
21 if (in_fd < 0)
22 {
23 printf ("%s: open failed\n", argv[1]);
24 return EXIT_FAILURE;
25 }
26
27 /* Create and open output file. */
28 if (!create (argv[2], filesize (in_fd)))
29 {
30 printf ("%s: create failed\n", argv[2]);
31 return EXIT_FAILURE;
32 }
33 out_fd = open (argv[2]);
34 if (out_fd < 0)
35 {
36 printf ("%s: open failed\n", argv[2]);
37 return EXIT_FAILURE;
38 }
39
40 /* Copy data. */
41 for (;;)
42 {
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)
48 {
49 printf ("%s: write failed\n", argv[2]);
50 return EXIT_FAILURE;
51 }
52 }
53
54 return EXIT_SUCCESS;
55}
int main(int argc, char *argv[])
cat.c
Definition: cp.c:9
static struct intq buffer
Stores keys from the keyboard and serial port.
Definition: input.c:7
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
int filesize(int fd)
Definition: syscall.c:109
bool create(const char *file, unsigned initial_size)
Definition: syscall.c:91
int open(const char *file)
Definition: syscall.c:103
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
int read(int fd, void *buffer, unsigned size)
Definition: syscall.c:115
#define EXIT_SUCCESS
Typical return values from main() and arguments to exit().
Definition: syscall.h:19
#define EXIT_FAILURE
Unsuccessful execution.
Definition: syscall.h:20