forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmv.c
48 lines (39 loc) · 953 Bytes
/
mv.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
44
45
46
47
48
#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: mv source destination\n");
exit();
}
if((fd_src = open(argv[1], O_RDONLY)) < 0){
printf(2, "mv: cannot open %s\n", argv[1]);
exit();
}
if((fd_dest = open(argv[2], O_WRONLY | O_CREATE)) < 0){
printf(2, "mv: 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, "mv: write error\n");
close(fd_src);
close(fd_dest);
exit();
}
}
close(fd_src);
close(fd_dest);
if(unlink(argv[1]) < 0){
printf(2, "mv: cannot delete %s\n", argv[1]);
exit();
}
exit();
}