aboutsummaryrefslogtreecommitdiff
path: root/coreutils/tee.c
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2000-08-28 03:53:27 +0000
committerMatt Kraai <kraai@debian.org>2000-08-28 03:53:27 +0000
commit1638488d26ef88e29ada0cb6faa12ac03c6705d5 (patch)
tree74e1db41e2fc1b4211bdb131519a88a47911e1e0 /coreutils/tee.c
parentab60f6987a8283f60988c7e7806bd7fbfd86fbd3 (diff)
downloadbusybox-w32-1638488d26ef88e29ada0cb6faa12ac03c6705d5.tar.gz
busybox-w32-1638488d26ef88e29ada0cb6faa12ac03c6705d5.tar.bz2
busybox-w32-1638488d26ef88e29ada0cb6faa12ac03c6705d5.zip
Rewritten by mistake.
Diffstat (limited to 'coreutils/tee.c')
-rw-r--r--coreutils/tee.c120
1 files changed, 32 insertions, 88 deletions
diff --git a/coreutils/tee.c b/coreutils/tee.c
index c15408b51..dc9876020 100644
--- a/coreutils/tee.c
+++ b/coreutils/tee.c
@@ -2,9 +2,8 @@
2/* 2/*
3 * Mini tee implementation for busybox 3 * Mini tee implementation for busybox
4 * 4 *
5 *
6 * Copyright (C) 1999,2000 by Lineo, inc. 5 * Copyright (C) 1999,2000 by Lineo, inc.
7 * Written by John Beppu <beppu@lineo.com> 6 * Written by Matt Kraai <kraai@alumni.carnegiemellon.edu>
8 * 7 *
9 * This program is free software; you can redistribute it and/or modify 8 * 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 9 * it under the terms of the GNU General Public License as published by
@@ -23,102 +22,47 @@
23 */ 22 */
24 23
25#include "internal.h" 24#include "internal.h"
26#include <errno.h> 25#include <getopt.h>
27#include <stdio.h> 26#include <stdio.h>
28 27
29/* FileList _______________________________________________________________ */ 28int
30 29tee_main(int argc, char **argv)
31#define FL_MAX 1024
32static FILE **FileList;
33static int FL_end;
34
35typedef void (FL_Function) (FILE * file, char c);
36
37
38/* apply a function to everything in FileList */
39static void FL_apply(FL_Function * f, char c)
40{
41 int i;
42
43 for (i = 0; i <= FL_end; i++) {
44 f(FileList[i], c);
45 }
46}
47
48/* FL_Function for writing to files*/
49static void tee_fwrite(FILE * file, char c)
50{
51 fputc(c, file);
52}
53
54/* FL_Function for closing files */
55static void tee_fclose(FILE * file, char c)
56{
57 fclose(file);
58}
59
60/* ________________________________________________________________________ */
61
62/* BusyBoxed tee(1) */
63int tee_main(int argc, char **argv)
64{ 30{
65 int i; 31 char *mode = "w";
66 char c; 32 int c, i, status = 0, nfiles = 0;
67 char opt; 33 FILE **files;
68 char opt_fopen[2] = "w"; 34
69 FILE *file; 35 while ((c = getopt(argc, argv, "a")) != EOF) {
70 36 switch (c) {
71 /* parse argv[] */ 37 case 'a':
72 for (i = 1; i < argc; i++) { 38 mode = "a";
73 if (argv[i][0] == '-') {
74 opt = argv[i][1];
75 switch (opt) {
76 case 'a':
77 opt_fopen[0] = 'a';
78 break;
79#if 0
80 case 'i':
81 fprintf(stderr, "ignore interrupt not implemented\n");
82 break;
83#endif
84 default:
85 usage(tee_usage);
86 }
87 } else {
88 break; 39 break;
40 default:
41 usage(tee_usage);
89 } 42 }
90 } 43 }
91 44
92 /* init FILE pointers */ 45 files = (FILE **)xmalloc(sizeof(FILE *) * (argc - optind + 1));
93 FileList = calloc(FL_MAX, sizeof(FILE*)); 46 files[nfiles++] = stdout;
94 if (!FileList) { 47 while (optind < argc) {
95 errorMsg("%s\n", strerror(errno)); 48 if ((files[nfiles++] = fopen(argv[optind++], mode)) == NULL) {
96 exit(1); 49 nfiles--;
97 } 50 errorMsg("%s: %s\n", argv[optind-1], strerror(errno));
98 FL_end = 0; 51 status = 1;
99 FileList[0] = stdout;
100 for (; i < argc; i++) {
101 /* add a file to FileList */
102 file = fopen(argv[i], opt_fopen);
103 if (!file) {
104 continue;
105 }
106 if (FL_end < FL_MAX) {
107 FileList[++FL_end] = file;
108 } 52 }
109 } 53 }
110 54
111 /* read and redirect */ 55 while ((c = getchar()) != EOF)
112 while ((c = (char) getchar()) && (!feof(stdin))) { 56 for (i = 0; i < nfiles; i++)
113 FL_apply(tee_fwrite, c); 57 putc(c, files[i]);
114 }
115 58
116 /* clean up */ 59 return status;
117 FL_apply(tee_fclose, 0);
118 /* Don't bother to close files Exit does that
119 * automagically, so we can save a few bytes */
120 /* free(FileList); */
121 return(0);
122} 60}
123 61
124/* $Id: tee.c,v 1.13 2000/07/16 20:57:15 kraai Exp $ */ 62/*
63Local Variables:
64c-file-style: "linux"
65c-basic-offset: 4
66tab-width: 4
67End:
68*/