1*13235Sralph /*	common.c	4.7	83/06/22	*/
212117Sralph /*
312117Sralph  * Routines and data common to all the line printer functions.
412117Sralph  */
512117Sralph 
612117Sralph #include "lp.h"
712117Sralph 
812117Sralph int	DU;		/* daeomon user-id */
912117Sralph int	MX;		/* maximum number of blocks to copy */
1013170Sralph int	MC;		/* maximum number of copies allowed */
1112117Sralph char	*LP;		/* line printer device name */
1212117Sralph char	*RM;		/* remote machine name */
1312117Sralph char	*RP;		/* remote printer name */
1412117Sralph char	*LO;		/* lock file name */
1512117Sralph char	*ST;		/* status file name */
1612117Sralph char	*SD;		/* spool directory */
1712117Sralph char	*AF;		/* accounting file */
1812117Sralph char	*LF;		/* log file for error messages */
1912117Sralph char	*OF;		/* name of output filter (created once) */
2012117Sralph char	*IF;		/* name of input filter (created per job) */
2112433Sralph char	*RF;		/* name of fortran text filter (per job) */
2212117Sralph char	*TF;		/* name of troff filter (per job) */
23*13235Sralph char	*NF;		/* name of ditroff filter (per job) */
2412117Sralph char	*DF;		/* name of tex filter (per job) */
2512117Sralph char	*GF;		/* name of graph(1G) filter (per job) */
2612117Sralph char	*VF;		/* name of vplot filter (per job) */
2712117Sralph char	*CF;		/* name of cifplot filter (per job) */
2812117Sralph char	*PF;		/* name of vrast filter (per job) */
2912117Sralph char	*FF;		/* form feed string */
3012117Sralph char	*TR;		/* trailer string to be output when Q empties */
3113170Sralph short	SC;		/* suppress multiple copies */
3212117Sralph short	SF;		/* suppress FF on each print job */
3312117Sralph short	SH;		/* suppress header page */
3412117Sralph short	SB;		/* short banner instead of normal header */
3512117Sralph short	RW;		/* open LP for reading and writing */
3612117Sralph short	PW;		/* page width */
3712117Sralph short	PL;		/* page length */
3812433Sralph short	PX;		/* page width in pixels */
3912433Sralph short	PY;		/* page length in pixels */
4012117Sralph short	BR;		/* baud rate if lp is a tty */
4113170Sralph int	FC;		/* flags to clear if lp is a tty */
4213170Sralph int	FS;		/* flags to set if lp is a tty */
4313170Sralph int	XC;		/* flags to clear for local mode */
4413170Sralph int	XS;		/* flags to set for local mode */
4512433Sralph short	RS;		/* restricted to those with local accounts */
4612117Sralph 
4712117Sralph char	line[BUFSIZ];
4812117Sralph char	pbuf[BUFSIZ/2];	/* buffer for printcap strings */
4912117Sralph char	*bp = pbuf;	/* pointer into pbuf for pgetent() */
5012117Sralph char	*name;		/* program name */
5112117Sralph char	*printer;	/* printer name */
5212117Sralph char	host[32];	/* host machine name */
5312117Sralph char	*from = host;	/* client's machine name */
5412117Sralph 
5512117Sralph /*
5612117Sralph  * Create a connection to the remote printer server.
5712117Sralph  * Most of this code comes from rcmd.c.
5812117Sralph  */
5912528Sralph getport(rhost)
6012528Sralph 	char *rhost;
6112117Sralph {
6212117Sralph 	struct hostent *hp;
6312117Sralph 	struct servent *sp;
6412117Sralph 	struct sockaddr_in sin;
6512117Sralph 	int s, timo = 1, lport = IPPORT_RESERVED - 1;
6612874Sralph 	int err;
6712117Sralph 
6812117Sralph 	/*
6912117Sralph 	 * Get the host address and port number to connect to.
7012117Sralph 	 */
7112528Sralph 	if (rhost == NULL)
7212117Sralph 		fatal("no remote host to connect to");
7312528Sralph 	hp = gethostbyname(rhost);
7412117Sralph 	if (hp == NULL)
7512528Sralph 		fatal("unknown host %s", rhost);
7612117Sralph 	sp = getservbyname("printer", "tcp");
7712117Sralph 	if (sp == NULL)
7812117Sralph 		fatal("printer/tcp: unknown service");
7912117Sralph 	bzero((char *)&sin, sizeof(sin));
8012117Sralph 	bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
8112117Sralph 	sin.sin_family = hp->h_addrtype;
8212117Sralph 	sin.sin_port = sp->s_port;
8312117Sralph 
8412117Sralph 	/*
8512117Sralph 	 * Try connecting to the server.
8612117Sralph 	 */
8712117Sralph retry:
8812117Sralph 	s = rresvport(&lport);
8912117Sralph 	if (s < 0)
9012117Sralph 		return(-1);
9112117Sralph 	if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) {
9212874Sralph 		err = errno;
9312874Sralph 		(void) close(s);
9412874Sralph 		errno = err;
9512117Sralph 		if (errno == EADDRINUSE) {
9612117Sralph 			lport--;
9712117Sralph 			goto retry;
9812117Sralph 		}
9912117Sralph 		if (errno == ECONNREFUSED && timo <= 16) {
10012117Sralph 			sleep(timo);
10112117Sralph 			timo *= 2;
10212117Sralph 			goto retry;
10312117Sralph 		}
10412117Sralph 		return(-1);
10512117Sralph 	}
10612117Sralph 	return(s);
10712117Sralph }
10812117Sralph 
10912117Sralph rresvport(alport)
11012117Sralph 	int *alport;
11112117Sralph {
11212117Sralph 	struct sockaddr_in sin;
11312117Sralph 	int s;
11412117Sralph 
11512117Sralph 	sin.sin_family = AF_INET;
11612117Sralph 	sin.sin_addr.s_addr = 0;
11712117Sralph 	s = socket(AF_INET, SOCK_STREAM, 0);
11812117Sralph 	if (s < 0)
11912117Sralph 		return(-1);
12012874Sralph 	for (; *alport > IPPORT_RESERVED/2; (*alport)--) {
12112117Sralph 		sin.sin_port = htons((u_short) *alport);
12212117Sralph 		if (bind(s, (caddr_t)&sin, sizeof(sin), 0) >= 0)
12312117Sralph 			return(s);
12412117Sralph 		if (errno != EADDRINUSE && errno != EADDRNOTAVAIL)
12512874Sralph 			break;
12612117Sralph 	}
12712874Sralph 	(void) close(s);
12812874Sralph 	return(-1);
12912117Sralph }
13012117Sralph 
13112117Sralph /*
13212117Sralph  * Getline reads a line from the control file cfp, removes tabs, converts
13312117Sralph  *  new-line to null and leaves it in line.
13412117Sralph  * Returns 0 at EOF or the number of characters read.
13512117Sralph  */
13612117Sralph getline(cfp)
13712117Sralph 	FILE *cfp;
13812117Sralph {
13912117Sralph 	register int linel = 0;
14012117Sralph 	register char *lp = line;
14112117Sralph 	register c;
14212117Sralph 
14312117Sralph 	while ((c = getc(cfp)) != '\n') {
14412117Sralph 		if (c == EOF)
14512117Sralph 			return(0);
14612117Sralph 		if (c == '\t') {
14712117Sralph 			do {
14812117Sralph 				*lp++ = ' ';
14912117Sralph 				linel++;
15012117Sralph 			} while ((linel & 07) != 0);
15112117Sralph 			continue;
15212117Sralph 		}
15312117Sralph 		*lp++ = c;
15412117Sralph 		linel++;
15512117Sralph 	}
15612117Sralph 	*lp++ = '\0';
15712117Sralph 	return(linel);
15812117Sralph }
15912117Sralph 
16012117Sralph /*
16112117Sralph  * Scan the current directory and make a list of daemon files sorted by
16212117Sralph  * creation time.
16312117Sralph  * Return the number of entries and a pointer to the list.
16412117Sralph  */
16512117Sralph getq(namelist)
16612117Sralph 	struct queue *(*namelist[]);
16712117Sralph {
16812117Sralph 	register struct direct *d;
16912117Sralph 	register struct queue *q, **queue;
17012117Sralph 	register int nitems;
17112117Sralph 	struct stat stbuf;
17212117Sralph 	int arraysz, compar();
17312117Sralph 	DIR *dirp;
17412117Sralph 
17513170Sralph 	if ((dirp = opendir(SD)) == NULL)
17612117Sralph 		return(-1);
17712117Sralph 	if (fstat(dirp->dd_fd, &stbuf) < 0)
17812874Sralph 		goto errdone;
17912117Sralph 
18012117Sralph 	/*
18112117Sralph 	 * Estimate the array size by taking the size of the directory file
18212117Sralph 	 * and dividing it by a multiple of the minimum size entry.
18312117Sralph 	 */
18412117Sralph 	arraysz = (stbuf.st_size / 24);
18512117Sralph 	queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
18612117Sralph 	if (queue == NULL)
18712874Sralph 		goto errdone;
18812117Sralph 
18912117Sralph 	nitems = 0;
19012117Sralph 	while ((d = readdir(dirp)) != NULL) {
19112117Sralph 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
19212117Sralph 			continue;	/* daemon control files only */
19312117Sralph 		if (stat(d->d_name, &stbuf) < 0)
19412117Sralph 			continue;	/* Doesn't exist */
19512117Sralph 		q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
19612117Sralph 		if (q == NULL)
19712874Sralph 			goto errdone;
19812117Sralph 		q->q_time = stbuf.st_mtime;
19912117Sralph 		strcpy(q->q_name, d->d_name);
20012117Sralph 		/*
20112117Sralph 		 * Check to make sure the array has space left and
20212117Sralph 		 * realloc the maximum size.
20312117Sralph 		 */
20412117Sralph 		if (++nitems > arraysz) {
20512117Sralph 			queue = (struct queue **)realloc((char *)queue,
20612117Sralph 				(stbuf.st_size/12) * sizeof(struct queue *));
20712117Sralph 			if (queue == NULL)
20812874Sralph 				goto errdone;
20912117Sralph 		}
21012117Sralph 		queue[nitems-1] = q;
21112117Sralph 	}
21212117Sralph 	closedir(dirp);
21312117Sralph 	if (nitems)
21412117Sralph 		qsort(queue, nitems, sizeof(struct queue *), compar);
21512117Sralph 	*namelist = queue;
21612117Sralph 	return(nitems);
21712874Sralph 
21812874Sralph errdone:
21912874Sralph 	closedir(dirp);
22012874Sralph 	return(-1);
22112117Sralph }
22212117Sralph 
22312117Sralph /*
22412117Sralph  * Compare modification times.
22512117Sralph  */
22612117Sralph static
22712117Sralph compar(p1, p2)
22812117Sralph 	register struct queue **p1, **p2;
22912117Sralph {
23012117Sralph 	if ((*p1)->q_time < (*p2)->q_time)
23112117Sralph 		return(-1);
23212117Sralph 	if ((*p1)->q_time > (*p2)->q_time)
23312117Sralph 		return(1);
23412117Sralph 	return(0);
23512117Sralph }
23612117Sralph 
23712117Sralph /*VARARGS1*/
23812117Sralph fatal(msg, a1, a2, a3)
23912117Sralph 	char *msg;
24012117Sralph {
24112117Sralph 	if (from != host)
24212117Sralph 		printf("%s: ", host);
24312117Sralph 	printf("%s: ", name);
24412117Sralph 	if (printer)
24512117Sralph 		printf("%s: ", printer);
24612117Sralph 	printf(msg, a1, a2, a3);
24712117Sralph 	putchar('\n');
24812117Sralph 	exit(1);
24912117Sralph }
250