blob: a3a1c8132b962f9b5e8e78a0433547875e8c1e6c [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppu059f1521999-12-10 05:27:16 +00002/*
3 * Mini tee implementation for busybox
4 *
5 *
6 * Copyright (C) 1999 by Lineo, inc.
Eric Andersen70e2f0b1999-12-10 06:45:42 +00007 * Written by John Beppu <beppu@lineo.com>
John Beppu059f1521999-12-10 05:27:16 +00008 *
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.
13 *
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.
18 *
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
22 *
23 */
24
John Beppua3e0d791999-12-10 07:41:03 +000025#include "internal.h"
John Beppu692a4502000-03-08 00:14:35 +000026#include <errno.h>
John Beppu059f1521999-12-10 05:27:16 +000027#include <stdio.h>
John Beppua3e0d791999-12-10 07:41:03 +000028
29static const char tee_usage[] =
Erik Andersene49d5ec2000-02-08 19:58:47 +000030 "tee [OPTION]... [FILE]...\n\n"
31 "Copy standard input to each FILE, and also to standard output.\n\n"
32 "Options:\n" "\t-a\tappend to the given FILEs, do not overwrite\n"
Eric Andersen2cb55071999-12-10 08:25:07 +000033#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000034 "\t-i\tignore interrupt signals\n"
Eric Andersen2cb55071999-12-10 08:25:07 +000035#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000036;
Eric Andersen2cb55071999-12-10 08:25:07 +000037
John Beppu059f1521999-12-10 05:27:16 +000038
39/* FileList _______________________________________________________________ */
40
41#define FL_MAX 1024
John Beppu692a4502000-03-08 00:14:35 +000042static FILE **FileList;
Erik Andersene49d5ec2000-02-08 19:58:47 +000043static int FL_end;
John Beppu059f1521999-12-10 05:27:16 +000044
Erik Andersene49d5ec2000-02-08 19:58:47 +000045typedef void (FL_Function) (FILE * file, char c);
46
John Beppu059f1521999-12-10 05:27:16 +000047
48/* apply a function to everything in FileList */
Erik Andersene49d5ec2000-02-08 19:58:47 +000049static void FL_apply(FL_Function * f, char c)
John Beppu059f1521999-12-10 05:27:16 +000050{
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 int i;
52
53 for (i = 0; i <= FL_end; i++) {
54 f(FileList[i], c);
55 }
John Beppu059f1521999-12-10 05:27:16 +000056}
57
John Beppu059f1521999-12-10 05:27:16 +000058/* FL_Function for writing to files*/
Erik Andersene49d5ec2000-02-08 19:58:47 +000059static void tee_fwrite(FILE * file, char c)
John Beppu059f1521999-12-10 05:27:16 +000060{
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 fputc(c, file);
John Beppu059f1521999-12-10 05:27:16 +000062}
63
64/* FL_Function for closing files */
Erik Andersene49d5ec2000-02-08 19:58:47 +000065static void tee_fclose(FILE * file, char c)
John Beppu059f1521999-12-10 05:27:16 +000066{
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 fclose(file);
John Beppu059f1521999-12-10 05:27:16 +000068}
69
Eric Andersen2cb55071999-12-10 08:25:07 +000070/* ________________________________________________________________________ */
71
John Beppu059f1521999-12-10 05:27:16 +000072/* BusyBoxed tee(1) */
Erik Andersene49d5ec2000-02-08 19:58:47 +000073int tee_main(int argc, char **argv)
John Beppu059f1521999-12-10 05:27:16 +000074{
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 int i;
76 char c;
77 char opt;
78 char opt_fopen[2] = "w";
79 FILE *file;
John Beppu059f1521999-12-10 05:27:16 +000080
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 /* parse argv[] */
82 for (i = 1; i < argc; i++) {
83 if (argv[i][0] == '-') {
84 opt = argv[i][1];
85 switch (opt) {
86 case 'a':
87 opt_fopen[0] = 'a';
88 break;
Eric Andersen2cb55071999-12-10 08:25:07 +000089#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 case 'i':
91 fprintf(stderr, "ignore interrupt not implemented\n");
92 break;
Eric Andersen2cb55071999-12-10 08:25:07 +000093#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 default:
95 usage(tee_usage);
96 }
97 } else {
98 break;
99 }
John Beppu059f1521999-12-10 05:27:16 +0000100 }
John Beppu059f1521999-12-10 05:27:16 +0000101
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 /* init FILE pointers */
John Beppu692a4502000-03-08 00:14:35 +0000103 FileList = calloc(FL_MAX, sizeof(FILE*));
104 if (!FileList) {
105 fprintf(stderr, "tee: %s\n", strerror(errno));
106 exit(1);
107 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 FL_end = 0;
109 FileList[0] = stdout;
110 for (; i < argc; i++) {
111 /* add a file to FileList */
112 file = fopen(argv[i], opt_fopen);
113 if (!file) {
114 continue;
115 }
116 if (FL_end < FL_MAX) {
117 FileList[++FL_end] = file;
118 }
Eric Andersen2cb55071999-12-10 08:25:07 +0000119 }
John Beppu059f1521999-12-10 05:27:16 +0000120
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 /* read and redirect */
122 while ((c = (char) getchar()) && (!feof(stdin))) {
123 FL_apply(tee_fwrite, c);
124 }
John Beppu059f1521999-12-10 05:27:16 +0000125
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 /* clean up */
127 FL_apply(tee_fclose, 0);
John Beppu692a4502000-03-08 00:14:35 +0000128 free(FileList);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 exit(0);
John Beppu059f1521999-12-10 05:27:16 +0000130}
131
John Beppu692a4502000-03-08 00:14:35 +0000132/* $Id: tee.c,v 1.7 2000/03/08 00:14:35 beppu Exp $ */