diff options
| author | John Beppu <beppu@lbox.org> | 1999-12-10 05:27:16 +0000 |
|---|---|---|
| committer | John Beppu <beppu@lbox.org> | 1999-12-10 05:27:16 +0000 |
| commit | 059f152a7a2d1796cf93d1a79f33055d38b94f2a (patch) | |
| tree | f099ca4df6d8a137af037e0bfd6976d06a627e66 /coreutils | |
| parent | 29fcc90f3e0ebd8b4d4d5f1a96741f982baa38f1 (diff) | |
| download | busybox-w32-059f152a7a2d1796cf93d1a79f33055d38b94f2a.tar.gz busybox-w32-059f152a7a2d1796cf93d1a79f33055d38b94f2a.tar.bz2 busybox-w32-059f152a7a2d1796cf93d1a79f33055d38b94f2a.zip | |
Implemented tee(1).
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/tee.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/coreutils/tee.c b/coreutils/tee.c new file mode 100644 index 000000000..fe444fcc4 --- /dev/null +++ b/coreutils/tee.c | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | /* | ||
| 2 | * Mini tee implementation for busybox | ||
| 3 | * | ||
| 4 | * | ||
| 5 | * Copyright (C) 1999 by Lineo, inc. | ||
| 6 | * Written by John Beppu <beppu@line.com> | ||
| 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 | |||
| 24 | #include <stdio.h> | ||
| 25 | #include <stdlib.h> | ||
| 26 | |||
| 27 | /* FileList _______________________________________________________________ */ | ||
| 28 | |||
| 29 | #define FL_MAX 1024 | ||
| 30 | static FILE *FileList[FL_MAX]; | ||
| 31 | static int FL_end; | ||
| 32 | |||
| 33 | typedef void (FL_Function)(FILE *file, char c); | ||
| 34 | |||
| 35 | /* initialize FileList */ | ||
| 36 | static void | ||
| 37 | FL_init() | ||
| 38 | { | ||
| 39 | FL_end = 0; | ||
| 40 | FileList[0] = stdout; | ||
| 41 | } | ||
| 42 | |||
| 43 | /* add a file to FileList */ | ||
| 44 | static int | ||
| 45 | FL_add(const char *filename, char *opt_open) | ||
| 46 | { | ||
| 47 | FILE *file; | ||
| 48 | |||
| 49 | file = fopen(filename, opt_open); | ||
| 50 | if (!file) { return 0; }; | ||
| 51 | if (FL_end < FL_MAX) { | ||
| 52 | FileList[++FL_end] = file; | ||
| 53 | } | ||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | /* apply a function to everything in FileList */ | ||
| 58 | static void | ||
| 59 | FL_apply(FL_Function *f, char c) | ||
| 60 | { | ||
| 61 | int i; | ||
| 62 | for (i = 0; i <= FL_end; i++) { | ||
| 63 | f(FileList[i], c); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | /* ________________________________________________________________________ */ | ||
| 68 | |||
| 69 | /* FL_Function for writing to files*/ | ||
| 70 | static void | ||
| 71 | tee_fwrite(FILE *file, char c) | ||
| 72 | { | ||
| 73 | fputc(c, file); | ||
| 74 | } | ||
| 75 | |||
| 76 | /* FL_Function for closing files */ | ||
| 77 | static void | ||
| 78 | tee_fclose(FILE *file, char c) | ||
| 79 | { | ||
| 80 | fclose(file); | ||
| 81 | } | ||
| 82 | |||
| 83 | /* help message */ | ||
| 84 | static void | ||
| 85 | tee_usage() | ||
| 86 | { | ||
| 87 | fprintf ( | ||
| 88 | stdout, | ||
| 89 | "%s\n%s\n%s\n%s\n%s\n", | ||
| 90 | "Usage: tee [OPTION]... [FILE]...", | ||
| 91 | "Copy standard input to each FILE, and also to standard output.\n", | ||
| 92 | " -a, append to the given FILEs, do not overwrite", | ||
| 93 | " -i, ignore interrupt signals", | ||
| 94 | " -h, this help message" | ||
| 95 | ); | ||
| 96 | exit(1); | ||
| 97 | } | ||
| 98 | |||
| 99 | /* BusyBoxed tee(1) */ | ||
| 100 | int | ||
| 101 | tee_main(int argc, char **argv) | ||
| 102 | { | ||
| 103 | int i; | ||
| 104 | char c; | ||
| 105 | char opt; | ||
| 106 | char opt_fopen[2] = "w"; | ||
| 107 | |||
| 108 | /* parse argv[] */ | ||
| 109 | for (i = 1; i < argc; i++) { | ||
| 110 | if (argv[i][0] == '-') { | ||
| 111 | opt = argv[i][1]; | ||
| 112 | switch (opt) { | ||
| 113 | case 'a': | ||
| 114 | opt_fopen[0] = 'a'; | ||
| 115 | break; | ||
| 116 | case 'i': | ||
| 117 | fprintf(stderr, "ingore interrupt not implemented\n"); | ||
| 118 | break; | ||
| 119 | case 'h': | ||
| 120 | tee_usage(); | ||
| 121 | break; | ||
| 122 | default: | ||
| 123 | fprintf(stderr, "tee: invalid option -- %c\n", opt); | ||
| 124 | tee_usage(); | ||
| 125 | } | ||
| 126 | } else { | ||
| 127 | break; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | /* init FILE pointers */ | ||
| 132 | FL_init(); | ||
| 133 | for ( ; i < argc; i++) { | ||
| 134 | FL_add(argv[i], opt_fopen); | ||
| 135 | } | ||
| 136 | |||
| 137 | /* read and redirect */ | ||
| 138 | while ((c = (char) getchar()) && (!feof(stdin))) { | ||
| 139 | FL_apply(tee_fwrite, c); | ||
| 140 | } | ||
| 141 | |||
| 142 | /* clean up */ | ||
| 143 | FL_apply(tee_fclose, 0); | ||
| 144 | exit(0); | ||
| 145 | } | ||
| 146 | |||
| 147 | /* $Id: tee.c,v 1.1 1999/12/10 05:27:16 beppu Exp $ */ | ||
