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 char *FF; /* form feed string */ 70 char *GF; /* name of graph(1G) filter (per job) */ 71 long HL; /* print header last */ 72 char *IF; /* name of input filter (created per job) */ 73 char *LF; /* log file for error messages */ 74 char *LO; /* lock file name */ 75 char *LP; /* line printer device name */ 76 long MC; /* maximum number of copies allowed */ 77 long MX; /* maximum number of blocks to copy */ 78 char *NF; /* name of ditroff filter (per job) */ 79 char *OF; /* name of output filter (created once) */ 80 char *PF; /* name of vrast filter (per job) */ 81 long PL; /* page length */ 82 long PW; /* page width */ 83 long PX; /* page width in pixels */ 84 long PY; /* page length in pixels */ 85 char *RF; /* name of fortran text filter (per job) */ 86 char *RG; /* resricted group */ 87 char *RM; /* remote machine name */ 88 char *RP; /* remote printer name */ 89 long RS; /* restricted to those with local accounts */ 90 long RW; /* open LP for reading and writing */ 91 long SB; /* short banner instead of normal header */ 92 long SC; /* suppress multiple copies */ 93 char *SD; /* spool directory */ 94 long SF; /* suppress FF on each print job */ 95 long SH; /* suppress header page */ 96 char *ST; /* status file name */ 97 char *TF; /* name of troff filter (per job) */ 98 char *TR; /* trailer string to be output when Q empties */ 99 char *MS; /* mode set, a la stty */ 100 char *VF; /* name of vplot filter (per job) */ 101 102 char line[BUFSIZ]; 103 char *bp; /* pointer into printcap buffer. */ 104 char *name; /* program name */ 105 char *printer; /* printer name */ 106 /* host machine name */ 107 char host[MAXHOSTNAMELEN]; 108 char *from = host; /* client's machine name */ 109 int remote; /* true if sending files to a remote host */ 110 char *printcapdb[2] = { _PATH_PRINTCAP, 0 }; 111 112 static int compar __P((const void *, const void *)); 113 114 /* 115 * Create a TCP connection to host "rhost" at port "rport". 116 * If rport == 0, then use the printer service port. 117 * Most of this code comes from rcmd.c. 118 */ 119 int 120 getport(rhost, rport) 121 char *rhost; 122 int rport; 123 { 124 struct hostent *hp; 125 struct servent *sp; 126 struct sockaddr_in sin; 127 int s, timo = 1, lport = IPPORT_RESERVED - 1; 128 int err; 129 130 /* 131 * Get the host address and port number to connect to. 132 */ 133 if (rhost == NULL) 134 fatal("no remote host to connect to"); 135 bzero((char *)&sin, sizeof(sin)); 136 sin.sin_addr.s_addr = inet_addr(rhost); 137 if (sin.sin_addr.s_addr != INADDR_NONE) 138 sin.sin_family = AF_INET; 139 else { 140 hp = gethostbyname(rhost); 141 if (hp == NULL) 142 fatal("unknown host %s", rhost); 143 bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length); 144 sin.sin_family = hp->h_addrtype; 145 } 146 if (rport == 0) { 147 sp = getservbyname("printer", "tcp"); 148 if (sp == NULL) 149 fatal("printer/tcp: unknown service"); 150 sin.sin_port = sp->s_port; 151 } else 152 sin.sin_port = htons(rport); 153 154 /* 155 * Try connecting to the server. 156 */ 157 retry: 158 s = rresvport(&lport); 159 if (s < 0) 160 return(-1); 161 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 162 err = errno; 163 (void) close(s); 164 errno = err; 165 if (errno == EADDRINUSE) { 166 lport--; 167 goto retry; 168 } 169 if (errno == ECONNREFUSED && timo <= 16) { 170 sleep(timo); 171 timo *= 2; 172 goto retry; 173 } 174 return(-1); 175 } 176 return(s); 177 } 178 179 /* 180 * Getline reads a line from the control file cfp, removes tabs, converts 181 * new-line to null and leaves it in line. 182 * Returns 0 at EOF or the number of characters read. 183 */ 184 int 185 getline(cfp) 186 FILE *cfp; 187 { 188 register int linel = 0; 189 register char *lp = line; 190 register c; 191 192 while ((c = getc(cfp)) != '\n') { 193 if (c == EOF) 194 return(0); 195 if (c == '\t') { 196 do { 197 *lp++ = ' '; 198 linel++; 199 } while ((linel & 07) != 0); 200 continue; 201 } 202 *lp++ = c; 203 linel++; 204 } 205 *lp++ = '\0'; 206 return(linel); 207 } 208 209 /* 210 * Scan the current directory and make a list of daemon files sorted by 211 * creation time. 212 * Return the number of entries and a pointer to the list. 213 */ 214 int 215 getq(namelist) 216 struct queue *(*namelist[]); 217 { 218 register struct dirent *d; 219 register struct queue *q, **queue; 220 register int nitems; 221 struct stat stbuf; 222 DIR *dirp; 223 int arraysz; 224 225 if ((dirp = opendir(SD)) == NULL) 226 return(-1); 227 if (fstat(dirp->dd_fd, &stbuf) < 0) 228 goto errdone; 229 230 /* 231 * Estimate the array size by taking the size of the directory file 232 * and dividing it by a multiple of the minimum size entry. 233 */ 234 arraysz = (stbuf.st_size / 24); 235 queue = (struct queue **)malloc(arraysz * sizeof(struct queue *)); 236 if (queue == NULL) 237 goto errdone; 238 239 nitems = 0; 240 while ((d = readdir(dirp)) != NULL) { 241 if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 242 continue; /* daemon control files only */ 243 if (stat(d->d_name, &stbuf) < 0) 244 continue; /* Doesn't exist */ 245 q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1); 246 if (q == NULL) 247 goto errdone; 248 q->q_time = stbuf.st_mtime; 249 strcpy(q->q_name, d->d_name); 250 /* 251 * Check to make sure the array has space left and 252 * realloc the maximum size. 253 */ 254 if (++nitems > arraysz) { 255 arraysz *= 2; 256 queue = (struct queue **)realloc((char *)queue, 257 arraysz * sizeof(struct queue *)); 258 if (queue == NULL) 259 goto errdone; 260 } 261 queue[nitems-1] = q; 262 } 263 closedir(dirp); 264 if (nitems) 265 qsort(queue, nitems, sizeof(struct queue *), compar); 266 *namelist = queue; 267 return(nitems); 268 269 errdone: 270 closedir(dirp); 271 return(-1); 272 } 273 274 /* 275 * Compare modification times. 276 */ 277 static int 278 compar(p1, p2) 279 const void *p1, *p2; 280 { 281 if ((*(struct queue **)p1)->q_time < (*(struct queue **)p2)->q_time) 282 return(-1); 283 if ((*(struct queue **)p1)->q_time > (*(struct queue **)p2)->q_time) 284 return(1); 285 return(0); 286 } 287 288 /* 289 * Figure out whether the local machine is the same 290 * as the remote machine (RM) entry (if it exists). 291 */ 292 char * 293 checkremote() 294 { 295 char name[MAXHOSTNAMELEN]; 296 register struct hostent *hp; 297 static char errbuf[128]; 298 299 remote = 0; /* assume printer is local */ 300 if (RM != NULL) { 301 /* get the official name of the local host */ 302 gethostname(name, sizeof(name)); 303 name[sizeof(name)-1] = '\0'; 304 hp = gethostbyname(name); 305 if (hp == (struct hostent *) NULL) { 306 (void) snprintf(errbuf, sizeof(errbuf), 307 "unable to get official name for local machine %s", 308 name); 309 return errbuf; 310 } else (void) strcpy(name, hp->h_name); 311 312 /* get the official name of RM */ 313 hp = gethostbyname(RM); 314 if (hp == (struct hostent *) NULL) { 315 (void) snprintf(errbuf, sizeof(errbuf), 316 "unable to get official name for remote machine %s", 317 RM); 318 return errbuf; 319 } 320 321 /* 322 * if the two hosts are not the same, 323 * then the printer must be remote. 324 */ 325 if (strcasecmp(name, hp->h_name) != 0) 326 remote = 1; 327 } 328 return NULL; 329 } 330 331 /* sleep n milliseconds */ 332 void 333 delay(n) 334 { 335 struct timeval tdelay; 336 337 if (n <= 0 || n > 10000) 338 fatal("unreasonable delay period (%d)", n); 339 tdelay.tv_sec = n / 1000; 340 tdelay.tv_usec = n * 1000 % 1000000; 341 (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tdelay); 342 } 343 344 #if __STDC__ 345 #include <stdarg.h> 346 #else 347 #include <varargs.h> 348 #endif 349 350 void 351 #if __STDC__ 352 fatal(const char *msg, ...) 353 #else 354 fatal(msg, va_alist) 355 char *msg; 356 va_dcl 357 #endif 358 { 359 va_list ap; 360 #if __STDC__ 361 va_start(ap, msg); 362 #else 363 va_start(ap); 364 #endif 365 if (from != host) 366 (void)printf("%s: ", host); 367 (void)printf("%s: ", name); 368 if (printer) 369 (void)printf("%s: ", printer); 370 (void)vprintf(msg, ap); 371 va_end(ap); 372 (void)putchar('\n'); 373 exit(1); 374 } 375