blob: 1c145426a166c0e796b5dc2cd7f23249d3d051ef [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 Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 2000,2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
John Beppu059f1521999-12-10 05:27:16 +00006 *
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 Andersen3570a342000-09-25 21:45:58 +000023#include "busybox.h"
Matt Kraai16384882000-08-28 03:53:27 +000024#include <getopt.h>
John Beppu059f1521999-12-10 05:27:16 +000025#include <stdio.h>
John Beppua3e0d791999-12-10 07:41:03 +000026
Matt Kraai16384882000-08-28 03:53:27 +000027int
28tee_main(int argc, char **argv)
John Beppu059f1521999-12-10 05:27:16 +000029{
Matt Kraai16384882000-08-28 03:53:27 +000030 char *mode = "w";
31 int c, i, status = 0, nfiles = 0;
32 FILE **files;
Erik Andersene49d5ec2000-02-08 19:58:47 +000033
Matt Kraai16384882000-08-28 03:53:27 +000034 while ((c = getopt(argc, argv, "a")) != EOF) {
35 switch (c) {
36 case 'a':
37 mode = "a";
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 break;
Matt Kraai16384882000-08-28 03:53:27 +000039 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000040 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 }
John Beppu059f1521999-12-10 05:27:16 +000042 }
John Beppu059f1521999-12-10 05:27:16 +000043
Matt Kraai16384882000-08-28 03:53:27 +000044 files = (FILE **)xmalloc(sizeof(FILE *) * (argc - optind + 1));
45 files[nfiles++] = stdout;
46 while (optind < argc) {
Glenn L McGrath1d098ce2001-10-05 05:24:19 +000047 if ((files[nfiles++] = wfopen(argv[optind++], mode)) == NULL) {
Matt Kraai16384882000-08-28 03:53:27 +000048 nfiles--;
Matt Kraai16384882000-08-28 03:53:27 +000049 status = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 }
Eric Andersen2cb55071999-12-10 08:25:07 +000051 }
John Beppu059f1521999-12-10 05:27:16 +000052
Matt Kraai16384882000-08-28 03:53:27 +000053 while ((c = getchar()) != EOF)
54 for (i = 0; i < nfiles; i++)
55 putc(c, files[i]);
John Beppu059f1521999-12-10 05:27:16 +000056
Matt Kraai16384882000-08-28 03:53:27 +000057 return status;
John Beppu059f1521999-12-10 05:27:16 +000058}
59
Matt Kraai16384882000-08-28 03:53:27 +000060/*
61Local Variables:
62c-file-style: "linux"
63c-basic-offset: 4
64tab-width: 4
65End:
66*/