#define NO 0
#define YES 1

#include "stdio.h"		/* include file overhead info */

main(argc, argv)		/* counts lines, words, chars in input */
int argc;
char *argv[];
{
	FILE	*fp;
	char	*gets();
	char	arg1[14];	/* long enough for d:filename.ext */
	char	*pa;		/* pointer to first argument in program line */
	char	*p1 = &arg1[0];	/* pointer to where first argument is copied */

	int c, nl, nw, nc, inword;

	if (argc != 2) {
		printf("File name: ");
		p1 = gets(p1);
		while (*p1 = toupper(*p1))
			p1++;
	}
	else {
		pa = argv[1];
		while (*p1++ = *pa++)
			;
	}
	p1 = &arg1[0];

	if ((fp = fopen(p1, "r")) == NULL) {
		printf("Can't open file %s\n", p1);
		exit(-1);
	}

	printf("\nWorking on file %s\n",p1);

	inword=NO;
	nl=nw=nc=0;
	while ((c=getc(fp)) != EOF) {
		++nc;
		if (c=='\n') {
			++nl;
			if ( nl%10 == 0 )
				printf("%4d\r", nl);
		}
		if (c==' ' || c=='\n' || c=='\t')
			inword=NO;
		else if (inword==NO) {
			inword=YES;
			++nw;
		}
	}
	printf("%d lines  %d words  %d chars\n", nl, nw, nc);

	fclose(fp);
}
