-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpt.c
76 lines (71 loc) · 1.6 KB
/
cpt.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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
#include "fcntl.h"
void cptOneFile(char* filePath) {
char buf[512];
int fd, n;
unlink(filePath);
fd = open(filePath, O_CREATE | O_RDWR);
while((n = read(0, buf, sizeof(buf))) > 0) {
if (write(fd, buf, n) != n) {
printf(0, "write error\n");
close(fd);
return;
}
}
if(n < 0){
printf(0, "read error\n");
close(fd);
return;
}
printf(0, "\nDone!\n");
close(fd);
return;
}
void cptTwoFile(char* firstFilePath, char* secondFilePath) {
char buf[512];
int fd1, fd2, n;
fd1 = open(firstFilePath, O_RDWR);
if(fd1 >= 0) {
unlink(secondFilePath);
fd2 = open(secondFilePath, O_CREATE | O_RDWR);
while((n = read(fd1, buf, sizeof(buf))) > 0) {
if (write(fd2, buf, n) != n) {
printf(0, "write error\n");
close(fd1);
close(fd2);
return;
}
}
if(n < 0){
printf(0, "read error\n");
close(fd1);
close(fd2);
return;
}
printf(0, "\nDone!\n");
close(fd1);
close(fd2);
return;
} else {
printf(0, "The file path to read the text from doesn't exist!!!\n");
close(fd1);
return;
}
}
int main(int argc, char *argv[])
{
if(argc == 2){
cptOneFile(argv[1]);
exit();
}
else if(argc == 3){
cptTwoFile(argv[1], argv[2]);
exit();
}
else {
}
exit();
}