112382Sralph #ifndef lint 2*13168Sralph static char sccsid[] = "@(#)cmds.c 4.5 (Berkeley) 06/17/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) { 4012514Sralph printf("cannot open printer description file\n"); 4112382Sralph continue; 4212382Sralph } else if (status == 0) { 4312514Sralph 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) { 7313146Ssam if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0) 7412514Sralph 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 } 9213146Ssam if (!getline(fp) || flock(fileno(fp), LOCK_SH|LOCK_NB) == 0) { 93*13168Sralph (void) fclose(fp); /* unlocks as well */ 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) { 13312514Sralph printf("cannot open printer description file\n"); 13412382Sralph continue; 13512382Sralph } else if (status == 0) { 13612514Sralph 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) { 20312514Sralph printf("cannot open printer description file\n"); 20412382Sralph continue; 20512382Sralph } else if (status == 0) { 20612514Sralph 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) 23012514Sralph 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) { 26512514Sralph printf("cannot open printer description file\n"); 26612382Sralph continue; 26712382Sralph } else if (status == 0) { 26812514Sralph 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) { 29613146Ssam if ((fd = open(line, O_WRONLY|O_CREAT, 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) { 34612514Sralph printf("cannot open printer description file\n"); 34712382Sralph continue; 34812382Sralph } else if (status == 0) { 34912514Sralph 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) { 38612514Sralph printf("cannot open printer description file\n"); 38712382Sralph continue; 38812382Sralph } else if (status == 0) { 38912514Sralph 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 (void) sprintf(line, "%s/%s", SD, LO); 40612382Sralph printf("%s:\n", printer); 40712382Sralph 40812382Sralph /* 40912382Sralph * Turn off the owner execute bit of the lock file to enable printing. 41012382Sralph */ 41112382Sralph if (enable && stat(line, &stbuf) >= 0) { 41212382Sralph if (chmod(line, stbuf.st_mode & 0677) < 0) 41312382Sralph printf("\tcannot enable printing\n"); 41412382Sralph else 41512382Sralph printf("\tprinting enabled\n"); 41612382Sralph } 41712877Sralph if (!startdaemon(host)) 41812382Sralph printf("\tcouldn't start daemon\n"); 41912382Sralph else 42012382Sralph printf("\tdaemon started\n"); 42112382Sralph } 42212382Sralph 42312382Sralph /* 42412382Sralph * Print the status of each queue listed or all the queues. 42512382Sralph */ 42612382Sralph status(argc, argv) 42712382Sralph char *argv[]; 42812382Sralph { 42912382Sralph register int c, status; 43012382Sralph register char *cp1, *cp2; 43112382Sralph char prbuf[100]; 43212382Sralph 43312382Sralph if (argc == 1) { 43412382Sralph printer = prbuf; 43512382Sralph while (getprent(line) > 0) { 43612382Sralph cp1 = prbuf; 43712382Sralph cp2 = line; 43812382Sralph while ((c = *cp2++) && c != '|' && c != ':') 43912382Sralph *cp1++ = c; 44012382Sralph *cp1 = '\0'; 44112382Sralph prstat(); 44212382Sralph } 44312382Sralph return; 44412382Sralph } 44512382Sralph while (--argc) { 44612382Sralph printer = *++argv; 44712382Sralph if ((status = pgetent(line, printer)) < 0) { 44812514Sralph printf("cannot open printer description file\n"); 44912382Sralph continue; 45012382Sralph } else if (status == 0) { 45112514Sralph printf("unknown printer %s\n", printer); 45212382Sralph continue; 45312382Sralph } 45412382Sralph prstat(); 45512382Sralph } 45612382Sralph } 45712382Sralph 45812382Sralph /* 45912382Sralph * Print the status of the printer queue. 46012382Sralph */ 46112382Sralph prstat() 46212382Sralph { 46312382Sralph struct stat stbuf; 46412382Sralph register int fd, i; 46512382Sralph register struct direct *dp; 46612382Sralph DIR *dirp; 46712382Sralph 46812382Sralph bp = pbuf; 46912382Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 47012382Sralph SD = DEFSPOOL; 47112382Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 47212382Sralph LO = DEFLOCK; 47312382Sralph if ((ST = pgetstr("st", &bp)) == NULL) 47412382Sralph ST = DEFSTAT; 47512382Sralph printf("%s:\n", printer); 47612382Sralph (void) sprintf(line, "%s/%s", SD, LO); 47712382Sralph if (stat(line, &stbuf) >= 0) { 47812382Sralph printf("\tqueuing is %s\n", 47912382Sralph (stbuf.st_mode & 010) ? "disabled" : "enabled"); 48012382Sralph printf("\tprinting is %s\n", 48112382Sralph (stbuf.st_mode & 0100) ? "disabled" : "enabled"); 48212382Sralph } else { 48312382Sralph printf("\tqueuing is enabled\n"); 48412382Sralph printf("\tprinting is enabled\n"); 48512382Sralph } 48612382Sralph if ((dirp = opendir(SD)) == NULL) { 48712382Sralph printf("\tcannot examine spool directory\n"); 48812382Sralph return; 48912382Sralph } 49012382Sralph i = 0; 49112382Sralph while ((dp = readdir(dirp)) != NULL) { 49212382Sralph if (*dp->d_name == 'c' && dp->d_name[1] == 'f') 49312382Sralph i++; 49412382Sralph } 49512382Sralph closedir(dirp); 49612382Sralph if (i == 0) 49712382Sralph printf("\tno entries\n"); 49812382Sralph else if (i == 1) 49912382Sralph printf("\t1 entry in spool area\n"); 50012382Sralph else 50112382Sralph printf("\t%d entries in spool area\n", i); 50213146Ssam fd = open(line, O_RDONLY); 50313146Ssam if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) { 504*13168Sralph (void) close(fd); /* unlocks as well */ 50512382Sralph printf("\tno daemon present\n"); 50612382Sralph return; 50712382Sralph } 50812382Sralph (void) close(fd); 50912382Sralph putchar('\t'); 51012382Sralph (void) sprintf(line, "%s/%s", SD, ST); 51113146Ssam fd = open(line, O_RDONLY); 51213146Ssam if (fd >= 0) { 51313146Ssam (void) flock(fd, LOCK_SH); 51412382Sralph while ((i = read(fd, line, sizeof(line))) > 0) 51512382Sralph (void) fwrite(line, 1, i, stdout); 516*13168Sralph (void) close(fd); /* unlocks as well */ 51712382Sralph } 51812382Sralph } 51912382Sralph 52012382Sralph /* 52112382Sralph * Stop the specified daemon after completing the current job and disable 52212382Sralph * printing. 52312382Sralph */ 52412382Sralph stop(argc, argv) 52512382Sralph char *argv[]; 52612382Sralph { 52712382Sralph register int c, status; 52812382Sralph register char *cp1, *cp2; 52912382Sralph char prbuf[100]; 53012382Sralph 53112382Sralph if (argc == 1) { 53212382Sralph printf("Usage: stop {all | printer ...}\n"); 53312382Sralph return; 53412382Sralph } 53512382Sralph if (argc == 2 && !strcmp(argv[1], "all")) { 53612382Sralph printer = prbuf; 53712382Sralph while (getprent(line) > 0) { 53812382Sralph cp1 = prbuf; 53912382Sralph cp2 = line; 54012382Sralph while ((c = *cp2++) && c != '|' && c != ':') 54112382Sralph *cp1++ = c; 54212382Sralph *cp1 = '\0'; 54312382Sralph stoppr(); 54412382Sralph } 54512382Sralph return; 54612382Sralph } 54712382Sralph while (--argc) { 54812382Sralph printer = *++argv; 54912382Sralph if ((status = pgetent(line, printer)) < 0) { 55012514Sralph printf("cannot open printer description file\n"); 55112382Sralph continue; 55212382Sralph } else if (status == 0) { 55312514Sralph printf("unknown printer %s\n", printer); 55412382Sralph continue; 55512382Sralph } 55612382Sralph stoppr(); 55712382Sralph } 55812382Sralph } 55912382Sralph 56012382Sralph stoppr() 56112382Sralph { 56212382Sralph register int fd; 56312382Sralph struct stat stbuf; 56412382Sralph 56512382Sralph bp = pbuf; 56612382Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 56712382Sralph SD = DEFSPOOL; 56812382Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 56912382Sralph LO = DEFLOCK; 57012382Sralph (void) sprintf(line, "%s/%s", SD, LO); 57112382Sralph printf("%s:\n", printer); 57212382Sralph 57312382Sralph /* 57412382Sralph * Turn on the owner execute bit of the lock file to disable printing. 57512382Sralph */ 57612382Sralph if (stat(line, &stbuf) >= 0) { 57712382Sralph if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0) 57812382Sralph printf("\tcannot disable printing\n"); 57912382Sralph else 58012382Sralph printf("\tprinting disabled\n"); 58112382Sralph } else if (errno == ENOENT) { 58213146Ssam if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0) 58312382Sralph printf("\tcannot create lock file\n"); 58412382Sralph else { 58512382Sralph (void) close(fd); 58612382Sralph printf("\tprinting disabled\n"); 58712382Sralph } 58812382Sralph } else 58912382Sralph printf("\tcannot stat lock file\n"); 59012382Sralph } 591