-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_cp.c
47 lines (38 loc) · 960 Bytes
/
my_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
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
if ((argc == 3) && (strcmp(argv[1], argv[2]) != 0))
{ //Make sure there are three parameters, and the source and destination file names are not the same.
int fd_src, fd_dest, ret;
//Open source files in read-only mode
fd_src = open(argv[1], O_RDONLY);
if (fd_src < 0)
{
perror("open argv[1]");
return -1;
}
//New destination file
fd_dest = open(argv[2], O_WRONLY | O_CREAT, 0755);
if (fd_dest < 0)
{
close(fd_src);
perror("open argv[2]");
return -1;
}
do
{
char buf[1024] = { 0 };
//Reading data from source file
ret = read(fd_src, buf, sizeof(buf));
//Write the data to the destination file, pay attention to the last parameter, and write the file size.
write(fd_dest, buf, ret);
} while (ret > 0);
//Close open files
close(fd_src);
close(fd_dest);
}
return 0;
}