-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_cp.c
122 lines (109 loc) · 2.07 KB
/
file_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
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
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
int fd_from,fd_to;//
int file_access=0;//
int byte_read,byte_write;
int i=0;
char file_buf[BUFFER_SIZE];
char *ptr;
if(argc!=3)//
{
printf("An incorrect input ! please check the parameter.\n");
exit(EXIT_FAILURE);
}
if(access(argv[1],F_OK))//
{
printf("there is no file %s !\n",argv[1]);
exit(EXIT_FAILURE);
}
if(access(argv[1],R_OK))//
{
printf("you have no read permission to file %s !\n",argv[1]);
exit(EXIT_FAILURE);
}
else
{
file_access+=4;
}
if(access(argv[1],W_OK)==0)//
{
file_access+=2;
}
if(access(argv[1],X_OK)==0)//
{
file_access+=1;
}
if((fd_from=open(argv[1],O_RDONLY))<0)
{
printf("you can't open the file %s !\n",argv[1]);
exit(EXIT_FAILURE);
}
if(file_access==5)
{
if((fd_to=creat(argv[2],0555))<0)
{
printf("creat file %s failure ! \n",argv[2]);
close(fd_from);
exit(EXIT_FAILURE);
}
}
else if(file_access==6)
{
if((fd_to=creat(argv[2],0655))<0)
{
printf("creat file %s failure ! \n",argv[2]);
close(fd_from);
exit(EXIT_FAILURE);
}
}
else if(file_access==7)
{
if((fd_to=creat(argv[2],0755))<0)
{
printf("creat file %s failure ! \n",argv[2]);
close(fd_from);
exit(EXIT_FAILURE);
}
}
while(byte_read=read(fd_from,file_buf,BUFFER_SIZE))
{
if(byte_read==-1)
{
printf("复制时源文件读取发生错误!\n");
break;
}
else if(byte_read>0)
{
ptr=file_buf;
while(byte_write=write(fd_to,ptr,byte_read))
{
if(byte_write==-1)
{
printf("复制时目标文件写入发生错误!\n");
break;
}
else if(byte_read==byte_write)
{
break;
}
else if(byte_write>0)
{
ptr+=byte_write;
byte_read-=byte_write;
}
}
if(byte_write==-1)
break;
}
}
close(fd_from);
close(fd_to);
exit(0);
}