/* bug: wc -l has one too many CRs */
#ifdef	ournix
#include "ournix.h"
#endif
char sccsID[] = "@(#) wc.c V0.1 Copyright http://www.berklix.com (pjc & jhs).\n" ;

/* wc - word count originally by PC
 * Coverted to be identical to bsd output  by JHS
 * Translator billing algorithm flag -t added by JHS:
 * (Tells how many lines have more than (default) 40 printable characters,)
 * To be added:
  if -t, then we should count multiple blank spaces between words as a single space, except that white space on line before any text does not count, nor does white space after any last character,
  commas, dots etc all count as characters.
 */

#include <stdio.h>
#include <ctype.h>

#define CHARS_PER_LINE	40	/* # chars per line >= translators bill */

char	**ARGV ;
long	T_lines;
long	lines;
long	words;
long	chars;

long	tot_T_lines;
long	tot_lines;
long	tot_words;
long	tot_chars;

int	c_flag;
int	l_flag;
int	w_flag;
int	t_flag;

#define TRUE	1
#define FALSE	0

int	length = CHARS_PER_LINE ;
int	one_file = FALSE;		/* if just one file no total */

main(argc, argv)
	char	**argv;
	{
	FILE	*fp;
	char	*p;
	char	*name ;

	ARGV = argv ;
#ifdef	VSL	/* { */
#include	"../../include/vsl.h"
#endif		/* } */
	for(argc--, argv++ ; argc ; argv++, argc--)
		{
		if(**argv != '-') break;
		p = *argv + 1;
		while(*p) switch(*p++)
			{
			case 'c':
			case 'C':
				c_flag = 1; break;
			case 'l':
			case 'L':
				l_flag = 1; break;
			case 'w':
			case 'W':
				w_flag = 1; break;
			case 't':
			case 'T':
				t_flag = 1;
				if (*p) 
					{
					length = atoi(p);
					while ((*p >='0')&&(*p<='9')) p++;
					}
				break;
			default:
				fprintf(stderr, "Unknown flag %c\n", *(p-1));
				break;
			}
		}
	if(c_flag == 0 && l_flag == 0 && w_flag == 0 && t_flag == 0)
		c_flag = l_flag = w_flag = 1;
	if(argc == 0) doit(stdin, "\0" );
	else	{ 
		if(argc == 1) one_file = TRUE;
		do	{
			T_lines = lines = words = chars = 0;
			name = *argv++ ;
			fp = fopen(name, "r");
			if(fp == NULL)
				{
				fprintf(stderr, "Cannot open '%s'\n",name);
				continue;
				}
			doit(fp,name);
			(void) fclose(fp);
			tot_lines += lines;
			tot_words += words;
			tot_chars += chars;
			tot_T_lines += T_lines ;
			}
		while(--argc);
		if(!one_file)
			{
			doprint(tot_lines, tot_words, tot_chars,tot_T_lines,
				"total");
			}
		}
#ifndef	MSDOS	/* { */
	putchar('\n');
#endif		/* } */
	exit(0);
	}

doit(fp,name)
	register FILE	*fp;
	char *name ;
	{
	register int	c;
	int	isword = FALSE;
	int	Tchars = 0 ;
	
	while((c = getc(fp)) != EOF)
		{
		chars++;
		if(c == '\n')
			{
			lines++;
			if(isword) words++;
			isword = FALSE;
#ifdef	MSDOS		/* count the cr as well as the lf */
			chars++;
#endif
			if (Tchars > length ) T_lines++ ;
			Tchars = 0 ;
			continue;
			}
		if(isspace(c))
			{
			if(isword)
				{
				words++;
				isword = FALSE;
				}
			continue;
			}
		isword = TRUE;
		if (isgraph(c)) Tchars++;
		/* considered redundant by jhs:- continue; */
		}
	if(isword) words++;
	doprint(lines, words, chars, T_lines, name);
	}

doprint(line, word, ch, tline, name)
	long	line, word, ch, tline;
	char *name ;
	{
	if(l_flag) printf("%8ld", line);
	if(w_flag) printf("%8ld", word);
	if(c_flag) printf("%8ld", ch);
	if(t_flag) printf( "%8ld lines with > %d characters.", tline, length );
	if(!one_file) printf(" %s\n", name );
	(void) fflush(stdout);
	}
