blob: a78edc039348f3d1a938fab8d342c5280a56caa8 [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 *
Erik Andersen61677fe2000-04-13 01:18:56 +00006 * Copyright (C) 1999,2000 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 Andersen7ab9c7e2000-05-12 19:41:47 +000030 "tee [OPTION]... [FILE]...\n"
31#ifndef BB_FEATURE_TRIVIAL_HELP
32 "\nCopy standard input to each FILE, and also to standard output.\n\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000033 "Options:\n" "\t-a\tappend to the given FILEs, do not overwrite\n"
Eric Andersen2cb55071999-12-10 08:25:07 +000034#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000035 "\t-i\tignore interrupt signals\n"
Eric Andersen2cb55071999-12-10 08:25:07 +000036#endif
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000037#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000038;
Eric Andersen2cb55071999-12-10 08:25:07 +000039
John Beppu059f1521999-12-10 05:27:16 +000040
41/* FileList _______________________________________________________________ */
42
43#define FL_MAX 1024
John Beppu692a4502000-03-08 00:14:35 +000044static FILE **FileList;
Erik Andersene49d5ec2000-02-08 19:58:47 +000045static int FL_end;
John Beppu059f1521999-12-10 05:27:16 +000046
Erik Andersene49d5ec2000-02-08 19:58:47 +000047typedef void (FL_Function) (FILE * file, char c);
48
John Beppu059f1521999-12-10 05:27:16 +000049
50/* apply a function to everything in FileList */
Erik Andersene49d5ec2000-02-08 19:58:47 +000051static void FL_apply(FL_Function * f, char c)
John Beppu059f1521999-12-10 05:27:16 +000052{
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 int i;
54
55 for (i = 0; i <= FL_end; i++) {
56 f(FileList[i], c);
57 }
John Beppu059f1521999-12-10 05:27:16 +000058}
59
John Beppu059f1521999-12-10 05:27:16 +000060/* FL_Function for writing to files*/
Erik Andersene49d5ec2000-02-08 19:58:47 +000061static void tee_fwrite(FILE * file, char c)
John Beppu059f1521999-12-10 05:27:16 +000062{
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 fputc(c, file);
John Beppu059f1521999-12-10 05:27:16 +000064}
65
66/* FL_Function for closing files */
Erik Andersene49d5ec2000-02-08 19:58:47 +000067static void tee_fclose(FILE * file, char c)
John Beppu059f1521999-12-10 05:27:16 +000068{
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 fclose(file);
John Beppu059f1521999-12-10 05:27:16 +000070}
71
Eric Andersen2cb55071999-12-10 08:25:07 +000072/* ________________________________________________________________________ */
73
John Beppu059f1521999-12-10 05:27:16 +000074/* BusyBoxed tee(1) */
Erik Andersene49d5ec2000-02-08 19:58:47 +000075int tee_main(int argc, char **argv)
John Beppu059f1521999-12-10 05:27:16 +000076{
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 int i;
78 char c;
79 char opt;
80 char opt_fopen[2] = "w";
81 FILE *file;
John Beppu059f1521999-12-10 05:27:16 +000082
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 /* parse argv[] */
84 for (i = 1; i < argc; i++) {
85 if (argv[i][0] == '-') {
86 opt = argv[i][1];
87 switch (opt) {
88 case 'a':
89 opt_fopen[0] = 'a';
90 break;
Eric Andersen2cb55071999-12-10 08:25:07 +000091#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 case 'i':
93 fprintf(stderr, "ignore interrupt not implemented\n");
94 break;
Eric Andersen2cb55071999-12-10 08:25:07 +000095#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 default:
97 usage(tee_usage);
98 }
99 } else {
100 break;
101 }
John Beppu059f1521999-12-10 05:27:16 +0000102 }
John Beppu059f1521999-12-10 05:27:16 +0000103
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104 /* init FILE pointers */
John Beppu692a4502000-03-08 00:14:35 +0000105 FileList = calloc(FL_MAX, sizeof(FILE*));
106 if (!FileList) {
107 fprintf(stderr, "tee: %s\n", strerror(errno));
108 exit(1);
109 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 FL_end = 0;
111 FileList[0] = stdout;
112 for (; i < argc; i++) {
113 /* add a file to FileList */
114 file = fopen(argv[i], opt_fopen);
115 if (!file) {
116 continue;
117 }
118 if (FL_end < FL_MAX) {
119 FileList[++FL_end] = file;
120 }
Eric Andersen2cb55071999-12-10 08:25:07 +0000121 }
John Beppu059f1521999-12-10 05:27:16 +0000122
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 /* read and redirect */
124 while ((c = (char) getchar()) && (!feof(stdin))) {
125 FL_apply(tee_fwrite, c);
126 }
John Beppu059f1521999-12-10 05:27:16 +0000127
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 /* clean up */
129 FL_apply(tee_fclose, 0);
Erik Andersen298854f2000-03-23 01:09:18 +0000130 /* Don't bother to close files Exit does that
131 * automagically, so we can save a few bytes */
132 /* free(FileList); */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133 exit(0);
John Beppu059f1521999-12-10 05:27:16 +0000134}
135
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000136/* $Id: tee.c,v 1.10 2000/05/12 19:41:47 erik Exp $ */