122424Sdist /*
261843Sbostic * Copyright (c) 1983, 1993
361843Sbostic * The Regents of the University of California. All rights reserved.
434203Sbostic *
556121Selan *
656250Selan * %sccs.include.redist.c%
722424Sdist */
822424Sdist
912382Sralph #ifndef lint
1056264Selan static char copyright[] =
1161843Sbostic "@(#) Copyright (c) 1983, 1993\n\
1261843Sbostic The Regents of the University of California. All rights reserved.\n";
1334203Sbostic #endif /* not lint */
1412382Sralph
1556250Selan #ifndef lint
16*69063Stef static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 04/28/95";
1756250Selan #endif /* not lint */
1856250Selan
1912382Sralph /*
2015719Sralph * lpc -- line printer control program -- commands:
2112382Sralph */
2212382Sralph
2355472Sbostic #include <sys/param.h>
2455472Sbostic #include <sys/time.h>
2555472Sbostic #include <sys/stat.h>
26*69063Stef #include <sys/file.h>
2755472Sbostic
2855472Sbostic #include <signal.h>
2955472Sbostic #include <fcntl.h>
3055472Sbostic #include <errno.h>
3155472Sbostic #include <dirent.h>
3255472Sbostic #include <unistd.h>
3355472Sbostic #include <stdlib.h>
3455472Sbostic #include <stdio.h>
3555472Sbostic #include <ctype.h>
3655472Sbostic #include <string.h>
3712382Sralph #include "lp.h"
3855472Sbostic #include "lp.local.h"
3955472Sbostic #include "lpc.h"
4055472Sbostic #include "extern.h"
4137968Sbostic #include "pathnames.h"
4212382Sralph
4355472Sbostic static void abortpr __P((int));
4455472Sbostic static void cleanpr __P((void));
4556121Selan static void disablepr __P((void));
4656121Selan static int doarg __P((char *));
4755472Sbostic static int doselect __P((struct dirent *));
4856121Selan static void enablepr __P((void));
4956121Selan static void prstat __P((void));
5056121Selan static void putmsg __P((int, char **));
5155472Sbostic static int sortq __P((const void *, const void *));
5256121Selan static void startpr __P((int));
5356121Selan static void stoppr __P((void));
5456121Selan static int touch __P((struct queue *));
5555472Sbostic static void unlinkf __P((char *));
5656121Selan static void upstat __P((char *));
5755472Sbostic
5812382Sralph /*
5912382Sralph * kill an existing daemon and disable printing.
6012382Sralph */
6155472Sbostic void
doabort(argc,argv)6255472Sbostic doabort(argc, argv)
6355472Sbostic int argc;
6412382Sralph char *argv[];
6512382Sralph {
6612382Sralph register int c, status;
6712382Sralph register char *cp1, *cp2;
6812382Sralph char prbuf[100];
6912382Sralph
7012382Sralph if (argc == 1) {
7112382Sralph printf("Usage: abort {all | printer ...}\n");
7212382Sralph return;
7312382Sralph }
7412382Sralph if (argc == 2 && !strcmp(argv[1], "all")) {
7512382Sralph printer = prbuf;
7656121Selan while (cgetnext(&bp, printcapdb) > 0) {
7712382Sralph cp1 = prbuf;
7856121Selan cp2 = bp;
7912382Sralph while ((c = *cp2++) && c != '|' && c != ':')
8012382Sralph *cp1++ = c;
8112382Sralph *cp1 = '\0';
8216755Sralph abortpr(1);
8312382Sralph }
8412382Sralph return;
8512382Sralph }
8612382Sralph while (--argc) {
8712382Sralph printer = *++argv;
8856121Selan if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
8912514Sralph printf("cannot open printer description file\n");
9012382Sralph continue;
9156121Selan } else if (status == -1) {
9212514Sralph printf("unknown printer %s\n", printer);
9312382Sralph continue;
9456121Selan } else if (status == -3)
9556121Selan fatal("potential reference loop detected in printcap file");
9616755Sralph abortpr(1);
9712382Sralph }
9812382Sralph }
9912382Sralph
10055472Sbostic static void
abortpr(dis)10116755Sralph abortpr(dis)
10255472Sbostic int dis;
10312382Sralph {
10412382Sralph register FILE *fp;
10512382Sralph struct stat stbuf;
10612382Sralph int pid, fd;
10712382Sralph
10856121Selan if (cgetstr(bp, "sd", &SD) == -1)
10937968Sbostic SD = _PATH_DEFSPOOL;
11056121Selan if (cgetstr(bp, "lo", &LO) == -1)
11112382Sralph LO = DEFLOCK;
11212382Sralph (void) sprintf(line, "%s/%s", SD, LO);
11312382Sralph printf("%s:\n", printer);
11412382Sralph
11512382Sralph /*
11612382Sralph * Turn on the owner execute bit of the lock file to disable printing.
11712382Sralph */
11816755Sralph if (dis) {
11916755Sralph if (stat(line, &stbuf) >= 0) {
12016755Sralph if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
12116755Sralph printf("\tcannot disable printing\n");
12238735Stef else {
12338735Stef upstat("printing disabled\n");
12416755Sralph printf("\tprinting disabled\n");
12538735Stef }
12616755Sralph } else if (errno == ENOENT) {
12716755Sralph if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
12816755Sralph printf("\tcannot create lock file\n");
12916755Sralph else {
13016755Sralph (void) close(fd);
13138735Stef upstat("printing disabled\n");
13216755Sralph printf("\tprinting disabled\n");
13316755Sralph printf("\tno daemon to abort\n");
13416755Sralph }
13516755Sralph return;
13616755Sralph } else {
13716755Sralph printf("\tcannot stat lock file\n");
13816755Sralph return;
13912382Sralph }
14012382Sralph }
14112382Sralph /*
14212382Sralph * Kill the current daemon to stop printing now.
14312382Sralph */
14412382Sralph if ((fp = fopen(line, "r")) == NULL) {
14512382Sralph printf("\tcannot open lock file\n");
14612382Sralph return;
14712382Sralph }
14813146Ssam if (!getline(fp) || flock(fileno(fp), LOCK_SH|LOCK_NB) == 0) {
14913168Sralph (void) fclose(fp); /* unlocks as well */
15012382Sralph printf("\tno daemon to abort\n");
15112382Sralph return;
15212382Sralph }
15312382Sralph (void) fclose(fp);
15416755Sralph if (kill(pid = atoi(line), SIGTERM) < 0)
15512382Sralph printf("\tWarning: daemon (pid %d) not killed\n", pid);
15612382Sralph else
15712382Sralph printf("\tdaemon (pid %d) killed\n", pid);
15812382Sralph }
15912382Sralph
16012382Sralph /*
16138735Stef * Write a message into the status file.
16238735Stef */
16355472Sbostic static void
upstat(msg)16438735Stef upstat(msg)
16538735Stef char *msg;
16638735Stef {
16738735Stef register int fd;
16838735Stef char statfile[BUFSIZ];
16938735Stef
17056121Selan if (cgetstr(bp, "st", &ST) == -1)
17138735Stef ST = DEFSTAT;
17238735Stef (void) sprintf(statfile, "%s/%s", SD, ST);
17338735Stef umask(0);
17438735Stef fd = open(statfile, O_WRONLY|O_CREAT, 0664);
17538735Stef if (fd < 0 || flock(fd, LOCK_EX) < 0) {
17638735Stef printf("\tcannot create status file\n");
17738735Stef return;
17838735Stef }
17938735Stef (void) ftruncate(fd, 0);
18038735Stef if (msg == (char *)NULL)
18138735Stef (void) write(fd, "\n", 1);
18238735Stef else
18338735Stef (void) write(fd, msg, strlen(msg));
18438735Stef (void) close(fd);
18538735Stef }
18638735Stef
18738735Stef /*
18812382Sralph * Remove all spool files and temporaries from the spooling area.
18912382Sralph */
19055472Sbostic void
clean(argc,argv)19112382Sralph clean(argc, argv)
19255472Sbostic int argc;
19312382Sralph char *argv[];
19412382Sralph {
19512382Sralph register int c, status;
19612382Sralph register char *cp1, *cp2;
19712382Sralph char prbuf[100];
19812382Sralph
19912382Sralph if (argc == 1) {
20012382Sralph printf("Usage: clean {all | printer ...}\n");
20112382Sralph return;
20212382Sralph }
20312382Sralph if (argc == 2 && !strcmp(argv[1], "all")) {
20412382Sralph printer = prbuf;
20556121Selan while (cgetnext(&bp, printcapdb) > 0) {
20612382Sralph cp1 = prbuf;
20756121Selan cp2 = bp;
20812382Sralph while ((c = *cp2++) && c != '|' && c != ':')
20912382Sralph *cp1++ = c;
21012382Sralph *cp1 = '\0';
21112382Sralph cleanpr();
21212382Sralph }
21312382Sralph return;
21412382Sralph }
21512382Sralph while (--argc) {
21612382Sralph printer = *++argv;
21756121Selan if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
21812514Sralph printf("cannot open printer description file\n");
21912382Sralph continue;
22056121Selan } else if (status == -1) {
22112514Sralph printf("unknown printer %s\n", printer);
22212382Sralph continue;
22356121Selan } else if (status == -3)
22456121Selan fatal("potential reference loop detected in printcap file");
22556121Selan
22612382Sralph cleanpr();
22712382Sralph }
22812382Sralph }
22912382Sralph
23055472Sbostic static int
doselect(d)23155472Sbostic doselect(d)
23255472Sbostic struct dirent *d;
23315719Sralph {
23415719Sralph int c = d->d_name[0];
23515719Sralph
23615719Sralph if ((c == 't' || c == 'c' || c == 'd') && d->d_name[1] == 'f')
23715719Sralph return(1);
23815719Sralph return(0);
23915719Sralph }
24015719Sralph
24115719Sralph /*
24215719Sralph * Comparison routine for scandir. Sort by job number and machine, then
24315719Sralph * by `cf', `tf', or `df', then by the sequence letter A-Z, a-z.
24415719Sralph */
24555472Sbostic static int
sortq(a,b)24655472Sbostic sortq(a, b)
24755472Sbostic const void *a, *b;
24815719Sralph {
24955472Sbostic struct dirent **d1, **d2;
25015719Sralph int c1, c2;
25115719Sralph
25255472Sbostic d1 = (struct dirent **)a;
25355472Sbostic d2 = (struct dirent **)b;
25415719Sralph if (c1 = strcmp((*d1)->d_name + 3, (*d2)->d_name + 3))
25515719Sralph return(c1);
25615719Sralph c1 = (*d1)->d_name[0];
25715719Sralph c2 = (*d2)->d_name[0];
25815719Sralph if (c1 == c2)
25915719Sralph return((*d1)->d_name[2] - (*d2)->d_name[2]);
26015719Sralph if (c1 == 'c')
26115719Sralph return(-1);
26215719Sralph if (c1 == 'd' || c2 == 'c')
26315719Sralph return(1);
26415719Sralph return(-1);
26515719Sralph }
26615719Sralph
26715719Sralph /*
26815719Sralph * Remove incomplete jobs from spooling area.
26915719Sralph */
27055472Sbostic static void
cleanpr()27112382Sralph cleanpr()
27212382Sralph {
27315719Sralph register int i, n;
27415719Sralph register char *cp, *cp1, *lp;
27555472Sbostic struct dirent **queue;
27615719Sralph int nitems;
27712382Sralph
27856121Selan if (cgetstr(bp, "sd", &SD) == -1)
27937968Sbostic SD = _PATH_DEFSPOOL;
28012382Sralph printf("%s:\n", printer);
28112382Sralph
28215719Sralph for (lp = line, cp = SD; *lp++ = *cp++; )
28315719Sralph ;
28415719Sralph lp[-1] = '/';
28515719Sralph
28655472Sbostic nitems = scandir(SD, &queue, doselect, sortq);
28715719Sralph if (nitems < 0) {
28812382Sralph printf("\tcannot examine spool directory\n");
28912382Sralph return;
29012382Sralph }
29115719Sralph if (nitems == 0)
29215719Sralph return;
29315719Sralph i = 0;
29415719Sralph do {
29515719Sralph cp = queue[i]->d_name;
29615719Sralph if (*cp == 'c') {
29715719Sralph n = 0;
29815719Sralph while (i + 1 < nitems) {
29915719Sralph cp1 = queue[i + 1]->d_name;
30015719Sralph if (*cp1 != 'd' || strcmp(cp + 3, cp1 + 3))
30115719Sralph break;
30215719Sralph i++;
30315719Sralph n++;
30415719Sralph }
30515719Sralph if (n == 0) {
30615719Sralph strcpy(lp, cp);
30715719Sralph unlinkf(line);
30815719Sralph }
30915719Sralph } else {
31015719Sralph /*
31115719Sralph * Must be a df with no cf (otherwise, it would have
31215719Sralph * been skipped above) or a tf file (which can always
31315719Sralph * be removed).
31415719Sralph */
31515719Sralph strcpy(lp, cp);
31615719Sralph unlinkf(line);
31712382Sralph }
31815719Sralph } while (++i < nitems);
31912382Sralph }
32015719Sralph
32155472Sbostic static void
unlinkf(name)32215719Sralph unlinkf(name)
32315719Sralph char *name;
32415719Sralph {
32515719Sralph if (unlink(name) < 0)
32615719Sralph printf("\tcannot remove %s\n", name);
32715719Sralph else
32815719Sralph printf("\tremoved %s\n", name);
32915719Sralph }
33012382Sralph
33112382Sralph /*
33212382Sralph * Enable queuing to the printer (allow lpr's).
33312382Sralph */
33455472Sbostic void
enable(argc,argv)33512382Sralph enable(argc, argv)
33655472Sbostic int argc;
33712382Sralph char *argv[];
33812382Sralph {
33912382Sralph register int c, status;
34012382Sralph register char *cp1, *cp2;
34112382Sralph char prbuf[100];
34212382Sralph
34312382Sralph if (argc == 1) {
34412382Sralph printf("Usage: enable {all | printer ...}\n");
34512382Sralph return;
34612382Sralph }
34712382Sralph if (argc == 2 && !strcmp(argv[1], "all")) {
34812382Sralph printer = prbuf;
34956121Selan while (cgetnext(&bp, printcapdb) > 0) {
35012382Sralph cp1 = prbuf;
35156121Selan cp2 = bp;
35212382Sralph while ((c = *cp2++) && c != '|' && c != ':')
35312382Sralph *cp1++ = c;
35412382Sralph *cp1 = '\0';
35512382Sralph enablepr();
35612382Sralph }
35712382Sralph return;
35812382Sralph }
35912382Sralph while (--argc) {
36012382Sralph printer = *++argv;
36156121Selan if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
36212514Sralph printf("cannot open printer description file\n");
36312382Sralph continue;
36456121Selan } else if (status == -1) {
36512514Sralph printf("unknown printer %s\n", printer);
36612382Sralph continue;
36756121Selan } else if (status == -3)
36856121Selan fatal("potential reference loop detected in printcap file");
36956121Selan
37012382Sralph enablepr();
37112382Sralph }
37212382Sralph }
37312382Sralph
37455472Sbostic static void
enablepr()37512382Sralph enablepr()
37612382Sralph {
37712382Sralph struct stat stbuf;
37812382Sralph
37956121Selan if (cgetstr(bp, "sd", &SD) == -1)
38037968Sbostic SD = _PATH_DEFSPOOL;
38156121Selan if (cgetstr(bp, "lo", &LO) == -1)
38212382Sralph LO = DEFLOCK;
38312382Sralph (void) sprintf(line, "%s/%s", SD, LO);
38412382Sralph printf("%s:\n", printer);
38512382Sralph
38612382Sralph /*
38712382Sralph * Turn off the group execute bit of the lock file to enable queuing.
38812382Sralph */
38912382Sralph if (stat(line, &stbuf) >= 0) {
39012382Sralph if (chmod(line, stbuf.st_mode & 0767) < 0)
39112514Sralph printf("\tcannot enable queuing\n");
39212382Sralph else
39312382Sralph printf("\tqueuing enabled\n");
39412382Sralph }
39512382Sralph }
39612382Sralph
39712382Sralph /*
39812382Sralph * Disable queuing.
39912382Sralph */
40055472Sbostic void
disable(argc,argv)40112382Sralph disable(argc, argv)
40255472Sbostic int argc;
40312382Sralph char *argv[];
40412382Sralph {
40512382Sralph register int c, status;
40612382Sralph register char *cp1, *cp2;
40712382Sralph char prbuf[100];
40812382Sralph
40912382Sralph if (argc == 1) {
41012382Sralph printf("Usage: disable {all | printer ...}\n");
41112382Sralph return;
41212382Sralph }
41312382Sralph if (argc == 2 && !strcmp(argv[1], "all")) {
41412382Sralph printer = prbuf;
41556121Selan while (cgetnext(&bp, printcapdb) > 0) {
41612382Sralph cp1 = prbuf;
41756121Selan cp2 = bp;
41812382Sralph while ((c = *cp2++) && c != '|' && c != ':')
41912382Sralph *cp1++ = c;
42012382Sralph *cp1 = '\0';
42112382Sralph disablepr();
42212382Sralph }
42312382Sralph return;
42412382Sralph }
42512382Sralph while (--argc) {
42612382Sralph printer = *++argv;
42756121Selan if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
42812514Sralph printf("cannot open printer description file\n");
42912382Sralph continue;
43056121Selan } else if (status == -1) {
43112514Sralph printf("unknown printer %s\n", printer);
43212382Sralph continue;
43356121Selan } else if (status == -3)
43456121Selan fatal("potential reference loop detected in printcap file");
43556121Selan
43612382Sralph disablepr();
43712382Sralph }
43812382Sralph }
43912382Sralph
44055472Sbostic static void
disablepr()44112382Sralph disablepr()
44212382Sralph {
44312382Sralph register int fd;
44412382Sralph struct stat stbuf;
44512382Sralph
44656121Selan if (cgetstr(bp, "sd", &SD) == -1)
44737968Sbostic SD = _PATH_DEFSPOOL;
44856121Selan if (cgetstr(bp, "lo", &LO) == -1)
44912382Sralph LO = DEFLOCK;
45012382Sralph (void) sprintf(line, "%s/%s", SD, LO);
45112382Sralph printf("%s:\n", printer);
45212382Sralph /*
45312382Sralph * Turn on the group execute bit of the lock file to disable queuing.
45412382Sralph */
45512382Sralph if (stat(line, &stbuf) >= 0) {
45612382Sralph if (chmod(line, (stbuf.st_mode & 0777) | 010) < 0)
45712382Sralph printf("\tcannot disable queuing\n");
45812382Sralph else
45912382Sralph printf("\tqueuing disabled\n");
46012382Sralph } else if (errno == ENOENT) {
46113146Ssam if ((fd = open(line, O_WRONLY|O_CREAT, 0670)) < 0)
46212382Sralph printf("\tcannot create lock file\n");
46312382Sralph else {
46412382Sralph (void) close(fd);
46512382Sralph printf("\tqueuing disabled\n");
46612382Sralph }
46712382Sralph return;
46812382Sralph } else
46912382Sralph printf("\tcannot stat lock file\n");
47012382Sralph }
47112382Sralph
47212382Sralph /*
47315907Sralph * Disable queuing and printing and put a message into the status file
47415907Sralph * (reason for being down).
47515907Sralph */
47655472Sbostic void
down(argc,argv)47715907Sralph down(argc, argv)
47855472Sbostic int argc;
47915907Sralph char *argv[];
48015907Sralph {
48115907Sralph register int c, status;
48215907Sralph register char *cp1, *cp2;
48315907Sralph char prbuf[100];
48415907Sralph
48515907Sralph if (argc == 1) {
48616204Sralph printf("Usage: down {all | printer} [message ...]\n");
48715907Sralph return;
48815907Sralph }
48915907Sralph if (!strcmp(argv[1], "all")) {
49015907Sralph printer = prbuf;
49156121Selan while (cgetnext(&bp, printcapdb) > 0) {
49215907Sralph cp1 = prbuf;
49356121Selan cp2 = bp;
49415907Sralph while ((c = *cp2++) && c != '|' && c != ':')
49515907Sralph *cp1++ = c;
49615907Sralph *cp1 = '\0';
49715907Sralph putmsg(argc - 2, argv + 2);
49815907Sralph }
49915907Sralph return;
50015907Sralph }
50115907Sralph printer = argv[1];
50256121Selan if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
50315907Sralph printf("cannot open printer description file\n");
50415907Sralph return;
50556121Selan } else if (status == -1) {
50615907Sralph printf("unknown printer %s\n", printer);
50715907Sralph return;
50856121Selan } else if (status == -3)
50956121Selan fatal("potential reference loop detected in printcap file");
51056121Selan
51115907Sralph putmsg(argc - 2, argv + 2);
51215907Sralph }
51315907Sralph
51455472Sbostic static void
putmsg(argc,argv)51515907Sralph putmsg(argc, argv)
51655472Sbostic int argc;
51715907Sralph char **argv;
51815907Sralph {
51915907Sralph register int fd;
52015907Sralph register char *cp1, *cp2;
52115907Sralph char buf[1024];
52215907Sralph struct stat stbuf;
52315907Sralph
52456121Selan if (cgetstr(bp, "sd", &SD) == -1)
52537968Sbostic SD = _PATH_DEFSPOOL;
52656121Selan if (cgetstr(bp, "lo", &LO) == -1)
52715907Sralph LO = DEFLOCK;
52856121Selan if (cgetstr(bp, "st", &ST) == -1)
52915907Sralph ST = DEFSTAT;
53015907Sralph printf("%s:\n", printer);
53115907Sralph /*
53215907Sralph * Turn on the group execute bit of the lock file to disable queuing and
53315907Sralph * turn on the owner execute bit of the lock file to disable printing.
53415907Sralph */
53515907Sralph (void) sprintf(line, "%s/%s", SD, LO);
53615907Sralph if (stat(line, &stbuf) >= 0) {
53715907Sralph if (chmod(line, (stbuf.st_mode & 0777) | 0110) < 0)
53815907Sralph printf("\tcannot disable queuing\n");
53915907Sralph else
54015907Sralph printf("\tprinter and queuing disabled\n");
54115907Sralph } else if (errno == ENOENT) {
54215907Sralph if ((fd = open(line, O_WRONLY|O_CREAT, 0770)) < 0)
54315907Sralph printf("\tcannot create lock file\n");
54415907Sralph else {
54515907Sralph (void) close(fd);
54615907Sralph printf("\tprinter and queuing disabled\n");
54715907Sralph }
54815907Sralph return;
54915907Sralph } else
55015907Sralph printf("\tcannot stat lock file\n");
55115907Sralph /*
55215907Sralph * Write the message into the status file.
55315907Sralph */
55415907Sralph (void) sprintf(line, "%s/%s", SD, ST);
55515907Sralph fd = open(line, O_WRONLY|O_CREAT, 0664);
55615907Sralph if (fd < 0 || flock(fd, LOCK_EX) < 0) {
55715907Sralph printf("\tcannot create status file\n");
55815907Sralph return;
55915907Sralph }
56015907Sralph (void) ftruncate(fd, 0);
56116204Sralph if (argc <= 0) {
56216204Sralph (void) write(fd, "\n", 1);
56316204Sralph (void) close(fd);
56416204Sralph return;
56516204Sralph }
56615907Sralph cp1 = buf;
56715907Sralph while (--argc >= 0) {
56815907Sralph cp2 = *argv++;
56915907Sralph while (*cp1++ = *cp2++)
57015907Sralph ;
57115907Sralph cp1[-1] = ' ';
57215907Sralph }
57315907Sralph cp1[-1] = '\n';
57415907Sralph *cp1 = '\0';
57515907Sralph (void) write(fd, buf, strlen(buf));
57615907Sralph (void) close(fd);
57715907Sralph }
57815907Sralph
57915907Sralph /*
58012382Sralph * Exit lpc
58112382Sralph */
58255472Sbostic void
quit(argc,argv)58312382Sralph quit(argc, argv)
58455472Sbostic int argc;
58512382Sralph char *argv[];
58612382Sralph {
58712382Sralph exit(0);
58812382Sralph }
58912382Sralph
59012382Sralph /*
59116755Sralph * Kill and restart the daemon.
59212382Sralph */
59355472Sbostic void
restart(argc,argv)59412382Sralph restart(argc, argv)
59555472Sbostic int argc;
59612382Sralph char *argv[];
59712382Sralph {
59812382Sralph register int c, status;
59912382Sralph register char *cp1, *cp2;
60012382Sralph char prbuf[100];
60112382Sralph
60212382Sralph if (argc == 1) {
60312382Sralph printf("Usage: restart {all | printer ...}\n");
60412382Sralph return;
60512382Sralph }
60612382Sralph if (argc == 2 && !strcmp(argv[1], "all")) {
60712382Sralph printer = prbuf;
60856121Selan while (cgetnext(&bp, printcapdb) > 0) {
60912382Sralph cp1 = prbuf;
61056121Selan cp2 = bp;
61112382Sralph while ((c = *cp2++) && c != '|' && c != ':')
61212382Sralph *cp1++ = c;
61312382Sralph *cp1 = '\0';
61416755Sralph abortpr(0);
61512382Sralph startpr(0);
61612382Sralph }
61712382Sralph return;
61812382Sralph }
61912382Sralph while (--argc) {
62012382Sralph printer = *++argv;
62156121Selan if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
62212514Sralph printf("cannot open printer description file\n");
62312382Sralph continue;
62456121Selan } else if (status == -1) {
62512514Sralph printf("unknown printer %s\n", printer);
62612382Sralph continue;
62756121Selan } else if (status == -3)
62856121Selan fatal("potential reference loop detected in printcap file");
62956121Selan
63016755Sralph abortpr(0);
63112382Sralph startpr(0);
63212382Sralph }
63312382Sralph }
63412382Sralph
63512382Sralph /*
63612382Sralph * Enable printing on the specified printer and startup the daemon.
63712382Sralph */
63855472Sbostic void
startcmd(argc,argv)63958985Sralph startcmd(argc, argv)
64055472Sbostic int argc;
64112382Sralph char *argv[];
64212382Sralph {
64312382Sralph register int c, status;
64412382Sralph register char *cp1, *cp2;
64512382Sralph char prbuf[100];
64612382Sralph
64712382Sralph if (argc == 1) {
64812382Sralph printf("Usage: start {all | printer ...}\n");
64912382Sralph return;
65012382Sralph }
65112382Sralph if (argc == 2 && !strcmp(argv[1], "all")) {
65212382Sralph printer = prbuf;
65356121Selan while (cgetnext(&bp, printcapdb) > 0) {
65412382Sralph cp1 = prbuf;
65556121Selan cp2 = bp;
65612382Sralph while ((c = *cp2++) && c != '|' && c != ':')
65712382Sralph *cp1++ = c;
65812382Sralph *cp1 = '\0';
65912382Sralph startpr(1);
66012382Sralph }
66112382Sralph return;
66212382Sralph }
66312382Sralph while (--argc) {
66412382Sralph printer = *++argv;
66556121Selan if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
66612514Sralph printf("cannot open printer description file\n");
66712382Sralph continue;
66856121Selan } else if (status == -1) {
66912514Sralph printf("unknown printer %s\n", printer);
67012382Sralph continue;
67156121Selan } else if (status == -3)
67256121Selan fatal("potential reference loop detected in printcap file");
67356121Selan
67412382Sralph startpr(1);
67512382Sralph }
67612382Sralph }
67712382Sralph
67855472Sbostic static void
startpr(enable)67912382Sralph startpr(enable)
68055472Sbostic int enable;
68112382Sralph {
68212382Sralph struct stat stbuf;
68312382Sralph
68456121Selan if (cgetstr(bp, "sd", &SD) == -1)
68537968Sbostic SD = _PATH_DEFSPOOL;
68656121Selan if (cgetstr(bp, "lo", &LO) == -1)
68712382Sralph LO = DEFLOCK;
68812382Sralph (void) sprintf(line, "%s/%s", SD, LO);
68912382Sralph printf("%s:\n", printer);
69012382Sralph
69112382Sralph /*
69212382Sralph * Turn off the owner execute bit of the lock file to enable printing.
69312382Sralph */
69412382Sralph if (enable && stat(line, &stbuf) >= 0) {
69516771Sralph if (chmod(line, stbuf.st_mode & (enable==2 ? 0666 : 0677)) < 0)
69612382Sralph printf("\tcannot enable printing\n");
69712382Sralph else
69812382Sralph printf("\tprinting enabled\n");
69912382Sralph }
70013727Sroot if (!startdaemon(printer))
70112382Sralph printf("\tcouldn't start daemon\n");
70212382Sralph else
70312382Sralph printf("\tdaemon started\n");
70412382Sralph }
70512382Sralph
70612382Sralph /*
70712382Sralph * Print the status of each queue listed or all the queues.
70812382Sralph */
70955472Sbostic void
status(argc,argv)71012382Sralph status(argc, argv)
71155472Sbostic int argc;
71212382Sralph char *argv[];
71312382Sralph {
71412382Sralph register int c, status;
71512382Sralph register char *cp1, *cp2;
71612382Sralph char prbuf[100];
71712382Sralph
71812382Sralph if (argc == 1) {
71912382Sralph printer = prbuf;
72056121Selan while (cgetnext(&bp, printcapdb) > 0) {
72112382Sralph cp1 = prbuf;
72256121Selan cp2 = bp;
72312382Sralph while ((c = *cp2++) && c != '|' && c != ':')
72412382Sralph *cp1++ = c;
72512382Sralph *cp1 = '\0';
72612382Sralph prstat();
72712382Sralph }
72812382Sralph return;
72912382Sralph }
73012382Sralph while (--argc) {
73112382Sralph printer = *++argv;
73256121Selan if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
73312514Sralph printf("cannot open printer description file\n");
73412382Sralph continue;
73556121Selan } else if (status == -1) {
73612514Sralph printf("unknown printer %s\n", printer);
73712382Sralph continue;
73856121Selan } else if (status == -3)
73956121Selan fatal("potential reference loop detected in printcap file");
74056121Selan
74112382Sralph prstat();
74212382Sralph }
74312382Sralph }
74412382Sralph
74512382Sralph /*
74612382Sralph * Print the status of the printer queue.
74712382Sralph */
74855472Sbostic static void
prstat()74912382Sralph prstat()
75012382Sralph {
75112382Sralph struct stat stbuf;
75212382Sralph register int fd, i;
75355472Sbostic register struct dirent *dp;
75412382Sralph DIR *dirp;
75512382Sralph
75656121Selan if (cgetstr(bp, "sd", &SD) == -1)
75737968Sbostic SD = _PATH_DEFSPOOL;
75856121Selan if (cgetstr(bp, "lo", &LO) == -1)
75912382Sralph LO = DEFLOCK;
76056121Selan if (cgetstr(bp, "st", &ST) == -1)
76112382Sralph ST = DEFSTAT;
76212382Sralph printf("%s:\n", printer);
76312382Sralph (void) sprintf(line, "%s/%s", SD, LO);
76412382Sralph if (stat(line, &stbuf) >= 0) {
76512382Sralph printf("\tqueuing is %s\n",
76612382Sralph (stbuf.st_mode & 010) ? "disabled" : "enabled");
76712382Sralph printf("\tprinting is %s\n",
76812382Sralph (stbuf.st_mode & 0100) ? "disabled" : "enabled");
76912382Sralph } else {
77012382Sralph printf("\tqueuing is enabled\n");
77112382Sralph printf("\tprinting is enabled\n");
77212382Sralph }
77312382Sralph if ((dirp = opendir(SD)) == NULL) {
77412382Sralph printf("\tcannot examine spool directory\n");
77512382Sralph return;
77612382Sralph }
77712382Sralph i = 0;
77812382Sralph while ((dp = readdir(dirp)) != NULL) {
77912382Sralph if (*dp->d_name == 'c' && dp->d_name[1] == 'f')
78012382Sralph i++;
78112382Sralph }
78212382Sralph closedir(dirp);
78312382Sralph if (i == 0)
78412382Sralph printf("\tno entries\n");
78512382Sralph else if (i == 1)
78612382Sralph printf("\t1 entry in spool area\n");
78712382Sralph else
78812382Sralph printf("\t%d entries in spool area\n", i);
78913146Ssam fd = open(line, O_RDONLY);
79013146Ssam if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
79113168Sralph (void) close(fd); /* unlocks as well */
79212382Sralph printf("\tno daemon present\n");
79312382Sralph return;
79412382Sralph }
79512382Sralph (void) close(fd);
79612382Sralph putchar('\t');
79712382Sralph (void) sprintf(line, "%s/%s", SD, ST);
79813146Ssam fd = open(line, O_RDONLY);
79913146Ssam if (fd >= 0) {
80013146Ssam (void) flock(fd, LOCK_SH);
80112382Sralph while ((i = read(fd, line, sizeof(line))) > 0)
80212382Sralph (void) fwrite(line, 1, i, stdout);
80313168Sralph (void) close(fd); /* unlocks as well */
80412382Sralph }
80512382Sralph }
80612382Sralph
80712382Sralph /*
80812382Sralph * Stop the specified daemon after completing the current job and disable
80912382Sralph * printing.
81012382Sralph */
81155472Sbostic void
stop(argc,argv)81212382Sralph stop(argc, argv)
81355472Sbostic int argc;
81412382Sralph char *argv[];
81512382Sralph {
81612382Sralph register int c, status;
81712382Sralph register char *cp1, *cp2;
81812382Sralph char prbuf[100];
81912382Sralph
82012382Sralph if (argc == 1) {
82112382Sralph printf("Usage: stop {all | printer ...}\n");
82212382Sralph return;
82312382Sralph }
82412382Sralph if (argc == 2 && !strcmp(argv[1], "all")) {
82512382Sralph printer = prbuf;
82656121Selan while (cgetnext(&bp, printcapdb) > 0) {
82712382Sralph cp1 = prbuf;
82856121Selan cp2 = bp;
82912382Sralph while ((c = *cp2++) && c != '|' && c != ':')
83012382Sralph *cp1++ = c;
83112382Sralph *cp1 = '\0';
83212382Sralph stoppr();
83312382Sralph }
83412382Sralph return;
83512382Sralph }
83612382Sralph while (--argc) {
83712382Sralph printer = *++argv;
83856121Selan if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
83912514Sralph printf("cannot open printer description file\n");
84012382Sralph continue;
84156121Selan } else if (status == -1) {
84212514Sralph printf("unknown printer %s\n", printer);
84312382Sralph continue;
84456121Selan } else if (status == -3)
84556121Selan fatal("potential reference loop detected in printcap file");
84656121Selan
84712382Sralph stoppr();
84812382Sralph }
84912382Sralph }
85012382Sralph
85155472Sbostic static void
stoppr()85212382Sralph stoppr()
85312382Sralph {
85412382Sralph register int fd;
85512382Sralph struct stat stbuf;
85612382Sralph
85756121Selan if (cgetstr(bp, "sd", &SD) == -1)
85837968Sbostic SD = _PATH_DEFSPOOL;
85956121Selan if (cgetstr(bp, "lo", &LO) == -1)
86012382Sralph LO = DEFLOCK;
86112382Sralph (void) sprintf(line, "%s/%s", SD, LO);
86212382Sralph printf("%s:\n", printer);
86312382Sralph
86412382Sralph /*
86512382Sralph * Turn on the owner execute bit of the lock file to disable printing.
86612382Sralph */
86712382Sralph if (stat(line, &stbuf) >= 0) {
86812382Sralph if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
86912382Sralph printf("\tcannot disable printing\n");
87038735Stef else {
87138735Stef upstat("printing disabled\n");
87212382Sralph printf("\tprinting disabled\n");
87338735Stef }
87412382Sralph } else if (errno == ENOENT) {
87513146Ssam if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
87612382Sralph printf("\tcannot create lock file\n");
87712382Sralph else {
87812382Sralph (void) close(fd);
87938735Stef upstat("printing disabled\n");
88012382Sralph printf("\tprinting disabled\n");
88112382Sralph }
88212382Sralph } else
88312382Sralph printf("\tcannot stat lock file\n");
88412382Sralph }
88513727Sroot
88615719Sralph struct queue **queue;
88715719Sralph int nitems;
88815719Sralph time_t mtime;
88915719Sralph
89013727Sroot /*
89113727Sroot * Put the specified jobs at the top of printer queue.
89213727Sroot */
89355472Sbostic void
topq(argc,argv)89413727Sroot topq(argc, argv)
89555472Sbostic int argc;
89613727Sroot char *argv[];
89713727Sroot {
89855472Sbostic register int i;
89913727Sroot struct stat stbuf;
90015719Sralph int status, changed;
90113727Sroot
90215719Sralph if (argc < 3) {
90313727Sroot printf("Usage: topq printer [jobnum ...] [user ...]\n");
90413727Sroot return;
90513727Sroot }
90613727Sroot
90713727Sroot --argc;
90813727Sroot printer = *++argv;
90956121Selan status = cgetent(&bp, printcapdb, printer);
91056121Selan if (status == -2) {
91113727Sroot printf("cannot open printer description file\n");
91213727Sroot return;
91356121Selan } else if (status == -1) {
91413727Sroot printf("%s: unknown printer\n", printer);
91513727Sroot return;
91656121Selan } else if (status == -3)
91756121Selan fatal("potential reference loop detected in printcap file");
91856121Selan
91956121Selan if (cgetstr(bp, "sd", &SD) == -1)
92037968Sbostic SD = _PATH_DEFSPOOL;
92156121Selan if (cgetstr(bp, "lo", &LO) == -1)
92213727Sroot LO = DEFLOCK;
92313727Sroot printf("%s:\n", printer);
92413727Sroot
92513727Sroot if (chdir(SD) < 0) {
92613727Sroot printf("\tcannot chdir to %s\n", SD);
92713727Sroot return;
92813727Sroot }
92913727Sroot nitems = getq(&queue);
93015719Sralph if (nitems == 0)
93115719Sralph return;
93215719Sralph changed = 0;
93315719Sralph mtime = queue[0]->q_time;
93415719Sralph for (i = argc; --i; ) {
93515719Sralph if (doarg(argv[i]) == 0) {
93615719Sralph printf("\tjob %s is not in the queue\n", argv[i]);
93713727Sroot continue;
93815719Sralph } else
93914151Sralph changed++;
94013727Sroot }
94115719Sralph for (i = 0; i < nitems; i++)
94215719Sralph free(queue[i]);
94315719Sralph free(queue);
94415719Sralph if (!changed) {
94515719Sralph printf("\tqueue order unchanged\n");
94615719Sralph return;
94713727Sroot }
94813727Sroot /*
94913727Sroot * Turn on the public execute bit of the lock file to
95013727Sroot * get lpd to rebuild the queue after the current job.
95113727Sroot */
95214151Sralph if (changed && stat(LO, &stbuf) >= 0)
95314151Sralph (void) chmod(LO, (stbuf.st_mode & 0777) | 01);
95413727Sroot }
95513727Sroot
95615719Sralph /*
95715719Sralph * Reposition the job by changing the modification time of
95815719Sralph * the control file.
95913727Sroot */
96055472Sbostic static int
touch(q)96115719Sralph touch(q)
96215719Sralph struct queue *q;
96313727Sroot {
96415719Sralph struct timeval tvp[2];
96513727Sroot
96615719Sralph tvp[0].tv_sec = tvp[1].tv_sec = --mtime;
96715719Sralph tvp[0].tv_usec = tvp[1].tv_usec = 0;
96815719Sralph return(utimes(q->q_name, tvp));
96913727Sroot }
97013727Sroot
97113727Sroot /*
97213727Sroot * Checks if specified job name is in the printer's queue.
97313727Sroot * Returns: negative (-1) if argument name is not in the queue.
97413727Sroot */
97555472Sbostic static int
doarg(job)97615719Sralph doarg(job)
97713727Sroot char *job;
97813727Sroot {
97915719Sralph register struct queue **qq;
98015719Sralph register int jobnum, n;
98115719Sralph register char *cp, *machine;
98215719Sralph int cnt = 0;
98314151Sralph FILE *fp;
98413727Sroot
98515719Sralph /*
98615719Sralph * Look for a job item consisting of system name, colon, number
98715719Sralph * (example: ucbarpa:114)
98815719Sralph */
98915719Sralph if ((cp = index(job, ':')) != NULL) {
99015719Sralph machine = job;
99115719Sralph *cp++ = '\0';
99215719Sralph job = cp;
99315719Sralph } else
99415719Sralph machine = NULL;
99515719Sralph
99615719Sralph /*
99715719Sralph * Check for job specified by number (example: 112 or 235ucbarpa).
99815719Sralph */
99913727Sroot if (isdigit(*job)) {
100013727Sroot jobnum = 0;
100113727Sroot do
100213727Sroot jobnum = jobnum * 10 + (*job++ - '0');
100313727Sroot while (isdigit(*job));
100415719Sralph for (qq = queue + nitems; --qq >= queue; ) {
100513727Sroot n = 0;
100615719Sralph for (cp = (*qq)->q_name+3; isdigit(*cp); )
100714151Sralph n = n * 10 + (*cp++ - '0');
100815719Sralph if (jobnum != n)
100915719Sralph continue;
101015719Sralph if (*job && strcmp(job, cp) != 0)
101115719Sralph continue;
101215719Sralph if (machine != NULL && strcmp(machine, cp) != 0)
101315719Sralph continue;
101415719Sralph if (touch(*qq) == 0) {
101515719Sralph printf("\tmoved %s\n", (*qq)->q_name);
101615719Sralph cnt++;
101715719Sralph }
101813727Sroot }
101915719Sralph return(cnt);
102015719Sralph }
102115719Sralph /*
102215719Sralph * Process item consisting of owner's name (example: henry).
102315719Sralph */
102415719Sralph for (qq = queue + nitems; --qq >= queue; ) {
102515719Sralph if ((fp = fopen((*qq)->q_name, "r")) == NULL)
102613727Sroot continue;
102715719Sralph while (getline(fp) > 0)
102815719Sralph if (line[0] == 'P')
102915719Sralph break;
103015719Sralph (void) fclose(fp);
103115719Sralph if (line[0] != 'P' || strcmp(job, line+1) != 0)
103215719Sralph continue;
103315719Sralph if (touch(*qq) == 0) {
103415719Sralph printf("\tmoved %s\n", (*qq)->q_name);
103515719Sralph cnt++;
103613727Sroot }
103713727Sroot }
103815719Sralph return(cnt);
103913727Sroot }
104016755Sralph
104116755Sralph /*
104216755Sralph * Enable everything and start printer (undo `down').
104316755Sralph */
104455472Sbostic void
up(argc,argv)104516755Sralph up(argc, argv)
104655472Sbostic int argc;
104716755Sralph char *argv[];
104816755Sralph {
104916755Sralph register int c, status;
105016755Sralph register char *cp1, *cp2;
105116755Sralph char prbuf[100];
105216755Sralph
105316755Sralph if (argc == 1) {
105416755Sralph printf("Usage: up {all | printer ...}\n");
105516755Sralph return;
105616755Sralph }
105716755Sralph if (argc == 2 && !strcmp(argv[1], "all")) {
105816755Sralph printer = prbuf;
105956121Selan while (cgetnext(&bp, printcapdb) > 0) {
106016755Sralph cp1 = prbuf;
106156121Selan cp2 = bp;
106216755Sralph while ((c = *cp2++) && c != '|' && c != ':')
106316755Sralph *cp1++ = c;
106416755Sralph *cp1 = '\0';
106516755Sralph startpr(2);
106616755Sralph }
106716755Sralph return;
106816755Sralph }
106916755Sralph while (--argc) {
107016755Sralph printer = *++argv;
107156121Selan if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
107216755Sralph printf("cannot open printer description file\n");
107316755Sralph continue;
107456121Selan } else if (status == -1) {
107516755Sralph printf("unknown printer %s\n", printer);
107616755Sralph continue;
107756121Selan } else if (status == -3)
107856121Selan fatal("potential reference loop detected in printcap file");
107956121Selan
108016755Sralph startpr(2);
108116755Sralph }
108216755Sralph }
1083