1 /* vi: set sw=4 ts=4: */
3 * Mini tee implementation for busybox
6 * Copyright (C) 1999,2000 by Lineo, inc.
7 * Written by John Beppu <beppu@lineo.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 static const char tee_usage[] =
30 "tee [OPTION]... [FILE]...\n"
31 #ifndef BB_FEATURE_TRIVIAL_HELP
32 "\nCopy standard input to each FILE, and also to standard output.\n\n"
33 "Options:\n" "\t-a\tappend to the given FILEs, do not overwrite\n"
35 "\t-i\tignore interrupt signals\n"
41 /* FileList _______________________________________________________________ */
44 static FILE **FileList;
47 typedef void (FL_Function) (FILE * file, char c);
50 /* apply a function to everything in FileList */
51 static void FL_apply(FL_Function * f, char c)
55 for (i = 0; i <= FL_end; i++) {
60 /* FL_Function for writing to files*/
61 static void tee_fwrite(FILE * file, char c)
66 /* FL_Function for closing files */
67 static void tee_fclose(FILE * file, char c)
72 /* ________________________________________________________________________ */
74 /* BusyBoxed tee(1) */
75 int tee_main(int argc, char **argv)
80 char opt_fopen[2] = "w";
84 for (i = 1; i < argc; i++) {
85 if (argv[i][0] == '-') {
93 fprintf(stderr, "ignore interrupt not implemented\n");
104 /* init FILE pointers */
105 FileList = calloc(FL_MAX, sizeof(FILE*));
107 fprintf(stderr, "tee: %s\n", strerror(errno));
111 FileList[0] = stdout;
112 for (; i < argc; i++) {
113 /* add a file to FileList */
114 file = fopen(argv[i], opt_fopen);
118 if (FL_end < FL_MAX) {
119 FileList[++FL_end] = file;
123 /* read and redirect */
124 while ((c = (char) getchar()) && (!feof(stdin))) {
125 FL_apply(tee_fwrite, c);
129 FL_apply(tee_fclose, 0);
130 /* Don't bother to close files Exit does that
131 * automagically, so we can save a few bytes */
132 /* free(FileList); */
136 /* $Id: tee.c,v 1.10 2000/05/12 19:41:47 erik Exp $ */