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 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000,2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 23 | #include "busybox.h" |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 24 | #include <getopt.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 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 27 | int |
| 28 | tee_main(int argc, char **argv) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 29 | { |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 30 | char *mode = "w"; |
| 31 | int c, i, status = 0, nfiles = 0; |
| 32 | FILE **files; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 34 | while ((c = getopt(argc, argv, "a")) != EOF) { |
| 35 | switch (c) { |
| 36 | case 'a': |
| 37 | mode = "a"; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | break; |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 39 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 40 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 42 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 43 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 44 | files = (FILE **)xmalloc(sizeof(FILE *) * (argc - optind + 1)); |
| 45 | files[nfiles++] = stdout; |
| 46 | while (optind < argc) { |
Glenn L McGrath | 1d098ce | 2001-10-05 05:24:19 +0000 | [diff] [blame] | 47 | if ((files[nfiles++] = wfopen(argv[optind++], mode)) == NULL) { |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 48 | nfiles--; |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 49 | status = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | } |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 51 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 52 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 53 | while ((c = getchar()) != EOF) |
| 54 | for (i = 0; i < nfiles; i++) |
| 55 | putc(c, files[i]); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 56 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 57 | return status; |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 60 | /* |
| 61 | Local Variables: |
| 62 | c-file-style: "linux" |
| 63 | c-basic-offset: 4 |
| 64 | tab-width: 4 |
| 65 | End: |
| 66 | */ |