1 /*	common.c	4.5	83/06/02	*/
2 /*
3  * Routines and data common to all the line printer functions.
4  */
5 
6 #include "lp.h"
7 
8 int	DU;		/* daeomon user-id */
9 int	MX;		/* maximum number of blocks to copy */
10 char	*LP;		/* line printer device name */
11 char	*RM;		/* remote machine name */
12 char	*RP;		/* remote printer name */
13 char	*LO;		/* lock file name */
14 char	*ST;		/* status file name */
15 char	*SD;		/* spool directory */
16 char	*AF;		/* accounting file */
17 char	*LF;		/* log file for error messages */
18 char	*OF;		/* name of output filter (created once) */
19 char	*IF;		/* name of input filter (created per job) */
20 char	*RF;		/* name of fortran text filter (per job) */
21 char	*TF;		/* name of troff filter (per job) */
22 char	*DF;		/* name of tex filter (per job) */
23 char	*GF;		/* name of graph(1G) filter (per job) */
24 char	*VF;		/* name of vplot filter (per job) */
25 char	*CF;		/* name of cifplot filter (per job) */
26 char	*PF;		/* name of vrast filter (per job) */
27 char	*FF;		/* form feed string */
28 char	*TR;		/* trailer string to be output when Q empties */
29 short	SF;		/* suppress FF on each print job */
30 short	SH;		/* suppress header page */
31 short	SB;		/* short banner instead of normal header */
32 short	RW;		/* open LP for reading and writing */
33 short	PW;		/* page width */
34 short	PL;		/* page length */
35 short	PX;		/* page width in pixels */
36 short	PY;		/* page length in pixels */
37 short	BR;		/* baud rate if lp is a tty */
38 short	FC;		/* flags to clear if lp is a tty */
39 short	FS;		/* flags to set if lp is a tty */
40 short	XC;		/* flags to clear for local mode */
41 short	XS;		/* flags to set for local mode */
42 short	RS;		/* restricted to those with local accounts */
43 
44 char	line[BUFSIZ];
45 char	pbuf[BUFSIZ/2];	/* buffer for printcap strings */
46 char	*bp = pbuf;	/* pointer into pbuf for pgetent() */
47 char	*name;		/* program name */
48 char	*printer;	/* printer name */
49 char	host[32];	/* host machine name */
50 char	*from = host;	/* client's machine name */
51 
52 /*
53  * Create a connection to the remote printer server.
54  * Most of this code comes from rcmd.c.
55  */
56 getport(rhost)
57 	char *rhost;
58 {
59 	struct hostent *hp;
60 	struct servent *sp;
61 	struct sockaddr_in sin;
62 	int s, timo = 1, lport = IPPORT_RESERVED - 1;
63 	int err;
64 
65 	/*
66 	 * Get the host address and port number to connect to.
67 	 */
68 	if (rhost == NULL)
69 		fatal("no remote host to connect to");
70 	hp = gethostbyname(rhost);
71 	if (hp == NULL)
72 		fatal("unknown host %s", rhost);
73 	sp = getservbyname("printer", "tcp");
74 	if (sp == NULL)
75 		fatal("printer/tcp: unknown service");
76 	bzero((char *)&sin, sizeof(sin));
77 	bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
78 	sin.sin_family = hp->h_addrtype;
79 	sin.sin_port = sp->s_port;
80 
81 	/*
82 	 * Try connecting to the server.
83 	 */
84 retry:
85 	s = rresvport(&lport);
86 	if (s < 0)
87 		return(-1);
88 	if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) {
89 		err = errno;
90 		(void) close(s);
91 		errno = err;
92 		if (errno == EADDRINUSE) {
93 			lport--;
94 			goto retry;
95 		}
96 		if (errno == ECONNREFUSED && timo <= 16) {
97 			sleep(timo);
98 			timo *= 2;
99 			goto retry;
100 		}
101 		return(-1);
102 	}
103 	return(s);
104 }
105 
106 rresvport(alport)
107 	int *alport;
108 {
109 	struct sockaddr_in sin;
110 	int s;
111 
112 	sin.sin_family = AF_INET;
113 	sin.sin_addr.s_addr = 0;
114 	s = socket(AF_INET, SOCK_STREAM, 0);
115 	if (s < 0)
116 		return(-1);
117 	for (; *alport > IPPORT_RESERVED/2; (*alport)--) {
118 		sin.sin_port = htons((u_short) *alport);
119 		if (bind(s, (caddr_t)&sin, sizeof(sin), 0) >= 0)
120 			return(s);
121 		if (errno != EADDRINUSE && errno != EADDRNOTAVAIL)
122 			break;
123 	}
124 	(void) close(s);
125 	return(-1);
126 }
127 
128 /*
129  * Getline reads a line from the control file cfp, removes tabs, converts
130  *  new-line to null and leaves it in line.
131  * Returns 0 at EOF or the number of characters read.
132  */
133 getline(cfp)
134 	FILE *cfp;
135 {
136 	register int linel = 0;
137 	register char *lp = line;
138 	register c;
139 
140 	while ((c = getc(cfp)) != '\n') {
141 		if (c == EOF)
142 			return(0);
143 		if (c == '\t') {
144 			do {
145 				*lp++ = ' ';
146 				linel++;
147 			} while ((linel & 07) != 0);
148 			continue;
149 		}
150 		*lp++ = c;
151 		linel++;
152 	}
153 	*lp++ = '\0';
154 	return(linel);
155 }
156 
157 /*
158  * Scan the current directory and make a list of daemon files sorted by
159  * creation time.
160  * Return the number of entries and a pointer to the list.
161  */
162 getq(namelist)
163 	struct queue *(*namelist[]);
164 {
165 	register struct direct *d;
166 	register struct queue *q, **queue;
167 	register int nitems;
168 	struct stat stbuf;
169 	int arraysz, compar();
170 	DIR *dirp;
171 
172 	if ((dirp = opendir(".")) == NULL)
173 		return(-1);
174 	if (fstat(dirp->dd_fd, &stbuf) < 0)
175 		goto errdone;
176 
177 	/*
178 	 * Estimate the array size by taking the size of the directory file
179 	 * and dividing it by a multiple of the minimum size entry.
180 	 */
181 	arraysz = (stbuf.st_size / 24);
182 	queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
183 	if (queue == NULL)
184 		goto errdone;
185 
186 	nitems = 0;
187 	while ((d = readdir(dirp)) != NULL) {
188 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
189 			continue;	/* daemon control files only */
190 		if (stat(d->d_name, &stbuf) < 0)
191 			continue;	/* Doesn't exist */
192 		q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
193 		if (q == NULL)
194 			goto errdone;
195 		q->q_time = stbuf.st_mtime;
196 		strcpy(q->q_name, d->d_name);
197 		/*
198 		 * Check to make sure the array has space left and
199 		 * realloc the maximum size.
200 		 */
201 		if (++nitems > arraysz) {
202 			queue = (struct queue **)realloc((char *)queue,
203 				(stbuf.st_size/12) * sizeof(struct queue *));
204 			if (queue == NULL)
205 				goto errdone;
206 		}
207 		queue[nitems-1] = q;
208 	}
209 	closedir(dirp);
210 	if (nitems)
211 		qsort(queue, nitems, sizeof(struct queue *), compar);
212 	*namelist = queue;
213 	return(nitems);
214 
215 errdone:
216 	closedir(dirp);
217 	return(-1);
218 }
219 
220 /*
221  * Compare modification times.
222  */
223 static
224 compar(p1, p2)
225 	register struct queue **p1, **p2;
226 {
227 	if ((*p1)->q_time < (*p2)->q_time)
228 		return(-1);
229 	if ((*p1)->q_time > (*p2)->q_time)
230 		return(1);
231 	return(0);
232 }
233 
234 /*VARARGS1*/
235 fatal(msg, a1, a2, a3)
236 	char *msg;
237 {
238 	if (from != host)
239 		printf("%s: ", host);
240 	printf("%s: ", name);
241 	if (printer)
242 		printf("%s: ", printer);
243 	printf(msg, a1, a2, a3);
244 	putchar('\n');
245 	exit(1);
246 }
247