/*
 * pagecnt : count the number of form feeds in a file.
 *            by Joel Swank November 1988
 *           Version 1.0
 */

#include <stdio.h>
#include <fcntl.h>
#include <exec/types.h>
#define STDIN 0   /* file descriptor for stdin */

FILE *fdopen();

int fileflag = FALSE;    /* detect no files - use stdin */

main(argc,argv)
int argc;
char *argv[];
{
	register FILE	*filep ;
	register cnt ;

		/* get command line filenames */
	for (cnt=1; cnt < argc; cnt++)
	{	if (*argv[cnt] != '-')
		{
			if ((filep = fopen(argv[cnt], "r")) == NULL)
				fatal("can't open %s", argv[cnt]) ;

			printf("FILE: %s ",argv[cnt]);
			dofile(filep);
			fclose(filep);
			fileflag = TRUE;
		}
	}

	if (!fileflag)  /* if no files given, use stdin */
	{
		filep = fdopen(STDIN, "r");
		dofile(filep);
		fclose(filep);
	}
	exit(0);
}

/*
 * process open file via stream pointer
 */

dofile(filep)
FILE *filep;
{

	int onemore, maxsize =0, fcount = 0, linecnt =0;
	char chr;
	while ((chr = getc(filep)) != EOF)
	{	
		if (chr == '\f')
			{
			fcount++;
			onemore =0;
			continue;
			}
		else onemore =1;
		if (chr == '\n')
			{
			if (linecnt > maxsize) maxsize = linecnt;
			linecnt = 0;
			}
		else linecnt++;
	}
	printf("%d Pages, Longest line is %d\n",fcount+onemore, maxsize);
}

/*
 *  fatal - print standard error msg and halt process
 */
fatal(ptrn, data1, data2)
register char	*ptrn, *data1, *data2 ;
{
	printf("ERROR: ");
	printf(ptrn, data1, data2) ;
	putchar('\n');
	exit(1);
}
