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 | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
John Beppu | a3e0d79 | 1999-12-10 07:41:03 +0000 | [diff] [blame] | 27 | |
| 28 | static const char tee_usage[] = |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 29 | "tee [OPTION]... [FILE]...\n\n" |
| 30 | "Copy standard input to each FILE, and also to standard output.\n\n" |
| 31 | "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] | 32 | #if 0 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | "\t-i\tignore interrupt signals\n" |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 34 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 35 | ; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 36 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 37 | |
| 38 | /* FileList _______________________________________________________________ */ |
| 39 | |
| 40 | #define FL_MAX 1024 |
| 41 | static FILE *FileList[FL_MAX]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | static int FL_end; |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 43 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | typedef void (FL_Function) (FILE * file, char c); |
| 45 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 46 | |
| 47 | /* apply a function to everything in FileList */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 48 | static void FL_apply(FL_Function * f, char c) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 49 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | int i; |
| 51 | |
| 52 | for (i = 0; i <= FL_end; i++) { |
| 53 | f(FileList[i], c); |
| 54 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 55 | } |
| 56 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 57 | /* FL_Function for writing to files*/ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | static void tee_fwrite(FILE * file, char c) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 59 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | fputc(c, file); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* FL_Function for closing files */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | static void tee_fclose(FILE * file, char c) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 65 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | fclose(file); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 69 | /* ________________________________________________________________________ */ |
| 70 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 71 | /* BusyBoxed tee(1) */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | int tee_main(int argc, char **argv) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 73 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | int i; |
| 75 | char c; |
| 76 | char opt; |
| 77 | char opt_fopen[2] = "w"; |
| 78 | FILE *file; |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 79 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 80 | /* parse argv[] */ |
| 81 | for (i = 1; i < argc; i++) { |
| 82 | if (argv[i][0] == '-') { |
| 83 | opt = argv[i][1]; |
| 84 | switch (opt) { |
| 85 | case 'a': |
| 86 | opt_fopen[0] = 'a'; |
| 87 | break; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 88 | #if 0 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | case 'i': |
| 90 | fprintf(stderr, "ignore interrupt not implemented\n"); |
| 91 | break; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 92 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 93 | default: |
| 94 | usage(tee_usage); |
| 95 | } |
| 96 | } else { |
| 97 | break; |
| 98 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 99 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 100 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 101 | /* init FILE pointers */ |
| 102 | FL_end = 0; |
| 103 | FileList[0] = stdout; |
| 104 | for (; i < argc; i++) { |
| 105 | /* add a file to FileList */ |
| 106 | file = fopen(argv[i], opt_fopen); |
| 107 | if (!file) { |
| 108 | continue; |
| 109 | } |
| 110 | if (FL_end < FL_MAX) { |
| 111 | FileList[++FL_end] = file; |
| 112 | } |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 113 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 114 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | /* read and redirect */ |
| 116 | while ((c = (char) getchar()) && (!feof(stdin))) { |
| 117 | FL_apply(tee_fwrite, c); |
| 118 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 119 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 120 | /* clean up */ |
| 121 | FL_apply(tee_fclose, 0); |
| 122 | exit(0); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 125 | /* $Id: tee.c,v 1.6 2000/02/08 19:58:47 erik Exp $ */ |