-
Notifications
You must be signed in to change notification settings - Fork 46
/
mv.c
66 lines (57 loc) · 1.35 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#define BUFFER_SIZ 256
static char buffer[BUFFER_SIZ];
int main(int argc, char *argv[]){
int ret;
struct stat statbuf, dest_statbuf;
char *path, *dest;
if(argc < 3){
fprintf(stderr, "usage: mv [SRC] [DEST]\n");
goto fail;
}
path = *++argv;
dest = *++argv;
ret = stat(path, &statbuf);
if(ret){
perror(path);
goto fail;
}
ret = stat(dest, &dest_statbuf);
if(ret == 0){
if(!S_ISREG(dest_statbuf.st_mode)){
fprintf(stderr, "dest not regular file\n");
goto fail;
}
while(1){
printf("overwrite %s (y/n)? ", dest);
ret = read(STDIN_FILENO, buffer, BUFFER_SIZ * sizeof(char));
if(buffer[0] == 'n'){
return 0;
}else if(buffer[0] == 'y'){
ret = unlink(dest);
if(ret){
perror("cant overwrite");
goto fail;
}
break;
}else{
printf("plz type y or n\n");
}
}
}
ret = link(path, dest);
if(ret){
perror("cant link");
goto fail;
}
ret = unlink(path);
if(ret){
perror("unlink");
goto fail;
}
return 0;
fail:
return 1;
}