blob: 64a0922b7ddb21ec81789cdfdf2cee0989bb69c1 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppu059f1521999-12-10 05:27:16 +00002/*
3 * Mini tee implementation for busybox
4 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Matt Kraai16384882000-08-28 03:53:27 +00006 * Written by Matt Kraai <kraai@alumni.carnegiemellon.edu>
John Beppu059f1521999-12-10 05:27:16 +00007 *
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 Andersen3570a342000-09-25 21:45:58 +000024#include "busybox.h"
Matt Kraai16384882000-08-28 03:53:27 +000025#include <getopt.h>
John Beppu059f1521999-12-10 05:27:16 +000026#include <stdio.h>
John Beppua3e0d791999-12-10 07:41:03 +000027
Matt Kraai16384882000-08-28 03:53:27 +000028int
29tee_main(int argc, char **argv)
John Beppu059f1521999-12-10 05:27:16 +000030{
Matt Kraai16384882000-08-28 03:53:27 +000031 char *mode = "w";
32 int c, i, status = 0, nfiles = 0;
33 FILE **files;
Erik Andersene49d5ec2000-02-08 19:58:47 +000034
Matt Kraai16384882000-08-28 03:53:27 +000035 while ((c = getopt(argc, argv, "a")) != EOF) {
36 switch (c) {
37 case 'a':
38 mode = "a";
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 break;
Matt Kraai16384882000-08-28 03:53:27 +000040 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000041 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 }
John Beppu059f1521999-12-10 05:27:16 +000043 }
John Beppu059f1521999-12-10 05:27:16 +000044
Matt Kraai16384882000-08-28 03:53:27 +000045 files = (FILE **)xmalloc(sizeof(FILE *) * (argc - optind + 1));
46 files[nfiles++] = stdout;
47 while (optind < argc) {
Glenn L McGrath1d098ce2001-10-05 05:24:19 +000048 if ((files[nfiles++] = wfopen(argv[optind++], mode)) == NULL) {
Matt Kraai16384882000-08-28 03:53:27 +000049 nfiles--;
Matt Kraai16384882000-08-28 03:53:27 +000050 status = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 }
Eric Andersen2cb55071999-12-10 08:25:07 +000052 }
John Beppu059f1521999-12-10 05:27:16 +000053
Matt Kraai16384882000-08-28 03:53:27 +000054 while ((c = getchar()) != EOF)
55 for (i = 0; i < nfiles; i++)
56 putc(c, files[i]);
John Beppu059f1521999-12-10 05:27:16 +000057
Matt Kraai16384882000-08-28 03:53:27 +000058 return status;
John Beppu059f1521999-12-10 05:27:16 +000059}
60
Matt Kraai16384882000-08-28 03:53:27 +000061/*
62Local Variables:
63c-file-style: "linux"
64c-basic-offset: 4
65tab-width: 4
66End:
67*/