xref: /minix3/minix/commands/lpd/lpd.c (revision d0055759dd8892194db7fce6acc5085d5c9aeaee)
1433d6423SLionel Sambuc /*	lpd 1.6 - Printer daemon			Author: Kees J. Bot
2433d6423SLionel Sambuc  *								3 Dec 1989
3433d6423SLionel Sambuc  */
4433d6423SLionel Sambuc #define nil 0
5433d6423SLionel Sambuc #include <stdio.h>
6433d6423SLionel Sambuc #include <stdlib.h>
7433d6423SLionel Sambuc #include <limits.h>
8433d6423SLionel Sambuc #include <string.h>
9433d6423SLionel Sambuc #include <errno.h>
10433d6423SLionel Sambuc #include <sys/types.h>
11433d6423SLionel Sambuc #include <sys/stat.h>
12433d6423SLionel Sambuc #include <dirent.h>
13433d6423SLionel Sambuc #include <fcntl.h>
14433d6423SLionel Sambuc #include <unistd.h>
15433d6423SLionel Sambuc #include <termcap.h>
16433d6423SLionel Sambuc 
17433d6423SLionel Sambuc char PRINTER[] =	"/dev/lp";
18433d6423SLionel Sambuc char SPOOL[] =		"/usr/spool/lpd";
19433d6423SLionel Sambuc char LOG[] =		"/dev/log";
20433d6423SLionel Sambuc 
report(char * mess)21433d6423SLionel Sambuc void report(char *mess)
22433d6423SLionel Sambuc {
23433d6423SLionel Sambuc 	fprintf(stderr, "lpd: %s: %s\n", mess, strerror(errno));
24433d6423SLionel Sambuc }
25433d6423SLionel Sambuc 
fatal(char * mess)26433d6423SLionel Sambuc void fatal(char *mess)
27433d6423SLionel Sambuc {
28433d6423SLionel Sambuc 	report(mess);
29433d6423SLionel Sambuc 	exit(1);
30433d6423SLionel Sambuc }
31433d6423SLionel Sambuc 
32433d6423SLionel Sambuc char jobX[] = "jobXXXXXX";
33433d6423SLionel Sambuc char tmpX[] = "tmpXXXXXX";
34433d6423SLionel Sambuc 
spoolerr(char * file)35433d6423SLionel Sambuc void spoolerr(char *file)
36433d6423SLionel Sambuc {
37433d6423SLionel Sambuc 	unlink(jobX);
38433d6423SLionel Sambuc 	unlink(tmpX);
39433d6423SLionel Sambuc 	fatal(file);
40433d6423SLionel Sambuc }
41433d6423SLionel Sambuc 
spool(char * path)42433d6423SLionel Sambuc void spool(char *path)
43433d6423SLionel Sambuc /* Place a file into the spool directory, either by copying it, or by leaving
44433d6423SLionel Sambuc  * a reference.
45433d6423SLionel Sambuc  */
46433d6423SLionel Sambuc {
47433d6423SLionel Sambuc 	char *file;
48433d6423SLionel Sambuc 	int j, u;
49433d6423SLionel Sambuc 
50433d6423SLionel Sambuc 	mktemp(jobX);
51433d6423SLionel Sambuc 	file= mktemp(tmpX);
52433d6423SLionel Sambuc 
53433d6423SLionel Sambuc 	if (path[0] == '/') {
54433d6423SLionel Sambuc 		int f;
55433d6423SLionel Sambuc 
56433d6423SLionel Sambuc 		if ((f= open(path, O_RDONLY)) >= 0) {
57433d6423SLionel Sambuc 			close(f);
58433d6423SLionel Sambuc 			file= path;
59433d6423SLionel Sambuc 		}
60433d6423SLionel Sambuc 	}
61433d6423SLionel Sambuc 	if (file != path) {
62433d6423SLionel Sambuc 		int c;
63433d6423SLionel Sambuc 		FILE *t;
64433d6423SLionel Sambuc 
65433d6423SLionel Sambuc 		if ((t= fopen(tmpX, "w")) == nil) spoolerr(tmpX);
66433d6423SLionel Sambuc 
67433d6423SLionel Sambuc 		while ((c= getchar()) != EOF && putc(c, t) != EOF) {}
68433d6423SLionel Sambuc 
69433d6423SLionel Sambuc 		if (ferror(stdin)) spoolerr(path);
70433d6423SLionel Sambuc 
71433d6423SLionel Sambuc 		if (ferror(t) || fclose(t) == EOF) spoolerr(tmpX);
72433d6423SLionel Sambuc 
73433d6423SLionel Sambuc 		fclose(stdin);
74433d6423SLionel Sambuc 	}
75433d6423SLionel Sambuc 
76433d6423SLionel Sambuc 	if ((j= open(jobX, O_WRONLY|O_CREAT|O_EXCL, 0000)) < 0) spoolerr(jobX);
77433d6423SLionel Sambuc 
78433d6423SLionel Sambuc 	u= getuid();
79433d6423SLionel Sambuc 	if (write(j, file, strlen(file)+1) < 0
80433d6423SLionel Sambuc 		|| write(j, &u, sizeof(u)) < 0
81433d6423SLionel Sambuc 		|| write(j, path, strlen(path)+1) < 0
82433d6423SLionel Sambuc 		|| close(j) < 0
83433d6423SLionel Sambuc 		|| chmod(jobX, 0600) < 0
84433d6423SLionel Sambuc 	) spoolerr(jobX);
85433d6423SLionel Sambuc }
86433d6423SLionel Sambuc 
87433d6423SLionel Sambuc struct job {
88433d6423SLionel Sambuc 	struct job *next;
89433d6423SLionel Sambuc 	time_t age;
90433d6423SLionel Sambuc 	char name[sizeof(jobX)];
91433d6423SLionel Sambuc } *jobs = nil;
92433d6423SLionel Sambuc 
job(void)93433d6423SLionel Sambuc int job(void)
94433d6423SLionel Sambuc /* Look for print jobs in the spool directory.  Make a list of them sorted
95433d6423SLionel Sambuc  * by age.  Return true iff the list is nonempty.
96433d6423SLionel Sambuc  */
97433d6423SLionel Sambuc {
98433d6423SLionel Sambuc 	DIR *spool;
99433d6423SLionel Sambuc 	struct dirent *entry;
100433d6423SLionel Sambuc 	struct job *newjob, **ajob;
101433d6423SLionel Sambuc 	struct stat st;
102433d6423SLionel Sambuc 
103433d6423SLionel Sambuc 	if (jobs != nil) return 1;
104433d6423SLionel Sambuc 
105433d6423SLionel Sambuc 	if ((spool= opendir(".")) == nil) fatal(SPOOL);
106433d6423SLionel Sambuc 
107433d6423SLionel Sambuc 	while ((entry= readdir(spool)) != nil) {
108433d6423SLionel Sambuc 		if (strncmp(entry->d_name, "job", 3) != 0) continue;
109433d6423SLionel Sambuc 
110433d6423SLionel Sambuc 		if (stat(entry->d_name, &st) < 0
111433d6423SLionel Sambuc 			|| (st.st_mode & 0777) == 0000) continue;
112433d6423SLionel Sambuc 
113433d6423SLionel Sambuc 		if ((newjob= malloc(sizeof(*newjob))) == nil) fatal("malloc()");
114433d6423SLionel Sambuc 		newjob->age = st.st_mtime;
115433d6423SLionel Sambuc 		strcpy(newjob->name, entry->d_name);
116433d6423SLionel Sambuc 
117433d6423SLionel Sambuc 		ajob= &jobs;
118433d6423SLionel Sambuc 		while (*ajob != nil && (*ajob)->age < newjob->age)
119433d6423SLionel Sambuc 			ajob= &(*ajob)->next;
120433d6423SLionel Sambuc 
121433d6423SLionel Sambuc 		newjob->next= *ajob;
122433d6423SLionel Sambuc 		*ajob= newjob;
123433d6423SLionel Sambuc 	}
124433d6423SLionel Sambuc 	closedir(spool);
125433d6423SLionel Sambuc 
126433d6423SLionel Sambuc 	return jobs != nil;
127433d6423SLionel Sambuc }
128433d6423SLionel Sambuc 
129433d6423SLionel Sambuc /* What to do with control-X:
130433d6423SLionel Sambuc  * 0 ignore,
131433d6423SLionel Sambuc  * 1 give up on controlling the printer, assume user knows how printer works,
132433d6423SLionel Sambuc  * 2 print.
133433d6423SLionel Sambuc  */
134433d6423SLionel Sambuc char control[] = {
135433d6423SLionel Sambuc 	0, 1, 1, 1, 1, 1, 1, 0,		/* \0, \a  don't show.	*/
136433d6423SLionel Sambuc 	2, 2, 2, 1, 2, 2, 1, 1,		/* \b, \t, \n, \f, \r	*/
137433d6423SLionel Sambuc 	1, 1, 1, 1, 1, 1, 1, 1,
138433d6423SLionel Sambuc 	1, 1, 1, 1, 1, 1, 1, 1
139433d6423SLionel Sambuc };
140433d6423SLionel Sambuc 
141433d6423SLionel Sambuc int lp;
142433d6423SLionel Sambuc char buf[BUFSIZ];
143433d6423SLionel Sambuc int count, column, line, ncols = 80, nlines = 66;
144433d6423SLionel Sambuc 
flush(void)145*d0055759SDavid van Moolenbroek void flush(void)
146433d6423SLionel Sambuc /* Copy the characters in the output buffer to the printer, with retries if
147433d6423SLionel Sambuc  * out of paper.
148433d6423SLionel Sambuc  */
149433d6423SLionel Sambuc {
150433d6423SLionel Sambuc 	char *bp= buf;
151433d6423SLionel Sambuc 
152433d6423SLionel Sambuc 	while (count > 0) {
153433d6423SLionel Sambuc 		int retry = 0, complain = 0;
154433d6423SLionel Sambuc 		int r;
155433d6423SLionel Sambuc 
156433d6423SLionel Sambuc 		while ((r= write(lp, bp, count)) < 0) {
157433d6423SLionel Sambuc 			if (errno != EAGAIN) fatal(PRINTER);
158433d6423SLionel Sambuc 			if (retry == complain) {
159433d6423SLionel Sambuc 				fprintf(stderr,
160433d6423SLionel Sambuc 					"lpd: %s: Printer out of paper\n",
161433d6423SLionel Sambuc 					PRINTER);
162433d6423SLionel Sambuc 				complain= retry + 60;
163433d6423SLionel Sambuc 			}
164433d6423SLionel Sambuc 			sleep(1);
165433d6423SLionel Sambuc 			retry++;
166433d6423SLionel Sambuc 		}
167433d6423SLionel Sambuc 		bp+= r;
168433d6423SLionel Sambuc 		count-= r;
169433d6423SLionel Sambuc 	}
170433d6423SLionel Sambuc 	count = 0;
171433d6423SLionel Sambuc }
172433d6423SLionel Sambuc 
put(int c)173433d6423SLionel Sambuc void put(int c)
174433d6423SLionel Sambuc /* Send characters to the output buffer to be printed and do so if the buffer
175433d6423SLionel Sambuc  * is full.  Track the position of the write-head in `column' and `line'.
176433d6423SLionel Sambuc  */
177433d6423SLionel Sambuc {
178433d6423SLionel Sambuc 	buf[count++] = c;
179433d6423SLionel Sambuc 
180433d6423SLionel Sambuc 	switch (c) {
181433d6423SLionel Sambuc 	case '\f':
182433d6423SLionel Sambuc 		column = 0;
183433d6423SLionel Sambuc 		line = 0;
184433d6423SLionel Sambuc 		break;
185433d6423SLionel Sambuc 	case '\r':
186433d6423SLionel Sambuc 		column = 0;
187433d6423SLionel Sambuc 		break;
188433d6423SLionel Sambuc 	case '\n':
189433d6423SLionel Sambuc 		line++;
190433d6423SLionel Sambuc 		break;
191433d6423SLionel Sambuc 	case '\b':
192433d6423SLionel Sambuc 		column--;
193433d6423SLionel Sambuc 		break;
194433d6423SLionel Sambuc 	default:
195433d6423SLionel Sambuc 		if (++column > ncols) { line++; column= 1; }
196433d6423SLionel Sambuc 	}
197433d6423SLionel Sambuc 	if (line == nlines) line= 0;
198433d6423SLionel Sambuc 
199433d6423SLionel Sambuc 	if (count == BUFSIZ) flush();
200433d6423SLionel Sambuc }
201433d6423SLionel Sambuc 
print(FILE * f)202433d6423SLionel Sambuc void print(FILE *f)
203433d6423SLionel Sambuc /* Send the contents of an open file to the printer.  Expand tabs and change
204433d6423SLionel Sambuc  * linefeed to a carriage-return linefeed sequence.  Print a formfeed at the
205433d6423SLionel Sambuc  * end if needed to reach the top of the next page.  If a control character
206433d6423SLionel Sambuc  * is printed that we do not know about, then the user is assumed to know
207433d6423SLionel Sambuc  * what they are doing, so output processing is disabled.
208433d6423SLionel Sambuc  */
209433d6423SLionel Sambuc {
210433d6423SLionel Sambuc 	int c;
211433d6423SLionel Sambuc 
212433d6423SLionel Sambuc 	count= column= line= 0;
213433d6423SLionel Sambuc 
214433d6423SLionel Sambuc 	while ((c= getc(f)) != EOF) {
215433d6423SLionel Sambuc 		if (c < ' ') {
216433d6423SLionel Sambuc 			switch (control[c]) {
217433d6423SLionel Sambuc 			case 0:	continue;	/* Ignore this one. */
218433d6423SLionel Sambuc 			case 1:
219433d6423SLionel Sambuc 				/* Can't handle this junk, assume smart user. */
220433d6423SLionel Sambuc 				do {
221433d6423SLionel Sambuc 					buf[count++] = c;
222433d6423SLionel Sambuc 					if (count == BUFSIZ) flush();
223433d6423SLionel Sambuc 				} while ((c= getc(f)) != EOF);
224433d6423SLionel Sambuc 
225433d6423SLionel Sambuc 				flush();
226433d6423SLionel Sambuc 				return;
227433d6423SLionel Sambuc 			case 2: /* fine */;
228433d6423SLionel Sambuc 			}
229433d6423SLionel Sambuc 		}
230433d6423SLionel Sambuc 
231433d6423SLionel Sambuc 		switch (c) {
232433d6423SLionel Sambuc 		case '\n':
233433d6423SLionel Sambuc 			put('\r');
234433d6423SLionel Sambuc 			put('\n');
235433d6423SLionel Sambuc 			break;
236433d6423SLionel Sambuc 		case '\t':
237433d6423SLionel Sambuc 			do {
238433d6423SLionel Sambuc 				put(' ');
239433d6423SLionel Sambuc 			} while (column & 07);
240433d6423SLionel Sambuc 			break;
241433d6423SLionel Sambuc 		case '\b':
242433d6423SLionel Sambuc 			if (column > 0) put(c);
243433d6423SLionel Sambuc 			break;
244433d6423SLionel Sambuc 		default:
245433d6423SLionel Sambuc 			put(c);
246433d6423SLionel Sambuc 		}
247433d6423SLionel Sambuc 	}
248433d6423SLionel Sambuc 	if (column > 0) { put('\r'); put('\n'); }
249433d6423SLionel Sambuc 	if (line > 0) put('\f');
250433d6423SLionel Sambuc 	flush();
251433d6423SLionel Sambuc 	return;
252433d6423SLionel Sambuc }
253433d6423SLionel Sambuc 
joberr(char * job)254433d6423SLionel Sambuc void joberr(char *job)
255433d6423SLionel Sambuc {
256433d6423SLionel Sambuc 	fprintf(stderr, "lpd: something is wrong with %s\n", job);
257433d6423SLionel Sambuc 
258433d6423SLionel Sambuc 	if (unlink(job) < 0) fatal("can't remove it");
259433d6423SLionel Sambuc }
260433d6423SLionel Sambuc 
work(void)261433d6423SLionel Sambuc void work(void)
262433d6423SLionel Sambuc /* Print all the jobs in the job list. */
263433d6423SLionel Sambuc {
264433d6423SLionel Sambuc 	FILE *j, *f;
265433d6423SLionel Sambuc 	char file[PATH_MAX+1], *pf=file;
266433d6423SLionel Sambuc 	int c;
267433d6423SLionel Sambuc 	struct job *job;
268433d6423SLionel Sambuc 
269433d6423SLionel Sambuc 	job= jobs;
270433d6423SLionel Sambuc 	jobs= jobs->next;
271433d6423SLionel Sambuc 
272433d6423SLionel Sambuc 	if ((j= fopen(job->name, "r")) == nil) {
273433d6423SLionel Sambuc 		joberr(job->name);
274433d6423SLionel Sambuc 		return;
275433d6423SLionel Sambuc 	}
276433d6423SLionel Sambuc 
277433d6423SLionel Sambuc 	do {
278433d6423SLionel Sambuc 		if (pf == file + sizeof(file) || (c= getc(j)) == EOF) {
279433d6423SLionel Sambuc 			fclose(j);
280433d6423SLionel Sambuc 			joberr(job->name);
281433d6423SLionel Sambuc 			return;
282433d6423SLionel Sambuc 		}
283433d6423SLionel Sambuc 		*pf++ = c;
284433d6423SLionel Sambuc 	} while (c != 0);
285433d6423SLionel Sambuc 
286433d6423SLionel Sambuc 	fclose(j);
287433d6423SLionel Sambuc 
288433d6423SLionel Sambuc 	if ((f= fopen(file, "r")) == nil)
289433d6423SLionel Sambuc 		fprintf(stderr, "lpd: can't read %s\n", file);
290433d6423SLionel Sambuc 	else {
291433d6423SLionel Sambuc 		print(f);
292433d6423SLionel Sambuc 		fclose(f);
293433d6423SLionel Sambuc 	}
294433d6423SLionel Sambuc 	if (file[0] != '/' && unlink(file) < 0) report(file);
295433d6423SLionel Sambuc 
296433d6423SLionel Sambuc 	if (unlink(job->name) < 0) fatal(job->name);
297433d6423SLionel Sambuc 	free(job);
298433d6423SLionel Sambuc }
299433d6423SLionel Sambuc 
getcap(void)300433d6423SLionel Sambuc void getcap(void)
301433d6423SLionel Sambuc /* Find the line printer dimensions in the termcap database under "lp". */
302433d6423SLionel Sambuc {
303433d6423SLionel Sambuc 	char printcap[1024];
304433d6423SLionel Sambuc 	int n;
305433d6423SLionel Sambuc 
306433d6423SLionel Sambuc 	if (tgetent(printcap, "lp") == 1) {
307433d6423SLionel Sambuc 		if ((n= tgetnum("co")) > 0) ncols= n;
308433d6423SLionel Sambuc 		if ((n= tgetnum("li")) > 0) nlines= n;
309433d6423SLionel Sambuc 	}
310433d6423SLionel Sambuc }
311433d6423SLionel Sambuc 
haunt(void)312433d6423SLionel Sambuc void haunt(void)
313433d6423SLionel Sambuc /* Become a daemon, print jobs while there are any, exit. */
314433d6423SLionel Sambuc {
315433d6423SLionel Sambuc 	int fd;
316433d6423SLionel Sambuc 
317433d6423SLionel Sambuc 	if ((fd= open("/dev/tty", O_RDONLY)) != -1) {
318433d6423SLionel Sambuc 		/* We have a controlling tty!  Disconnect. */
319433d6423SLionel Sambuc 		close(fd);
320433d6423SLionel Sambuc 
321433d6423SLionel Sambuc 		switch(fork()) {
322433d6423SLionel Sambuc 		case -1:	fatal("can't fork");
323433d6423SLionel Sambuc 		case  0:	break;
324433d6423SLionel Sambuc 		default:	exit(0);
325433d6423SLionel Sambuc 		}
326433d6423SLionel Sambuc 
327433d6423SLionel Sambuc 		if ((fd= open("/dev/null", O_RDONLY)) < 0) fatal("/dev/null");
328433d6423SLionel Sambuc 		dup2(fd, 0);
329433d6423SLionel Sambuc 		close(fd);
330433d6423SLionel Sambuc 		if ((fd= open(LOG, O_WRONLY)) < 0) fatal(LOG);
331433d6423SLionel Sambuc 		dup2(fd, 1);
332433d6423SLionel Sambuc 		dup2(fd, 2);
333433d6423SLionel Sambuc 		close(fd);
334433d6423SLionel Sambuc 		setsid();
335433d6423SLionel Sambuc 	}
336433d6423SLionel Sambuc 
337433d6423SLionel Sambuc 	getcap();
338433d6423SLionel Sambuc 
339433d6423SLionel Sambuc 	do {
340433d6423SLionel Sambuc 		if ((lp= open(PRINTER, O_WRONLY)) < 0) {
341433d6423SLionel Sambuc 			/* Another lpd? */
342433d6423SLionel Sambuc 			if (errno == EBUSY) exit(0);
343433d6423SLionel Sambuc 			fatal(PRINTER);
344433d6423SLionel Sambuc 		}
345433d6423SLionel Sambuc 
346433d6423SLionel Sambuc 		while (job()) work();
347433d6423SLionel Sambuc 
348433d6423SLionel Sambuc 		close(lp);
349433d6423SLionel Sambuc 	} while (job());
350433d6423SLionel Sambuc }
351433d6423SLionel Sambuc 
main(int argc,char ** argv)352433d6423SLionel Sambuc int main(int argc, char **argv)
353433d6423SLionel Sambuc {
354433d6423SLionel Sambuc 	if (argc > 2) {
355433d6423SLionel Sambuc 		fprintf(stderr, "Usage: %s [path | stdin < path]\n", argv[0]);
356433d6423SLionel Sambuc 		exit(1);
357433d6423SLionel Sambuc 	}
358433d6423SLionel Sambuc 
359433d6423SLionel Sambuc 	umask(0077);
360433d6423SLionel Sambuc 
361433d6423SLionel Sambuc 	if (chdir(SPOOL) < 0) fatal(SPOOL);
362433d6423SLionel Sambuc 
363433d6423SLionel Sambuc 	if (argc == 2) spool(argv[1]);
364433d6423SLionel Sambuc 
365433d6423SLionel Sambuc 	haunt();
366433d6423SLionel Sambuc }
367