-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp3.c
198 lines (179 loc) · 4.08 KB
/
cp3.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<dirent.h>
#include <libgen.h>
#define BUFFERSIZE 4096
#define COPYMODE 0644
char *deal_path(char *,char *);
char *deal_path1(char *,struct dirent *);
int is_file(char *);
char *deal_with(char *,char *);
void oops(char *,char *);
int exists(char *);
void do_cp(char *,char *);
int main(int argc,char *argv[])
{
char answer[10];
char c;
//设置buf
struct stat filebuf1;
struct stat filebuf2;
struct stat tmpbuf;
char *filename=NULL;
char *filename2=NULL;
struct dirent *dirname;
DIR *dir_ptr;
//判断参数
if(argc != 3){
fprintf(stderr,"Usage: %s source destination\n",*argv);
exit(1);
}
//判断源文件和目标文件是否相等
if(strcmp(argv[1],argv[2]) == 0)
{
fprintf(stderr,"No Copy Source File equal Dest File\n");
exit(1);
}
//测试文件是否可以访问
/*
if(access(argv[2],F_OK) == 0){
//询问文件是否可以覆盖
printf("Can you ovver the file (y/n):");
scanf("%9s",&answer);
while((c = getchar()) != EOF && c != '\n');
}
*/
//判断用户的输入
/*
if(*answer != 'y' && *answer != 'Y'){
fprintf(stderr,"the dst file exists,don't over\n");
exit(1);
}
*/
//判断目标是否是目录
stat(argv[2],&filebuf2);
stat(argv[1],&filebuf1);
if(!S_ISDIR(filebuf1.st_mode)){
if(S_ISDIR(filebuf2.st_mode))
filename = deal_path(argv[2],argv[1]);
else
filename = argv[2];
// printf("%s\n",filename);
do_cp(argv[1],filename);
free(filename);
}
else{
if(S_ISDIR(filebuf2.st_mode))
if((dir_ptr = opendir(argv[1])) == NULL)
sprintf("stderr","Can't open dir %s\n",argv[1]);
else
while((dirname=readdir(dir_ptr)) != NULL){
filename = deal_path(argv[1],dirname->d_name);
if(is_file(filename)){
filename2 = deal_with(filename,argv[2]);
do_cp(filename,filename2);
free(filename);
free(filename2);
}else{
free(filename);
continue;
}
}
}
closedir(dir_ptr);
}
void do_cp(char *path1,char *path2)
{
int in_fd,out_fd,n_chars;
char buf[BUFFERSIZE];
if((in_fd = open(path1,O_RDONLY)) == -1)
oops("Cannot open",path1);
if((out_fd = open(path2,O_WRONLY|O_CREAT,COPYMODE)) == -1)
oops("Cannot create",path2);
//开始读取
while((n_chars = read(in_fd,buf,BUFFERSIZE)) > 0)
if(write(out_fd,buf,n_chars) != n_chars)
oops("Write error to",path2);
//判断最后是否写入
if(n_chars == -1)
oops("Read error from,path1","");
//关闭文件
if(close(in_fd) == -1||close(out_fd) == -1)
oops("Error closing files","");
}
void oops(char *s1,char *s2)
{
fprintf(stderr,"Error:%s ",s1);
perror(s2);
exit(1);
}
int exists(char *filename)
{
return access(filename,F_OK);
}
int is_file(char *filename)
{
struct stat filebuf;
stat(filename,&filebuf);
if(S_ISREG(filebuf.st_mode))
return 1;
else
return 0;
}
char *deal_with(char *filename,char *filename2)
{
char *file=NULL;;
if((file = (char *)malloc(strlen(basename(filename)+strlen(filename2)+3))) == NULL)
perror("malloc error");
else{
if(filename2[strlen(filename2)-1] == '/'){
strcpy(file,filename2);
strcat(file,basename(filename));
}else{
strcpy(file,filename2);
strcat(file,"/");
strcat(file,basename(filename));
}
}
return file;
}
char *deal_path1(char *file,struct dirent *dirname)
{
char *filename=NULL;
if((filename = (char *)malloc(strlen(file)+strlen(dirname->d_name)+3)) == NULL)
perror("Malloc erro:");
else{
if(file[strlen(file)-1] == '/'){
strcpy(filename,file);
strcat(filename,dirname->d_name);
}else{
strcpy(filename,file);
strcat(filename,"/");
strcat(filename,dirname->d_name);
}
}
return filename;
}
char *deal_path(char *file,char *file2)
{
char *filename=NULL;
if((filename = (char *)malloc(strlen(file)+strlen(file2)+3)) == NULL)
perror("Malloc erro:");
else{
if(file[strlen(file)-1] == '/'){
strcpy(filename,file);
strcat(filename,file2);
}else{
strcpy(filename,file);
strcat(filename,"/");
strcat(filename,file2);
}
}
return filename;
}