-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopy.c
62 lines (53 loc) · 996 Bytes
/
copy.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define MAX 1000
char* buf[MAX];
int main(int argc, char** argv)
{
int fd1, fd2;
//valid number of arguments
if(argc==3)
{
fd1=open(argv[1],O_RDONLY);
//check if file is opened
if(fd1==-1)
{
perror("File creation error");
return 99;
}
//file created : file descriptor returned
else
{
fd2=open(argv[2],O_WRONLY|O_CREAT,0777);
//if file is created
if(fd2!=-1)
{
if(read(fd1,buf,MAX)!=-1)
{
if(write(fd2,buf,MAX)!=-1)
{
printf("File %s copied to %s\n",argv[1],argv[2]);
return 0;
}
}
//file had read/write errors; hence delete the 2nd file created
else
{
if(!unlink(argv[2]))
{
return 99;
}
}
}
}
}
//invalid number of arguments
else
{
printf("Error: Enter command followed by file to be copied and destination file\n");
return 100;
}
}