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 | 8ec10a9 | 2001-01-27 09:33:39 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000,2001 by Lineo, inc. |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 6 | * Written by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
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 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 24 | #include "busybox.h" |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 25 | #include <getopt.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 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 28 | int |
| 29 | tee_main(int argc, char **argv) |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 30 | { |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 31 | char *mode = "w"; |
| 32 | int c, i, status = 0, nfiles = 0; |
| 33 | FILE **files; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 34 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 35 | while ((c = getopt(argc, argv, "a")) != EOF) { |
| 36 | switch (c) { |
| 37 | case 'a': |
| 38 | mode = "a"; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 39 | break; |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 40 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 41 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 43 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 44 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 45 | files = (FILE **)xmalloc(sizeof(FILE *) * (argc - optind + 1)); |
| 46 | files[nfiles++] = stdout; |
| 47 | while (optind < argc) { |
Glenn L McGrath | 1d098ce | 2001-10-05 05:24:19 +0000 | [diff] [blame^] | 48 | if ((files[nfiles++] = wfopen(argv[optind++], mode)) == NULL) { |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 49 | nfiles--; |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 50 | status = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 51 | } |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 52 | } |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 53 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 54 | while ((c = getchar()) != EOF) |
| 55 | for (i = 0; i < nfiles; i++) |
| 56 | putc(c, files[i]); |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 57 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 58 | return status; |
John Beppu | 059f152 | 1999-12-10 05:27:16 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Matt Kraai | 1638488 | 2000-08-28 03:53:27 +0000 | [diff] [blame] | 61 | /* |
| 62 | Local Variables: |
| 63 | c-file-style: "linux" |
| 64 | c-basic-offset: 4 |
| 65 | tab-width: 4 |
| 66 | End: |
| 67 | */ |