VirtualBox

Ticket #12597: sendfile-copy.c

File sendfile-copy.c, 766 bytes (added by MaximKulkin, 11 years ago)
Line 
1#include <stdio.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <fcntl.h>
5#include <sys/sendfile.h>
6
7int main(int argc, const char *argv[]) {
8 if (argc < 3) {
9 printf("Usage: %s <source> <destination>", argv[0]);
10 return 1;
11 }
12
13 const char *srcPath = argv[1];
14 const char *dstPath = argv[2];
15
16 struct stat srcStat;
17 if (stat(srcPath, &srcStat) < 0) {
18 perror("stat source file");
19 return 2;
20 }
21
22 int fromFd = open(argv[1], O_RDONLY);
23 if (fromFd < 0) {
24 perror("open source file");
25 return 2;
26 }
27
28 int toFd = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
29 if (toFd < 0) {
30 perror("open destination file");
31 return 2;
32 }
33
34 sendfile(toFd, fromFd, NULL, srcStat.st_size);
35
36 return 0;
37}
38

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy