1*12528Sralph /*	common.c	4.3	83/05/18	*/
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 */
1012117Sralph char	*LP;		/* line printer device name */
1112117Sralph char	*RM;		/* remote machine name */
1212117Sralph char	*RP;		/* remote printer name */
1312117Sralph char	*LO;		/* lock file name */
1412117Sralph char	*ST;		/* status file name */
1512117Sralph char	*SD;		/* spool directory */
1612117Sralph char	*AF;		/* accounting file */
1712117Sralph char	*LF;		/* log file for error messages */
1812117Sralph char	*OF;		/* name of output filter (created once) */
1912117Sralph char	*IF;		/* name of input filter (created per job) */
2012433Sralph char	*RF;		/* name of fortran text filter (per job) */
2112117Sralph char	*TF;		/* name of troff filter (per job) */
2212117Sralph char	*DF;		/* name of tex filter (per job) */
2312117Sralph char	*GF;		/* name of graph(1G) filter (per job) */
2412117Sralph char	*VF;		/* name of vplot filter (per job) */
2512117Sralph char	*CF;		/* name of cifplot filter (per job) */
2612117Sralph char	*PF;		/* name of vrast filter (per job) */
2712117Sralph char	*FF;		/* form feed string */
2812117Sralph char	*TR;		/* trailer string to be output when Q empties */
2912117Sralph short	SF;		/* suppress FF on each print job */
3012117Sralph short	SH;		/* suppress header page */
3112117Sralph short	SB;		/* short banner instead of normal header */
3212117Sralph short	RW;		/* open LP for reading and writing */
3312117Sralph short	PW;		/* page width */
3412117Sralph short	PL;		/* page length */
3512433Sralph short	PX;		/* page width in pixels */
3612433Sralph short	PY;		/* page length in pixels */
3712117Sralph short	BR;		/* baud rate if lp is a tty */
3812117Sralph short	FC;		/* flags to clear if lp is a tty */
3912117Sralph short	FS;		/* flags to set if lp is a tty */
4012117Sralph short	XC;		/* flags to clear for local mode */
4112117Sralph short	XS;		/* flags to set for local mode */
4212433Sralph short	RS;		/* restricted to those with local accounts */
4312117Sralph 
4412117Sralph char	line[BUFSIZ];
4512117Sralph char	pbuf[BUFSIZ/2];	/* buffer for printcap strings */
4612117Sralph char	*bp = pbuf;	/* pointer into pbuf for pgetent() */
4712117Sralph char	*name;		/* program name */
4812117Sralph char	*printer;	/* printer name */
4912117Sralph char	host[32];	/* host machine name */
5012117Sralph char	*from = host;	/* client's machine name */
5112117Sralph 
5212117Sralph /*
5312117Sralph  * Create a connection to the remote printer server.
5412117Sralph  * Most of this code comes from rcmd.c.
5512117Sralph  */
56*12528Sralph getport(rhost)
57*12528Sralph 	char *rhost;
5812117Sralph {
5912117Sralph 	struct hostent *hp;
6012117Sralph 	struct servent *sp;
6112117Sralph 	struct sockaddr_in sin;
6212117Sralph 	int s, timo = 1, lport = IPPORT_RESERVED - 1;
6312117Sralph 
6412117Sralph 	/*
6512117Sralph 	 * Get the host address and port number to connect to.
6612117Sralph 	 */
67*12528Sralph 	if (rhost == NULL)
6812117Sralph 		fatal("no remote host to connect to");
69*12528Sralph 	hp = gethostbyname(rhost);
7012117Sralph 	if (hp == NULL)
71*12528Sralph 		fatal("unknown host %s", rhost);
7212117Sralph 	sp = getservbyname("printer", "tcp");
7312117Sralph 	if (sp == NULL)
7412117Sralph 		fatal("printer/tcp: unknown service");
7512117Sralph 	bzero((char *)&sin, sizeof(sin));
7612117Sralph 	bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
7712117Sralph 	sin.sin_family = hp->h_addrtype;
7812117Sralph 	sin.sin_port = sp->s_port;
7912117Sralph 
8012117Sralph 	/*
8112117Sralph 	 * Try connecting to the server.
8212117Sralph 	 */
8312117Sralph retry:
8412117Sralph 	s = rresvport(&lport);
8512117Sralph 	if (s < 0)
8612117Sralph 		return(-1);
8712117Sralph 	if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) {
8812117Sralph 		if (errno == EADDRINUSE) {
8912117Sralph 			close(s);
9012117Sralph 			lport--;
9112117Sralph 			goto retry;
9212117Sralph 		}
9312117Sralph 		if (errno == ECONNREFUSED && timo <= 16) {
9412117Sralph 			(void) close(s);
9512117Sralph 			sleep(timo);
9612117Sralph 			timo *= 2;
9712117Sralph 			goto retry;
9812117Sralph 		}
9912117Sralph 		return(-1);
10012117Sralph 	}
10112117Sralph 	return(s);
10212117Sralph }
10312117Sralph 
10412117Sralph rresvport(alport)
10512117Sralph 	int *alport;
10612117Sralph {
10712117Sralph 	struct sockaddr_in sin;
10812117Sralph 	int s;
10912117Sralph 
11012117Sralph 	sin.sin_family = AF_INET;
11112117Sralph 	sin.sin_addr.s_addr = 0;
11212117Sralph 	s = socket(AF_INET, SOCK_STREAM, 0);
11312117Sralph 	if (s < 0)
11412117Sralph 		return(-1);
11512117Sralph 	for (;;) {
11612117Sralph 		sin.sin_port = htons((u_short) *alport);
11712117Sralph 		if (bind(s, (caddr_t)&sin, sizeof(sin), 0) >= 0)
11812117Sralph 			return(s);
11912117Sralph 		if (errno != EADDRINUSE && errno != EADDRNOTAVAIL)
12012117Sralph 			return(-1);
12112117Sralph 		(*alport)--;
122*12528Sralph 		if (*alport == IPPORT_RESERVED/2)
12312117Sralph 			return(-1);
12412117Sralph 	}
12512117Sralph }
12612117Sralph 
12712117Sralph /*
12812117Sralph  * Getline reads a line from the control file cfp, removes tabs, converts
12912117Sralph  *  new-line to null and leaves it in line.
13012117Sralph  * Returns 0 at EOF or the number of characters read.
13112117Sralph  */
13212117Sralph getline(cfp)
13312117Sralph 	FILE *cfp;
13412117Sralph {
13512117Sralph 	register int linel = 0;
13612117Sralph 	register char *lp = line;
13712117Sralph 	register c;
13812117Sralph 
13912117Sralph 	while ((c = getc(cfp)) != '\n') {
14012117Sralph 		if (c == EOF)
14112117Sralph 			return(0);
14212117Sralph 		if (c == '\t') {
14312117Sralph 			do {
14412117Sralph 				*lp++ = ' ';
14512117Sralph 				linel++;
14612117Sralph 			} while ((linel & 07) != 0);
14712117Sralph 			continue;
14812117Sralph 		}
14912117Sralph 		*lp++ = c;
15012117Sralph 		linel++;
15112117Sralph 	}
15212117Sralph 	*lp++ = '\0';
15312117Sralph 	return(linel);
15412117Sralph }
15512117Sralph 
15612117Sralph /*
15712117Sralph  * Scan the current directory and make a list of daemon files sorted by
15812117Sralph  * creation time.
15912117Sralph  * Return the number of entries and a pointer to the list.
16012117Sralph  */
16112117Sralph getq(namelist)
16212117Sralph 	struct queue *(*namelist[]);
16312117Sralph {
16412117Sralph 	register struct direct *d;
16512117Sralph 	register struct queue *q, **queue;
16612117Sralph 	register int nitems;
16712117Sralph 	struct stat stbuf;
16812117Sralph 	int arraysz, compar();
16912117Sralph 	DIR *dirp;
17012117Sralph 
17112117Sralph 	if ((dirp = opendir(".")) == NULL)
17212117Sralph 		return(-1);
17312117Sralph 	if (fstat(dirp->dd_fd, &stbuf) < 0)
17412117Sralph 		return(-1);
17512117Sralph 
17612117Sralph 	/*
17712117Sralph 	 * Estimate the array size by taking the size of the directory file
17812117Sralph 	 * and dividing it by a multiple of the minimum size entry.
17912117Sralph 	 */
18012117Sralph 	arraysz = (stbuf.st_size / 24);
18112117Sralph 	queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
18212117Sralph 	if (queue == NULL)
18312117Sralph 		return(-1);
18412117Sralph 
18512117Sralph 	nitems = 0;
18612117Sralph 	while ((d = readdir(dirp)) != NULL) {
18712117Sralph 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
18812117Sralph 			continue;	/* daemon control files only */
18912117Sralph 		if (stat(d->d_name, &stbuf) < 0)
19012117Sralph 			continue;	/* Doesn't exist */
19112117Sralph 		q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
19212117Sralph 		if (q == NULL)
19312117Sralph 			return(-1);
19412117Sralph 		q->q_time = stbuf.st_mtime;
19512117Sralph 		strcpy(q->q_name, d->d_name);
19612117Sralph 		/*
19712117Sralph 		 * Check to make sure the array has space left and
19812117Sralph 		 * realloc the maximum size.
19912117Sralph 		 */
20012117Sralph 		if (++nitems > arraysz) {
20112117Sralph 			queue = (struct queue **)realloc((char *)queue,
20212117Sralph 				(stbuf.st_size/12) * sizeof(struct queue *));
20312117Sralph 			if (queue == NULL)
20412117Sralph 				return(-1);
20512117Sralph 		}
20612117Sralph 		queue[nitems-1] = q;
20712117Sralph 	}
20812117Sralph 	closedir(dirp);
20912117Sralph 	if (nitems)
21012117Sralph 		qsort(queue, nitems, sizeof(struct queue *), compar);
21112117Sralph 	*namelist = queue;
21212117Sralph 	return(nitems);
21312117Sralph }
21412117Sralph 
21512117Sralph /*
21612117Sralph  * Compare modification times.
21712117Sralph  */
21812117Sralph static
21912117Sralph compar(p1, p2)
22012117Sralph 	register struct queue **p1, **p2;
22112117Sralph {
22212117Sralph 	if ((*p1)->q_time < (*p2)->q_time)
22312117Sralph 		return(-1);
22412117Sralph 	if ((*p1)->q_time > (*p2)->q_time)
22512117Sralph 		return(1);
22612117Sralph 	return(0);
22712117Sralph }
22812117Sralph 
22912117Sralph /*VARARGS1*/
23012117Sralph fatal(msg, a1, a2, a3)
23112117Sralph 	char *msg;
23212117Sralph {
23312117Sralph 	if (from != host)
23412117Sralph 		printf("%s: ", host);
23512117Sralph 	printf("%s: ", name);
23612117Sralph 	if (printer)
23712117Sralph 		printf("%s: ", printer);
23812117Sralph 	printf(msg, a1, a2, a3);
23912117Sralph 	putchar('\n');
24012117Sralph 	exit(1);
24112117Sralph }
24212117Sralph 
24312117Sralph fatalerror(msg)
24412117Sralph 	char *msg;
24512117Sralph {
24612117Sralph 	extern int sys_nerr;
24712117Sralph 	extern char *sys_errlist[];
24812117Sralph 
24912117Sralph 	printf("%s: ", name);
25012117Sralph 	if (*msg)
25112117Sralph 		printf("%s: ", msg);
25212117Sralph 	fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout);
25312117Sralph 	putchar('\n');
25412117Sralph 	exit(1);
25512117Sralph }
256