Materiale su splice, ed esempio non funzionante
[gapil.git] / sources / splicecp.c
1 /* splicecp.c
2  * 
3  * Copyright (C) 2007 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /****************************************************************
20  *
21  * Program splicecp.c: 
22  * A sample program that copy a file using splice
23  *
24  * Author: Simone Piccardi
25  * Aug. 2007
26  *
27  ****************************************************************/
28 /* 
29  * Include needed headers
30  */
31 #define _GNU_SOURCE
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>
39 #include <sys/types.h>
40
41 /* 
42  * Function and globals definitions
43  */
44 void usage(void);  /* Help printing routine */
45
46 /*
47  * Main program
48  */
49 int main(int argc, char *argv[])
50 {
51    /*
52     * Variables definition
53     */
54     int i;
55     int size = 4096;
56     int pipefd[2];
57     int in_fd, out_fd;
58     int nread, nwrite;
59     /*
60      * Input section: decode command line parameters 
61      * Use getopt function
62      */
63     opterr = 0;  /* don't want writing to stderr */
64     while ( (i = getopt(argc, argv, "hs:")) != -1) {
65         switch (i) {
66         /* 
67          * Handling options 
68          */ 
69         case 'h':      /* help option */
70             printf("Wrong -h option use\n");
71             usage();
72             return -1;
73             break;
74         case 's':      /* take wait time for childen */
75             size = strtol(optarg, NULL, 10);    /* convert input */
76             break;
77         case '?':      /* unrecognized options */
78             printf("Unrecognized options -%c\n", optopt);
79             usage();
80         default:       /* should not reached */
81             usage();
82         }
83     }
84    /*
85     * Main body
86     */
87     if ((argc - optind) != 2) { /* There must two argument */
88         printf("Wrong number of arguments %d\n", argc - optind);
89         usage();
90     }
91     /* open pipe, input and output file */
92     if (pipe(pipefd) == -1) { 
93         perror("Cannot create buffer pipe"); 
94         exit(EXIT_FAILURE); 
95     }
96     in_fd = open(argv[optind], O_RDONLY);
97     if (in_fd < 0) {
98         printf("Input error %s on %s\n", strerror(errno), argv[optind]);
99         exit(EXIT_FAILURE); 
100     }
101     out_fd = open(argv[optind+1], O_CREAT|O_RDWR, 0644);
102     if (out_fd < 0) {
103         printf("Cannot open %s, error %s\n", argv[optind+1], strerror(errno));
104         exit(EXIT_FAILURE); 
105     }
106     /* copy loop */
107     printf("Size %d\n", size);
108     while (1) {
109         nread = splice(in_fd, NULL, pipefd[1], NULL, size, 0);
110         printf("read %d bytes\n", nread);
111         if (nread == 0) break;
112         if (nread < 0) {
113             if (errno == EINTR) {
114                 continue;
115             } else {
116                 perror("read error");
117                 exit(EXIT_FAILURE); 
118             } 
119         }
120         do {
121             nwrite = splice(pipefd[0], NULL, out_fd, NULL, nread, 0); 
122             printf("write %d bytes\n", nwrite);
123             if (nwrite == 0) continue;
124             if (nwrite < 0) {
125                 if (errno == EINTR)
126                     continue;
127             } else {
128                 perror("write error");
129                 exit(EXIT_FAILURE); 
130             }
131             nread -= nwrite;
132             printf("left %d bytes", nread);
133         } while (nread);
134     }
135     return EXIT_SUCCESS;
136 }
137
138
139
140 /*
141  * routine to print usage info and exit 
142  */
143 void usage(void) {
144     printf("Program splicecp: copy two file using splice syscall\n");
145     printf("Usage:\n");
146     printf("  splicecp [-h] [-s N] filesrc filedst \n");
147     printf("  -h           print this help\n");
148     printf("  -s N         set a buffer size of N bytes \n");
149     exit(1);
150 }