| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
#include <fcntl.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <sys/sendfile.h> |
| 11 |
#include <sys/stat.h> |
| 12 |
#include <sys/types.h> |
| 13 |
#include <unistd.h> |
| 14 |
|
| 15 |
int main (int argc, char* argv[]) |
| 16 |
{ |
| 17 |
int read_fd; |
| 18 |
int write_fd; |
| 19 |
struct stat stat_buf; |
| 20 |
off_t offset = 0; |
| 21 |
|
| 22 |
/* Open the input file. */ |
| 23 |
read_fd = open (argv[1], O_RDONLY); |
| 24 |
/* Stat the input file to obtain its size. */ |
| 25 |
fstat (read_fd, &stat_buf); |
| 26 |
/* Open the ouput file for writing, with the same permissions as the |
| 27 |
source file. */ |
| 28 |
write_fd = open (argv[2], O_WRONLY | O_CREAT, stat_buf.st_mode); |
| 29 |
/* Blast the bytes from one file to the other. */ |
| 30 |
sendfile (write_fd, read_fd, &offset, stat_buf.st_size); |
| 31 |
/* Close up. */ |
| 32 |
close (read_fd); |
| 33 |
close (write_fd); |
| 34 |
|
| 35 |
return 0; |
| 36 |
} |