3 * Copyright (C) 2007 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
22 * A sample program that copy a file using splice
24 * Author: Simone Piccardi
27 ****************************************************************/
29 * Include needed headers
32 #include <fcntl.h> /* file control functions */
33 #include <unistd.h> /* unix standard library */
34 #include <stdlib.h> /* C standard library */
35 #include <errno.h> /* error definitions and routines */
36 #include <stdio.h> /* standard I/O library */
37 #include <string.h> /* C strings library */
38 #include <sys/stat.h> /* file characteristics constants and functions */
39 #include <sys/types.h> /* primitive system data types */
43 * Function and globals definitions
45 void usage(void); /* Help printing routine */
49 int main(int argc, char *argv[])
52 * Variables definition
60 * Input section: decode command line parameters
63 opterr = 0; /* don't want writing to stderr */
64 while ( (i = getopt(argc, argv, "hs:")) != -1) {
69 case 'h': /* help option */
70 printf("Wrong -h option use\n");
74 case 's': /* take wait time for childen */
75 size = strtol(optarg, NULL, 10); /* convert input */
77 case '?': /* unrecognized options */
78 printf("Unrecognized options -%c\n", optopt);
80 default: /* should not reached */
87 if ((argc - optind) != 2) { /* There must two argument */
88 printf("Wrong number of arguments %d\n", argc - optind);
91 /* open pipe, input and output file */
92 in_fd = open(argv[optind], O_RDONLY);
94 printf("Input error %s on %s\n", strerror(errno), argv[optind]);
97 out_fd = open(argv[optind+1], O_CREAT|O_RDWR|O_TRUNC, 0644);
99 printf("Cannot open %s, error %s\n", argv[optind+1], strerror(errno));
102 if (pipe(pipefd) == -1) {
103 perror("Cannot create buffer pipe");
107 debug("Size %d\n", size);
109 nread = splice(in_fd, NULL, pipefd[1], NULL, size,
110 SPLICE_F_MOVE|SPLICE_F_MORE);
111 debug("read %d bytes\n", nread);
112 if (nread == 0) break;
114 if (errno == EINTR) {
117 perror("read error");
122 nwrite = splice(pipefd[0], NULL, out_fd, NULL, nread,
123 SPLICE_F_MOVE|SPLICE_F_MORE);
124 debug("write %d bytes\n", nwrite);
129 perror("write error");
134 debug("left %d bytes\n", nread);
140 * routine to print usage info and exit
143 printf("Program splicecp: copy two file using splice syscall\n");
145 printf(" splicecp [-h] [-s N] filesrc filedst \n");
146 printf(" -h print this help\n");
147 printf(" -s N set a buffer size of N bytes \n");