#ifdef	ournix
#include "ournix.h"
#endif
char sccsID[] = "@(#) tee.c V2.0 Copyright Julian H. Stacey 19th August 1989.\n";
	/* this is not a conversion of JmcB tee.c, but a complete rewrite,
		JmcB's was riddled with beginners C mistakes */

#include	<stdio.h>

char		**ARGV;
typedef	char	FLAG ;

main(argc,argv)
	int	argc ;
	char	**argv ;
	{
	int	count1 = 0 ;
	int	count2 ;
	FLAG	ignore ;
	FLAG	append = 0 ;
	FILE	*fil[_NFILE] ;
	FILE	**file_p ;
	char	*names[_NFILE] ;
	char	**name_p ;
	int	c_int ;
	char	c_char ;

	ARGV = argv ;
#ifdef	VSL	/* { */
#include	"../../include/vsl.h"
#endif		/* } */
	argc-- ; argv++ ;
	while (argc && (**argv == '-') )
		{
		switch ( *++*argv)
			{
			case 'i':
				ignore = 1 ;
				printf("%s: -i ignored\n",*ARGV ) ;
				break ;
			case 'a':
				append = 1 ;
				break ;
			default :
				syntax() ;
				exit(1) ;
				break ;
			}
		argc-- ; argv++ ;
		}
	for (file_p = fil, name_p = names ; argc-- && (++count1 <= _NFILE) ; )
		{
		*name_p = *argv++ ;
#ifdef	DEBUG
		fprintf(stderr,"File name %s\n",*name_p);
#endif
		if ((*file_p = fopen(*name_p,(append)?"a":"w")) == (FILE *)0)
			{
			fprintf(stderr,"%s: Aborting, couldnt open %s.\n",
				*ARGV, *name_p) ;
			exit(1) ;
			}
		name_p++ ; file_p++ ;
		}
	count2 = count1 ;
	/* ---------- */
#ifdef	DEBUG
		fprintf(stderr,"Scanning pipe data for %d files\n", count2);
#endif
	while ((c_int = getchar()) != EOF)
		{
		c_char = (char)(c_int & 0xFF) ;
		putchar(c_char) ;
		if (c_char == '\n') (void)fflush(stdout) ;
		for (count1 = count2 , file_p = fil, name_p = names ;
			count1-- ; file_p++, name_p++ ) putc(c_char, *file_p) ;
		}
	/* ---------- */
#ifdef	DEBUG
		fprintf(stderr,"Closing files\n");
#endif
	for (file_p = fil, name_p = names ; count2-- ; file_p++, name_p++ )
		{
		if (fclose(*file_p) == EOF)
			fprintf(stderr,"%s: Couldnt close %s.\n",
				*ARGV, *name_p) ;
		}
	exit(0) ;
	}

syntax()
	{
	fprintf(stderr,"Error, Correct Syntax is :\n\t%s [-i] [-a] file[s]\n",
		*ARGV) ;
	}
