1 /*	common.c	4.3	83/05/18	*/
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 
64 	/*
65 	 * Get the host address and port number to connect to.
66 	 */
67 	if (rhost == NULL)
68 		fatal("no remote host to connect to");
69 	hp = gethostbyname(rhost);
70 	if (hp == NULL)
71 		fatal("unknown host %s", rhost);
72 	sp = getservbyname("printer", "tcp");
73 	if (sp == NULL)
74 		fatal("printer/tcp: unknown service");
75 	bzero((char *)&sin, sizeof(sin));
76 	bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
77 	sin.sin_family = hp->h_addrtype;
78 	sin.sin_port = sp->s_port;
79 
80 	/*
81 	 * Try connecting to the server.
82 	 */
83 retry:
84 	s = rresvport(&lport);
85 	if (s < 0)
86 		return(-1);
87 	if (connect(s, (caddr_t)&sin, sizeof(sin), 0) < 0) {
88 		if (errno == EADDRINUSE) {
89 			close(s);
90 			lport--;
91 			goto retry;
92 		}
93 		if (errno == ECONNREFUSED && timo <= 16) {
94 			(void) close(s);
95 			sleep(timo);
96 			timo *= 2;
97 			goto retry;
98 		}
99 		return(-1);
100 	}
101 	return(s);
102 }
103 
104 rresvport(alport)
105 	int *alport;
106 {
107 	struct sockaddr_in sin;
108 	int s;
109 
110 	sin.sin_family = AF_INET;
111 	sin.sin_addr.s_addr = 0;
112 	s = socket(AF_INET, SOCK_STREAM, 0);
113 	if (s < 0)
114 		return(-1);
115 	for (;;) {
116 		sin.sin_port = htons((u_short) *alport);
117 		if (bind(s, (caddr_t)&sin, sizeof(sin), 0) >= 0)
118 			return(s);
119 		if (errno != EADDRINUSE && errno != EADDRNOTAVAIL)
120 			return(-1);
121 		(*alport)--;
122 		if (*alport == IPPORT_RESERVED/2)
123 			return(-1);
124 	}
125 }
126 
127 /*
128  * Getline reads a line from the control file cfp, removes tabs, converts
129  *  new-line to null and leaves it in line.
130  * Returns 0 at EOF or the number of characters read.
131  */
132 getline(cfp)
133 	FILE *cfp;
134 {
135 	register int linel = 0;
136 	register char *lp = line;
137 	register c;
138 
139 	while ((c = getc(cfp)) != '\n') {
140 		if (c == EOF)
141 			return(0);
142 		if (c == '\t') {
143 			do {
144 				*lp++ = ' ';
145 				linel++;
146 			} while ((linel & 07) != 0);
147 			continue;
148 		}
149 		*lp++ = c;
150 		linel++;
151 	}
152 	*lp++ = '\0';
153 	return(linel);
154 }
155 
156 /*
157  * Scan the current directory and make a list of daemon files sorted by
158  * creation time.
159  * Return the number of entries and a pointer to the list.
160  */
161 getq(namelist)
162 	struct queue *(*namelist[]);
163 {
164 	register struct direct *d;
165 	register struct queue *q, **queue;
166 	register int nitems;
167 	struct stat stbuf;
168 	int arraysz, compar();
169 	DIR *dirp;
170 
171 	if ((dirp = opendir(".")) == NULL)
172 		return(-1);
173 	if (fstat(dirp->dd_fd, &stbuf) < 0)
174 		return(-1);
175 
176 	/*
177 	 * Estimate the array size by taking the size of the directory file
178 	 * and dividing it by a multiple of the minimum size entry.
179 	 */
180 	arraysz = (stbuf.st_size / 24);
181 	queue = (struct queue **)malloc(arraysz * sizeof(struct queue *));
182 	if (queue == NULL)
183 		return(-1);
184 
185 	nitems = 0;
186 	while ((d = readdir(dirp)) != NULL) {
187 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
188 			continue;	/* daemon control files only */
189 		if (stat(d->d_name, &stbuf) < 0)
190 			continue;	/* Doesn't exist */
191 		q = (struct queue *)malloc(sizeof(time_t)+strlen(d->d_name)+1);
192 		if (q == NULL)
193 			return(-1);
194 		q->q_time = stbuf.st_mtime;
195 		strcpy(q->q_name, d->d_name);
196 		/*
197 		 * Check to make sure the array has space left and
198 		 * realloc the maximum size.
199 		 */
200 		if (++nitems > arraysz) {
201 			queue = (struct queue **)realloc((char *)queue,
202 				(stbuf.st_size/12) * sizeof(struct queue *));
203 			if (queue == NULL)
204 				return(-1);
205 		}
206 		queue[nitems-1] = q;
207 	}
208 	closedir(dirp);
209 	if (nitems)
210 		qsort(queue, nitems, sizeof(struct queue *), compar);
211 	*namelist = queue;
212 	return(nitems);
213 }
214 
215 /*
216  * Compare modification times.
217  */
218 static
219 compar(p1, p2)
220 	register struct queue **p1, **p2;
221 {
222 	if ((*p1)->q_time < (*p2)->q_time)
223 		return(-1);
224 	if ((*p1)->q_time > (*p2)->q_time)
225 		return(1);
226 	return(0);
227 }
228 
229 /*VARARGS1*/
230 fatal(msg, a1, a2, a3)
231 	char *msg;
232 {
233 	if (from != host)
234 		printf("%s: ", host);
235 	printf("%s: ", name);
236 	if (printer)
237 		printf("%s: ", printer);
238 	printf(msg, a1, a2, a3);
239 	putchar('\n');
240 	exit(1);
241 }
242 
243 fatalerror(msg)
244 	char *msg;
245 {
246 	extern int sys_nerr;
247 	extern char *sys_errlist[];
248 
249 	printf("%s: ", name);
250 	if (*msg)
251 		printf("%s: ", msg);
252 	fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout);
253 	putchar('\n');
254 	exit(1);
255 }
256