Aggiornamento note copyright
[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 fdata;
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, "hs:")) != -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':      /* buffer size */
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 be one argument */
89         printf("Wrong number of arguments %d\n", argc - optind);
90         usage();
91     }
92     /* open destination file and check stdin and stdout */
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     if (fstat(STDIN_FILENO, &fdata) < 0) {
100         perror("stat");
101         exit(EXIT_FAILURE);
102     }
103     if (!S_ISFIFO(fdata.st_mode)) {
104         fprintf(stderr, "stdin must be a pipe\n");
105         exit(EXIT_FAILURE);
106     }
107     if (fstat(STDOUT_FILENO, &fdata) < 0) {
108         perror("stat");
109         exit(EXIT_FAILURE);
110     }
111     if (!S_ISFIFO(fdata.st_mode)) {
112         fprintf(stderr, "stdout must be a pipe\n");
113         exit(EXIT_FAILURE);
114     }
115     /* tee loop */
116     while (1) {
117         /* copy stdin to stdout */
118         len = tee(STDIN_FILENO, STDOUT_FILENO, size, 0);
119         fprintf(stderr, "Copied %d byte\n", len); /* debug (use stderr!) */
120         if (len == 0) break;
121         if (len < 0) {
122             if (errno == EAGAIN) {
123                 continue;
124             } else {
125                 perror("error on tee stdin to stdout");
126                 exit(EXIT_FAILURE);
127             }
128         }
129         /* write data to the file using splice */
130         while (len > 0) {
131             nwrite = splice(STDIN_FILENO, NULL, fd, NULL, len, SPLICE_F_MOVE);
132             if (nwrite < 0) {
133                 perror("error on splice stdin to file");
134                 break;
135             }
136             len -= nwrite;
137         }
138     }
139     close(fd);
140     exit(EXIT_SUCCESS);
141 }
142
143 /*
144  * routine to print usage info and exit 
145  */
146 void usage(void) {
147     printf("Program tee: duplicate stdin to stdout and a file\n");
148     printf("Usage:\n");
149     printf("  tee [-h] [-s N] filename\n");
150     printf("  -h           print this help\n");
151     printf("  -s N         set a buffer size of N bytes \n");
152     exit(1);
153 }
154