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 duolicete stdin to stdout and a file
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 */
40 #include <limits.h> /* system limits constants, types and functions */
44 * Function and globals definitions
46 void usage(void); /* Help printing routine */
50 int main(int argc, char *argv[])
53 * Variables definition
61 * Input section: decode command line parameters
64 opterr = 0; /* don't want writing to stderr */
65 while ( (i = getopt(argc, argv, "hs:")) != -1) {
70 case 'h': /* help option */
71 printf("Wrong -h option use\n");
75 case 's': /* buffer size */
76 size = strtol(optarg, NULL, 10); /* convert input */
78 case '?': /* unrecognized options */
79 printf("Unrecognized options -%c\n", optopt);
81 default: /* should not reached */
88 if ((argc - optind) != 1) { /* There must be one argument */
89 printf("Wrong number of arguments %d\n", argc - optind);
92 /* open destination file and check stdin and stdout */
93 fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);
95 printf("cannot open destination file %s, %s", argv[1],
99 if (fstat(STDIN_FILENO, &fdata) < 0) {
103 if (!S_ISFIFO(fdata.st_mode)) {
104 fprintf(stderr, "stdin must be a pipe\n");
107 if (fstat(STDOUT_FILENO, &fdata) < 0) {
111 if (!S_ISFIFO(fdata.st_mode)) {
112 fprintf(stderr, "stdout must be a pipe\n");
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!) */
122 if (errno == EAGAIN) {
125 perror("error on tee stdin to stdout");
129 /* write data to the file using splice */
131 nwrite = splice(STDIN_FILENO, NULL, fd, NULL, len, SPLICE_F_MOVE);
133 perror("error on splice stdin to file");
144 * routine to print usage info and exit
147 printf("Program tee: duplicate stdin to stdout and a file\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");