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