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 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000 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 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 29 | /* FileList _______________________________________________________________ */ |
| 30 | |
| 31 | #define FL_MAX 1024 |
John Beppu | 692a450 | 2000-03-08 00:14:35 +0000 | [diff] [blame] | 32 | static FILE **FileList; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | static int FL_end; |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 34 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 35 | typedef void (FL_Function) (FILE * file, char c); |
| 36 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 37 | |
| 38 | /* apply a function to everything in FileList */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 39 | static void FL_apply(FL_Function * f, char c) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 40 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | int i; |
| 42 | |
| 43 | for (i = 0; i <= FL_end; i++) { |
| 44 | f(FileList[i], c); |
| 45 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 46 | } |
| 47 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 48 | /* FL_Function for writing to files*/ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | static void tee_fwrite(FILE * file, 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 | fputc(c, file); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* FL_Function for closing files */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | static void tee_fclose(FILE * file, char c) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 56 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | fclose(file); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 60 | /* ________________________________________________________________________ */ |
| 61 | |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 62 | /* BusyBoxed tee(1) */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | int tee_main(int argc, char **argv) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 64 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | int i; |
| 66 | char c; |
| 67 | char opt; |
| 68 | char opt_fopen[2] = "w"; |
| 69 | FILE *file; |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 70 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | /* parse argv[] */ |
| 72 | for (i = 1; i < argc; i++) { |
| 73 | if (argv[i][0] == '-') { |
| 74 | opt = argv[i][1]; |
| 75 | switch (opt) { |
| 76 | case 'a': |
| 77 | opt_fopen[0] = 'a'; |
| 78 | break; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 79 | #if 0 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 80 | case 'i': |
| 81 | fprintf(stderr, "ignore interrupt not implemented\n"); |
| 82 | break; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 83 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | default: |
| 85 | usage(tee_usage); |
| 86 | } |
| 87 | } else { |
| 88 | break; |
| 89 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 90 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 91 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 92 | /* init FILE pointers */ |
John Beppu | 692a450 | 2000-03-08 00:14:35 +0000 | [diff] [blame] | 93 | FileList = calloc(FL_MAX, sizeof(FILE*)); |
| 94 | if (!FileList) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 95 | errorMsg("%s\n", strerror(errno)); |
John Beppu | 692a450 | 2000-03-08 00:14:35 +0000 | [diff] [blame] | 96 | exit(1); |
| 97 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | FL_end = 0; |
| 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 | } |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 109 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 110 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 111 | /* read and redirect */ |
| 112 | while ((c = (char) getchar()) && (!feof(stdin))) { |
| 113 | FL_apply(tee_fwrite, c); |
| 114 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 115 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 116 | /* clean up */ |
| 117 | FL_apply(tee_fclose, 0); |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 118 | /* Don't bother to close files Exit does that |
| 119 | * automagically, so we can save a few bytes */ |
| 120 | /* free(FileList); */ |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 121 | return(0); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Matt Kraai | bf181b9 | 2000-07-16 20:57:15 +0000 | [diff] [blame] | 124 | /* $Id: tee.c,v 1.13 2000/07/16 20:57:15 kraai Exp $ */ |