#include <stdio.h>
#include <time.h>

#define	TRUE	1
#define	FALSE	0
#define MAXCOL	255
#define DPGSIZ	58
#define DHDLEN	3

static	char	OBufr[BUFSIZ];		/* cause sysprint to buffer */
static	char	IBufr[BUFSIZ];		/* cause sysin to buffer    */

static	char	*PROGRAM[] = { "S. Leoce PR filter",
			       "V1.0 R1.0 SVCLVL 0" };
#include "b:cmdline.c"

int page (unsigned int, char *, char *, short unsigned int *);

main (argc, argv)
int	argc;
char   *argv[];		/* the command line is here	*/

{
	static	char	line[MAXCOL];		/* input line ...	   */
	char	*filename  = "SYSIN";		/* the default stream input*/
	short int PageSize = DPGSIZ;		/* 58 lines / page default */
	short int Verbose  = FALSE;		/* no talkies as go ...	   */
	short int LinesNow = 0;			/* lines output so far	   */
	
 	short unsigned int LineOption = FALSE; 	/* change option present   */
 	short unsigned int number = FALSE;      /* number output lines?    */
 	short unsigned int Feed = FALSE;	/* allow first form feed?  */
 	short unsigned int Burst = FALSE;	/* burst page required     */
        unsigned long  int linect = 0L;		/* no output lines yet     */
        unsigned int	   pagect = 0;		/* pagecounter		   */
        short int col = MAXCOL;			/* columns to be a line    */

	long	secs_now = time(&secs_now);
	char	*stamp  = ctime(&secs_now);

	opterr = FALSE;		/* this program handles errors		   */
	while ((LineOption = getopt(argc, argv, "BbFfL:l:NnVvC:c:")) != EOF) {
		switch (LineOption) {
		 	case '?':
				fprintf(stderr,"Invalid option %c\n", badopt);
				fprintf(stderr,"usage: pr [/BFNVLn Cn] [file]\n");
				_exit (0xff);
			case 'L':
			case 'l': PageSize = atoi(optarg) + DHDLEN;
				  if (PageSize < 1)
					PageSize = DPGSIZ;
				  break;
			case 'V':
			case 'v': Verbose = TRUE;
				  break;
			case 'N':
			case 'n': number = TRUE;
				  break;
			case 'C':
			case 'c': col = atoi(optarg);
				  if (col <= 0 || col > MAXCOL)
					col = MAXCOL;
				  break;
			case 'F':	/* cancel first form-feed */
			case 'f':
				  Feed = TRUE;
				  break;
			case 'B':	/* supply a burst page */
			case 'b':
				  Burst = TRUE;
				  break;
		};
	};
	if (argv [optind] != NULL) {
		if(freopen(argv[optind], "r", stdin) == NULL) {
			fprintf(stderr, "pr: no such file %s\n",argv[optind]);
			exit(0x20);
		};
		filename = strupr(argv[optind]);
	};

	LinesNow = PageSize - 1;

	if( Burst ) {
		if( Feed )
			fprintf( stdout, "\f");
		fprintf( stdout, "\n\n\n\n\n\n\n\n\n\n\tFile   : %s\n\t"
				"Printed: %s", filename, stamp );
		if( !Feed )
			fprintf( stdout, "\f");
	}
		
	while (gets(line) != NULL) {
		++linect;
		line[col] = '\0';	 /* terminate long ones here ...    */
		if (++LinesNow == PageSize)
			LinesNow = page(++pagect, stamp, filename, &Feed);
		if (number)
			printf("%5ld ", linect);
		if(puts(line) == EOF)	{	/* no ball room to write */
			fprintf(stderr,"IELPUT01 Device denied write\n");
			_exit(0x36);
		};
	};

	if (fflush(stdout) == EOF)
		fprintf(stderr, "pr: couldn't flush sysprint");

	if(Verbose) {
		fprintf(stderr, "pr: ");
		if (linect == 0)
                    fprintf(stderr, "SYSIN empty\n");
		else
                    fprintf(stderr,"%ld lines on %d pages\n",linect,pagect);
	};
	exit(0x00);
}

int page (pagect, stamp, file, Feed )
unsigned pagect;
char *stamp;
char *file;
short unsigned int * Feed;
{
	if( *Feed )
		fprintf( stdout, "\f" );

	fprintf(stdout,"Page %02u  %-35s  Printed %s\n\n",
 			pagect,file,stamp);

	*Feed = TRUE;	  /* after first page, feeding is required */
 	
	return (DHDLEN);  /* number of lines already used up  */
}