xref: /csrg-svn/usr.bin/tee/tee.c (revision 25599)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 char copyright[] =
9 "@(#) Copyright (c) 1985 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char *sccsid = "@(#)tee.c	5.3 (Berkeley) 12/14/85";
15 #endif
16 /*
17  * tee-- pipe fitting
18  */
19 
20 #include <stdio.h>
21 #include <signal.h>
22 
23 main(argc,argv)
24 	int argc;
25 	char *argv[];
26 {
27 	register FILE **openf, **lastf, **fdp;
28 	register i, cc;
29 	char buf[8192], *calloc();
30 	int aflag = 0;
31 
32 	argc--, argv++;
33 	while (argc > 0 && argv[0][0] == '-') {
34 		switch (argv[0][1]) {
35 		case 'a':
36 			aflag++;
37 			break;
38 		case 'i':
39 		case '\0':
40 			signal(SIGINT, SIG_IGN);
41 			break;
42 		}
43 		argv++, argc--;
44 	}
45 	lastf = openf = (FILE **)calloc(argc+1, sizeof (FILE *));
46 	if (openf == 0) {
47 		fprintf(stderr, "tee: Out of memory.\n");
48 		exit(-1);
49 	}
50 	*lastf++ = stdout;	/* default */
51 	for (; argc > 0; argc--, argv++) {
52 		*lastf = fopen(argv[0], aflag ? "a" : "w");
53 		if (*lastf == NULL)
54 			fprintf(stderr, "tee: %s: cannot open.\n", argv[0]);
55 		else
56 			lastf++;
57 	}
58 	while ((cc = read(fileno(stdin), buf, sizeof (buf))) > 0)
59 		for (fdp = openf; fdp < lastf; fdp++)
60 			fwrite(buf, 1, cc, *fdp);
61 	exit(0);
62 }
63