-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcp.c
More file actions
37 lines (29 loc) · 816 Bytes
/
cp.c
File metadata and controls
37 lines (29 loc) · 816 Bytes
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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFER_SIZE 65536
static char buffer[BUFFER_SIZE];
int main(int argc, char **argv) {
int src_fd, dst_fd;
ssize_t bytes_read, bytes_written;
if (argc != 3) return 1;
src_fd = open(argv[1], O_RDONLY);
if (src_fd == -1) return 1;
dst_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dst_fd == -1) {
close(src_fd);
return 1;
}
while ((bytes_read = read(src_fd, buffer, BUFFER_SIZE)) > 0) {
bytes_written = write(dst_fd, buffer, bytes_read);
if (bytes_written != bytes_read) {
close(src_fd);
close(dst_fd);
return 1;
}
}
close(src_fd);
close(dst_fd);
return 0;
}