-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcp.c
More file actions
170 lines (145 loc) · 3.3 KB
/
cp.c
File metadata and controls
170 lines (145 loc) · 3.3 KB
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
/* **************************
* The cp command of shell
* *************************/
#include "main.h"
int cps(const char *p)
{
int res = 0;
size_t count = 0;
size_t pcount = BUFSIZ;
int fd_src,fd_des;
char *start,*end;
char src[PATH_SIZE],des[PATH_SIZE];
char buf[BUFSIZ];
FILE *p_src,*p_des;
memset(src,'\0',PATH_SIZE); // must init ,ohtrway there is a wrong result with @
memset(des,'\0',PATH_SIZE);
memset(buf,'\0',BUFSIZ);
p +=3; //conten of p is:"cp src des",move the point to location of 's'
start = strchr(p,' ');
end = strchr(p,'\n');
if(!start || !end)
{
printf("can't support this format \n");
return 1;
}
strncpy(src,p,start-p); //get the source file name in inputting command
p += start-p+1; //move the pointer to location of 'd'
strncpy(des,p,end-start-1); //get the des file name inputting command, there is a \n, so -1
//printf("src:%s \n",src);
//printf("des:%s \n",des);
//open source file
p_src = fopen(src,"r");
if(NULL == p_src)
{
printf("can't open file :%s \n",src);
res = 1;
}
//open des file
p_des = fopen(des,"w+");
if(NULL == p_des)
{
printf("can't open file :%s \n",des);
res = 1;
}
//read character from src and writ them to des
while(0 < (count=fread(buf,sizeof(char),pcount,p_src)) )
{
if(ferror(p_src))
{
printf("reading file failed \n");
res = 1;
break;
}
fwrite(buf,sizeof(char),count,p_des);
if(ferror(p_des))
{
printf("writing files failed \n");
res = 1;
break;
}
}
//close source file
if(-1 == fclose(p_src) )
{
printf("close file: %s failed \n",src);
res = 1;
}
//close des file
if(-1 == fclose(p_des) )
{
printf("close file: %s failed \n",des);
res = 1;
}
return res;
}
/*
int cps(const char *p)
{
int res = 0;
size_t count = 0;
int fd_src,fd_des;
char *start,*end;
char src[PATH_SIZE],des[PATH_SIZE];
char buf[BUFSIZ];
memset(src,'\0',PATH_SIZE); // must init ,ohtrway there is a wrong result with @
memset(des,'\0',PATH_SIZE);
memset(buf,'\0',BUFSIZ);
p +=3; //conten of p is:"cp src des",move the point to location of 's'
start = strchr(p,' ');
end = strchr(p,'\n');
if(!start || !end)
{
printf("can't support this format \n");
return 1;
}
strncpy(src,p,start-p); //get the source file name in inputting command
p += start-p+1; //move the pointer to location of 'd'
strncpy(des,p,end-start-1); //get the des file name inputting command, there is a \n, so -1
//printf("src:%s \n",src);
//printf("des:%s \n",des);
//open source file
fd_src = open(src,O_RDONLY);
if(fd_src == -1)
{
printf("can't open file :%s \n",src);
res = 1;
}
//open des file
fd_des = open(des,O_WRONLY|O_CREAT,S_IRWXU|S_IRGRP|S_IROTH); //creat a file wiht 744 permision
if(fd_des == -1)
{
printf("can't open file :%s \n",des);
res = 1;
}
//read character from src and writ them to des
while(0 != (count=read(fd_src,buf,BUFSIZ)) )
{
if(-1 == count)
{
printf("reading file failed \n");
res = 1;
break;
}
if(count != write(fd_des,buf,count) )
{
printf("writing files failed \n");
res = 1;
break;
}
}
//close source file
if(-1 == close(fd_src) )
{
printf("close file: %s failed \n",src);
res = 1;
}
//close des file
if(-1 == close(fd_des) )
{
printf("close file: %s failed \n",des);
res = 1;
}
return res;
}
*/