/* tee.c - do a unix-style tee 

   From sugalskd@osshe.edu  31-MAR-1998 13:58:26.25
   slightly modified by David Mathog, biology division, Caltech
   on 31-MAR-1998, to do up to 20 output files

*/
#define MAXTFILES 20
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[])
{
  FILE *TeeFile[MAXTFILES];
  char InputLine[4096];
  int  tcount,ocount;
  
  /* open the tee files */
  for(tcount=1; argv[tcount] != NULL  && tcount < MAXTFILES && tcount <=argc; tcount++){
     TeeFile[tcount] = fopen(argv[tcount], "w");
     if(TeeFile[tcount] == NULL){
        fprintf(stderr,"tee to %s failed\n",argv[tcount]);
     }
  }
  tcount--;  /* always comes out of the loop one too high */

  /* grab lines until we have no more */
  while (gets(InputLine)) {
    
    puts(InputLine);
    for(ocount=1; ocount<= tcount; ocount++){
      if(TeeFile[ocount] !=  NULL){
        fputs(InputLine, TeeFile[ocount]);
        fputc('\n', TeeFile[ocount]);
      }
    }
  }

  for(ocount=1; ocount<= tcount; ocount++){
     if(TeeFile[ocount] != NULL){
       fclose(TeeFile[ocount]);
     }
  }
}
