/* tee.c - copy standard input to standard output and one other file.	*
 *	This is useful for splitting (tapping) the pipe.  Presumes you	*
 *	are using a shell program which can pipe the output from one	*
 *	program into the input of another or some other pipe device,	*
 *	but also can be used to make 2 copies of a file.		*
 *									*
 * tee <file1 >file2 file3						*
 * prog1 file1 | tee file3 | prog2 ...					*
 *									*
 * tee (C) 1988 by Gary L. Brant					*
 *									*
 * :ts=8								*/

#include <stdio.h>
#include <fcntl.h>
#define MAXLINE 256
#define ERROR	-1

void copy ();
int close (), open (), read (), write ();
int err, out2;			/* file handles */

main (argc, argv)
int  argc;
char *argv[];
{

   err = fileno (stderr);
   if (argc != 2) {
      write (err, "usage: tee <file1 >file2 file3\n", 31);
      exit (20);
   }

   /* Manx documentation (lib.35) claims that if O_TRUNC is used, */
   /* then O_CREAT is not needed; HOG_WASH; open() first deletes */
   /* the file & then complains ENOENT (File does not exist!!! */

   if ((out2 = open (argv[1], O_WRONLY | O_TRUNC | O_CREAT)) == ERROR) {
      write (err, "tee: cant open ", 15);
      write (err, argv[1], strlen (argv[1]));
      write (err, "\n", 1);
      exit (20);
   }

   copy (out2);
   close (out2);
}


/* copy stdin to stdout and one other file */

void copy (out2)
int out2;
{
   int p, in, out1;
   char line[MAXLINE];

   in = fileno (stdin);
   out1 = fileno (stdout);
   while ((p = read (in, line, MAXLINE)) > 0) {
      write (out1, line, p);
      write (out2, line, p);
   }
}


_wb_parse ()
{
}
