-
Notifications
You must be signed in to change notification settings - Fork 0
/
mv1.c
56 lines (50 loc) · 1.05 KB
/
mv1.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
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
void do_dir(char *path1,char *path2);
void do_file(char *path1,char *path2);
int main(int argc,char *argv[])
{
struct stat fileinfo;
if(argc != 3){
fprintf(stderr,"mv:Usage source dst");
exit(1);
}
stat(argv[1],&fileinfo);
if((access(argv[1],F_OK)==0) && S_ISREG(fileinfo.st_mode)){
stat(argv[2],&fileinfo);
if(S_ISDIR(fileinfo.st_mode))
do_dir(argv[1],argv[2]);
else
do_file(argv[1],argv[2]);
}
else{
fprintf(stderr,"source not common file");
exit(1);
}
}
void do_file(char *path1,char *path2)
{
if(access(path2,F_OK) == 0){
fprintf(stderr,"dst file have exists");
exit(1);
}
if(rename(path1,path2) == -1)
perror("rename fail:");
}
void do_dir(char *path1,char *path2)
{
char dst[strlen(path1)+strlen(path2)+3];
if(access(path2,F_OK) != 0){
fprintf(stderr,"dst file not exists");
exit(1);
}
strcpy(dst,path2);
if(path2[strlen(path2)-1] != '/')
strcat(dst,"/");
strcat(dst,path1);
rename(path1,dst);
}