112382Sralph #ifndef lint 2*12514Sralph static char sccsid[] = "@(#)cmds.c 4.2 (Berkeley) 05/18/83"; 312382Sralph #endif 412382Sralph 512382Sralph /* 612382Sralph * lpc -- line printer control program 712382Sralph */ 812382Sralph 912382Sralph #include "lp.h" 1012382Sralph 1112382Sralph /* 1212382Sralph * kill an existing daemon and disable printing. 1312382Sralph */ 1412382Sralph abort(argc, argv) 1512382Sralph char *argv[]; 1612382Sralph { 1712382Sralph register int c, status; 1812382Sralph register char *cp1, *cp2; 1912382Sralph char prbuf[100]; 2012382Sralph 2112382Sralph if (argc == 1) { 2212382Sralph printf("Usage: abort {all | printer ...}\n"); 2312382Sralph return; 2412382Sralph } 2512382Sralph if (argc == 2 && !strcmp(argv[1], "all")) { 2612382Sralph printer = prbuf; 2712382Sralph while (getprent(line) > 0) { 2812382Sralph cp1 = prbuf; 2912382Sralph cp2 = line; 3012382Sralph while ((c = *cp2++) && c != '|' && c != ':') 3112382Sralph *cp1++ = c; 3212382Sralph *cp1 = '\0'; 3312382Sralph abortpr(); 3412382Sralph } 3512382Sralph return; 3612382Sralph } 3712382Sralph while (--argc) { 3812382Sralph printer = *++argv; 3912382Sralph if ((status = pgetent(line, printer)) < 0) { 40*12514Sralph printf("cannot open printer description file\n"); 4112382Sralph continue; 4212382Sralph } else if (status == 0) { 43*12514Sralph printf("unknown printer %s\n", printer); 4412382Sralph continue; 4512382Sralph } 4612382Sralph abortpr(); 4712382Sralph } 4812382Sralph } 4912382Sralph 5012382Sralph abortpr() 5112382Sralph { 5212382Sralph register FILE *fp; 5312382Sralph struct stat stbuf; 5412382Sralph int pid, fd; 5512382Sralph 5612382Sralph bp = pbuf; 5712382Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 5812382Sralph SD = DEFSPOOL; 5912382Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 6012382Sralph LO = DEFLOCK; 6112382Sralph (void) sprintf(line, "%s/%s", SD, LO); 6212382Sralph printf("%s:\n", printer); 6312382Sralph 6412382Sralph /* 6512382Sralph * Turn on the owner execute bit of the lock file to disable printing. 6612382Sralph */ 6712382Sralph if (stat(line, &stbuf) >= 0) { 6812382Sralph if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0) 6912382Sralph printf("\tcannot disable printing\n"); 7012382Sralph else 7112382Sralph printf("\tprinting disabled\n"); 7212382Sralph } else if (errno == ENOENT) { 7312382Sralph if ((fd = open(line, FWRONLY|FCREATE, 0760)) < 0) 74*12514Sralph printf("\tcannot create lock file\n"); 7512382Sralph else { 7612382Sralph (void) close(fd); 7712382Sralph printf("\tprinting disabled\n"); 7812382Sralph printf("\tno daemon to abort\n"); 7912382Sralph } 8012382Sralph return; 8112382Sralph } else { 8212382Sralph printf("\tcannot stat lock file\n"); 8312382Sralph return; 8412382Sralph } 8512382Sralph /* 8612382Sralph * Kill the current daemon to stop printing now. 8712382Sralph */ 8812382Sralph if ((fp = fopen(line, "r")) == NULL) { 8912382Sralph printf("\tcannot open lock file\n"); 9012382Sralph return; 9112382Sralph } 9212382Sralph if (!getline(fp) || flock(fileno(fp), FSHLOCK|FNBLOCK) == 0) { 9312382Sralph (void) fclose(fp); 9412382Sralph printf("\tno daemon to abort\n"); 9512382Sralph return; 9612382Sralph } 9712382Sralph (void) fclose(fp); 9812382Sralph if (kill(pid = atoi(line), SIGINT) < 0) 9912382Sralph printf("\tWarning: daemon (pid %d) not killed\n", pid); 10012382Sralph else 10112382Sralph printf("\tdaemon (pid %d) killed\n", pid); 10212382Sralph } 10312382Sralph 10412382Sralph /* 10512382Sralph * Remove all spool files and temporaries from the spooling area. 10612382Sralph */ 10712382Sralph clean(argc, argv) 10812382Sralph char *argv[]; 10912382Sralph { 11012382Sralph register int c, status; 11112382Sralph register char *cp1, *cp2; 11212382Sralph char prbuf[100]; 11312382Sralph 11412382Sralph if (argc == 1) { 11512382Sralph printf("Usage: clean {all | printer ...}\n"); 11612382Sralph return; 11712382Sralph } 11812382Sralph if (argc == 2 && !strcmp(argv[1], "all")) { 11912382Sralph printer = prbuf; 12012382Sralph while (getprent(line) > 0) { 12112382Sralph cp1 = prbuf; 12212382Sralph cp2 = line; 12312382Sralph while ((c = *cp2++) && c != '|' && c != ':') 12412382Sralph *cp1++ = c; 12512382Sralph *cp1 = '\0'; 12612382Sralph cleanpr(); 12712382Sralph } 12812382Sralph return; 12912382Sralph } 13012382Sralph while (--argc) { 13112382Sralph printer = *++argv; 13212382Sralph if ((status = pgetent(line, printer)) < 0) { 133*12514Sralph printf("cannot open printer description file\n"); 13412382Sralph continue; 13512382Sralph } else if (status == 0) { 136*12514Sralph printf("unknown printer %s\n", printer); 13712382Sralph continue; 13812382Sralph } 13912382Sralph cleanpr(); 14012382Sralph } 14112382Sralph } 14212382Sralph 14312382Sralph cleanpr() 14412382Sralph { 14512382Sralph register int c; 14612382Sralph register DIR *dirp; 14712382Sralph register struct direct *dp; 14812382Sralph char *cp, *cp1; 14912382Sralph 15012382Sralph bp = pbuf; 15112382Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 15212382Sralph SD = DEFSPOOL; 15312382Sralph for (cp = line, cp1 = SD; *cp++ = *cp1++; ); 15412382Sralph cp[-1] = '/'; 15512382Sralph printf("%s:\n", printer); 15612382Sralph 15712382Sralph if ((dirp = opendir(SD)) == NULL) { 15812382Sralph printf("\tcannot examine spool directory\n"); 15912382Sralph return; 16012382Sralph } 16112382Sralph while ((dp = readdir(dirp)) != NULL) { 16212382Sralph c = dp->d_name[0]; 16312382Sralph if ((c == 'c' || c == 't' || c == 'd') && dp->d_name[1]=='f') { 16412382Sralph strcpy(cp, dp->d_name); 16512382Sralph if (unlink(line) < 0) 16612382Sralph printf("\tcannot remove %s\n", line); 16712382Sralph else 16812382Sralph printf("\tremoved %s\n", line); 16912382Sralph } 17012382Sralph } 17112382Sralph closedir(dirp); 17212382Sralph } 17312382Sralph 17412382Sralph /* 17512382Sralph * Enable queuing to the printer (allow lpr's). 17612382Sralph */ 17712382Sralph enable(argc, argv) 17812382Sralph char *argv[]; 17912382Sralph { 18012382Sralph register int c, status; 18112382Sralph register char *cp1, *cp2; 18212382Sralph char prbuf[100]; 18312382Sralph 18412382Sralph if (argc == 1) { 18512382Sralph printf("Usage: enable {all | printer ...}\n"); 18612382Sralph return; 18712382Sralph } 18812382Sralph if (argc == 2 && !strcmp(argv[1], "all")) { 18912382Sralph printer = prbuf; 19012382Sralph while (getprent(line) > 0) { 19112382Sralph cp1 = prbuf; 19212382Sralph cp2 = line; 19312382Sralph while ((c = *cp2++) && c != '|' && c != ':') 19412382Sralph *cp1++ = c; 19512382Sralph *cp1 = '\0'; 19612382Sralph enablepr(); 19712382Sralph } 19812382Sralph return; 19912382Sralph } 20012382Sralph while (--argc) { 20112382Sralph printer = *++argv; 20212382Sralph if ((status = pgetent(line, printer)) < 0) { 203*12514Sralph printf("cannot open printer description file\n"); 20412382Sralph continue; 20512382Sralph } else if (status == 0) { 206*12514Sralph printf("unknown printer %s\n", printer); 20712382Sralph continue; 20812382Sralph } 20912382Sralph enablepr(); 21012382Sralph } 21112382Sralph } 21212382Sralph 21312382Sralph enablepr() 21412382Sralph { 21512382Sralph struct stat stbuf; 21612382Sralph 21712382Sralph bp = pbuf; 21812382Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 21912382Sralph SD = DEFSPOOL; 22012382Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 22112382Sralph LO = DEFLOCK; 22212382Sralph (void) sprintf(line, "%s/%s", SD, LO); 22312382Sralph printf("%s:\n", printer); 22412382Sralph 22512382Sralph /* 22612382Sralph * Turn off the group execute bit of the lock file to enable queuing. 22712382Sralph */ 22812382Sralph if (stat(line, &stbuf) >= 0) { 22912382Sralph if (chmod(line, stbuf.st_mode & 0767) < 0) 230*12514Sralph printf("\tcannot enable queuing\n"); 23112382Sralph else 23212382Sralph printf("\tqueuing enabled\n"); 23312382Sralph } 23412382Sralph } 23512382Sralph 23612382Sralph /* 23712382Sralph * Disable queuing. 23812382Sralph */ 23912382Sralph disable(argc, argv) 24012382Sralph char *argv[]; 24112382Sralph { 24212382Sralph register int c, status; 24312382Sralph register char *cp1, *cp2; 24412382Sralph char prbuf[100]; 24512382Sralph 24612382Sralph if (argc == 1) { 24712382Sralph printf("Usage: disable {all | printer ...}\n"); 24812382Sralph return; 24912382Sralph } 25012382Sralph if (argc == 2 && !strcmp(argv[1], "all")) { 25112382Sralph printer = prbuf; 25212382Sralph while (getprent(line) > 0) { 25312382Sralph cp1 = prbuf; 25412382Sralph cp2 = line; 25512382Sralph while ((c = *cp2++) && c != '|' && c != ':') 25612382Sralph *cp1++ = c; 25712382Sralph *cp1 = '\0'; 25812382Sralph disablepr(); 25912382Sralph } 26012382Sralph return; 26112382Sralph } 26212382Sralph while (--argc) { 26312382Sralph printer = *++argv; 26412382Sralph if ((status = pgetent(line, printer)) < 0) { 265*12514Sralph printf("cannot open printer description file\n"); 26612382Sralph continue; 26712382Sralph } else if (status == 0) { 268*12514Sralph printf("unknown printer %s\n", printer); 26912382Sralph continue; 27012382Sralph } 27112382Sralph disablepr(); 27212382Sralph } 27312382Sralph } 27412382Sralph 27512382Sralph disablepr() 27612382Sralph { 27712382Sralph register int fd; 27812382Sralph struct stat stbuf; 27912382Sralph 28012382Sralph bp = pbuf; 28112382Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 28212382Sralph SD = DEFSPOOL; 28312382Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 28412382Sralph LO = DEFLOCK; 28512382Sralph (void) sprintf(line, "%s/%s", SD, LO); 28612382Sralph printf("%s:\n", printer); 28712382Sralph /* 28812382Sralph * Turn on the group execute bit of the lock file to disable queuing. 28912382Sralph */ 29012382Sralph if (stat(line, &stbuf) >= 0) { 29112382Sralph if (chmod(line, (stbuf.st_mode & 0777) | 010) < 0) 29212382Sralph printf("\tcannot disable queuing\n"); 29312382Sralph else 29412382Sralph printf("\tqueuing disabled\n"); 29512382Sralph } else if (errno == ENOENT) { 29612382Sralph if ((fd = open(line, FWRONLY|FCREATE, 0670)) < 0) 29712382Sralph printf("\tcannot create lock file\n"); 29812382Sralph else { 29912382Sralph (void) close(fd); 30012382Sralph printf("\tqueuing disabled\n"); 30112382Sralph } 30212382Sralph return; 30312382Sralph } else 30412382Sralph printf("\tcannot stat lock file\n"); 30512382Sralph } 30612382Sralph 30712382Sralph /* 30812382Sralph * Exit lpc 30912382Sralph */ 31012382Sralph quit(argc, argv) 31112382Sralph char *argv[]; 31212382Sralph { 31312382Sralph exit(0); 31412382Sralph } 31512382Sralph 31612382Sralph /* 31712382Sralph * Startup the daemon. 31812382Sralph */ 31912382Sralph restart(argc, argv) 32012382Sralph char *argv[]; 32112382Sralph { 32212382Sralph register int c, status; 32312382Sralph register char *cp1, *cp2; 32412382Sralph char prbuf[100]; 32512382Sralph 32612382Sralph if (argc == 1) { 32712382Sralph printf("Usage: restart {all | printer ...}\n"); 32812382Sralph return; 32912382Sralph } 33012382Sralph gethostname(host, sizeof(host)); 33112382Sralph if (argc == 2 && !strcmp(argv[1], "all")) { 33212382Sralph printer = prbuf; 33312382Sralph while (getprent(line) > 0) { 33412382Sralph cp1 = prbuf; 33512382Sralph cp2 = line; 33612382Sralph while ((c = *cp2++) && c != '|' && c != ':') 33712382Sralph *cp1++ = c; 33812382Sralph *cp1 = '\0'; 33912382Sralph startpr(0); 34012382Sralph } 34112382Sralph return; 34212382Sralph } 34312382Sralph while (--argc) { 34412382Sralph printer = *++argv; 34512382Sralph if ((status = pgetent(line, printer)) < 0) { 346*12514Sralph printf("cannot open printer description file\n"); 34712382Sralph continue; 34812382Sralph } else if (status == 0) { 349*12514Sralph printf("unknown printer %s\n", printer); 35012382Sralph continue; 35112382Sralph } 35212382Sralph startpr(0); 35312382Sralph } 35412382Sralph } 35512382Sralph 35612382Sralph /* 35712382Sralph * Enable printing on the specified printer and startup the daemon. 35812382Sralph */ 35912382Sralph start(argc, argv) 36012382Sralph char *argv[]; 36112382Sralph { 36212382Sralph register int c, status; 36312382Sralph register char *cp1, *cp2; 36412382Sralph char prbuf[100]; 36512382Sralph 36612382Sralph if (argc == 1) { 36712382Sralph printf("Usage: start {all | printer ...}\n"); 36812382Sralph return; 36912382Sralph } 37012382Sralph gethostname(host, sizeof(host)); 37112382Sralph if (argc == 2 && !strcmp(argv[1], "all")) { 37212382Sralph printer = prbuf; 37312382Sralph while (getprent(line) > 0) { 37412382Sralph cp1 = prbuf; 37512382Sralph cp2 = line; 37612382Sralph while ((c = *cp2++) && c != '|' && c != ':') 37712382Sralph *cp1++ = c; 37812382Sralph *cp1 = '\0'; 37912382Sralph startpr(1); 38012382Sralph } 38112382Sralph return; 38212382Sralph } 38312382Sralph while (--argc) { 38412382Sralph printer = *++argv; 38512382Sralph if ((status = pgetent(line, printer)) < 0) { 386*12514Sralph printf("cannot open printer description file\n"); 38712382Sralph continue; 38812382Sralph } else if (status == 0) { 389*12514Sralph printf("unknown printer %s\n", printer); 39012382Sralph continue; 39112382Sralph } 39212382Sralph startpr(1); 39312382Sralph } 39412382Sralph } 39512382Sralph 39612382Sralph startpr(enable) 39712382Sralph { 39812382Sralph struct stat stbuf; 39912382Sralph 40012382Sralph bp = pbuf; 40112382Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 40212382Sralph SD = DEFSPOOL; 40312382Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 40412382Sralph LO = DEFLOCK; 40512382Sralph RM = host; 40612382Sralph (void) sprintf(line, "%s/%s", SD, LO); 40712382Sralph printf("%s:\n", printer); 40812382Sralph 40912382Sralph /* 41012382Sralph * Turn off the owner execute bit of the lock file to enable printing. 41112382Sralph */ 41212382Sralph if (enable && stat(line, &stbuf) >= 0) { 41312382Sralph if (chmod(line, stbuf.st_mode & 0677) < 0) 41412382Sralph printf("\tcannot enable printing\n"); 41512382Sralph else 41612382Sralph printf("\tprinting enabled\n"); 41712382Sralph } 41812382Sralph if (!startdaemon()) 41912382Sralph printf("\tcouldn't start daemon\n"); 42012382Sralph else 42112382Sralph printf("\tdaemon started\n"); 42212382Sralph } 42312382Sralph 42412382Sralph /* 42512382Sralph * Print the status of each queue listed or all the queues. 42612382Sralph */ 42712382Sralph status(argc, argv) 42812382Sralph char *argv[]; 42912382Sralph { 43012382Sralph register int c, status; 43112382Sralph register char *cp1, *cp2; 43212382Sralph char prbuf[100]; 43312382Sralph 43412382Sralph if (argc == 1) { 43512382Sralph printer = prbuf; 43612382Sralph while (getprent(line) > 0) { 43712382Sralph cp1 = prbuf; 43812382Sralph cp2 = line; 43912382Sralph while ((c = *cp2++) && c != '|' && c != ':') 44012382Sralph *cp1++ = c; 44112382Sralph *cp1 = '\0'; 44212382Sralph prstat(); 44312382Sralph } 44412382Sralph return; 44512382Sralph } 44612382Sralph while (--argc) { 44712382Sralph printer = *++argv; 44812382Sralph if ((status = pgetent(line, printer)) < 0) { 449*12514Sralph printf("cannot open printer description file\n"); 45012382Sralph continue; 45112382Sralph } else if (status == 0) { 452*12514Sralph printf("unknown printer %s\n", printer); 45312382Sralph continue; 45412382Sralph } 45512382Sralph prstat(); 45612382Sralph } 45712382Sralph } 45812382Sralph 45912382Sralph /* 46012382Sralph * Print the status of the printer queue. 46112382Sralph */ 46212382Sralph prstat() 46312382Sralph { 46412382Sralph struct stat stbuf; 46512382Sralph register int fd, i; 46612382Sralph register struct direct *dp; 46712382Sralph DIR *dirp; 46812382Sralph 46912382Sralph bp = pbuf; 47012382Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 47112382Sralph SD = DEFSPOOL; 47212382Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 47312382Sralph LO = DEFLOCK; 47412382Sralph if ((ST = pgetstr("st", &bp)) == NULL) 47512382Sralph ST = DEFSTAT; 47612382Sralph printf("%s:\n", printer); 47712382Sralph (void) sprintf(line, "%s/%s", SD, LO); 47812382Sralph if (stat(line, &stbuf) >= 0) { 47912382Sralph printf("\tqueuing is %s\n", 48012382Sralph (stbuf.st_mode & 010) ? "disabled" : "enabled"); 48112382Sralph printf("\tprinting is %s\n", 48212382Sralph (stbuf.st_mode & 0100) ? "disabled" : "enabled"); 48312382Sralph } else { 48412382Sralph printf("\tqueuing is enabled\n"); 48512382Sralph printf("\tprinting is enabled\n"); 48612382Sralph } 48712382Sralph if ((dirp = opendir(SD)) == NULL) { 48812382Sralph printf("\tcannot examine spool directory\n"); 48912382Sralph return; 49012382Sralph } 49112382Sralph i = 0; 49212382Sralph while ((dp = readdir(dirp)) != NULL) { 49312382Sralph if (*dp->d_name == 'c' && dp->d_name[1] == 'f') 49412382Sralph i++; 49512382Sralph } 49612382Sralph closedir(dirp); 49712382Sralph if (i == 0) 49812382Sralph printf("\tno entries\n"); 49912382Sralph else if (i == 1) 50012382Sralph printf("\t1 entry in spool area\n"); 50112382Sralph else 50212382Sralph printf("\t%d entries in spool area\n", i); 50312382Sralph fd = open(line, FRDONLY); 50412382Sralph if (fd < 0 || flock(fd, FSHLOCK|FNBLOCK) == 0) { 50512382Sralph (void) close(fd); 50612382Sralph printf("\tno daemon present\n"); 50712382Sralph return; 50812382Sralph } 50912382Sralph (void) close(fd); 51012382Sralph putchar('\t'); 51112382Sralph (void) sprintf(line, "%s/%s", SD, ST); 51212382Sralph if ((fd = open(line, FRDONLY|FSHLOCK)) >= 0) { 51312382Sralph while ((i = read(fd, line, sizeof(line))) > 0) 51412382Sralph (void) fwrite(line, 1, i, stdout); 51512382Sralph (void) close(fd); 51612382Sralph } 51712382Sralph } 51812382Sralph 51912382Sralph /* 52012382Sralph * Stop the specified daemon after completing the current job and disable 52112382Sralph * printing. 52212382Sralph */ 52312382Sralph stop(argc, argv) 52412382Sralph char *argv[]; 52512382Sralph { 52612382Sralph register int c, status; 52712382Sralph register char *cp1, *cp2; 52812382Sralph char prbuf[100]; 52912382Sralph 53012382Sralph if (argc == 1) { 53112382Sralph printf("Usage: stop {all | printer ...}\n"); 53212382Sralph return; 53312382Sralph } 53412382Sralph if (argc == 2 && !strcmp(argv[1], "all")) { 53512382Sralph printer = prbuf; 53612382Sralph while (getprent(line) > 0) { 53712382Sralph cp1 = prbuf; 53812382Sralph cp2 = line; 53912382Sralph while ((c = *cp2++) && c != '|' && c != ':') 54012382Sralph *cp1++ = c; 54112382Sralph *cp1 = '\0'; 54212382Sralph stoppr(); 54312382Sralph } 54412382Sralph return; 54512382Sralph } 54612382Sralph while (--argc) { 54712382Sralph printer = *++argv; 54812382Sralph if ((status = pgetent(line, printer)) < 0) { 549*12514Sralph printf("cannot open printer description file\n"); 55012382Sralph continue; 55112382Sralph } else if (status == 0) { 552*12514Sralph printf("unknown printer %s\n", printer); 55312382Sralph continue; 55412382Sralph } 55512382Sralph stoppr(); 55612382Sralph } 55712382Sralph } 55812382Sralph 55912382Sralph stoppr() 56012382Sralph { 56112382Sralph register int fd; 56212382Sralph struct stat stbuf; 56312382Sralph 56412382Sralph bp = pbuf; 56512382Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 56612382Sralph SD = DEFSPOOL; 56712382Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 56812382Sralph LO = DEFLOCK; 56912382Sralph (void) sprintf(line, "%s/%s", SD, LO); 57012382Sralph printf("%s:\n", printer); 57112382Sralph 57212382Sralph /* 57312382Sralph * Turn on the owner execute bit of the lock file to disable printing. 57412382Sralph */ 57512382Sralph if (stat(line, &stbuf) >= 0) { 57612382Sralph if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0) 57712382Sralph printf("\tcannot disable printing\n"); 57812382Sralph else 57912382Sralph printf("\tprinting disabled\n"); 58012382Sralph } else if (errno == ENOENT) { 58112382Sralph if ((fd = open(line, FWRONLY|FCREATE, 0760)) < 0) 58212382Sralph printf("\tcannot create lock file\n"); 58312382Sralph else { 58412382Sralph (void) close(fd); 58512382Sralph printf("\tprinting disabled\n"); 58612382Sralph } 58712382Sralph } else 58812382Sralph printf("\tcannot stat lock file\n"); 58912382Sralph } 590