blob: fe444fcc4a66b6708b7cf6cb088d7c96b698ffc9 [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.
6 * Written by John Beppu <beppu@line.com>
7 *
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
24#include <stdio.h>
25#include <stdlib.h>
26
27/* FileList _______________________________________________________________ */
28
29#define FL_MAX 1024
30static FILE *FileList[FL_MAX];
31static int FL_end;
32
33typedef void (FL_Function)(FILE *file, char c);
34
35/* initialize FileList */
36static void
37FL_init()
38{
39 FL_end = 0;
40 FileList[0] = stdout;
41}
42
43/* add a file to FileList */
44static int
45FL_add(const char *filename, char *opt_open)
46{
47 FILE *file;
48
49 file = fopen(filename, opt_open);
50 if (!file) { return 0; };
51 if (FL_end < FL_MAX) {
52 FileList[++FL_end] = file;
53 }
54 return 1;
55}
56
57/* apply a function to everything in FileList */
58static void
59FL_apply(FL_Function *f, char c)
60{
61 int i;
62 for (i = 0; i <= FL_end; i++) {
63 f(FileList[i], c);
64 }
65}
66
67/* ________________________________________________________________________ */
68
69/* FL_Function for writing to files*/
70static void
71tee_fwrite(FILE *file, char c)
72{
73 fputc(c, file);
74}
75
76/* FL_Function for closing files */
77static void
78tee_fclose(FILE *file, char c)
79{
80 fclose(file);
81}
82
83/* help message */
84static void
85tee_usage()
86{
87 fprintf (
88 stdout,
89 "%s\n%s\n%s\n%s\n%s\n",
90 "Usage: tee [OPTION]... [FILE]...",
91 "Copy standard input to each FILE, and also to standard output.\n",
92 " -a, append to the given FILEs, do not overwrite",
93 " -i, ignore interrupt signals",
94 " -h, this help message"
95 );
96 exit(1);
97}
98
99/* BusyBoxed tee(1) */
100int
101tee_main(int argc, char **argv)
102{
103 int i;
104 char c;
105 char opt;
106 char opt_fopen[2] = "w";
107
108 /* parse argv[] */
109 for (i = 1; i < argc; i++) {
110 if (argv[i][0] == '-') {
111 opt = argv[i][1];
112 switch (opt) {
113 case 'a':
114 opt_fopen[0] = 'a';
115 break;
116 case 'i':
117 fprintf(stderr, "ingore interrupt not implemented\n");
118 break;
119 case 'h':
120 tee_usage();
121 break;
122 default:
123 fprintf(stderr, "tee: invalid option -- %c\n", opt);
124 tee_usage();
125 }
126 } else {
127 break;
128 }
129 }
130
131 /* init FILE pointers */
132 FL_init();
133 for ( ; i < argc; i++) {
134 FL_add(argv[i], opt_fopen);
135 }
136
137 /* read and redirect */
138 while ((c = (char) getchar()) && (!feof(stdin))) {
139 FL_apply(tee_fwrite, c);
140 }
141
142 /* clean up */
143 FL_apply(tee_fclose, 0);
144 exit(0);
145}
146
147/* $Id: tee.c,v 1.1 1999/12/10 05:27:16 beppu Exp $ */