Skip to content

Instantly share code, notes, and snippets.

@hackaugusto
Last active May 10, 2019 23:35
Show Gist options
  • Select an option

  • Save hackaugusto/093234e7019f3c2248ac9efa8a7bade8 to your computer and use it in GitHub Desktop.

Select an option

Save hackaugusto/093234e7019f3c2248ac9efa8a7bade8 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
int
main(int argc, char *argv[])
{
int in_fd, out_fd, len;
if (argc != 3) {
fprintf(stderr, "Usage: %s <from> <to>\n", argv[0]);
exit(EXIT_FAILURE);
}
in_fd = open(argv[1], O_RDONLY);
out_fd = open(argv[2], O_WRONLY);
if (in_fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
if (out_fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
do {
len = copy_file_range(in_fd, NULL, out_fd, NULL, INT_MAX, 0);
if (len < 0) {
if (errno == EAGAIN) {
continue;
}
perror("copy_file_range");
exit(EXIT_FAILURE);
} else if (len == 0) {
break;
}
} while (1);
close(in_fd);
close(out_fd);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment