common.c revision 1.1.1.3 1 1.1.1.2 cgd /*
2 1.1.1.2 cgd * Copyright (c) 1983, 1993
3 1.1.1.2 cgd * The Regents of the University of California. All rights reserved.
4 1.1.1.2 cgd * (c) UNIX System Laboratories, Inc.
5 1.1.1.2 cgd * All or some portions of this file are derived from material licensed
6 1.1.1.2 cgd * to the University of California by American Telephone and Telegraph
7 1.1.1.2 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 1.1.1.2 cgd * the permission of UNIX System Laboratories, Inc.
9 1.1.1.2 cgd *
10 1.1.1.2 cgd * Redistribution and use in source and binary forms, with or without
11 1.1.1.2 cgd * modification, are permitted provided that the following conditions
12 1.1.1.2 cgd * are met:
13 1.1.1.2 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1.1.2 cgd * notice, this list of conditions and the following disclaimer.
15 1.1.1.2 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.1.2 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1.1.2 cgd * documentation and/or other materials provided with the distribution.
18 1.1.1.2 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1.1.2 cgd * must display the following acknowledgement:
20 1.1.1.2 cgd * This product includes software developed by the University of
21 1.1.1.2 cgd * California, Berkeley and its contributors.
22 1.1.1.2 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1.1.2 cgd * may be used to endorse or promote products derived from this software
24 1.1.1.2 cgd * without specific prior written permission.
25 1.1.1.2 cgd *
26 1.1.1.2 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1.1.2 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1.1.2 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1.1.2 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1.1.2 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1.1.2 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1.1.2 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1.1.2 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1.1.2 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1.1.2 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1.1.2 cgd * SUCH DAMAGE.
37 1.1.1.2 cgd */
38 1.1.1.2 cgd
39 1.1.1.2 cgd #ifndef lint
40 1.1.1.3 mrg static char sccsid[] = "@(#)common.c 8.5 (Berkeley) 4/28/95";
41 1.1.1.2 cgd #endif /* not lint */
42 1.1.1.2 cgd
43 1.1.1.2 cgd #include <sys/param.h>
44 1.1.1.2 cgd #include <sys/stat.h>
45 1.1.1.3 mrg #include <sys/time.h>
46 1.1.1.2 cgd
47 1.1.1.2 cgd #include <sys/socket.h>
48 1.1.1.2 cgd #include <netinet/in.h>
49 1.1.1.2 cgd #include <netdb.h>
50 1.1.1.2 cgd
51 1.1.1.2 cgd #include <dirent.h>
52 1.1.1.2 cgd #include <errno.h>
53 1.1.1.2 cgd #include <unistd.h>
54 1.1.1.2 cgd #include <stdlib.h>
55 1.1.1.2 cgd #include <stdio.h>
56 1.1.1.2 cgd #include <string.h>
57 1.1.1.2 cgd #include "lp.h"
58 1.1.1.2 cgd #include "pathnames.h"
59 1.1.1.2 cgd
60 1.1.1.2 cgd /*
61 1.1.1.2 cgd * Routines and data common to all the line printer functions.
62 1.1.1.2 cgd */
63 1.1.1.2 cgd
64 1.1.1.2 cgd char *AF; /* accounting file */
65 1.1.1.2 cgd long BR; /* baud rate if lp is a tty */
66 1.1.1.2 cgd char *CF; /* name of cifplot filter (per job) */
67 1.1.1.2 cgd char *DF; /* name of tex filter (per job) */
68 1.1.1.2 cgd long DU; /* daeomon user-id */
69 1.1.1.2 cgd long FC; /* flags to clear if lp is a tty */
70 1.1.1.2 cgd char *FF; /* form feed string */
71 1.1.1.2 cgd long FS; /* flags to set if lp is a tty */
72 1.1.1.2 cgd char *GF; /* name of graph(1G) filter (per job) */
73 1.1.1.2 cgd long HL; /* print header last */
74 1.1.1.2 cgd char *IF; /* name of input filter (created per job) */
75 1.1.1.2 cgd char *LF; /* log file for error messages */
76 1.1.1.2 cgd char *LO; /* lock file name */
77 1.1.1.2 cgd char *LP; /* line printer device name */
78 1.1.1.2 cgd long MC; /* maximum number of copies allowed */
79 1.1.1.2 cgd long MX; /* maximum number of blocks to copy */
80 1.1.1.2 cgd char *NF; /* name of ditroff filter (per job) */
81 1.1.1.2 cgd char *OF; /* name of output filter (created once) */
82 1.1.1.2 cgd char *PF; /* name of vrast filter (per job) */
83 1.1.1.2 cgd long PL; /* page length */
84 1.1.1.2 cgd long PW; /* page width */
85 1.1.1.2 cgd long PX; /* page width in pixels */
86 1.1.1.2 cgd long PY; /* page length in pixels */
87 1.1.1.2 cgd char *RF; /* name of fortran text filter (per job) */
88 1.1.1.2 cgd char *RG; /* resricted group */
89 1.1.1.2 cgd char *RM; /* remote machine name */
90 1.1.1.2 cgd char *RP; /* remote printer name */
91 1.1.1.2 cgd long RS; /* restricted to those with local accounts */
92 1.1.1.2 cgd long RW; /* open LP for reading and writing */
93 1.1.1.2 cgd long SB; /* short banner instead of normal header */
94 1.1.1.2 cgd long SC; /* suppress multiple copies */
95 1.1.1.2 cgd char *SD; /* spool directory */
96 1.1.1.2 cgd long SF; /* suppress FF on each print job */
97 1.1.1.2 cgd long SH; /* suppress header page */
98 1.1.1.2 cgd char *ST; /* status file name */
99 1.1.1.2 cgd char *TF; /* name of troff filter (per job) */
100 1.1.1.2 cgd char *TR; /* trailer string to be output when Q empties */
101 1.1.1.2 cgd char *VF; /* name of vplot filter (per job) */
102 1.1.1.2 cgd long XC; /* flags to clear for local mode */
103 1.1.1.2 cgd long XS; /* flags to set for local mode */
104 1.1.1.2 cgd
105 1.1.1.2 cgd char line[BUFSIZ];
106 1.1.1.2 cgd char *bp; /* pointer into printcap buffer. */
107 1.1.1.2 cgd char *name; /* program name */
108 1.1.1.2 cgd char *printer; /* printer name */
109 1.1.1.2 cgd /* host machine name */
110 1.1.1.2 cgd char host[MAXHOSTNAMELEN];
111 1.1.1.2 cgd char *from = host; /* client's machine name */
112 1.1.1.3 mrg int remote; /* true if sending files to a remote host */
113 1.1.1.2 cgd char *printcapdb[2] = { _PATH_PRINTCAP, 0 };
114 1.1.1.2 cgd
115 1.1.1.2 cgd static int compar __P((const void *, const void *));
116 1.1.1.2 cgd
117 1.1.1.2 cgd /*
118 1.1.1.3 mrg * Create a TCP connection to host "rhost" at port "rport".
119 1.1.1.3 mrg * If rport == 0, then use the printer service port.
120 1.1.1.2 cgd * Most of this code comes from rcmd.c.
121 1.1.1.2 cgd */
122 1.1.1.2 cgd int
123 1.1.1.3 mrg getport(rhost, rport)
124 1.1.1.2 cgd char *rhost;
125 1.1.1.3 mrg int rport;
126 1.1.1.2 cgd {
127 1.1.1.2 cgd struct hostent *hp;
128 1.1.1.2 cgd struct servent *sp;
129 1.1.1.2 cgd struct sockaddr_in sin;
130 1.1.1.2 cgd int s, timo = 1, lport = IPPORT_RESERVED - 1;
131 1.1.1.2 cgd int err;
132 1.1.1.2 cgd
133 1.1.1.2 cgd /*
134 1.1.1.2 cgd * Get the host address and port number to connect to.
135 1.1.1.2 cgd */
136 1.1.1.2 cgd if (rhost == NULL)
137 1.1.1.2 cgd fatal("no remote host to connect to");
138 1.1.1.2 cgd bzero((char *)&sin, sizeof(sin));
139 1.1.1.3 mrg sin.sin_addr.s_addr = inet_addr(rhost);
140 1.1.1.3 mrg if (sin.sin_addr.s_addr != INADDR_NONE)
141 1.1.1.3 mrg sin.sin_family = AF_INET;
142 1.1.1.3 mrg else {
143 1.1.1.3 mrg hp = gethostbyname(rhost);
144 1.1.1.3 mrg if (hp == NULL)
145 1.1.1.3 mrg fatal("unknown host %s", rhost);
146 1.1.1.3 mrg bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
147 1.1.1.3 mrg sin.sin_family = hp->h_addrtype;
148 1.1.1.3 mrg }
149 1.1.1.3 mrg if (rport == 0) {
150 1.1.1.3 mrg sp = getservbyname("printer", "tcp");
151 1.1.1.3 mrg if (sp == NULL)
152 1.1.1.3 mrg fatal("printer/tcp: unknown service");
153 1.1.1.3 mrg sin.sin_port = sp->s_port;
154 1.1.1.3 mrg } else
155 1.1.1.3 mrg sin.sin_port = htons(rport);
156 1.1.1.2 cgd
157 1.1.1.2 cgd /*
158 1.1.1.2 cgd * Try connecting to the server.
159 1.1.1.2 cgd */
160 1.1.1.2 cgd retry:
161 1.1.1.2 cgd s = rresvport(&lport);
162 1.1.1.2 cgd if (s < 0)
163 1.1.1.2 cgd return(-1);
164 1.1.1.2 cgd if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
165 1.1.1.2 cgd err = errno;
166 1.1.1.2 cgd (void) close(s);
167 1.1.1.2 cgd errno = err;
168 1.1.1.2 cgd if (errno == EADDRINUSE) {
169 1.1.1.2 cgd lport--;
170 1.1.1.2 cgd goto retry;
171 1.1.1.2 cgd }
172 1.1.1.2 cgd if (errno == ECONNREFUSED && timo <= 16) {
173 1.1.1.2 cgd sleep(timo);
174 1.1.1.2 cgd timo *= 2;
175 1.1.1.2 cgd goto retry;
176 1.1.1.2 cgd }
177 1.1.1.2 cgd return(-1);
178 1.1.1.2 cgd }
179 1.1.1.2 cgd return(s);
180 1.1.1.2 cgd }
181 1.1.1.2 cgd
182 1.1.1.2 cgd /*
183 1.1.1.2 cgd * Getline reads a line from the control file cfp, removes tabs, converts
184 1.1.1.2 cgd * new-line to null and leaves it in line.
185 1.1.1.2 cgd * Returns 0 at EOF or the number of characters read.
186 1.1.1.2 cgd */
187 1.1.1.2 cgd int
188 1.1.1.2 cgd getline(cfp)
189 1.1.1.2 cgd FILE *cfp;
190 1.1.1.2 cgd {
191 1.1.1.2 cgd register int linel = 0;
192 1.1.1.2 cgd register char *lp = line;
193 1.1.1.2 cgd register c;
194 1.1.1.2 cgd
195 1.1.1.2 cgd while ((c = getc(cfp)) != '\n') {
196 1.1.1.2 cgd if (c == EOF)
197 1.1.1.2 cgd return(0);
198 1.1.1.2 cgd if (c == '\t') {
199 1.1.1.2 cgd do {
200 1.1.1.2 cgd *lp++ = ' ';
201 1.1.1.2 cgd linel++;
202 1.1.1.2 cgd } while ((linel & 07) != 0);
203 1.1.1.2 cgd continue;
204 1.1.1.2 cgd }
205 1.1.1.2 cgd *lp++ = c;
206 1.1.1.2 cgd linel++;
207 1.1.1.2 cgd }
208 1.1.1.2 cgd *lp++ = '\0';
209 1.1.1.2 cgd return(linel);
210 1.1.1.2 cgd }
211 1.1.1.2 cgd
212 1.1.1.2 cgd /*
213 1.1.1.2 cgd * Scan the current directory and make a list of daemon files sorted by
214 1.1.1.2 cgd * creation time.
215 1.1.1.2 cgd * Return the number of entries and a pointer to the list.
216 1.1.1.2 cgd */
217 1.1.1.2 cgd int
218 1.1.1.2 cgd getq(namelist)
219 1.1.1.2 cgd struct queue *(*namelist[]);
220 1.1.1.2 cgd {
221 1.1.1.2 cgd register struct dirent *d;
222 1.1.1.2 cgd register struct queue *q, **queue;
223 1.1.1.2 cgd register int nitems;
224 1.1.1.2 cgd struct stat stbuf;
225 1.1.1.2 cgd DIR *dirp;
226 1.1.1.2 cgd int arraysz;
227 1.1.1.2 cgd
228 1.1.1.2 cgd if ((dirp = opendir(SD)) == NULL)
229 1.1.1.2 cgd return(-1);
230 1.1.1.2 cgd if (fstat(dirp->dd_fd, &stbuf) < 0)
231 1.1.1.2 cgd goto errdone;
232 1.1.1.2 cgd
233 1.1.1.2 cgd /*
234 1.1.1.2 cgd * Estimate the array size by taking the size of the directory file
235 1.1.1.2 cgd * and dividing it by a multiple of the minimum size entry.
236 1.1.1.2 cgd */
237 1.1.1.2 cgd arraysz = (stbuf.st_size / 24);
238 1.1.1.2 cgd queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
239 1.1.1.2 cgd if (queue == NULL)
240 1.1.1.2 cgd goto errdone;
241 1.1.1.2 cgd
242 1.1.1.2 cgd nitems = 0;
243 1.1.1.2 cgd while ((d = readdir(dirp)) != NULL) {
244 1.1.1.2 cgd if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
245 1.1.1.2 cgd continue; /* daemon control files only */
246 1.1.1.2 cgd if (stat(d->d_name, &stbuf) < 0)
247 1.1.1.2 cgd continue; /* Doesn't exist */
248 1.1.1.2 cgd q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
249 1.1.1.2 cgd if (q == NULL)
250 1.1.1.2 cgd goto errdone;
251 1.1.1.2 cgd q->q_time = stbuf.st_mtime;
252 1.1.1.2 cgd strcpy(q->q_name, d->d_name);
253 1.1.1.2 cgd /*
254 1.1.1.2 cgd * Check to make sure the array has space left and
255 1.1.1.2 cgd * realloc the maximum size.
256 1.1.1.2 cgd */
257 1.1.1.2 cgd if (++nitems > arraysz) {
258 1.1.1.3 mrg arraysz *= 2;
259 1.1.1.2 cgd queue = (struct queue **)realloc((char *)queue,
260 1.1.1.3 mrg arraysz * sizeof(struct queue *));
261 1.1.1.2 cgd if (queue == NULL)
262 1.1.1.2 cgd goto errdone;
263 1.1.1.2 cgd }
264 1.1.1.2 cgd queue[nitems-1] = q;
265 1.1.1.2 cgd }
266 1.1.1.2 cgd closedir(dirp);
267 1.1.1.2 cgd if (nitems)
268 1.1.1.2 cgd qsort(queue, nitems, sizeof(struct queue *), compar);
269 1.1.1.2 cgd *namelist = queue;
270 1.1.1.2 cgd return(nitems);
271 1.1.1.2 cgd
272 1.1.1.2 cgd errdone:
273 1.1.1.2 cgd closedir(dirp);
274 1.1.1.2 cgd return(-1);
275 1.1.1.2 cgd }
276 1.1.1.2 cgd
277 1.1.1.2 cgd /*
278 1.1.1.2 cgd * Compare modification times.
279 1.1.1.2 cgd */
280 1.1.1.2 cgd static int
281 1.1.1.2 cgd compar(p1, p2)
282 1.1.1.2 cgd const void *p1, *p2;
283 1.1.1.2 cgd {
284 1.1.1.2 cgd if ((*(struct queue **)p1)->q_time < (*(struct queue **)p2)->q_time)
285 1.1.1.2 cgd return(-1);
286 1.1.1.2 cgd if ((*(struct queue **)p1)->q_time > (*(struct queue **)p2)->q_time)
287 1.1.1.2 cgd return(1);
288 1.1.1.2 cgd return(0);
289 1.1.1.2 cgd }
290 1.1.1.2 cgd
291 1.1.1.2 cgd /*
292 1.1.1.2 cgd * Figure out whether the local machine is the same
293 1.1.1.2 cgd * as the remote machine (RM) entry (if it exists).
294 1.1.1.2 cgd */
295 1.1.1.2 cgd char *
296 1.1.1.2 cgd checkremote()
297 1.1.1.2 cgd {
298 1.1.1.2 cgd char name[MAXHOSTNAMELEN];
299 1.1.1.2 cgd register struct hostent *hp;
300 1.1.1.2 cgd static char errbuf[128];
301 1.1.1.2 cgd
302 1.1.1.3 mrg remote = 0; /* assume printer is local */
303 1.1.1.3 mrg if (RM != NULL) {
304 1.1.1.2 cgd /* get the official name of the local host */
305 1.1.1.2 cgd gethostname(name, sizeof(name));
306 1.1.1.2 cgd name[sizeof(name)-1] = '\0';
307 1.1.1.2 cgd hp = gethostbyname(name);
308 1.1.1.2 cgd if (hp == (struct hostent *) NULL) {
309 1.1.1.2 cgd (void) snprintf(errbuf, sizeof(errbuf),
310 1.1.1.2 cgd "unable to get official name for local machine %s",
311 1.1.1.2 cgd name);
312 1.1.1.2 cgd return errbuf;
313 1.1.1.2 cgd } else (void) strcpy(name, hp->h_name);
314 1.1.1.2 cgd
315 1.1.1.2 cgd /* get the official name of RM */
316 1.1.1.2 cgd hp = gethostbyname(RM);
317 1.1.1.2 cgd if (hp == (struct hostent *) NULL) {
318 1.1.1.2 cgd (void) snprintf(errbuf, sizeof(errbuf),
319 1.1.1.2 cgd "unable to get official name for remote machine %s",
320 1.1.1.2 cgd RM);
321 1.1.1.2 cgd return errbuf;
322 1.1.1.2 cgd }
323 1.1.1.2 cgd
324 1.1.1.2 cgd /*
325 1.1.1.2 cgd * if the two hosts are not the same,
326 1.1.1.2 cgd * then the printer must be remote.
327 1.1.1.2 cgd */
328 1.1.1.3 mrg if (strcasecmp(name, hp->h_name) != 0)
329 1.1.1.3 mrg remote = 1;
330 1.1.1.2 cgd }
331 1.1.1.3 mrg return NULL;
332 1.1.1.3 mrg }
333 1.1.1.3 mrg
334 1.1.1.3 mrg /* sleep n milliseconds */
335 1.1.1.3 mrg void
336 1.1.1.3 mrg delay(n)
337 1.1.1.3 mrg {
338 1.1.1.3 mrg struct timeval tdelay;
339 1.1.1.3 mrg
340 1.1.1.3 mrg if (n <= 0 || n > 10000)
341 1.1.1.3 mrg fatal("unreasonable delay period (%d)", n);
342 1.1.1.3 mrg tdelay.tv_sec = n / 1000;
343 1.1.1.3 mrg tdelay.tv_usec = n * 1000 % 1000000;
344 1.1.1.3 mrg (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tdelay);
345 1.1.1.2 cgd }
346 1.1.1.2 cgd
347 1.1.1.2 cgd #if __STDC__
348 1.1.1.2 cgd #include <stdarg.h>
349 1.1.1.2 cgd #else
350 1.1.1.2 cgd #include <varargs.h>
351 1.1.1.2 cgd #endif
352 1.1.1.2 cgd
353 1.1.1.2 cgd void
354 1.1.1.2 cgd #if __STDC__
355 1.1.1.2 cgd fatal(const char *msg, ...)
356 1.1.1.2 cgd #else
357 1.1.1.2 cgd fatal(msg, va_alist)
358 1.1.1.2 cgd char *msg;
359 1.1.1.2 cgd va_dcl
360 1.1.1.2 cgd #endif
361 1.1.1.2 cgd {
362 1.1.1.2 cgd va_list ap;
363 1.1.1.2 cgd #if __STDC__
364 1.1.1.2 cgd va_start(ap, msg);
365 1.1.1.2 cgd #else
366 1.1.1.2 cgd va_start(ap);
367 1.1.1.2 cgd #endif
368 1.1.1.2 cgd if (from != host)
369 1.1.1.2 cgd (void)printf("%s: ", host);
370 1.1.1.2 cgd (void)printf("%s: ", name);
371 1.1.1.2 cgd if (printer)
372 1.1.1.2 cgd (void)printf("%s: ", printer);
373 1.1.1.2 cgd (void)vprintf(msg, ap);
374 1.1.1.2 cgd va_end(ap);
375 1.1.1.2 cgd (void)putchar('\n');
376 1.1.1.2 cgd exit(1);
377 1.1.1.2 cgd }
378