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