#define _GNU_SOURCE #include #include #include int main(int argc, char *argv[]) { int to, from, res; int pip[2]; int siz = 65536; if (argc != 3) err(1, "usage: %s from_file to_file", argv[0]); from = open(argv[1], O_RDONLY); if (from == -1) err(1, "opening %s", argv[1]); to = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); if (to == -1) err(1, "opening %s", argv[2]); res = pipe(pip); if (res == -1) err(1, "creating pipe"); while (1) { int num; res = splice(from, NULL, pip[1], NULL, siz, 0); if (res == -1) err(1, "splicing from %s to pipe", argv[1]); num = res; if (num == 0) break; do { res = splice(pip[0], NULL, to, NULL, num, 0); if (res == -1) err(1, "splicing from pipe to %s", argv[2]); if (res == 0) break; num -= res; } while (num); } res = close(to); if (res == -1) err(1, "closing %s", argv[2]); res = close(from); if (res == -1) err(1, "closing %s", argv[1]); return 0; }