Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini tee implementation for busybox |
| 4 | * |
| 5 | * |
| 6 | * Copyright (C) 1999 by Lineo, inc. |
Eric Andersen | 70e2f0b | 1999-12-10 06:45:42 +0000 | [diff] [blame] | 7 | * Written by John Beppu <beppu@lineo.com> |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 8 | * |
| 9 | * 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 |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
John Beppu | a3e0d79 | 1999-12-10 07:41:03 +0000 | [diff] [blame] | 25 | #include "internal.h" |
John Beppu | 692a450 | 2000-03-08 00:14:35 +0000 | [diff] [blame] | 26 | #include <errno.h> |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
John Beppu | a3e0d79 | 1999-12-10 07:41:03 +0000 | [diff] [blame] | 28 | |
| 29 | static const char tee_usage[] = |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 30 | "tee [OPTION]... [FILE]...\n\n" |
| 31 | "Copy standard input to each FILE, and also to standard output.\n\n" |
| 32 | "Options:\n" "\t-a\tappend to the given FILEs, do not overwrite\n" |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 33 | #if 0 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 34 | "\t-i\tignore interrupt signals\n" |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 35 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 36 | ; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 37 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 38 | |
| 39 | /* FileList _______________________________________________________________ */ |
| 40 | |
| 41 | #define FL_MAX 1024 |
John Beppu | 692a450 | 2000-03-08 00:14:35 +0000 | [diff] [blame] | 42 | static FILE **FileList; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 43 | static int FL_end; |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 44 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 45 | typedef void (FL_Function) (FILE * file, char c); |
| 46 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 47 | |
| 48 | /* apply a function to everything in FileList */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | static void FL_apply(FL_Function * f, char c) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 50 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 51 | int i; |
| 52 | |
| 53 | for (i = 0; i <= FL_end; i++) { |
| 54 | f(FileList[i], c); |
| 55 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 56 | } |
| 57 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 58 | /* FL_Function for writing to files*/ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | static void tee_fwrite(FILE * file, char c) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 60 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | fputc(c, file); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* FL_Function for closing files */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | static void tee_fclose(FILE * file, char c) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 66 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | fclose(file); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 70 | /* ________________________________________________________________________ */ |
| 71 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 72 | /* BusyBoxed tee(1) */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | int tee_main(int argc, char **argv) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 74 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | int i; |
| 76 | char c; |
| 77 | char opt; |
| 78 | char opt_fopen[2] = "w"; |
| 79 | FILE *file; |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 80 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | /* parse argv[] */ |
| 82 | for (i = 1; i < argc; i++) { |
| 83 | if (argv[i][0] == '-') { |
| 84 | opt = argv[i][1]; |
| 85 | switch (opt) { |
| 86 | case 'a': |
| 87 | opt_fopen[0] = 'a'; |
| 88 | break; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 89 | #if 0 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | case 'i': |
| 91 | fprintf(stderr, "ignore interrupt not implemented\n"); |
| 92 | break; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 93 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 94 | default: |
| 95 | usage(tee_usage); |
| 96 | } |
| 97 | } else { |
| 98 | break; |
| 99 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 100 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 101 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 102 | /* init FILE pointers */ |
John Beppu | 692a450 | 2000-03-08 00:14:35 +0000 | [diff] [blame] | 103 | FileList = calloc(FL_MAX, sizeof(FILE*)); |
| 104 | if (!FileList) { |
| 105 | fprintf(stderr, "tee: %s\n", strerror(errno)); |
| 106 | exit(1); |
| 107 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 108 | FL_end = 0; |
| 109 | FileList[0] = stdout; |
| 110 | for (; i < argc; i++) { |
| 111 | /* add a file to FileList */ |
| 112 | file = fopen(argv[i], opt_fopen); |
| 113 | if (!file) { |
| 114 | continue; |
| 115 | } |
| 116 | if (FL_end < FL_MAX) { |
| 117 | FileList[++FL_end] = file; |
| 118 | } |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 119 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 120 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 121 | /* read and redirect */ |
| 122 | while ((c = (char) getchar()) && (!feof(stdin))) { |
| 123 | FL_apply(tee_fwrite, c); |
| 124 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 125 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 126 | /* clean up */ |
| 127 | FL_apply(tee_fclose, 0); |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 128 | /* Don't bother to close files Exit does that |
| 129 | * automagically, so we can save a few bytes */ |
| 130 | /* free(FileList); */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 131 | exit(0); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 134 | /* $Id: tee.c,v 1.8 2000/03/23 01:09:18 erik Exp $ */ |