122426Sdist /*
261840Sbostic  * Copyright (c) 1983, 1993
361840Sbostic  *	The Regents of the University of California.  All rights reserved.
4*65779Sbostic  * (c) UNIX System Laboratories, Inc.
5*65779Sbostic  * All or some portions of this file are derived from material licensed
6*65779Sbostic  * to the University of California by American Telephone and Telegraph
7*65779Sbostic  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8*65779Sbostic  * the permission of UNIX System Laboratories, Inc.
934203Sbostic  *
1056248Selan  * %sccs.include.redist.c%
1122426Sdist  */
1222426Sdist 
1313952Ssam #ifndef lint
14*65779Sbostic static char sccsid[] = "@(#)common.c	8.2 (Berkeley) 01/21/94";
1534203Sbostic #endif /* not lint */
1613952Ssam 
1755470Sbostic #include <sys/param.h>
1855470Sbostic #include <sys/stat.h>
1955470Sbostic 
2055470Sbostic #include <sys/socket.h>
2155470Sbostic #include <netinet/in.h>
2255470Sbostic #include <netdb.h>
2355470Sbostic 
2455470Sbostic #include <dirent.h>
2555470Sbostic #include <errno.h>
2655470Sbostic #include <unistd.h>
2755470Sbostic #include <stdlib.h>
2855470Sbostic #include <stdio.h>
2955470Sbostic #include <string.h>
3055470Sbostic #include "lp.h"
3156120Selan #include "pathnames.h"
3255470Sbostic 
3312117Sralph /*
3412117Sralph  * Routines and data common to all the line printer functions.
3512117Sralph  */
3612117Sralph 
3756120Selan char	*AF;		/* accounting file */
3856120Selan long	 BR;		/* baud rate if lp is a tty */
3956120Selan char	*CF;		/* name of cifplot filter (per job) */
4056120Selan char	*DF;		/* name of tex filter (per job) */
4156120Selan long	 DU;		/* daeomon user-id */
4256120Selan long	 FC;		/* flags to clear if lp is a tty */
4356120Selan char	*FF;		/* form feed string */
4456120Selan long	 FS;		/* flags to set if lp is a tty */
4556120Selan char	*GF;		/* name of graph(1G) filter (per job) */
4656120Selan long	 HL;		/* print header last */
4756120Selan char	*IF;		/* name of input filter (created per job) */
4856120Selan char	*LF;		/* log file for error messages */
4956120Selan char	*LO;		/* lock file name */
5012117Sralph char	*LP;		/* line printer device name */
5156120Selan long	 MC;		/* maximum number of copies allowed */
5256120Selan long	 MX;		/* maximum number of blocks to copy */
5356120Selan char	*NF;		/* name of ditroff filter (per job) */
5456120Selan char	*OF;		/* name of output filter (created once) */
5556120Selan char	*PF;		/* name of vrast filter (per job) */
5656120Selan long	 PL;		/* page length */
5756120Selan long	 PW;		/* page width */
5856120Selan long	 PX;		/* page width in pixels */
5956120Selan long	 PY;		/* page length in pixels */
6056120Selan char	*RF;		/* name of fortran text filter (per job) */
6156120Selan char    *RG;		/* resricted group */
6212117Sralph char	*RM;		/* remote machine name */
6312117Sralph char	*RP;		/* remote printer name */
6456120Selan long	 RS;		/* restricted to those with local accounts */
6556120Selan long	 RW;		/* open LP for reading and writing */
6656120Selan long	 SB;		/* short banner instead of normal header */
6756120Selan long	 SC;		/* suppress multiple copies */
6856120Selan char	*SD;		/* spool directory */
6956120Selan long	 SF;		/* suppress FF on each print job */
7056120Selan long	 SH;		/* suppress header page */
7112117Sralph char	*ST;		/* status file name */
7212117Sralph char	*TF;		/* name of troff filter (per job) */
7356120Selan char	*TR;		/* trailer string to be output when Q empties */
7412117Sralph char	*VF;		/* name of vplot filter (per job) */
7556120Selan long	 XC;		/* flags to clear for local mode */
7656120Selan long	 XS;		/* flags to set for local mode */
7712117Sralph 
7812117Sralph char	line[BUFSIZ];
7956120Selan char	*bp;		/* pointer into printcap buffer. */
8012117Sralph char	*name;		/* program name */
8112117Sralph char	*printer;	/* printer name */
8255470Sbostic 			/* host machine name */
8355470Sbostic char	host[MAXHOSTNAMELEN];
8412117Sralph char	*from = host;	/* client's machine name */
8538736Stef int	sendtorem;	/* are we sending to a remote? */
8656120Selan char	*printcapdb[2] = { _PATH_PRINTCAP, 0 };
8712117Sralph 
8855470Sbostic static int compar __P((const void *, const void *));
8955470Sbostic 
9012117Sralph /*
9112117Sralph  * Create a connection to the remote printer server.
9212117Sralph  * Most of this code comes from rcmd.c.
9312117Sralph  */
9455470Sbostic int
9512528Sralph getport(rhost)
9612528Sralph 	char *rhost;
9712117Sralph {
9812117Sralph 	struct hostent *hp;
9912117Sralph 	struct servent *sp;
10012117Sralph 	struct sockaddr_in sin;
10112117Sralph 	int s, timo = 1, lport = IPPORT_RESERVED - 1;
10212874Sralph 	int err;
10312117Sralph 
10412117Sralph 	/*
10512117Sralph 	 * Get the host address and port number to connect to.
10612117Sralph 	 */
10712528Sralph 	if (rhost == NULL)
10812117Sralph 		fatal("no remote host to connect to");
10912528Sralph 	hp = gethostbyname(rhost);
11012117Sralph 	if (hp == NULL)
11112528Sralph 		fatal("unknown host %s", rhost);
11212117Sralph 	sp = getservbyname("printer", "tcp");
11312117Sralph 	if (sp == NULL)
11412117Sralph 		fatal("printer/tcp: unknown service");
11512117Sralph 	bzero((char *)&sin, sizeof(sin));
11612117Sralph 	bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
11712117Sralph 	sin.sin_family = hp->h_addrtype;
11812117Sralph 	sin.sin_port = sp->s_port;
11912117Sralph 
12012117Sralph 	/*
12112117Sralph 	 * Try connecting to the server.
12212117Sralph 	 */
12312117Sralph retry:
12412117Sralph 	s = rresvport(&lport);
12512117Sralph 	if (s < 0)
12612117Sralph 		return(-1);
12746910Sbostic 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
12812874Sralph 		err = errno;
12912874Sralph 		(void) close(s);
13012874Sralph 		errno = err;
13112117Sralph 		if (errno == EADDRINUSE) {
13212117Sralph 			lport--;
13312117Sralph 			goto retry;
13412117Sralph 		}
13512117Sralph 		if (errno == ECONNREFUSED && timo <= 16) {
13612117Sralph 			sleep(timo);
13712117Sralph 			timo *= 2;
13812117Sralph 			goto retry;
13912117Sralph 		}
14012117Sralph 		return(-1);
14112117Sralph 	}
14212117Sralph 	return(s);
14312117Sralph }
14412117Sralph 
14512117Sralph /*
14612117Sralph  * Getline reads a line from the control file cfp, removes tabs, converts
14712117Sralph  *  new-line to null and leaves it in line.
14812117Sralph  * Returns 0 at EOF or the number of characters read.
14912117Sralph  */
15055470Sbostic int
15112117Sralph getline(cfp)
15212117Sralph 	FILE *cfp;
15312117Sralph {
15412117Sralph 	register int linel = 0;
15512117Sralph 	register char *lp = line;
15612117Sralph 	register c;
15712117Sralph 
15812117Sralph 	while ((c = getc(cfp)) != '\n') {
15912117Sralph 		if (c == EOF)
16012117Sralph 			return(0);
16112117Sralph 		if (c == '\t') {
16212117Sralph 			do {
16312117Sralph 				*lp++ = ' ';
16412117Sralph 				linel++;
16512117Sralph 			} while ((linel & 07) != 0);
16612117Sralph 			continue;
16712117Sralph 		}
16812117Sralph 		*lp++ = c;
16912117Sralph 		linel++;
17012117Sralph 	}
17112117Sralph 	*lp++ = '\0';
17212117Sralph 	return(linel);
17312117Sralph }
17412117Sralph 
17512117Sralph /*
17612117Sralph  * Scan the current directory and make a list of daemon files sorted by
17712117Sralph  * creation time.
17812117Sralph  * Return the number of entries and a pointer to the list.
17912117Sralph  */
18055470Sbostic int
18112117Sralph getq(namelist)
18212117Sralph 	struct queue *(*namelist[]);
18312117Sralph {
18455470Sbostic 	register struct dirent *d;
18512117Sralph 	register struct queue *q, **queue;
18612117Sralph 	register int nitems;
18712117Sralph 	struct stat stbuf;
18812117Sralph 	DIR *dirp;
18946910Sbostic 	int arraysz;
19012117Sralph 
19113170Sralph 	if ((dirp = opendir(SD)) == NULL)
19212117Sralph 		return(-1);
19312117Sralph 	if (fstat(dirp->dd_fd, &stbuf) < 0)
19412874Sralph 		goto errdone;
19512117Sralph 
19612117Sralph 	/*
19712117Sralph 	 * Estimate the array size by taking the size of the directory file
19812117Sralph 	 * and dividing it by a multiple of the minimum size entry.
19912117Sralph 	 */
20012117Sralph 	arraysz = (stbuf.st_size / 24);
20112117Sralph 	queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
20212117Sralph 	if (queue == NULL)
20312874Sralph 		goto errdone;
20412117Sralph 
20512117Sralph 	nitems = 0;
20612117Sralph 	while ((d = readdir(dirp)) != NULL) {
20712117Sralph 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
20812117Sralph 			continue;	/* daemon control files only */
20912117Sralph 		if (stat(d->d_name, &stbuf) < 0)
21012117Sralph 			continue;	/* Doesn't exist */
21112117Sralph 		q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
21212117Sralph 		if (q == NULL)
21312874Sralph 			goto errdone;
21412117Sralph 		q->q_time = stbuf.st_mtime;
21512117Sralph 		strcpy(q->q_name, d->d_name);
21612117Sralph 		/*
21712117Sralph 		 * Check to make sure the array has space left and
21812117Sralph 		 * realloc the maximum size.
21912117Sralph 		 */
22012117Sralph 		if (++nitems > arraysz) {
22112117Sralph 			queue = (struct queue **)realloc((char *)queue,
22212117Sralph 				(stbuf.st_size/12) * sizeof(struct queue *));
22312117Sralph 			if (queue == NULL)
22412874Sralph 				goto errdone;
22512117Sralph 		}
22612117Sralph 		queue[nitems-1] = q;
22712117Sralph 	}
22812117Sralph 	closedir(dirp);
22912117Sralph 	if (nitems)
23012117Sralph 		qsort(queue, nitems, sizeof(struct queue *), compar);
23112117Sralph 	*namelist = queue;
23212117Sralph 	return(nitems);
23312874Sralph 
23412874Sralph errdone:
23512874Sralph 	closedir(dirp);
23612874Sralph 	return(-1);
23712117Sralph }
23812117Sralph 
23912117Sralph /*
24012117Sralph  * Compare modification times.
24112117Sralph  */
24255470Sbostic static int
24312117Sralph compar(p1, p2)
24455470Sbostic 	const void *p1, *p2;
24512117Sralph {
24655470Sbostic 	if ((*(struct queue **)p1)->q_time < (*(struct queue **)p2)->q_time)
24712117Sralph 		return(-1);
24855470Sbostic 	if ((*(struct queue **)p1)->q_time > (*(struct queue **)p2)->q_time)
24912117Sralph 		return(1);
25012117Sralph 	return(0);
25112117Sralph }
25212117Sralph 
25338736Stef /*
25438736Stef  * Figure out whether the local machine is the same
25538736Stef  * as the remote machine (RM) entry (if it exists).
25638736Stef  */
25738736Stef char *
25838736Stef checkremote()
25938736Stef {
26038736Stef 	char name[MAXHOSTNAMELEN];
26138736Stef 	register struct hostent *hp;
26238736Stef 	static char errbuf[128];
26338736Stef 
26438736Stef 	sendtorem = 0;	/* assume printer is local */
26538736Stef 	if (RM != (char *)NULL) {
26638736Stef 		/* get the official name of the local host */
26738736Stef 		gethostname(name, sizeof(name));
26838736Stef 		name[sizeof(name)-1] = '\0';
26938736Stef 		hp = gethostbyname(name);
27038736Stef 		if (hp == (struct hostent *) NULL) {
27155470Sbostic 		    (void) snprintf(errbuf, sizeof(errbuf),
27238736Stef 			"unable to get official name for local machine %s",
27338736Stef 			name);
27438736Stef 		    return errbuf;
27538736Stef 		} else (void) strcpy(name, hp->h_name);
27638736Stef 
27738736Stef 		/* get the official name of RM */
27838736Stef 		hp = gethostbyname(RM);
27938736Stef 		if (hp == (struct hostent *) NULL) {
28055470Sbostic 		    (void) snprintf(errbuf, sizeof(errbuf),
28138736Stef 			"unable to get official name for remote machine %s",
28238736Stef 			RM);
28338736Stef 		    return errbuf;
28438736Stef 		}
28538736Stef 
28638736Stef 		/*
28738736Stef 		 * if the two hosts are not the same,
28838736Stef 		 * then the printer must be remote.
28938736Stef 		 */
29038736Stef 		if (strcmp(name, hp->h_name) != 0)
29138736Stef 			sendtorem = 1;
29238736Stef 	}
29338736Stef 	return (char *)0;
29438736Stef }
29538736Stef 
29655470Sbostic #if __STDC__
29755470Sbostic #include <stdarg.h>
29855470Sbostic #else
29955470Sbostic #include <varargs.h>
30055470Sbostic #endif
30155470Sbostic 
30255470Sbostic void
30355470Sbostic #if __STDC__
30455470Sbostic fatal(const char *msg, ...)
30555470Sbostic #else
30655470Sbostic fatal(msg, va_alist)
30712117Sralph 	char *msg;
30855470Sbostic         va_dcl
30955470Sbostic #endif
31012117Sralph {
31155470Sbostic 	va_list ap;
31255470Sbostic #if __STDC__
31355470Sbostic 	va_start(ap, msg);
31455470Sbostic #else
31555470Sbostic 	va_start(ap);
31655470Sbostic #endif
31712117Sralph 	if (from != host)
31855470Sbostic 		(void)printf("%s: ", host);
31955470Sbostic 	(void)printf("%s: ", name);
32012117Sralph 	if (printer)
32155470Sbostic 		(void)printf("%s: ", printer);
32255470Sbostic 	(void)vprintf(msg, ap);
32355470Sbostic 	va_end(ap);
32455470Sbostic 	(void)putchar('\n');
32512117Sralph 	exit(1);
32612117Sralph }
327