blob: f0eca070d558249379aaefffddd85ee969c8cfc4 [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 *
Erik Andersen61677fe2000-04-13 01:18:56 +00005 * Copyright (C) 1999,2000 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:
41 usage(tee_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) {
48 if ((files[nfiles++] = fopen(argv[optind++], mode)) == NULL) {
49 nfiles--;
Matt Kraai1fa1ade2000-12-18 03:57:16 +000050 perror_msg("%s", argv[optind-1]);
Matt Kraai16384882000-08-28 03:53:27 +000051 status = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 }
Eric Andersen2cb55071999-12-10 08:25:07 +000053 }
John Beppu059f1521999-12-10 05:27:16 +000054
Matt Kraai16384882000-08-28 03:53:27 +000055 while ((c = getchar()) != EOF)
56 for (i = 0; i < nfiles; i++)
57 putc(c, files[i]);
John Beppu059f1521999-12-10 05:27:16 +000058
Matt Kraai16384882000-08-28 03:53:27 +000059 return status;
John Beppu059f1521999-12-10 05:27:16 +000060}
61
Matt Kraai16384882000-08-28 03:53:27 +000062/*
63Local Variables:
64c-file-style: "linux"
65c-basic-offset: 4
66tab-width: 4
67End:
68*/