18f01a2a3a6dc053ed895a80daa2636fe9bd0ac6
[gapil.git] / sources / tee.c
1 /* tee.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 tee.c: 
22  * A sample program that duolicete stdin to stdout and a file
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 #include <limits.h>      /* system limits constants, types and functions */
41
42 #include "macros.h"
43 /* 
44  * Function and globals definitions
45  */
46 void usage(void);  /* Help printing routine */
47 /*
48  * Main program
49  */
50 int main(int argc, char *argv[])
51 {
52    /*
53     * Variables definition
54     */
55     int i;
56     size_t size = 4096;
57     int fd;
58     int len, nwrite;
59     struct stat sb;
60     /*
61      * Input section: decode command line parameters 
62      * Use getopt function
63      */
64     opterr = 0;  /* don't want writing to stderr */
65     while ( (i = getopt(argc, argv, "h")) != -1) {
66         switch (i) {
67         /* 
68          * Handling options 
69          */ 
70         case 'h':      /* help option */
71             printf("Wrong -h option use\n");
72             usage();
73             return -1;
74             break;
75         case 's':      /* take wait time for childen */
76             size = strtol(optarg, NULL, 10);    /* convert input */
77             break;
78         case '?':      /* unrecognized options */
79             printf("Unrecognized options -%c\n", optopt);
80             usage();
81         default:       /* should not reached */
82             usage();
83         }
84     }
85    /*
86     * Main body
87     */
88     if ((argc - optind) != 1) { /* There must two argument */
89         printf("Wrong number of arguments %d\n", argc - optind);
90         usage();
91     }
92     /* open destination file */
93     fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);
94     if (fd == -1) {
95         printf("cannot open destination file %s, %s", argv[1], 
96                strerror(errno));
97         exit(EXIT_FAILURE);
98     }
99
100
101     if (fstat(STDIN_FILENO, &sb) < 0) {
102         perror("stat");
103         exit(EXIT_FAILURE);
104     }
105     if (!S_ISFIFO(sb.st_mode)) {
106         fprintf(stderr, "stdin must be a pipe\n");
107         exit(EXIT_FAILURE);
108     }
109     if (fstat(STDOUT_FILENO, &sb) < 0) {
110         perror("stat");
111         exit(EXIT_FAILURE);
112     }
113     if (!S_ISFIFO(sb.st_mode)) {
114         fprintf(stderr, "stdout must be a pipe\n");
115         exit(EXIT_FAILURE);
116     }
117
118     /* tee loop */
119     //debug("Size %d\n", size);
120     while (1) {
121         /* copy stdin to stdout */
122         len = tee(STDIN_FILENO, STDOUT_FILENO, size, SPLICE_F_NONBLOCK);
123         if (len < 0) {
124             if (errno == EAGAIN) {
125                 continue;
126             } else {
127                 perror("error on tee stdin to stdout");
128                 exit(EXIT_FAILURE);
129             }
130         } else {
131             if (len == 0) break;
132         }
133         fprintf(stderr, "Copied %d byte\n", len);
134         /* write data to the file using splice */
135         while (len > 0) {
136             nwrite = splice(STDIN_FILENO, NULL, fd, NULL, len, SPLICE_F_MOVE);
137             if (nwrite < 0) {
138                 perror("error on splice stdin to file");
139                 break;
140             }
141             len -= nwrite;
142         }
143     }
144     close(fd);
145     exit(EXIT_SUCCESS);
146 }
147
148 /*
149  * routine to print usage info and exit 
150  */
151 void usage(void) {
152     printf("Program tee: duplicate stdin to stdout and a file\n");
153     printf("Usage:\n");
154     printf("  splicecp [-h] [-s N] filename\n");
155     printf("  -h           print this help\n");
156     printf("  -s N         set a buffer size of N bytes \n");
157     exit(1);
158 }
159