1 /* tee - read from standard input and write to standard output and files.
2 Copyright (C) 85,1990-2001 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Mike Parker, Richard M. Stallman, and David MacKenzie */
22 #include <sys/types.h>
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "tee"
33 #define AUTHORS N_ ("Mike Parker, Richard M. Stallman, and David MacKenzie")
35 static int tee
PARAMS ((int nfiles
, const char **files
));
37 /* If nonzero, append to output files rather than truncating them. */
40 /* If nonzero, ignore interrupts. */
41 static int ignore_interrupts
;
43 /* The name that this program was run with. */
46 static struct option
const long_options
[] =
48 {"append", no_argument
, NULL
, 'a'},
49 {"ignore-interrupts", no_argument
, NULL
, 'i'},
50 {GETOPT_HELP_OPTION_DECL
},
51 {GETOPT_VERSION_OPTION_DECL
},
59 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
63 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name
);
65 Copy standard input to each FILE, and also to standard output.\n\
67 -a, --append append to the given FILEs, do not overwrite\n\
68 -i, --ignore-interrupts ignore interrupt signals\n\
70 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
71 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
72 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
78 main (int argc
, char **argv
)
83 program_name
= argv
[0];
84 setlocale (LC_ALL
, "");
85 bindtextdomain (PACKAGE
, LOCALEDIR
);
88 atexit (close_stdout
);
91 ignore_interrupts
= 0;
93 while ((optc
= getopt_long (argc
, argv
, "ai", long_options
, NULL
)) != -1)
105 ignore_interrupts
= 1;
108 case_GETOPT_HELP_CHAR
;
110 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
117 if (ignore_interrupts
)
120 struct sigaction sigact
;
122 sigact
.sa_handler
= SIG_IGN
;
123 sigemptyset (&sigact
.sa_mask
);
125 sigaction (SIGINT
, &sigact
, NULL
);
126 #else /* !_POSIX_SOURCE */
127 signal (SIGINT
, SIG_IGN
);
128 #endif /* _POSIX_SOURCE */
131 /* Don't let us be killed if one of the output files is a pipe that
132 doesn't consume all its input. */
135 struct sigaction sigact
;
137 sigact
.sa_handler
= SIG_IGN
;
138 sigemptyset (&sigact
.sa_mask
);
140 sigaction (SIGPIPE
, &sigact
, NULL
);
143 signal (SIGPIPE
, SIG_IGN
);
146 /* Do *not* warn if tee is given no file arguments.
147 POSIX requires that it work when given no arguments. */
149 errs
= tee (argc
- optind
, (const char **) &argv
[optind
]);
151 error (1, errno
, _("standard input"));
156 /* Copy the standard input into each of the NFILES files in FILES
157 and into the standard output.
158 Return 0 if successful, 1 if any errors occur. */
161 tee (int nfiles
, const char **files
)
167 const char *mode_string
= (append
? "a" : "w");
169 descriptors
= (FILE **) xmalloc ((nfiles
+ 1) * sizeof (descriptors
[0]));
171 /* Move all the names `up' one in the argv array to make room for
172 the entry for standard output. This writes into argv[argc]. */
173 for (i
= nfiles
; i
>= 1; i
--)
174 files
[i
] = files
[i
- 1];
178 /* In the array of NFILES + 1 descriptors, make
179 the first one correspond to standard output. */
180 descriptors
[0] = stdout
;
181 files
[0] = _("standard output");
182 SETVBUF (stdout
, NULL
, _IONBF
, 0);
184 for (i
= 1; i
<= nfiles
; i
++)
186 descriptors
[i
] = fopen (files
[i
], mode_string
);
187 if (descriptors
[i
] == NULL
)
189 error (0, errno
, "%s", files
[i
]);
194 SETVBUF (descriptors
[i
], NULL
, _IONBF
, 0);
195 SET_BINARY (fileno (descriptors
[i
]));
201 bytes_read
= read (0, buffer
, sizeof buffer
);
203 if (bytes_read
< 0 && errno
== EINTR
)
209 /* Write to all NFILES + 1 descriptors.
210 Standard output is the first one. */
211 for (i
= 0; i
<= nfiles
; i
++)
213 if (descriptors
[i
] != NULL
)
214 fwrite (buffer
, bytes_read
, 1, descriptors
[i
]);
218 if (bytes_read
== -1)
220 error (0, errno
, _("read error"));
224 /* Close the files, but not standard output. */
225 for (i
= 1; i
<= nfiles
; i
++)
226 if (descriptors
[i
] != NULL
227 && (ferror (descriptors
[i
]) || fclose (descriptors
[i
]) == EOF
))
229 error (0, errno
, "%s", files
[i
]);