-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmycopy.c
46 lines (36 loc) · 946 Bytes
/
mycopy.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
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
char buffer[2048]; // 2 ^ 11
int version = 1; // Chapter 2 explains this
void copy(int old, int new) {
int count;
while ((count = read(old, buffer, sizeof(buffer))) > 0) {
printf("%d characters copied\n", count);
write(new, buffer, count);
}
}
int main(int argc, char* argv[]) {
int fdold, fdnew;
if (argc != 3) {
printf("need 2 arguments for copy program\n");
exit(1);
}
fdold = open(argv[1], O_RDONLY);
if (-1 == fdold) {
printf("cannot open file %s\n", argv[1]);
exit(1);
}
// 0777 gives r,w,execute permission to all
fdnew = creat(argv[2], 0666); // 6 = 2,4 // Create target file with rw permissions for all
if (-1 == fdnew) {
printf("cannot open file %s\n", argv[2]);
exit(1);
}
copy(fdold, fdnew);
// Not closing a previously opened file is a sin!
close(fdold);
close(fdnew);
exit(0);
}