122426Sdist /*
222426Sdist  * Copyright (c) 1983 Regents of the University of California.
334203Sbostic  * All rights reserved.
434203Sbostic  *
534203Sbostic  * Redistribution and use in source and binary forms are permitted
6*34936Sbostic  * provided that the above copyright notice and this paragraph are
7*34936Sbostic  * duplicated in all such forms and that any documentation,
8*34936Sbostic  * advertising materials, and other materials related to such
9*34936Sbostic  * distribution and use acknowledge that the software was developed
10*34936Sbostic  * by the University of California, Berkeley.  The name of the
11*34936Sbostic  * University may not be used to endorse or promote products derived
12*34936Sbostic  * from this software without specific prior written permission.
13*34936Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34936Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34936Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622426Sdist  */
1722426Sdist 
1813952Ssam #ifndef lint
19*34936Sbostic static char sccsid[] = "@(#)common.c	5.4 (Berkeley) 06/30/88";
2034203Sbostic #endif /* not lint */
2113952Ssam 
2212117Sralph /*
2312117Sralph  * Routines and data common to all the line printer functions.
2412117Sralph  */
2512117Sralph 
2612117Sralph #include "lp.h"
2712117Sralph 
2812117Sralph int	DU;		/* daeomon user-id */
2912117Sralph int	MX;		/* maximum number of blocks to copy */
3013170Sralph int	MC;		/* maximum number of copies allowed */
3112117Sralph char	*LP;		/* line printer device name */
3212117Sralph char	*RM;		/* remote machine name */
3312117Sralph char	*RP;		/* remote printer name */
3412117Sralph char	*LO;		/* lock file name */
3512117Sralph char	*ST;		/* status file name */
3612117Sralph char	*SD;		/* spool directory */
3712117Sralph char	*AF;		/* accounting file */
3812117Sralph char	*LF;		/* log file for error messages */
3912117Sralph char	*OF;		/* name of output filter (created once) */
4012117Sralph char	*IF;		/* name of input filter (created per job) */
4112433Sralph char	*RF;		/* name of fortran text filter (per job) */
4212117Sralph char	*TF;		/* name of troff filter (per job) */
4313235Sralph char	*NF;		/* name of ditroff filter (per job) */
4412117Sralph char	*DF;		/* name of tex filter (per job) */
4512117Sralph char	*GF;		/* name of graph(1G) filter (per job) */
4612117Sralph char	*VF;		/* name of vplot filter (per job) */
4712117Sralph char	*CF;		/* name of cifplot filter (per job) */
4812117Sralph char	*PF;		/* name of vrast filter (per job) */
4912117Sralph char	*FF;		/* form feed string */
5012117Sralph char	*TR;		/* trailer string to be output when Q empties */
5113170Sralph short	SC;		/* suppress multiple copies */
5212117Sralph short	SF;		/* suppress FF on each print job */
5312117Sralph short	SH;		/* suppress header page */
5412117Sralph short	SB;		/* short banner instead of normal header */
5518128Sralph short	HL;		/* print header last */
5612117Sralph short	RW;		/* open LP for reading and writing */
5712117Sralph short	PW;		/* page width */
5812117Sralph short	PL;		/* page length */
5912433Sralph short	PX;		/* page width in pixels */
6012433Sralph short	PY;		/* page length in pixels */
6112117Sralph short	BR;		/* baud rate if lp is a tty */
6213170Sralph int	FC;		/* flags to clear if lp is a tty */
6313170Sralph int	FS;		/* flags to set if lp is a tty */
6413170Sralph int	XC;		/* flags to clear for local mode */
6513170Sralph int	XS;		/* flags to set for local mode */
6612433Sralph short	RS;		/* restricted to those with local accounts */
6712117Sralph 
6812117Sralph char	line[BUFSIZ];
6912117Sralph char	pbuf[BUFSIZ/2];	/* buffer for printcap strings */
7012117Sralph char	*bp = pbuf;	/* pointer into pbuf for pgetent() */
7112117Sralph char	*name;		/* program name */
7212117Sralph char	*printer;	/* printer name */
7312117Sralph char	host[32];	/* host machine name */
7412117Sralph char	*from = host;	/* client's machine name */
7512117Sralph 
7612117Sralph /*
7712117Sralph  * Create a connection to the remote printer server.
7812117Sralph  * Most of this code comes from rcmd.c.
7912117Sralph  */
8012528Sralph getport(rhost)
8112528Sralph 	char *rhost;
8212117Sralph {
8312117Sralph 	struct hostent *hp;
8412117Sralph 	struct servent *sp;
8512117Sralph 	struct sockaddr_in sin;
8612117Sralph 	int s, timo = 1, lport = IPPORT_RESERVED - 1;
8712874Sralph 	int err;
8812117Sralph 
8912117Sralph 	/*
9012117Sralph 	 * Get the host address and port number to connect to.
9112117Sralph 	 */
9212528Sralph 	if (rhost == NULL)
9312117Sralph 		fatal("no remote host to connect to");
9412528Sralph 	hp = gethostbyname(rhost);
9512117Sralph 	if (hp == NULL)
9612528Sralph 		fatal("unknown host %s", rhost);
9712117Sralph 	sp = getservbyname("printer", "tcp");
9812117Sralph 	if (sp == NULL)
9912117Sralph 		fatal("printer/tcp: unknown service");
10012117Sralph 	bzero((char *)&sin, sizeof(sin));
10112117Sralph 	bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
10212117Sralph 	sin.sin_family = hp->h_addrtype;
10312117Sralph 	sin.sin_port = sp->s_port;
10412117Sralph 
10512117Sralph 	/*
10612117Sralph 	 * Try connecting to the server.
10712117Sralph 	 */
10812117Sralph retry:
10912117Sralph 	s = rresvport(&lport);
11012117Sralph 	if (s < 0)
11112117Sralph 		return(-1);
11212117Sralph 	if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) {
11312874Sralph 		err = errno;
11412874Sralph 		(void) close(s);
11512874Sralph 		errno = err;
11612117Sralph 		if (errno == EADDRINUSE) {
11712117Sralph 			lport--;
11812117Sralph 			goto retry;
11912117Sralph 		}
12012117Sralph 		if (errno == ECONNREFUSED && timo <= 16) {
12112117Sralph 			sleep(timo);
12212117Sralph 			timo *= 2;
12312117Sralph 			goto retry;
12412117Sralph 		}
12512117Sralph 		return(-1);
12612117Sralph 	}
12712117Sralph 	return(s);
12812117Sralph }
12912117Sralph 
13012117Sralph /*
13112117Sralph  * Getline reads a line from the control file cfp, removes tabs, converts
13212117Sralph  *  new-line to null and leaves it in line.
13312117Sralph  * Returns 0 at EOF or the number of characters read.
13412117Sralph  */
13512117Sralph getline(cfp)
13612117Sralph 	FILE *cfp;
13712117Sralph {
13812117Sralph 	register int linel = 0;
13912117Sralph 	register char *lp = line;
14012117Sralph 	register c;
14112117Sralph 
14212117Sralph 	while ((c = getc(cfp)) != '\n') {
14312117Sralph 		if (c == EOF)
14412117Sralph 			return(0);
14512117Sralph 		if (c == '\t') {
14612117Sralph 			do {
14712117Sralph 				*lp++ = ' ';
14812117Sralph 				linel++;
14912117Sralph 			} while ((linel & 07) != 0);
15012117Sralph 			continue;
15112117Sralph 		}
15212117Sralph 		*lp++ = c;
15312117Sralph 		linel++;
15412117Sralph 	}
15512117Sralph 	*lp++ = '\0';
15612117Sralph 	return(linel);
15712117Sralph }
15812117Sralph 
15912117Sralph /*
16012117Sralph  * Scan the current directory and make a list of daemon files sorted by
16112117Sralph  * creation time.
16212117Sralph  * Return the number of entries and a pointer to the list.
16312117Sralph  */
16412117Sralph getq(namelist)
16512117Sralph 	struct queue *(*namelist[]);
16612117Sralph {
16712117Sralph 	register struct direct *d;
16812117Sralph 	register struct queue *q, **queue;
16912117Sralph 	register int nitems;
17012117Sralph 	struct stat stbuf;
17112117Sralph 	int arraysz, compar();
17212117Sralph 	DIR *dirp;
17312117Sralph 
17413170Sralph 	if ((dirp = opendir(SD)) == NULL)
17512117Sralph 		return(-1);
17612117Sralph 	if (fstat(dirp->dd_fd, &stbuf) < 0)
17712874Sralph 		goto errdone;
17812117Sralph 
17912117Sralph 	/*
18012117Sralph 	 * Estimate the array size by taking the size of the directory file
18112117Sralph 	 * and dividing it by a multiple of the minimum size entry.
18212117Sralph 	 */
18312117Sralph 	arraysz = (stbuf.st_size / 24);
18412117Sralph 	queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
18512117Sralph 	if (queue == NULL)
18612874Sralph 		goto errdone;
18712117Sralph 
18812117Sralph 	nitems = 0;
18912117Sralph 	while ((d = readdir(dirp)) != NULL) {
19012117Sralph 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
19112117Sralph 			continue;	/* daemon control files only */
19212117Sralph 		if (stat(d->d_name, &stbuf) < 0)
19312117Sralph 			continue;	/* Doesn't exist */
19412117Sralph 		q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
19512117Sralph 		if (q == NULL)
19612874Sralph 			goto errdone;
19712117Sralph 		q->q_time = stbuf.st_mtime;
19812117Sralph 		strcpy(q->q_name, d->d_name);
19912117Sralph 		/*
20012117Sralph 		 * Check to make sure the array has space left and
20112117Sralph 		 * realloc the maximum size.
20212117Sralph 		 */
20312117Sralph 		if (++nitems > arraysz) {
20412117Sralph 			queue = (struct queue **)realloc((char *)queue,
20512117Sralph 				(stbuf.st_size/12) * sizeof(struct queue *));
20612117Sralph 			if (queue == NULL)
20712874Sralph 				goto errdone;
20812117Sralph 		}
20912117Sralph 		queue[nitems-1] = q;
21012117Sralph 	}
21112117Sralph 	closedir(dirp);
21212117Sralph 	if (nitems)
21312117Sralph 		qsort(queue, nitems, sizeof(struct queue *), compar);
21412117Sralph 	*namelist = queue;
21512117Sralph 	return(nitems);
21612874Sralph 
21712874Sralph errdone:
21812874Sralph 	closedir(dirp);
21912874Sralph 	return(-1);
22012117Sralph }
22112117Sralph 
22212117Sralph /*
22312117Sralph  * Compare modification times.
22412117Sralph  */
22512117Sralph static
22612117Sralph compar(p1, p2)
22712117Sralph 	register struct queue **p1, **p2;
22812117Sralph {
22912117Sralph 	if ((*p1)->q_time < (*p2)->q_time)
23012117Sralph 		return(-1);
23112117Sralph 	if ((*p1)->q_time > (*p2)->q_time)
23212117Sralph 		return(1);
23312117Sralph 	return(0);
23412117Sralph }
23512117Sralph 
23612117Sralph /*VARARGS1*/
23712117Sralph fatal(msg, a1, a2, a3)
23812117Sralph 	char *msg;
23912117Sralph {
24012117Sralph 	if (from != host)
24112117Sralph 		printf("%s: ", host);
24212117Sralph 	printf("%s: ", name);
24312117Sralph 	if (printer)
24412117Sralph 		printf("%s: ", printer);
24512117Sralph 	printf(msg, a1, a2, a3);
24612117Sralph 	putchar('\n');
24712117Sralph 	exit(1);
24812117Sralph }
249