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