1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)common.c	5.6 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 /*
13  * Routines and data common to all the line printer functions.
14  */
15 
16 #include "lp.h"
17 
18 int	DU;		/* daeomon user-id */
19 int	MX;		/* maximum number of blocks to copy */
20 int	MC;		/* maximum number of copies allowed */
21 char	*LP;		/* line printer device name */
22 char	*RM;		/* remote machine name */
23 char	*RP;		/* remote printer name */
24 char	*LO;		/* lock file name */
25 char	*ST;		/* status file name */
26 char	*SD;		/* spool directory */
27 char	*AF;		/* accounting file */
28 char	*LF;		/* log file for error messages */
29 char	*OF;		/* name of output filter (created once) */
30 char	*IF;		/* name of input filter (created per job) */
31 char	*RF;		/* name of fortran text filter (per job) */
32 char	*TF;		/* name of troff filter (per job) */
33 char	*NF;		/* name of ditroff filter (per job) */
34 char	*DF;		/* name of tex filter (per job) */
35 char	*GF;		/* name of graph(1G) filter (per job) */
36 char	*VF;		/* name of vplot filter (per job) */
37 char	*CF;		/* name of cifplot filter (per job) */
38 char	*PF;		/* name of vrast filter (per job) */
39 char	*FF;		/* form feed string */
40 char	*TR;		/* trailer string to be output when Q empties */
41 short	SC;		/* suppress multiple copies */
42 short	SF;		/* suppress FF on each print job */
43 short	SH;		/* suppress header page */
44 short	SB;		/* short banner instead of normal header */
45 short	HL;		/* print header last */
46 short	RW;		/* open LP for reading and writing */
47 short	PW;		/* page width */
48 short	PL;		/* page length */
49 short	PX;		/* page width in pixels */
50 short	PY;		/* page length in pixels */
51 short	BR;		/* baud rate if lp is a tty */
52 int	FC;		/* flags to clear if lp is a tty */
53 int	FS;		/* flags to set if lp is a tty */
54 int	XC;		/* flags to clear for local mode */
55 int	XS;		/* flags to set for local mode */
56 short	RS;		/* restricted to those with local accounts */
57 
58 char	line[BUFSIZ];
59 char	pbuf[BUFSIZ/2];	/* buffer for printcap strings */
60 char	*bp = pbuf;	/* pointer into pbuf for pgetent() */
61 char	*name;		/* program name */
62 char	*printer;	/* printer name */
63 char	host[32];	/* host machine name */
64 char	*from = host;	/* client's machine name */
65 int	sendtorem;	/* are we sending to a remote? */
66 
67 /*
68  * Create a connection to the remote printer server.
69  * Most of this code comes from rcmd.c.
70  */
71 getport(rhost)
72 	char *rhost;
73 {
74 	struct hostent *hp;
75 	struct servent *sp;
76 	struct sockaddr_in sin;
77 	int s, timo = 1, lport = IPPORT_RESERVED - 1;
78 	int err;
79 
80 	/*
81 	 * Get the host address and port number to connect to.
82 	 */
83 	if (rhost == NULL)
84 		fatal("no remote host to connect to");
85 	hp = gethostbyname(rhost);
86 	if (hp == NULL)
87 		fatal("unknown host %s", rhost);
88 	sp = getservbyname("printer", "tcp");
89 	if (sp == NULL)
90 		fatal("printer/tcp: unknown service");
91 	bzero((char *)&sin, sizeof(sin));
92 	bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
93 	sin.sin_family = hp->h_addrtype;
94 	sin.sin_port = sp->s_port;
95 
96 	/*
97 	 * Try connecting to the server.
98 	 */
99 retry:
100 	s = rresvport(&lport);
101 	if (s < 0)
102 		return(-1);
103 	if (connect(s, (caddr_t)&sin, sizeof(sin)) < 0) {
104 		err = errno;
105 		(void) close(s);
106 		errno = err;
107 		if (errno == EADDRINUSE) {
108 			lport--;
109 			goto retry;
110 		}
111 		if (errno == ECONNREFUSED && timo <= 16) {
112 			sleep(timo);
113 			timo *= 2;
114 			goto retry;
115 		}
116 		return(-1);
117 	}
118 	return(s);
119 }
120 
121 /*
122  * Getline reads a line from the control file cfp, removes tabs, converts
123  *  new-line to null and leaves it in line.
124  * Returns 0 at EOF or the number of characters read.
125  */
126 getline(cfp)
127 	FILE *cfp;
128 {
129 	register int linel = 0;
130 	register char *lp = line;
131 	register c;
132 
133 	while ((c = getc(cfp)) != '\n') {
134 		if (c == EOF)
135 			return(0);
136 		if (c == '\t') {
137 			do {
138 				*lp++ = ' ';
139 				linel++;
140 			} while ((linel & 07) != 0);
141 			continue;
142 		}
143 		*lp++ = c;
144 		linel++;
145 	}
146 	*lp++ = '\0';
147 	return(linel);
148 }
149 
150 /*
151  * Scan the current directory and make a list of daemon files sorted by
152  * creation time.
153  * Return the number of entries and a pointer to the list.
154  */
155 getq(namelist)
156 	struct queue *(*namelist[]);
157 {
158 	register struct direct *d;
159 	register struct queue *q, **queue;
160 	register int nitems;
161 	struct stat stbuf;
162 	int arraysz, compar();
163 	DIR *dirp;
164 
165 	if ((dirp = opendir(SD)) == NULL)
166 		return(-1);
167 	if (fstat(dirp->dd_fd, &stbuf) < 0)
168 		goto errdone;
169 
170 	/*
171 	 * Estimate the array size by taking the size of the directory file
172 	 * and dividing it by a multiple of the minimum size entry.
173 	 */
174 	arraysz = (stbuf.st_size / 24);
175 	queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
176 	if (queue == NULL)
177 		goto errdone;
178 
179 	nitems = 0;
180 	while ((d = readdir(dirp)) != NULL) {
181 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
182 			continue;	/* daemon control files only */
183 		if (stat(d->d_name, &stbuf) < 0)
184 			continue;	/* Doesn't exist */
185 		q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
186 		if (q == NULL)
187 			goto errdone;
188 		q->q_time = stbuf.st_mtime;
189 		strcpy(q->q_name, d->d_name);
190 		/*
191 		 * Check to make sure the array has space left and
192 		 * realloc the maximum size.
193 		 */
194 		if (++nitems > arraysz) {
195 			queue = (struct queue **)realloc((char *)queue,
196 				(stbuf.st_size/12) * sizeof(struct queue *));
197 			if (queue == NULL)
198 				goto errdone;
199 		}
200 		queue[nitems-1] = q;
201 	}
202 	closedir(dirp);
203 	if (nitems)
204 		qsort(queue, nitems, sizeof(struct queue *), compar);
205 	*namelist = queue;
206 	return(nitems);
207 
208 errdone:
209 	closedir(dirp);
210 	return(-1);
211 }
212 
213 /*
214  * Compare modification times.
215  */
216 static
217 compar(p1, p2)
218 	register struct queue **p1, **p2;
219 {
220 	if ((*p1)->q_time < (*p2)->q_time)
221 		return(-1);
222 	if ((*p1)->q_time > (*p2)->q_time)
223 		return(1);
224 	return(0);
225 }
226 
227 /*
228  * Figure out whether the local machine is the same
229  * as the remote machine (RM) entry (if it exists).
230  */
231 char *
232 checkremote()
233 {
234 	char name[MAXHOSTNAMELEN];
235 	register struct hostent *hp;
236 	static char errbuf[128];
237 
238 	sendtorem = 0;	/* assume printer is local */
239 	if (RM != (char *)NULL) {
240 		/* get the official name of the local host */
241 		gethostname(name, sizeof(name));
242 		name[sizeof(name)-1] = '\0';
243 		hp = gethostbyname(name);
244 		if (hp == (struct hostent *) NULL) {
245 		    (void) sprintf(errbuf,
246 			"unable to get official name for local machine %s",
247 			name);
248 		    return errbuf;
249 		} else (void) strcpy(name, hp->h_name);
250 
251 		/* get the official name of RM */
252 		hp = gethostbyname(RM);
253 		if (hp == (struct hostent *) NULL) {
254 		    (void) sprintf(errbuf,
255 			"unable to get official name for remote machine %s",
256 			RM);
257 		    return errbuf;
258 		}
259 
260 		/*
261 		 * if the two hosts are not the same,
262 		 * then the printer must be remote.
263 		 */
264 		if (strcmp(name, hp->h_name) != 0)
265 			sendtorem = 1;
266 	}
267 	return (char *)0;
268 }
269 
270 /*VARARGS1*/
271 fatal(msg, a1, a2, a3)
272 	char *msg;
273 {
274 	if (from != host)
275 		printf("%s: ", host);
276 	printf("%s: ", name);
277 	if (printer)
278 		printf("%s: ", printer);
279 	printf(msg, a1, a2, a3);
280 	putchar('\n');
281 	exit(1);
282 }
283