blob: 8d1ca6efd49e7dd21467ecca03776df384ad5258 [file] [log] [blame]
John Beppu059f1521999-12-10 05:27:16 +00001/*
2 * Mini tee implementation for busybox
3 *
4 *
5 * Copyright (C) 1999 by Lineo, inc.
Eric Andersen70e2f0b1999-12-10 06:45:42 +00006 * Written by John Beppu <beppu@lineo.com>
John Beppu059f1521999-12-10 05:27:16 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
John Beppua3e0d791999-12-10 07:41:03 +000024#include "internal.h"
John Beppu059f1521999-12-10 05:27:16 +000025#include <stdio.h>
John Beppua3e0d791999-12-10 07:41:03 +000026
27static const char tee_usage[] =
Eric Andersen2cb55071999-12-10 08:25:07 +000028 "tee [OPTION]... [FILE]...\n\n"
29 "Copy standard input to each FILE, and also to standard output.\n\n"
30 "Options:\n"
31 "\t-a\tappend to the given FILEs, do not overwrite\n"
32#if 0
33 "\t-i\tignore interrupt signals\n"
34#endif
35 ;
36
John Beppu059f1521999-12-10 05:27:16 +000037
38/* FileList _______________________________________________________________ */
39
40#define FL_MAX 1024
41static FILE *FileList[FL_MAX];
42static int FL_end;
43
44typedef void (FL_Function)(FILE *file, char c);
45
John Beppu059f1521999-12-10 05:27:16 +000046
47/* apply a function to everything in FileList */
48static void
49FL_apply(FL_Function *f, char c)
50{
51 int i;
52 for (i = 0; i <= FL_end; i++) {
53 f(FileList[i], c);
54 }
55}
56
John Beppu059f1521999-12-10 05:27:16 +000057/* FL_Function for writing to files*/
58static void
59tee_fwrite(FILE *file, char c)
60{
61 fputc(c, file);
62}
63
64/* FL_Function for closing files */
65static void
66tee_fclose(FILE *file, char c)
67{
68 fclose(file);
69}
70
Eric Andersen2cb55071999-12-10 08:25:07 +000071/* ________________________________________________________________________ */
72
John Beppu059f1521999-12-10 05:27:16 +000073/* BusyBoxed tee(1) */
74int
75tee_main(int argc, char **argv)
76{
77 int i;
78 char c;
79 char opt;
80 char opt_fopen[2] = "w";
Eric Andersen2cb55071999-12-10 08:25:07 +000081 FILE *file;
John Beppu059f1521999-12-10 05:27:16 +000082
83 /* 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
John Beppu059f1521999-12-10 05:27:16 +000092 case 'i':
Eric Andersen2cb55071999-12-10 08:25:07 +000093 fprintf(stderr, "ignore interrupt not implemented\n");
John Beppu059f1521999-12-10 05:27:16 +000094 break;
Eric Andersen2cb55071999-12-10 08:25:07 +000095#endif
John Beppu059f1521999-12-10 05:27:16 +000096 default:
John Beppua3e0d791999-12-10 07:41:03 +000097 usage(tee_usage);
John Beppu059f1521999-12-10 05:27:16 +000098 }
99 } else {
100 break;
101 }
102 }
103
104 /* init FILE pointers */
Eric Andersen2cb55071999-12-10 08:25:07 +0000105 FL_end = 0;
106 FileList[0] = stdout;
John Beppu059f1521999-12-10 05:27:16 +0000107 for ( ; i < argc; i++) {
Eric Andersen2cb55071999-12-10 08:25:07 +0000108 /* add a file to FileList */
109 file = fopen(argv[i], opt_fopen);
110 if (!file) { continue; }
111 if (FL_end < FL_MAX) {
112 FileList[++FL_end] = file;
113 }
John Beppu059f1521999-12-10 05:27:16 +0000114 }
115
116 /* read and redirect */
117 while ((c = (char) getchar()) && (!feof(stdin))) {
118 FL_apply(tee_fwrite, c);
119 }
120
121 /* clean up */
122 FL_apply(tee_fclose, 0);
123 exit(0);
124}
125
Eric Andersen2cb55071999-12-10 08:25:07 +0000126/* $Id: tee.c,v 1.4 1999/12/10 08:25:07 andersen Exp $ */