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