-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcp.c
50 lines (41 loc) · 990 Bytes
/
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
46
47
48
49
50
#include <stdio.h>
/*
Simulates the behavior of the 'cp' command in Linux
cp
copy files and directories.
*/
#define size 1024
int main(int argc,char *argv[])
{
if (argc < 3)
{
printf("Usage: %s file \n", argv[0]);
return 1;
}
FILE *source = fopen(argv[1], "rb");
if (source == NULL)
{
perror("Error opening source file");
return 1;
}
FILE *destination = fopen(argv[2], "wb");
if (destination == NULL)
{
// If destination file doesn't exist, create a new file
destination = fopen(argv[2], "wb");
if (destination == NULL) {
perror("Error creating destination file");
fclose(source);
return 1;
}
}
char buffer[size];
size_t by_read;
while((by_read=fread(buffer,1,sizeof(buffer),source))>0)
{
fwrite(buffer,1,by_read,destination);
}
fclose(source);
fclose(destination);
return 0;
}