-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp.c
45 lines (37 loc) · 1.05 KB
/
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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#define MAX_BUF 100000
int main(int argc, char ** argv)
{
int i;
int og_fd, copy_fd;
char buffer[MAX_BUF];
int read_count;
if(argc != 3){
printf("ERROR: Incorrect number of arguments\nUse: cp file1 file2\n");
return -1;
}
// Open the original file and the copy file
og_fd = open(argv[1], O_RDONLY);
if(og_fd == -1){
printf("ERROR: File \"%s\" does not exist\n", argv[1]);
return -1;
}
copy_fd = open(argv[2], O_CREAT|O_WRONLY, S_IRWXU);
// Copy the content
while(1){
read_count = read(og_fd, buffer, MAX_BUF);
if(read_count == -1){
printf("ERROR: Reading from file \"%s\" failed\n", argv[1]);
return -1;
}
if(read_count == 0) // reached EOF
break;
write(copy_fd, buffer, read_count);
}
printf("Copied file: \"%s\" into file: \"%s\" successfuly\n", argv[1], argv[2]);
return 0;
}