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, "h")) != -1) {
70 case 'h': /* help option */
71 printf("Wrong -h option use\n");
75 case 's': /* take wait time for childen */
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 two argument */
89 printf("Wrong number of arguments %d\n", argc - optind);
92 /* open destination file */
93 fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);
95 printf("cannot open destination file %s, %s", argv[1],
101 if (fstat(STDIN_FILENO, &sb) < 0) {
105 if (!S_ISFIFO(sb.st_mode)) {
106 fprintf(stderr, "stdin must be a pipe\n");
109 if (fstat(STDOUT_FILENO, &sb) < 0) {
113 if (!S_ISFIFO(sb.st_mode)) {
114 fprintf(stderr, "stdout must be a pipe\n");
119 //debug("Size %d\n", size);
121 /* copy stdin to stdout */
122 len = tee(STDIN_FILENO, STDOUT_FILENO, size, SPLICE_F_NONBLOCK);
124 if (errno == EAGAIN) {
127 perror("error on tee stdin to stdout");
133 fprintf(stderr, "Copied %d byte\n", len);
134 /* write data to the file using splice */
136 nwrite = splice(STDIN_FILENO, NULL, fd, NULL, len, SPLICE_F_MOVE);
138 perror("error on splice stdin to file");
149 * routine to print usage info and exit
152 printf("Program tee: duplicate stdin to stdout and a file\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");