11123Sbill /* 2*35063Sbostic * Copyright (c) 1988 Regents of the University of California. 3*35063Sbostic * All rights reserved. 4*35063Sbostic * 5*35063Sbostic * Redistribution and use in source and binary forms are permitted 6*35063Sbostic * provided that the above copyright notice and this paragraph are 7*35063Sbostic * duplicated in all such forms and that any documentation, 8*35063Sbostic * advertising materials, and other materials related to such 9*35063Sbostic * distribution and use acknowledge that the software was developed 10*35063Sbostic * by the University of California, Berkeley. The name of the 11*35063Sbostic * University may not be used to endorse or promote products derived 12*35063Sbostic * from this software without specific prior written permission. 13*35063Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*35063Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*35063Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 161123Sbill */ 171123Sbill 18*35063Sbostic #ifndef lint 19*35063Sbostic char copyright[] = 20*35063Sbostic "@(#) Copyright (c) 1988 Regents of the University of California.\n\ 21*35063Sbostic All rights reserved.\n"; 22*35063Sbostic #endif /* not lint */ 231123Sbill 24*35063Sbostic #ifndef lint 25*35063Sbostic static char sccsid[] = "@(#)tee.c 5.5 (Berkeley) 07/10/88"; 26*35063Sbostic #endif /* not lint */ 2725600Ssam 28*35063Sbostic #include <sys/types.h> 29*35063Sbostic #include <sys/file.h> 30*35063Sbostic #include <signal.h> 31*35063Sbostic #include <stdio.h> 3225600Ssam 33*35063Sbostic main(argc, argv) 34*35063Sbostic int argc; 35*35063Sbostic char **argv; 36*35063Sbostic { 37*35063Sbostic extern int optind; 38*35063Sbostic register int cnt, n, step; 39*35063Sbostic int append, ch, *fd; 40*35063Sbostic char buf[8192], *malloc(); 41*35063Sbostic off_t lseek(); 4225600Ssam 43*35063Sbostic append = 0; 44*35063Sbostic while ((ch = getopt(argc, argv, "ai")) != EOF) 45*35063Sbostic switch((char)ch) { 461123Sbill case 'a': 47*35063Sbostic append = 1; 481123Sbill break; 491123Sbill case 'i': 50*35063Sbostic (void)signal(SIGINT, SIG_IGN); 51*35063Sbostic break; 52*35063Sbostic case '?': 53*35063Sbostic default: 54*35063Sbostic fprintf(stderr, "usage: tee [-ai] [file ...]\n"); 55*35063Sbostic exit(2); 561123Sbill } 57*35063Sbostic argv += optind; 58*35063Sbostic argc -= optind; 59*35063Sbostic 60*35063Sbostic if (!(fd = (int *)malloc((u_int)((argc + 1) * sizeof(int))))) { 61*35063Sbostic fprintf(stderr, "tee: out of space.\n"); 62*35063Sbostic exit(2); 631123Sbill } 64*35063Sbostic fd[0] = 1; /* always write to stdout */ 65*35063Sbostic for (cnt = 1; *argv; ++argv) 66*35063Sbostic if ((fd[cnt] = open(*argv, append ? O_WRONLY|O_CREAT : 67*35063Sbostic O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) { 68*35063Sbostic fprintf(stderr, "tee: %s: ", *argv); 69*35063Sbostic perror((char *)NULL); 7025600Ssam } 71*35063Sbostic else { 72*35063Sbostic if (append) 73*35063Sbostic (void)lseek(fd[cnt], 0L, L_XTND); 74*35063Sbostic ++cnt; 7525600Ssam } 76*35063Sbostic for (--cnt; (n = read(0, buf, sizeof(buf))) > 0;) 77*35063Sbostic for (step = cnt; step >= 0; --step) 78*35063Sbostic (void)write(fd[step], buf, n); 79*35063Sbostic exit(0); 801123Sbill } 81