xref: /csrg-svn/usr.sbin/lpr/lpc/cmds.c (revision 12382)
1*12382Sralph #ifndef lint
2*12382Sralph static char sccsid[] = "@(#)cmds.c	4.1 (Berkeley) 05/11/83";
3*12382Sralph #endif
4*12382Sralph 
5*12382Sralph /*
6*12382Sralph  * lpc -- line printer control program
7*12382Sralph  */
8*12382Sralph 
9*12382Sralph #include "lp.h"
10*12382Sralph 
11*12382Sralph /*
12*12382Sralph  * kill an existing daemon and disable printing.
13*12382Sralph  */
14*12382Sralph abort(argc, argv)
15*12382Sralph 	char *argv[];
16*12382Sralph {
17*12382Sralph 	register int c, status;
18*12382Sralph 	register char *cp1, *cp2;
19*12382Sralph 	char prbuf[100];
20*12382Sralph 
21*12382Sralph 	if (argc == 1) {
22*12382Sralph 		printf("Usage: abort {all | printer ...}\n");
23*12382Sralph 		return;
24*12382Sralph 	}
25*12382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
26*12382Sralph 		printer = prbuf;
27*12382Sralph 		while (getprent(line) > 0) {
28*12382Sralph 			cp1 = prbuf;
29*12382Sralph 			cp2 = line;
30*12382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
31*12382Sralph 				*cp1++ = c;
32*12382Sralph 			*cp1 = '\0';
33*12382Sralph 			abortpr();
34*12382Sralph 		}
35*12382Sralph 		return;
36*12382Sralph 	}
37*12382Sralph 	while (--argc) {
38*12382Sralph 		printer = *++argv;
39*12382Sralph 		if ((status = pgetent(line, printer)) < 0) {
40*12382Sralph 			printf("cannot open printer description file");
41*12382Sralph 			continue;
42*12382Sralph 		} else if (status == 0) {
43*12382Sralph 			printf("unknown printer %s", printer);
44*12382Sralph 			continue;
45*12382Sralph 		}
46*12382Sralph 		abortpr();
47*12382Sralph 	}
48*12382Sralph }
49*12382Sralph 
50*12382Sralph abortpr()
51*12382Sralph {
52*12382Sralph 	register FILE *fp;
53*12382Sralph 	struct stat stbuf;
54*12382Sralph 	int pid, fd;
55*12382Sralph 
56*12382Sralph 	bp = pbuf;
57*12382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
58*12382Sralph 		SD = DEFSPOOL;
59*12382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
60*12382Sralph 		LO = DEFLOCK;
61*12382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
62*12382Sralph 	printf("%s:\n", printer);
63*12382Sralph 
64*12382Sralph 	/*
65*12382Sralph 	 * Turn on the owner execute bit of the lock file to disable printing.
66*12382Sralph 	 */
67*12382Sralph 	if (stat(line, &stbuf) >= 0) {
68*12382Sralph 		if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
69*12382Sralph 			printf("\tcannot disable printing\n");
70*12382Sralph 		else
71*12382Sralph 			printf("\tprinting disabled\n");
72*12382Sralph 	} else if (errno == ENOENT) {
73*12382Sralph 		if ((fd = open(line, FWRONLY|FCREATE, 0760)) < 0)
74*12382Sralph 			printf("\tcannot create lock file");
75*12382Sralph 		else {
76*12382Sralph 			(void) close(fd);
77*12382Sralph 			printf("\tprinting disabled\n");
78*12382Sralph 			printf("\tno daemon to abort\n");
79*12382Sralph 		}
80*12382Sralph 		return;
81*12382Sralph 	} else {
82*12382Sralph 		printf("\tcannot stat lock file\n");
83*12382Sralph 		return;
84*12382Sralph 	}
85*12382Sralph 	/*
86*12382Sralph 	 * Kill the current daemon to stop printing now.
87*12382Sralph 	 */
88*12382Sralph 	if ((fp = fopen(line, "r")) == NULL) {
89*12382Sralph 		printf("\tcannot open lock file\n");
90*12382Sralph 		return;
91*12382Sralph 	}
92*12382Sralph 	if (!getline(fp) || flock(fileno(fp), FSHLOCK|FNBLOCK) == 0) {
93*12382Sralph 		(void) fclose(fp);
94*12382Sralph 		printf("\tno daemon to abort\n");
95*12382Sralph 		return;
96*12382Sralph 	}
97*12382Sralph 	(void) fclose(fp);
98*12382Sralph 	if (kill(pid = atoi(line), SIGINT) < 0)
99*12382Sralph 		printf("\tWarning: daemon (pid %d) not killed\n", pid);
100*12382Sralph 	else
101*12382Sralph 		printf("\tdaemon (pid %d) killed\n", pid);
102*12382Sralph }
103*12382Sralph 
104*12382Sralph /*
105*12382Sralph  * Remove all spool files and temporaries from the spooling area.
106*12382Sralph  */
107*12382Sralph clean(argc, argv)
108*12382Sralph 	char *argv[];
109*12382Sralph {
110*12382Sralph 	register int c, status;
111*12382Sralph 	register char *cp1, *cp2;
112*12382Sralph 	char prbuf[100];
113*12382Sralph 
114*12382Sralph 	if (argc == 1) {
115*12382Sralph 		printf("Usage: clean {all | printer ...}\n");
116*12382Sralph 		return;
117*12382Sralph 	}
118*12382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
119*12382Sralph 		printer = prbuf;
120*12382Sralph 		while (getprent(line) > 0) {
121*12382Sralph 			cp1 = prbuf;
122*12382Sralph 			cp2 = line;
123*12382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
124*12382Sralph 				*cp1++ = c;
125*12382Sralph 			*cp1 = '\0';
126*12382Sralph 			cleanpr();
127*12382Sralph 		}
128*12382Sralph 		return;
129*12382Sralph 	}
130*12382Sralph 	while (--argc) {
131*12382Sralph 		printer = *++argv;
132*12382Sralph 		if ((status = pgetent(line, printer)) < 0) {
133*12382Sralph 			printf("cannot open printer description file");
134*12382Sralph 			continue;
135*12382Sralph 		} else if (status == 0) {
136*12382Sralph 			printf("unknown printer %s", printer);
137*12382Sralph 			continue;
138*12382Sralph 		}
139*12382Sralph 		cleanpr();
140*12382Sralph 	}
141*12382Sralph }
142*12382Sralph 
143*12382Sralph cleanpr()
144*12382Sralph {
145*12382Sralph 	register int c;
146*12382Sralph 	register DIR *dirp;
147*12382Sralph 	register struct direct *dp;
148*12382Sralph 	char *cp, *cp1;
149*12382Sralph 
150*12382Sralph 	bp = pbuf;
151*12382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
152*12382Sralph 		SD = DEFSPOOL;
153*12382Sralph 	for (cp = line, cp1 = SD; *cp++ = *cp1++; );
154*12382Sralph 	cp[-1] = '/';
155*12382Sralph 	printf("%s:\n", printer);
156*12382Sralph 
157*12382Sralph 	if ((dirp = opendir(SD)) == NULL) {
158*12382Sralph 		printf("\tcannot examine spool directory\n");
159*12382Sralph 		return;
160*12382Sralph 	}
161*12382Sralph 	while ((dp = readdir(dirp)) != NULL) {
162*12382Sralph 		c = dp->d_name[0];
163*12382Sralph 		if ((c == 'c' || c == 't' || c == 'd') && dp->d_name[1]=='f') {
164*12382Sralph 			strcpy(cp, dp->d_name);
165*12382Sralph 			if (unlink(line) < 0)
166*12382Sralph 				printf("\tcannot remove %s\n", line);
167*12382Sralph 			else
168*12382Sralph 				printf("\tremoved %s\n", line);
169*12382Sralph 		}
170*12382Sralph 	}
171*12382Sralph 	closedir(dirp);
172*12382Sralph }
173*12382Sralph 
174*12382Sralph /*
175*12382Sralph  * Enable queuing to the printer (allow lpr's).
176*12382Sralph  */
177*12382Sralph enable(argc, argv)
178*12382Sralph 	char *argv[];
179*12382Sralph {
180*12382Sralph 	register int c, status;
181*12382Sralph 	register char *cp1, *cp2;
182*12382Sralph 	char prbuf[100];
183*12382Sralph 
184*12382Sralph 	if (argc == 1) {
185*12382Sralph 		printf("Usage: enable {all | printer ...}\n");
186*12382Sralph 		return;
187*12382Sralph 	}
188*12382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
189*12382Sralph 		printer = prbuf;
190*12382Sralph 		while (getprent(line) > 0) {
191*12382Sralph 			cp1 = prbuf;
192*12382Sralph 			cp2 = line;
193*12382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
194*12382Sralph 				*cp1++ = c;
195*12382Sralph 			*cp1 = '\0';
196*12382Sralph 			enablepr();
197*12382Sralph 		}
198*12382Sralph 		return;
199*12382Sralph 	}
200*12382Sralph 	while (--argc) {
201*12382Sralph 		printer = *++argv;
202*12382Sralph 		if ((status = pgetent(line, printer)) < 0) {
203*12382Sralph 			printf("cannot open printer description file");
204*12382Sralph 			continue;
205*12382Sralph 		} else if (status == 0) {
206*12382Sralph 			printf("unknown printer %s", printer);
207*12382Sralph 			continue;
208*12382Sralph 		}
209*12382Sralph 		enablepr();
210*12382Sralph 	}
211*12382Sralph }
212*12382Sralph 
213*12382Sralph enablepr()
214*12382Sralph {
215*12382Sralph 	struct stat stbuf;
216*12382Sralph 
217*12382Sralph 	bp = pbuf;
218*12382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
219*12382Sralph 		SD = DEFSPOOL;
220*12382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
221*12382Sralph 		LO = DEFLOCK;
222*12382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
223*12382Sralph 	printf("%s:\n", printer);
224*12382Sralph 
225*12382Sralph 	/*
226*12382Sralph 	 * Turn off the group execute bit of the lock file to enable queuing.
227*12382Sralph 	 */
228*12382Sralph 	if (stat(line, &stbuf) >= 0) {
229*12382Sralph 		if (chmod(line, stbuf.st_mode & 0767) < 0)
230*12382Sralph 			printf("\tcannot enable queuing");
231*12382Sralph 		else
232*12382Sralph 			printf("\tqueuing enabled\n");
233*12382Sralph 	}
234*12382Sralph }
235*12382Sralph 
236*12382Sralph /*
237*12382Sralph  * Disable queuing.
238*12382Sralph  */
239*12382Sralph disable(argc, argv)
240*12382Sralph 	char *argv[];
241*12382Sralph {
242*12382Sralph 	register int c, status;
243*12382Sralph 	register char *cp1, *cp2;
244*12382Sralph 	char prbuf[100];
245*12382Sralph 
246*12382Sralph 	if (argc == 1) {
247*12382Sralph 		printf("Usage: disable {all | printer ...}\n");
248*12382Sralph 		return;
249*12382Sralph 	}
250*12382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
251*12382Sralph 		printer = prbuf;
252*12382Sralph 		while (getprent(line) > 0) {
253*12382Sralph 			cp1 = prbuf;
254*12382Sralph 			cp2 = line;
255*12382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
256*12382Sralph 				*cp1++ = c;
257*12382Sralph 			*cp1 = '\0';
258*12382Sralph 			disablepr();
259*12382Sralph 		}
260*12382Sralph 		return;
261*12382Sralph 	}
262*12382Sralph 	while (--argc) {
263*12382Sralph 		printer = *++argv;
264*12382Sralph 		if ((status = pgetent(line, printer)) < 0) {
265*12382Sralph 			printf("cannot open printer description file");
266*12382Sralph 			continue;
267*12382Sralph 		} else if (status == 0) {
268*12382Sralph 			printf("unknown printer %s", printer);
269*12382Sralph 			continue;
270*12382Sralph 		}
271*12382Sralph 		disablepr();
272*12382Sralph 	}
273*12382Sralph }
274*12382Sralph 
275*12382Sralph disablepr()
276*12382Sralph {
277*12382Sralph 	register int fd;
278*12382Sralph 	struct stat stbuf;
279*12382Sralph 
280*12382Sralph 	bp = pbuf;
281*12382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
282*12382Sralph 		SD = DEFSPOOL;
283*12382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
284*12382Sralph 		LO = DEFLOCK;
285*12382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
286*12382Sralph 	printf("%s:\n", printer);
287*12382Sralph 	/*
288*12382Sralph 	 * Turn on the group execute bit of the lock file to disable queuing.
289*12382Sralph 	 */
290*12382Sralph 	if (stat(line, &stbuf) >= 0) {
291*12382Sralph 		if (chmod(line, (stbuf.st_mode & 0777) | 010) < 0)
292*12382Sralph 			printf("\tcannot disable queuing\n");
293*12382Sralph 		else
294*12382Sralph 			printf("\tqueuing disabled\n");
295*12382Sralph 	} else if (errno == ENOENT) {
296*12382Sralph 		if ((fd = open(line, FWRONLY|FCREATE, 0670)) < 0)
297*12382Sralph 			printf("\tcannot create lock file\n");
298*12382Sralph 		else {
299*12382Sralph 			(void) close(fd);
300*12382Sralph 			printf("\tqueuing disabled\n");
301*12382Sralph 		}
302*12382Sralph 		return;
303*12382Sralph 	} else
304*12382Sralph 		printf("\tcannot stat lock file\n");
305*12382Sralph }
306*12382Sralph 
307*12382Sralph /*
308*12382Sralph  * Exit lpc
309*12382Sralph  */
310*12382Sralph quit(argc, argv)
311*12382Sralph 	char *argv[];
312*12382Sralph {
313*12382Sralph 	exit(0);
314*12382Sralph }
315*12382Sralph 
316*12382Sralph /*
317*12382Sralph  * Startup the daemon.
318*12382Sralph  */
319*12382Sralph restart(argc, argv)
320*12382Sralph 	char *argv[];
321*12382Sralph {
322*12382Sralph 	register int c, status;
323*12382Sralph 	register char *cp1, *cp2;
324*12382Sralph 	char prbuf[100];
325*12382Sralph 
326*12382Sralph 	if (argc == 1) {
327*12382Sralph 		printf("Usage: restart {all | printer ...}\n");
328*12382Sralph 		return;
329*12382Sralph 	}
330*12382Sralph 	gethostname(host, sizeof(host));
331*12382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
332*12382Sralph 		printer = prbuf;
333*12382Sralph 		while (getprent(line) > 0) {
334*12382Sralph 			cp1 = prbuf;
335*12382Sralph 			cp2 = line;
336*12382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
337*12382Sralph 				*cp1++ = c;
338*12382Sralph 			*cp1 = '\0';
339*12382Sralph 			startpr(0);
340*12382Sralph 		}
341*12382Sralph 		return;
342*12382Sralph 	}
343*12382Sralph 	while (--argc) {
344*12382Sralph 		printer = *++argv;
345*12382Sralph 		if ((status = pgetent(line, printer)) < 0) {
346*12382Sralph 			printf("cannot open printer description file");
347*12382Sralph 			continue;
348*12382Sralph 		} else if (status == 0) {
349*12382Sralph 			printf("unknown printer %s", printer);
350*12382Sralph 			continue;
351*12382Sralph 		}
352*12382Sralph 		startpr(0);
353*12382Sralph 	}
354*12382Sralph }
355*12382Sralph 
356*12382Sralph /*
357*12382Sralph  * Enable printing on the specified printer and startup the daemon.
358*12382Sralph  */
359*12382Sralph start(argc, argv)
360*12382Sralph 	char *argv[];
361*12382Sralph {
362*12382Sralph 	register int c, status;
363*12382Sralph 	register char *cp1, *cp2;
364*12382Sralph 	char prbuf[100];
365*12382Sralph 
366*12382Sralph 	if (argc == 1) {
367*12382Sralph 		printf("Usage: start {all | printer ...}\n");
368*12382Sralph 		return;
369*12382Sralph 	}
370*12382Sralph 	gethostname(host, sizeof(host));
371*12382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
372*12382Sralph 		printer = prbuf;
373*12382Sralph 		while (getprent(line) > 0) {
374*12382Sralph 			cp1 = prbuf;
375*12382Sralph 			cp2 = line;
376*12382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
377*12382Sralph 				*cp1++ = c;
378*12382Sralph 			*cp1 = '\0';
379*12382Sralph 			startpr(1);
380*12382Sralph 		}
381*12382Sralph 		return;
382*12382Sralph 	}
383*12382Sralph 	while (--argc) {
384*12382Sralph 		printer = *++argv;
385*12382Sralph 		if ((status = pgetent(line, printer)) < 0) {
386*12382Sralph 			printf("cannot open printer description file");
387*12382Sralph 			continue;
388*12382Sralph 		} else if (status == 0) {
389*12382Sralph 			printf("unknown printer %s", printer);
390*12382Sralph 			continue;
391*12382Sralph 		}
392*12382Sralph 		startpr(1);
393*12382Sralph 	}
394*12382Sralph }
395*12382Sralph 
396*12382Sralph startpr(enable)
397*12382Sralph {
398*12382Sralph 	struct stat stbuf;
399*12382Sralph 
400*12382Sralph 	bp = pbuf;
401*12382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
402*12382Sralph 		SD = DEFSPOOL;
403*12382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
404*12382Sralph 		LO = DEFLOCK;
405*12382Sralph 	RM = host;
406*12382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
407*12382Sralph 	printf("%s:\n", printer);
408*12382Sralph 
409*12382Sralph 	/*
410*12382Sralph 	 * Turn off the owner execute bit of the lock file to enable printing.
411*12382Sralph 	 */
412*12382Sralph 	if (enable && stat(line, &stbuf) >= 0) {
413*12382Sralph 		if (chmod(line, stbuf.st_mode & 0677) < 0)
414*12382Sralph 			printf("\tcannot enable printing\n");
415*12382Sralph 		else
416*12382Sralph 			printf("\tprinting enabled\n");
417*12382Sralph 	}
418*12382Sralph 	if (!startdaemon())
419*12382Sralph 		printf("\tcouldn't start daemon\n");
420*12382Sralph 	else
421*12382Sralph 		printf("\tdaemon started\n");
422*12382Sralph }
423*12382Sralph 
424*12382Sralph /*
425*12382Sralph  * Print the status of each queue listed or all the queues.
426*12382Sralph  */
427*12382Sralph status(argc, argv)
428*12382Sralph 	char *argv[];
429*12382Sralph {
430*12382Sralph 	register int c, status;
431*12382Sralph 	register char *cp1, *cp2;
432*12382Sralph 	char prbuf[100];
433*12382Sralph 
434*12382Sralph 	if (argc == 1) {
435*12382Sralph 		printer = prbuf;
436*12382Sralph 		while (getprent(line) > 0) {
437*12382Sralph 			cp1 = prbuf;
438*12382Sralph 			cp2 = line;
439*12382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
440*12382Sralph 				*cp1++ = c;
441*12382Sralph 			*cp1 = '\0';
442*12382Sralph 			prstat();
443*12382Sralph 		}
444*12382Sralph 		return;
445*12382Sralph 	}
446*12382Sralph 	while (--argc) {
447*12382Sralph 		printer = *++argv;
448*12382Sralph 		if ((status = pgetent(line, printer)) < 0) {
449*12382Sralph 			printf("cannot open printer description file");
450*12382Sralph 			continue;
451*12382Sralph 		} else if (status == 0) {
452*12382Sralph 			printf("unknown printer %s", printer);
453*12382Sralph 			continue;
454*12382Sralph 		}
455*12382Sralph 		prstat();
456*12382Sralph 	}
457*12382Sralph }
458*12382Sralph 
459*12382Sralph /*
460*12382Sralph  * Print the status of the printer queue.
461*12382Sralph  */
462*12382Sralph prstat()
463*12382Sralph {
464*12382Sralph 	struct stat stbuf;
465*12382Sralph 	register int fd, i;
466*12382Sralph 	register struct direct *dp;
467*12382Sralph 	DIR *dirp;
468*12382Sralph 
469*12382Sralph 	bp = pbuf;
470*12382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
471*12382Sralph 		SD = DEFSPOOL;
472*12382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
473*12382Sralph 		LO = DEFLOCK;
474*12382Sralph 	if ((ST = pgetstr("st", &bp)) == NULL)
475*12382Sralph 		ST = DEFSTAT;
476*12382Sralph 	printf("%s:\n", printer);
477*12382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
478*12382Sralph 	if (stat(line, &stbuf) >= 0) {
479*12382Sralph 		printf("\tqueuing is %s\n",
480*12382Sralph 			(stbuf.st_mode & 010) ? "disabled" : "enabled");
481*12382Sralph 		printf("\tprinting is %s\n",
482*12382Sralph 			(stbuf.st_mode & 0100) ? "disabled" : "enabled");
483*12382Sralph 	} else {
484*12382Sralph 		printf("\tqueuing is enabled\n");
485*12382Sralph 		printf("\tprinting is enabled\n");
486*12382Sralph 	}
487*12382Sralph 	if ((dirp = opendir(SD)) == NULL) {
488*12382Sralph 		printf("\tcannot examine spool directory\n");
489*12382Sralph 		return;
490*12382Sralph 	}
491*12382Sralph 	i = 0;
492*12382Sralph 	while ((dp = readdir(dirp)) != NULL) {
493*12382Sralph 		if (*dp->d_name == 'c' && dp->d_name[1] == 'f')
494*12382Sralph 			i++;
495*12382Sralph 	}
496*12382Sralph 	closedir(dirp);
497*12382Sralph 	if (i == 0)
498*12382Sralph 		printf("\tno entries\n");
499*12382Sralph 	else if (i == 1)
500*12382Sralph 		printf("\t1 entry in spool area\n");
501*12382Sralph 	else
502*12382Sralph 		printf("\t%d entries in spool area\n", i);
503*12382Sralph 	fd = open(line, FRDONLY);
504*12382Sralph 	if (fd < 0 || flock(fd, FSHLOCK|FNBLOCK) == 0) {
505*12382Sralph 		(void) close(fd);
506*12382Sralph 		printf("\tno daemon present\n");
507*12382Sralph 		return;
508*12382Sralph 	}
509*12382Sralph 	(void) close(fd);
510*12382Sralph 	putchar('\t');
511*12382Sralph 	(void) sprintf(line, "%s/%s", SD, ST);
512*12382Sralph 	if ((fd = open(line, FRDONLY|FSHLOCK)) >= 0) {
513*12382Sralph 		while ((i = read(fd, line, sizeof(line))) > 0)
514*12382Sralph 			(void) fwrite(line, 1, i, stdout);
515*12382Sralph 		(void) close(fd);
516*12382Sralph 	}
517*12382Sralph }
518*12382Sralph 
519*12382Sralph /*
520*12382Sralph  * Stop the specified daemon after completing the current job and disable
521*12382Sralph  * printing.
522*12382Sralph  */
523*12382Sralph stop(argc, argv)
524*12382Sralph 	char *argv[];
525*12382Sralph {
526*12382Sralph 	register int c, status;
527*12382Sralph 	register char *cp1, *cp2;
528*12382Sralph 	char prbuf[100];
529*12382Sralph 
530*12382Sralph 	if (argc == 1) {
531*12382Sralph 		printf("Usage: stop {all | printer ...}\n");
532*12382Sralph 		return;
533*12382Sralph 	}
534*12382Sralph 	if (argc == 2 && !strcmp(argv[1], "all")) {
535*12382Sralph 		printer = prbuf;
536*12382Sralph 		while (getprent(line) > 0) {
537*12382Sralph 			cp1 = prbuf;
538*12382Sralph 			cp2 = line;
539*12382Sralph 			while ((c = *cp2++) && c != '|' && c != ':')
540*12382Sralph 				*cp1++ = c;
541*12382Sralph 			*cp1 = '\0';
542*12382Sralph 			stoppr();
543*12382Sralph 		}
544*12382Sralph 		return;
545*12382Sralph 	}
546*12382Sralph 	while (--argc) {
547*12382Sralph 		printer = *++argv;
548*12382Sralph 		if ((status = pgetent(line, printer)) < 0) {
549*12382Sralph 			printf("cannot open printer description file");
550*12382Sralph 			continue;
551*12382Sralph 		} else if (status == 0) {
552*12382Sralph 			printf("unknown printer %s", printer);
553*12382Sralph 			continue;
554*12382Sralph 		}
555*12382Sralph 		stoppr();
556*12382Sralph 	}
557*12382Sralph }
558*12382Sralph 
559*12382Sralph stoppr()
560*12382Sralph {
561*12382Sralph 	register int fd;
562*12382Sralph 	struct stat stbuf;
563*12382Sralph 
564*12382Sralph 	bp = pbuf;
565*12382Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
566*12382Sralph 		SD = DEFSPOOL;
567*12382Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
568*12382Sralph 		LO = DEFLOCK;
569*12382Sralph 	(void) sprintf(line, "%s/%s", SD, LO);
570*12382Sralph 	printf("%s:\n", printer);
571*12382Sralph 
572*12382Sralph 	/*
573*12382Sralph 	 * Turn on the owner execute bit of the lock file to disable printing.
574*12382Sralph 	 */
575*12382Sralph 	if (stat(line, &stbuf) >= 0) {
576*12382Sralph 		if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
577*12382Sralph 			printf("\tcannot disable printing\n");
578*12382Sralph 		else
579*12382Sralph 			printf("\tprinting disabled\n");
580*12382Sralph 	} else if (errno == ENOENT) {
581*12382Sralph 		if ((fd = open(line, FWRONLY|FCREATE, 0760)) < 0)
582*12382Sralph 			printf("\tcannot create lock file\n");
583*12382Sralph 		else {
584*12382Sralph 			(void) close(fd);
585*12382Sralph 			printf("\tprinting disabled\n");
586*12382Sralph 		}
587*12382Sralph 	} else
588*12382Sralph 		printf("\tcannot stat lock file\n");
589*12382Sralph }
590