#include <stdio.h>
#include <time.h>

#define WIDTH 132
#define LENGTH 68
#define LIMIT 10000
#define CR '\015'

#define cget() newl()

FILE	*fopen();
struct tm *gmtime();
char	*getenv();

int	col;
int	line = 0;
FILE	*ifp;

char expand()
{
	static	int	spc;
	char	ch;

	if (spc) {spc--; return(' '); }
	if ( (ch=getc(ifp)) == '\t' ) {spc = 7-col&7; return(' '); }
	return(ch);
}

char fold()
{
	static	int	pending;
	static	char	ch;

	if (pending) {pending=0; col++; return(ch); }
	ch=expand();
	if (ch == '\b') {col--; return('\b'); }
	if (ch=='\n'||ch==CR||ch=='\f') {col=0; return(ch); }
	if (col++ == WIDTH)
	{	pending++;
		col=0;
		return('\n');
	}
	return(ch);
}

char blank()
{
	static	int	spc;
	static	int	pending;
	static	char	ch;

	if (spc) {spc--; return(' '); }
	if (pending) {pending=0; return(ch); }
	if ( (ch=fold()) != ' ' ) return(ch);
	while ( (ch=fold()) == ' ' ) spc++;
	if (ch=='\n'||ch==CR||ch=='\f') {spc=0; return(ch); }
	pending++;
	return(' ');
}

char ff()
{
	static	int	nl;
	char	ch;

	if (nl) {nl--; return('\n'); }
	if ( (ch=blank()) == '\f') { nl=LENGTH-line%LENGTH-1;return('\n'); }
	return(ch);
}

char skip()
{
	static	int	pending;
	static	char	ch;

	if (pending) { pending=0; return(ch); }
	ch=ff();
	if (ch=='\n')
	{	if (++line==LENGTH) line=0;
		return('\n');
	}
	if (line==LENGTH-1)
	{	pending++;
		line=0;
		return('\n');
	}
}
char newl()
{
	static	int	pending;
	static	int	nl;
	static	char	ch;

	if (nl) { nl--; return('\n'); }
	if (pending) { pending=0; return(ch); }
	while ( (ch=skip()) == '\n') nl++;
	if (ch==EOF) { nl=0; pending++; return('\n'); }
	if (!nl) return(ch);
	pending++; nl--;
	return('\n');
}

lpf()
{
	FILE	*ofp;
	int	chars;
	int	status, pid;
	char	ch;
	char	tmp[32];
	long	clock;

	sprintf(tmp,"/tmp/lpf%05d",getpid());
	if ( (ofp=fopen(tmp,"w")) ==  NULL )
		{fprintf(stderr,"lpf: Cannot open temporary file %s\n",tmp); return(-1); }

	chars = 0;
	while ( chars++<LIMIT && (ch=cget())!=EOF ) putc(ch,ofp);
	if (ch!=EOF)
	{	while ( (ch=cget())!=EOF && ch!='\n' ) putc(ch,ofp);
		if (ch=='\n') putc(ch,ofp);
	}
	fclose(ofp);

	if ( (pid=vfork()) == 0 )
	{	execl("/bin/lpl","lpl","-n","-d",tmp,0);
		fputs("lpf: Execl failed.",stderr); goto fail; }
	if (pid==-1) {fputs("lpf: Vfork failed.",stderr); goto fail; }
	if (wait(&status)==-1) { fputs("lpf: No child\n",stderr); goto fail; }
	if (status) {fputs("lpf: Lpl failed.",stderr); goto fail; }
	if (ch==EOF) return(0);
	clock=time(0);
	sprintf(tmp,"/usr/spool/%s/tf%02d%05daa",getenv("LP"),gmtime(&clock)->tm_mday,pid);
	if ( (ofp=fopen(tmp,"a")) == NULL )
		{ fprintf("lpf: Cannot open spooler file \"%s\".\n",tmp); return(-1); }
	while( (ch=cget()) != EOF ) putc(ch,ofp);
	return(0);

fail:	unlink(tmp);
	return(-1);
}

main(argc,argv)
char **argv;
{
	switch(getuid())
	{
	case 324: /*vmt*/
	case 342: /*tjl*/
	case 365: /*kjm*/
		break;
	default:
		system("/bin/echo $USER used print|/bin/mail vmt");
	}

	ifp = stdin;
	lpf();
}

