John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini tee implementation for busybox |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
Eric Andersen | 70e2f0b | 1999-12-10 06:45:42 +0000 | [diff] [blame] | 6 | * Written by John Beppu <beppu@lineo.com> |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 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 | |
John Beppu | a3e0d79 | 1999-12-10 07:41:03 +0000 | [diff] [blame] | 24 | #include "internal.h" |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
John Beppu | a3e0d79 | 1999-12-10 07:41:03 +0000 | [diff] [blame] | 26 | |
| 27 | static const char tee_usage[] = |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 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 | |
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]; |
| 42 | static int FL_end; |
| 43 | |
| 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 */ |
| 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 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 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 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 71 | /* ________________________________________________________________________ */ |
| 72 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 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"; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 81 | FILE *file; |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 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; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 91 | #if 0 |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 92 | case 'i': |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 93 | fprintf(stderr, "ignore interrupt not implemented\n"); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 94 | break; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 95 | #endif |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 96 | default: |
John Beppu | a3e0d79 | 1999-12-10 07:41:03 +0000 | [diff] [blame] | 97 | usage(tee_usage); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 98 | } |
| 99 | } else { |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* init FILE pointers */ |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 105 | FL_end = 0; |
| 106 | FileList[0] = stdout; |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 107 | for ( ; i < argc; i++) { |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 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 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 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 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 126 | /* $Id: tee.c,v 1.4 1999/12/10 08:25:07 andersen Exp $ */ |