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