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