forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcp.c
43 lines (35 loc) · 847 Bytes
/
cp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
#define BUF_SIZE 512
int
main(int argc, char *argv[])
{
int fd_src, fd_dest, n;
char buf[BUF_SIZE];
if(argc != 3){
printf(2, "Usage: cp source destination\n");
exit();
}
if((fd_src = open(argv[1], O_RDONLY)) < 0){
printf(2, "cp: cannot open %s\n", argv[1]);
exit();
}
if((fd_dest = open(argv[2], O_WRONLY | O_CREATE)) < 0){
printf(2, "cp: cannot create %s\n", argv[2]);
close(fd_src);
exit();
}
while((n = read(fd_src, buf, sizeof(buf))) > 0){
if(write(fd_dest, buf, n) != n){
printf(2, "cp: write error\n");
close(fd_src);
close(fd_dest);
exit();
}
}
close(fd_src);
close(fd_dest);
exit();
}