xref: /csrg-svn/usr.sbin/lpr/lpc/cmds.c (revision 15719)
112382Sralph #ifndef lint
2*15719Sralph static char sccsid[] = "@(#)cmds.c	4.9 (Berkeley) 12/16/83";
312382Sralph #endif
412382Sralph 
512382Sralph /*
6*15719Sralph  * lpc -- line printer control program -- commands:
712382Sralph  */
812382Sralph 
912382Sralph #include "lp.h"
10*15719Sralph #include <sys/time.h>
1112382Sralph 
1212382Sralph /*
1312382Sralph  * kill an existing daemon and disable printing.
1412382Sralph  */
1512382Sralph abort(argc, argv)
1612382Sralph 	char *argv[];
1712382Sralph {
1812382Sralph 	register int c, status;
1912382Sralph 	register char *cp1, *cp2;
2012382Sralph 	char prbuf[100];
2112382Sralph 
2212382Sralph 	if (argc == 1) {
2312382Sralph 		printf("Usage: abort {all | printer ...}\n");
2412382Sralph 		return;
2512382Sralph 	}
2612382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
2712382Sralph 		printer = prbuf;
2812382Sralph 		while (getprent(line) > 0) {
2912382Sralph 			cp1 = prbuf;
3012382Sralph 			cp2 = line;
3112382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
3212382Sralph 				*cp1++ = c;
3312382Sralph 			*cp1 = '\0';
3412382Sralph 			abortpr();
3512382Sralph 		}
3612382Sralph 		return;
3712382Sralph 	}
3812382Sralph 	while (--argc) {
3912382Sralph 		printer = *++argv;
4012382Sralph 		if ((status = pgetent(line, printer)) < 0) {
4112514Sralph 			printf("cannot open printer description file\n");
4212382Sralph 			continue;
4312382Sralph 		} else if (status == 0) {
4412514Sralph 			printf("unknown printer %s\n", printer);
4512382Sralph 			continue;
4612382Sralph 		}
4712382Sralph 		abortpr();
4812382Sralph 	}
4912382Sralph }
5012382Sralph 
5112382Sralph abortpr()
5212382Sralph {
5312382Sralph 	register FILE *fp;
5412382Sralph 	struct stat stbuf;
5512382Sralph 	int pid, fd;
5612382Sralph 
5712382Sralph 	bp = pbuf;
5812382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
5912382Sralph 		SD = DEFSPOOL;
6012382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
6112382Sralph 		LO = DEFLOCK;
6212382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
6312382Sralph 	printf("%s:\n", printer);
6412382Sralph 
6512382Sralph 	/*
6612382Sralph 	 * Turn on the owner execute bit of the lock file to disable printing.
6712382Sralph 	 */
6812382Sralph 	if (stat(line, &stbuf) >= 0) {
6912382Sralph 		if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
7012382Sralph 			printf("\tcannot disable printing\n");
7112382Sralph 		else
7212382Sralph 			printf("\tprinting disabled\n");
7312382Sralph 	} else if (errno == ENOENT) {
7413146Ssam 		if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
7512514Sralph 			printf("\tcannot create lock file\n");
7612382Sralph 		else {
7712382Sralph 			(void) close(fd);
7812382Sralph 			printf("\tprinting disabled\n");
7912382Sralph 			printf("\tno daemon to abort\n");
8012382Sralph 		}
8112382Sralph 		return;
8212382Sralph 	} else {
8312382Sralph 		printf("\tcannot stat lock file\n");
8412382Sralph 		return;
8512382Sralph 	}
8612382Sralph 	/*
8712382Sralph 	 * Kill the current daemon to stop printing now.
8812382Sralph 	 */
8912382Sralph 	if ((fp = fopen(line, "r")) == NULL) {
9012382Sralph 		printf("\tcannot open lock file\n");
9112382Sralph 		return;
9212382Sralph 	}
9313146Ssam 	if (!getline(fp) || flock(fileno(fp), LOCK_SH|LOCK_NB) == 0) {
9413168Sralph 		(void) fclose(fp);	/* unlocks as well */
9512382Sralph 		printf("\tno daemon to abort\n");
9612382Sralph 		return;
9712382Sralph 	}
9812382Sralph 	(void) fclose(fp);
9912382Sralph 	if (kill(pid = atoi(line), SIGINT) < 0)
10012382Sralph 		printf("\tWarning: daemon (pid %d) not killed\n", pid);
10112382Sralph 	else
10212382Sralph 		printf("\tdaemon (pid %d) killed\n", pid);
10312382Sralph }
10412382Sralph 
10512382Sralph /*
10612382Sralph  * Remove all spool files and temporaries from the spooling area.
10712382Sralph  */
10812382Sralph clean(argc, argv)
10912382Sralph 	char *argv[];
11012382Sralph {
11112382Sralph 	register int c, status;
11212382Sralph 	register char *cp1, *cp2;
11312382Sralph 	char prbuf[100];
11412382Sralph 
11512382Sralph 	if (argc == 1) {
11612382Sralph 		printf("Usage: clean {all | printer ...}\n");
11712382Sralph 		return;
11812382Sralph 	}
11912382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
12012382Sralph 		printer = prbuf;
12112382Sralph 		while (getprent(line) > 0) {
12212382Sralph 			cp1 = prbuf;
12312382Sralph 			cp2 = line;
12412382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
12512382Sralph 				*cp1++ = c;
12612382Sralph 			*cp1 = '\0';
12712382Sralph 			cleanpr();
12812382Sralph 		}
12912382Sralph 		return;
13012382Sralph 	}
13112382Sralph 	while (--argc) {
13212382Sralph 		printer = *++argv;
13312382Sralph 		if ((status = pgetent(line, printer)) < 0) {
13412514Sralph 			printf("cannot open printer description file\n");
13512382Sralph 			continue;
13612382Sralph 		} else if (status == 0) {
13712514Sralph 			printf("unknown printer %s\n", printer);
13812382Sralph 			continue;
13912382Sralph 		}
14012382Sralph 		cleanpr();
14112382Sralph 	}
14212382Sralph }
14312382Sralph 
144*15719Sralph select(d)
145*15719Sralph struct direct *d;
146*15719Sralph {
147*15719Sralph 	int c = d->d_name[0];
148*15719Sralph 
149*15719Sralph 	if ((c == 't' || c == 'c' || c == 'd') && d->d_name[1] == 'f')
150*15719Sralph 		return(1);
151*15719Sralph 	return(0);
152*15719Sralph }
153*15719Sralph 
154*15719Sralph /*
155*15719Sralph  * Comparison routine for scandir. Sort by job number and machine, then
156*15719Sralph  * by `cf', `tf', or `df', then by the sequence letter A-Z, a-z.
157*15719Sralph  */
158*15719Sralph sortq(d1, d2)
159*15719Sralph struct direct **d1, **d2;
160*15719Sralph {
161*15719Sralph 	int c1, c2;
162*15719Sralph 
163*15719Sralph 	if (c1 = strcmp((*d1)->d_name + 3, (*d2)->d_name + 3))
164*15719Sralph 		return(c1);
165*15719Sralph 	c1 = (*d1)->d_name[0];
166*15719Sralph 	c2 = (*d2)->d_name[0];
167*15719Sralph 	if (c1 == c2)
168*15719Sralph 		return((*d1)->d_name[2] - (*d2)->d_name[2]);
169*15719Sralph 	if (c1 == 'c')
170*15719Sralph 		return(-1);
171*15719Sralph 	if (c1 == 'd' || c2 == 'c')
172*15719Sralph 		return(1);
173*15719Sralph 	return(-1);
174*15719Sralph }
175*15719Sralph 
176*15719Sralph /*
177*15719Sralph  * Remove incomplete jobs from spooling area.
178*15719Sralph  */
17912382Sralph cleanpr()
18012382Sralph {
181*15719Sralph 	register int i, n;
182*15719Sralph 	register char *cp, *cp1, *lp;
183*15719Sralph 	struct direct **queue;
184*15719Sralph 	int nitems;
18512382Sralph 
18612382Sralph 	bp = pbuf;
18712382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
18812382Sralph 		SD = DEFSPOOL;
18912382Sralph 	printf("%s:\n", printer);
19012382Sralph 
191*15719Sralph 	for (lp = line, cp = SD; *lp++ = *cp++; )
192*15719Sralph 		;
193*15719Sralph 	lp[-1] = '/';
194*15719Sralph 
195*15719Sralph 	nitems = scandir(SD, &queue, select, sortq);
196*15719Sralph 	if (nitems < 0) {
19712382Sralph 		printf("\tcannot examine spool directory\n");
19812382Sralph 		return;
19912382Sralph 	}
200*15719Sralph 	if (nitems == 0)
201*15719Sralph 		return;
202*15719Sralph 	i = 0;
203*15719Sralph 	do {
204*15719Sralph 		cp = queue[i]->d_name;
205*15719Sralph 		if (*cp == 'c') {
206*15719Sralph 			n = 0;
207*15719Sralph 			while (i + 1 < nitems) {
208*15719Sralph 				cp1 = queue[i + 1]->d_name;
209*15719Sralph 				if (*cp1 != 'd' || strcmp(cp + 3, cp1 + 3))
210*15719Sralph 					break;
211*15719Sralph 				i++;
212*15719Sralph 				n++;
213*15719Sralph 			}
214*15719Sralph 			if (n == 0) {
215*15719Sralph 				strcpy(lp, cp);
216*15719Sralph 				unlinkf(line);
217*15719Sralph 			}
218*15719Sralph 		} else {
219*15719Sralph 			/*
220*15719Sralph 			 * Must be a df with no cf (otherwise, it would have
221*15719Sralph 			 * been skipped above) or a tf file (which can always
222*15719Sralph 			 * be removed).
223*15719Sralph 			 */
224*15719Sralph 			strcpy(lp, cp);
225*15719Sralph 			unlinkf(line);
22612382Sralph 		}
227*15719Sralph      	} while (++i < nitems);
22812382Sralph }
229*15719Sralph 
230*15719Sralph unlinkf(name)
231*15719Sralph 	char	*name;
232*15719Sralph {
233*15719Sralph 	if (unlink(name) < 0)
234*15719Sralph 		printf("\tcannot remove %s\n", name);
235*15719Sralph 	else
236*15719Sralph 		printf("\tremoved %s\n", name);
237*15719Sralph }
23812382Sralph 
23912382Sralph /*
24012382Sralph  * Enable queuing to the printer (allow lpr's).
24112382Sralph  */
24212382Sralph enable(argc, argv)
24312382Sralph 	char *argv[];
24412382Sralph {
24512382Sralph 	register int c, status;
24612382Sralph 	register char *cp1, *cp2;
24712382Sralph 	char prbuf[100];
24812382Sralph 
24912382Sralph 	if (argc == 1) {
25012382Sralph 		printf("Usage: enable {all | printer ...}\n");
25112382Sralph 		return;
25212382Sralph 	}
25312382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
25412382Sralph 		printer = prbuf;
25512382Sralph 		while (getprent(line) > 0) {
25612382Sralph 			cp1 = prbuf;
25712382Sralph 			cp2 = line;
25812382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
25912382Sralph 				*cp1++ = c;
26012382Sralph 			*cp1 = '\0';
26112382Sralph 			enablepr();
26212382Sralph 		}
26312382Sralph 		return;
26412382Sralph 	}
26512382Sralph 	while (--argc) {
26612382Sralph 		printer = *++argv;
26712382Sralph 		if ((status = pgetent(line, printer)) < 0) {
26812514Sralph 			printf("cannot open printer description file\n");
26912382Sralph 			continue;
27012382Sralph 		} else if (status == 0) {
27112514Sralph 			printf("unknown printer %s\n", printer);
27212382Sralph 			continue;
27312382Sralph 		}
27412382Sralph 		enablepr();
27512382Sralph 	}
27612382Sralph }
27712382Sralph 
27812382Sralph enablepr()
27912382Sralph {
28012382Sralph 	struct stat stbuf;
28112382Sralph 
28212382Sralph 	bp = pbuf;
28312382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
28412382Sralph 		SD = DEFSPOOL;
28512382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
28612382Sralph 		LO = DEFLOCK;
28712382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
28812382Sralph 	printf("%s:\n", printer);
28912382Sralph 
29012382Sralph 	/*
29112382Sralph 	 * Turn off the group execute bit of the lock file to enable queuing.
29212382Sralph 	 */
29312382Sralph 	if (stat(line, &stbuf) >= 0) {
29412382Sralph 		if (chmod(line, stbuf.st_mode & 0767) < 0)
29512514Sralph 			printf("\tcannot enable queuing\n");
29612382Sralph 		else
29712382Sralph 			printf("\tqueuing enabled\n");
29812382Sralph 	}
29912382Sralph }
30012382Sralph 
30112382Sralph /*
30212382Sralph  * Disable queuing.
30312382Sralph  */
30412382Sralph disable(argc, argv)
30512382Sralph 	char *argv[];
30612382Sralph {
30712382Sralph 	register int c, status;
30812382Sralph 	register char *cp1, *cp2;
30912382Sralph 	char prbuf[100];
31012382Sralph 
31112382Sralph 	if (argc == 1) {
31212382Sralph 		printf("Usage: disable {all | printer ...}\n");
31312382Sralph 		return;
31412382Sralph 	}
31512382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
31612382Sralph 		printer = prbuf;
31712382Sralph 		while (getprent(line) > 0) {
31812382Sralph 			cp1 = prbuf;
31912382Sralph 			cp2 = line;
32012382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
32112382Sralph 				*cp1++ = c;
32212382Sralph 			*cp1 = '\0';
32312382Sralph 			disablepr();
32412382Sralph 		}
32512382Sralph 		return;
32612382Sralph 	}
32712382Sralph 	while (--argc) {
32812382Sralph 		printer = *++argv;
32912382Sralph 		if ((status = pgetent(line, printer)) < 0) {
33012514Sralph 			printf("cannot open printer description file\n");
33112382Sralph 			continue;
33212382Sralph 		} else if (status == 0) {
33312514Sralph 			printf("unknown printer %s\n", printer);
33412382Sralph 			continue;
33512382Sralph 		}
33612382Sralph 		disablepr();
33712382Sralph 	}
33812382Sralph }
33912382Sralph 
34012382Sralph disablepr()
34112382Sralph {
34212382Sralph 	register int fd;
34312382Sralph 	struct stat stbuf;
34412382Sralph 
34512382Sralph 	bp = pbuf;
34612382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
34712382Sralph 		SD = DEFSPOOL;
34812382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
34912382Sralph 		LO = DEFLOCK;
35012382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
35112382Sralph 	printf("%s:\n", printer);
35212382Sralph 	/*
35312382Sralph 	 * Turn on the group execute bit of the lock file to disable queuing.
35412382Sralph 	 */
35512382Sralph 	if (stat(line, &stbuf) >= 0) {
35612382Sralph 		if (chmod(line, (stbuf.st_mode & 0777) | 010) < 0)
35712382Sralph 			printf("\tcannot disable queuing\n");
35812382Sralph 		else
35912382Sralph 			printf("\tqueuing disabled\n");
36012382Sralph 	} else if (errno == ENOENT) {
36113146Ssam 		if ((fd = open(line, O_WRONLY|O_CREAT, 0670)) < 0)
36212382Sralph 			printf("\tcannot create lock file\n");
36312382Sralph 		else {
36412382Sralph 			(void) close(fd);
36512382Sralph 			printf("\tqueuing disabled\n");
36612382Sralph 		}
36712382Sralph 		return;
36812382Sralph 	} else
36912382Sralph 		printf("\tcannot stat lock file\n");
37012382Sralph }
37112382Sralph 
37212382Sralph /*
37312382Sralph  * Exit lpc
37412382Sralph  */
37512382Sralph quit(argc, argv)
37612382Sralph 	char *argv[];
37712382Sralph {
37812382Sralph 	exit(0);
37912382Sralph }
38012382Sralph 
38112382Sralph /*
38212382Sralph  * Startup the daemon.
38312382Sralph  */
38412382Sralph restart(argc, argv)
38512382Sralph 	char *argv[];
38612382Sralph {
38712382Sralph 	register int c, status;
38812382Sralph 	register char *cp1, *cp2;
38912382Sralph 	char prbuf[100];
39012382Sralph 
39112382Sralph 	if (argc == 1) {
39212382Sralph 		printf("Usage: restart {all | printer ...}\n");
39312382Sralph 		return;
39412382Sralph 	}
39512382Sralph 	gethostname(host, sizeof(host));
39612382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
39712382Sralph 		printer = prbuf;
39812382Sralph 		while (getprent(line) > 0) {
39912382Sralph 			cp1 = prbuf;
40012382Sralph 			cp2 = line;
40112382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
40212382Sralph 				*cp1++ = c;
40312382Sralph 			*cp1 = '\0';
40412382Sralph 			startpr(0);
40512382Sralph 		}
40612382Sralph 		return;
40712382Sralph 	}
40812382Sralph 	while (--argc) {
40912382Sralph 		printer = *++argv;
41012382Sralph 		if ((status = pgetent(line, printer)) < 0) {
41112514Sralph 			printf("cannot open printer description file\n");
41212382Sralph 			continue;
41312382Sralph 		} else if (status == 0) {
41412514Sralph 			printf("unknown printer %s\n", printer);
41512382Sralph 			continue;
41612382Sralph 		}
41712382Sralph 		startpr(0);
41812382Sralph 	}
41912382Sralph }
42012382Sralph 
42112382Sralph /*
42212382Sralph  * Enable printing on the specified printer and startup the daemon.
42312382Sralph  */
42412382Sralph start(argc, argv)
42512382Sralph 	char *argv[];
42612382Sralph {
42712382Sralph 	register int c, status;
42812382Sralph 	register char *cp1, *cp2;
42912382Sralph 	char prbuf[100];
43012382Sralph 
43112382Sralph 	if (argc == 1) {
43212382Sralph 		printf("Usage: start {all | printer ...}\n");
43312382Sralph 		return;
43412382Sralph 	}
43512382Sralph 	gethostname(host, sizeof(host));
43612382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
43712382Sralph 		printer = prbuf;
43812382Sralph 		while (getprent(line) > 0) {
43912382Sralph 			cp1 = prbuf;
44012382Sralph 			cp2 = line;
44112382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
44212382Sralph 				*cp1++ = c;
44312382Sralph 			*cp1 = '\0';
44412382Sralph 			startpr(1);
44512382Sralph 		}
44612382Sralph 		return;
44712382Sralph 	}
44812382Sralph 	while (--argc) {
44912382Sralph 		printer = *++argv;
45012382Sralph 		if ((status = pgetent(line, printer)) < 0) {
45112514Sralph 			printf("cannot open printer description file\n");
45212382Sralph 			continue;
45312382Sralph 		} else if (status == 0) {
45412514Sralph 			printf("unknown printer %s\n", printer);
45512382Sralph 			continue;
45612382Sralph 		}
45712382Sralph 		startpr(1);
45812382Sralph 	}
45912382Sralph }
46012382Sralph 
46112382Sralph startpr(enable)
46212382Sralph {
46312382Sralph 	struct stat stbuf;
46412382Sralph 
46512382Sralph 	bp = pbuf;
46612382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
46712382Sralph 		SD = DEFSPOOL;
46812382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
46912382Sralph 		LO = DEFLOCK;
47012382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
47112382Sralph 	printf("%s:\n", printer);
47212382Sralph 
47312382Sralph 	/*
47412382Sralph 	 * Turn off the owner execute bit of the lock file to enable printing.
47512382Sralph 	 */
47612382Sralph 	if (enable && stat(line, &stbuf) >= 0) {
47712382Sralph 		if (chmod(line, stbuf.st_mode & 0677) < 0)
47812382Sralph 			printf("\tcannot enable printing\n");
47912382Sralph 		else
48012382Sralph 			printf("\tprinting enabled\n");
48112382Sralph 	}
48213727Sroot 	if (!startdaemon(printer))
48312382Sralph 		printf("\tcouldn't start daemon\n");
48412382Sralph 	else
48512382Sralph 		printf("\tdaemon started\n");
48612382Sralph }
48712382Sralph 
48812382Sralph /*
48912382Sralph  * Print the status of each queue listed or all the queues.
49012382Sralph  */
49112382Sralph status(argc, argv)
49212382Sralph 	char *argv[];
49312382Sralph {
49412382Sralph 	register int c, status;
49512382Sralph 	register char *cp1, *cp2;
49612382Sralph 	char prbuf[100];
49712382Sralph 
49812382Sralph 	if (argc == 1) {
49912382Sralph 		printer = prbuf;
50012382Sralph 		while (getprent(line) > 0) {
50112382Sralph 			cp1 = prbuf;
50212382Sralph 			cp2 = line;
50312382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
50412382Sralph 				*cp1++ = c;
50512382Sralph 			*cp1 = '\0';
50612382Sralph 			prstat();
50712382Sralph 		}
50812382Sralph 		return;
50912382Sralph 	}
51012382Sralph 	while (--argc) {
51112382Sralph 		printer = *++argv;
51212382Sralph 		if ((status = pgetent(line, printer)) < 0) {
51312514Sralph 			printf("cannot open printer description file\n");
51412382Sralph 			continue;
51512382Sralph 		} else if (status == 0) {
51612514Sralph 			printf("unknown printer %s\n", printer);
51712382Sralph 			continue;
51812382Sralph 		}
51912382Sralph 		prstat();
52012382Sralph 	}
52112382Sralph }
52212382Sralph 
52312382Sralph /*
52412382Sralph  * Print the status of the printer queue.
52512382Sralph  */
52612382Sralph prstat()
52712382Sralph {
52812382Sralph 	struct stat stbuf;
52912382Sralph 	register int fd, i;
53012382Sralph 	register struct direct *dp;
53112382Sralph 	DIR *dirp;
53212382Sralph 
53312382Sralph 	bp = pbuf;
53412382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
53512382Sralph 		SD = DEFSPOOL;
53612382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
53712382Sralph 		LO = DEFLOCK;
53812382Sralph 	if ((ST = pgetstr("st", &bp)) == NULL)
53912382Sralph 		ST = DEFSTAT;
54012382Sralph 	printf("%s:\n", printer);
54112382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
54212382Sralph 	if (stat(line, &stbuf) >= 0) {
54312382Sralph 		printf("\tqueuing is %s\n",
54412382Sralph 			(stbuf.st_mode & 010) ? "disabled" : "enabled");
54512382Sralph 		printf("\tprinting is %s\n",
54612382Sralph 			(stbuf.st_mode & 0100) ? "disabled" : "enabled");
54712382Sralph 	} else {
54812382Sralph 		printf("\tqueuing is enabled\n");
54912382Sralph 		printf("\tprinting is enabled\n");
55012382Sralph 	}
55112382Sralph 	if ((dirp = opendir(SD)) == NULL) {
55212382Sralph 		printf("\tcannot examine spool directory\n");
55312382Sralph 		return;
55412382Sralph 	}
55512382Sralph 	i = 0;
55612382Sralph 	while ((dp = readdir(dirp)) != NULL) {
55712382Sralph 		if (*dp->d_name == 'c' && dp->d_name[1] == 'f')
55812382Sralph 			i++;
55912382Sralph 	}
56012382Sralph 	closedir(dirp);
56112382Sralph 	if (i == 0)
56212382Sralph 		printf("\tno entries\n");
56312382Sralph 	else if (i == 1)
56412382Sralph 		printf("\t1 entry in spool area\n");
56512382Sralph 	else
56612382Sralph 		printf("\t%d entries in spool area\n", i);
56713146Ssam 	fd = open(line, O_RDONLY);
56813146Ssam 	if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
56913168Sralph 		(void) close(fd);	/* unlocks as well */
57012382Sralph 		printf("\tno daemon present\n");
57112382Sralph 		return;
57212382Sralph 	}
57312382Sralph 	(void) close(fd);
57412382Sralph 	putchar('\t');
57512382Sralph 	(void) sprintf(line, "%s/%s", SD, ST);
57613146Ssam 	fd = open(line, O_RDONLY);
57713146Ssam 	if (fd >= 0) {
57813146Ssam 		(void) flock(fd, LOCK_SH);
57912382Sralph 		while ((i = read(fd, line, sizeof(line))) > 0)
58012382Sralph 			(void) fwrite(line, 1, i, stdout);
58113168Sralph 		(void) close(fd);	/* unlocks as well */
58212382Sralph 	}
58312382Sralph }
58412382Sralph 
58512382Sralph /*
58612382Sralph  * Stop the specified daemon after completing the current job and disable
58712382Sralph  * printing.
58812382Sralph  */
58912382Sralph stop(argc, argv)
59012382Sralph 	char *argv[];
59112382Sralph {
59212382Sralph 	register int c, status;
59312382Sralph 	register char *cp1, *cp2;
59412382Sralph 	char prbuf[100];
59512382Sralph 
59612382Sralph 	if (argc == 1) {
59712382Sralph 		printf("Usage: stop {all | printer ...}\n");
59812382Sralph 		return;
59912382Sralph 	}
60012382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
60112382Sralph 		printer = prbuf;
60212382Sralph 		while (getprent(line) > 0) {
60312382Sralph 			cp1 = prbuf;
60412382Sralph 			cp2 = line;
60512382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
60612382Sralph 				*cp1++ = c;
60712382Sralph 			*cp1 = '\0';
60812382Sralph 			stoppr();
60912382Sralph 		}
61012382Sralph 		return;
61112382Sralph 	}
61212382Sralph 	while (--argc) {
61312382Sralph 		printer = *++argv;
61412382Sralph 		if ((status = pgetent(line, printer)) < 0) {
61512514Sralph 			printf("cannot open printer description file\n");
61612382Sralph 			continue;
61712382Sralph 		} else if (status == 0) {
61812514Sralph 			printf("unknown printer %s\n", printer);
61912382Sralph 			continue;
62012382Sralph 		}
62112382Sralph 		stoppr();
62212382Sralph 	}
62312382Sralph }
62412382Sralph 
62512382Sralph stoppr()
62612382Sralph {
62712382Sralph 	register int fd;
62812382Sralph 	struct stat stbuf;
62912382Sralph 
63012382Sralph 	bp = pbuf;
63112382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
63212382Sralph 		SD = DEFSPOOL;
63312382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
63412382Sralph 		LO = DEFLOCK;
63512382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
63612382Sralph 	printf("%s:\n", printer);
63712382Sralph 
63812382Sralph 	/*
63912382Sralph 	 * Turn on the owner execute bit of the lock file to disable printing.
64012382Sralph 	 */
64112382Sralph 	if (stat(line, &stbuf) >= 0) {
64212382Sralph 		if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
64312382Sralph 			printf("\tcannot disable printing\n");
64412382Sralph 		else
64512382Sralph 			printf("\tprinting disabled\n");
64612382Sralph 	} else if (errno == ENOENT) {
64713146Ssam 		if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
64812382Sralph 			printf("\tcannot create lock file\n");
64912382Sralph 		else {
65012382Sralph 			(void) close(fd);
65112382Sralph 			printf("\tprinting disabled\n");
65212382Sralph 		}
65312382Sralph 	} else
65412382Sralph 		printf("\tcannot stat lock file\n");
65512382Sralph }
65613727Sroot 
657*15719Sralph struct	queue **queue;
658*15719Sralph int	nitems;
659*15719Sralph time_t	mtime;
660*15719Sralph 
66113727Sroot /*
66213727Sroot  * Put the specified jobs at the top of printer queue.
66313727Sroot  */
66413727Sroot topq(argc, argv)
66513727Sroot 	char *argv[];
66613727Sroot {
667*15719Sralph 	register int n, i;
66813727Sroot 	struct stat stbuf;
66913727Sroot 	register char *cfname;
670*15719Sralph 	int status, changed;
67113727Sroot 
672*15719Sralph 	if (argc < 3) {
67313727Sroot 		printf("Usage: topq printer [jobnum ...] [user ...]\n");
67413727Sroot 		return;
67513727Sroot 	}
67613727Sroot 
67713727Sroot 	--argc;
67813727Sroot 	printer = *++argv;
67913727Sroot 	status = pgetent(line, printer);
68013727Sroot 	if (status < 0) {
68113727Sroot 		printf("cannot open printer description file\n");
68213727Sroot 		return;
68314151Sralph 	} else if (status == 0) {
68413727Sroot 		printf("%s: unknown printer\n", printer);
68513727Sroot 		return;
68613727Sroot 	}
68713727Sroot 	bp = pbuf;
68813727Sroot 	if ((SD = pgetstr("sd", &bp)) == NULL)
68913727Sroot 		SD = DEFSPOOL;
69013727Sroot 	if ((LO = pgetstr("lo", &bp)) == NULL)
69113727Sroot 		LO = DEFLOCK;
69213727Sroot 	printf("%s:\n", printer);
69313727Sroot 
69413727Sroot 	if (chdir(SD) < 0) {
69513727Sroot 		printf("\tcannot chdir to %s\n", SD);
69613727Sroot 		return;
69713727Sroot 	}
69813727Sroot 	nitems = getq(&queue);
699*15719Sralph 	if (nitems == 0)
700*15719Sralph 		return;
701*15719Sralph 	changed = 0;
702*15719Sralph 	mtime = queue[0]->q_time;
703*15719Sralph 	for (i = argc; --i; ) {
704*15719Sralph 		if (doarg(argv[i]) == 0) {
705*15719Sralph 			printf("\tjob %s is not in the queue\n", argv[i]);
70613727Sroot 			continue;
707*15719Sralph 		} else
70814151Sralph 			changed++;
70913727Sroot 	}
710*15719Sralph 	for (i = 0; i < nitems; i++)
711*15719Sralph 		free(queue[i]);
712*15719Sralph 	free(queue);
713*15719Sralph 	if (!changed) {
714*15719Sralph 		printf("\tqueue order unchanged\n");
715*15719Sralph 		return;
71613727Sroot 	}
71713727Sroot 	/*
71813727Sroot 	 * Turn on the public execute bit of the lock file to
71913727Sroot 	 * get lpd to rebuild the queue after the current job.
72013727Sroot 	 */
72114151Sralph 	if (changed && stat(LO, &stbuf) >= 0)
72214151Sralph 		(void) chmod(LO, (stbuf.st_mode & 0777) | 01);
72313727Sroot }
72413727Sroot 
725*15719Sralph /*
726*15719Sralph  * Reposition the job by changing the modification time of
727*15719Sralph  * the control file.
72813727Sroot  */
729*15719Sralph touch(q)
730*15719Sralph 	struct queue *q;
73113727Sroot {
732*15719Sralph 	struct timeval tvp[2];
73313727Sroot 
734*15719Sralph 	tvp[0].tv_sec = tvp[1].tv_sec = --mtime;
735*15719Sralph 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
736*15719Sralph 	return(utimes(q->q_name, tvp));
73713727Sroot }
73813727Sroot 
73913727Sroot /*
74013727Sroot  * Checks if specified job name is in the printer's queue.
74113727Sroot  * Returns:  negative (-1) if argument name is not in the queue.
74213727Sroot  */
743*15719Sralph doarg(job)
74413727Sroot 	char *job;
74513727Sroot {
746*15719Sralph 	register struct queue **qq;
747*15719Sralph 	register int jobnum, n;
748*15719Sralph 	register char *cp, *machine;
749*15719Sralph 	int cnt = 0;
75014151Sralph 	FILE *fp;
75113727Sroot 
752*15719Sralph 	/*
753*15719Sralph 	 * Look for a job item consisting of system name, colon, number
754*15719Sralph 	 * (example: ucbarpa:114)
755*15719Sralph 	 */
756*15719Sralph 	if ((cp = index(job, ':')) != NULL) {
757*15719Sralph 		machine = job;
758*15719Sralph 		*cp++ = '\0';
759*15719Sralph 		job = cp;
760*15719Sralph 	} else
761*15719Sralph 		machine = NULL;
762*15719Sralph 
763*15719Sralph 	/*
764*15719Sralph 	 * Check for job specified by number (example: 112 or 235ucbarpa).
765*15719Sralph 	 */
76613727Sroot 	if (isdigit(*job)) {
76713727Sroot 		jobnum = 0;
76813727Sroot 		do
76913727Sroot 			jobnum = jobnum * 10 + (*job++ - '0');
77013727Sroot 		while (isdigit(*job));
771*15719Sralph 		for (qq = queue + nitems; --qq >= queue; ) {
77213727Sroot 			n = 0;
773*15719Sralph 			for (cp = (*qq)->q_name+3; isdigit(*cp); )
77414151Sralph 				n = n * 10 + (*cp++ - '0');
775*15719Sralph 			if (jobnum != n)
776*15719Sralph 				continue;
777*15719Sralph 			if (*job && strcmp(job, cp) != 0)
778*15719Sralph 				continue;
779*15719Sralph 			if (machine != NULL && strcmp(machine, cp) != 0)
780*15719Sralph 				continue;
781*15719Sralph 			if (touch(*qq) == 0) {
782*15719Sralph 				printf("\tmoved %s\n", (*qq)->q_name);
783*15719Sralph 				cnt++;
784*15719Sralph 			}
78513727Sroot 		}
786*15719Sralph 		return(cnt);
787*15719Sralph 	}
788*15719Sralph 	/*
789*15719Sralph 	 * Process item consisting of owner's name (example: henry).
790*15719Sralph 	 */
791*15719Sralph 	for (qq = queue + nitems; --qq >= queue; ) {
792*15719Sralph 		if ((fp = fopen((*qq)->q_name, "r")) == NULL)
79313727Sroot 			continue;
794*15719Sralph 		while (getline(fp) > 0)
795*15719Sralph 			if (line[0] == 'P')
796*15719Sralph 				break;
797*15719Sralph 		(void) fclose(fp);
798*15719Sralph 		if (line[0] != 'P' || strcmp(job, line+1) != 0)
799*15719Sralph 			continue;
800*15719Sralph 		if (touch(*qq) == 0) {
801*15719Sralph 			printf("\tmoved %s\n", (*qq)->q_name);
802*15719Sralph 			cnt++;
80313727Sroot 		}
80413727Sroot 	}
805*15719Sralph 	return(cnt);
80613727Sroot }
807