Added poweroff (and adjusted init to use it). Inlined function
[oweals/busybox.git] / coreutils / 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     "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
37
38 /* FileList _______________________________________________________________ */
39
40 #define FL_MAX  1024
41 static FILE *FileList[FL_MAX];
42 static int  FL_end;
43
44 typedef void (FL_Function)(FILE *file, char c);
45     
46
47 /* apply a function to everything in FileList */
48 static void
49 FL_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
57 /* FL_Function for writing to files*/
58 static void
59 tee_fwrite(FILE *file, char c)
60 {
61     fputc(c, file);
62 }
63
64 /* FL_Function for closing files */
65 static void
66 tee_fclose(FILE *file, char c)
67 {
68     fclose(file);
69 }
70
71 /* ________________________________________________________________________ */
72
73 /* BusyBoxed tee(1) */
74 int
75 tee_main(int argc, char **argv)
76 {
77     int     i;
78     char    c;
79     char    opt;
80     char    opt_fopen[2] = "w";
81     FILE    *file;
82
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;
91 #if 0
92                 case 'i':
93                     fprintf(stderr, "ignore interrupt not implemented\n");
94                     break;
95 #endif
96                 default:
97                     usage(tee_usage);
98             }
99         } else {
100             break;
101         }
102     }
103
104     /* init FILE pointers */
105     FL_end = 0;
106     FileList[0] = stdout;
107     for ( ; i < argc; i++) {
108         /* 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         }
114     }
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
126 /* $Id: tee.c,v 1.4 1999/12/10 08:25:07 andersen Exp $ */