summaryrefslogtreecommitdiffstats
path: root/usr.sbin/lpr/lpc
diff options
context:
space:
mode:
authorwollman <wollman@FreeBSD.org>1997-12-02 20:46:22 +0000
committerwollman <wollman@FreeBSD.org>1997-12-02 20:46:22 +0000
commitbf21e6b4e0e85ae08cb5d1f2335da248f52e2d26 (patch)
tree041fd28f30eaadf848820e6ceed6899636d31478 /usr.sbin/lpr/lpc
parentfa82bf4e17d3b6422eb922374e04631e08af93ef (diff)
downloadFreeBSD-src-bf21e6b4e0e85ae08cb5d1f2335da248f52e2d26.zip
FreeBSD-src-bf21e6b4e0e85ae08cb5d1f2335da248f52e2d26.tar.gz
Mega lpd/lpd upgrade, part I:
- Get rid of a lot of the static variables which were shared by many routines and programs in the suite. - Create an abstract interface to the printcap database, so that other retrieval and iteration mechanisms could be developed (e.g., YP, Hesiod, or automatic retrieval from a trusted server). - Give each capability a human-readable name in addition to the historic two-character one. - Otherwise generally clean up a lot of dark corners. Many still remain. - When submitting jobs, use the official login name record (from getlogin()) if there is one, rather than reverse-mapping the uid. More to come...
Diffstat (limited to 'usr.sbin/lpr/lpc')
-rw-r--r--usr.sbin/lpr/lpc/Makefile8
-rw-r--r--usr.sbin/lpr/lpc/cmds.c760
-rw-r--r--usr.sbin/lpr/lpc/cmdtab.c22
-rw-r--r--usr.sbin/lpr/lpc/extern.h19
-rw-r--r--usr.sbin/lpr/lpc/lpc.c15
-rw-r--r--usr.sbin/lpr/lpc/lpc.h3
6 files changed, 288 insertions, 539 deletions
diff --git a/usr.sbin/lpr/lpc/Makefile b/usr.sbin/lpr/lpc/Makefile
index 4aeeab7..8d1326e 100644
--- a/usr.sbin/lpr/lpc/Makefile
+++ b/usr.sbin/lpr/lpc/Makefile
@@ -1,12 +1,14 @@
-# @(#)Makefile 8.1 (Berkeley) 6/6/93
+# From: @(#)Makefile 8.1 (Berkeley) 6/6/93
+# $Id$
PROG= lpc
-CFLAGS+=-I${.CURDIR}/../common_source
+CFLAGS+=-I${.CURDIR}/../common_source ${CWARNFLAGS}
MAN8= lpc.8
-SRCS= lpc.c cmds.c cmdtab.c startdaemon.c common.c
+SRCS= lpc.c cmds.c cmdtab.c
BINGRP= daemon
BINMODE=2555
.PATH: ${.CURDIR}/../common_source
+LDADD= -L${.OBJDIR}/../common_source -llpr
.include "../../Makefile.inc"
.include <bsd.prog.mk>
diff --git a/usr.sbin/lpr/lpc/cmds.c b/usr.sbin/lpr/lpc/cmds.c
index 07ac45a..650ab00 100644
--- a/usr.sbin/lpr/lpc/cmds.c
+++ b/usr.sbin/lpr/lpc/cmds.c
@@ -33,13 +33,17 @@
*/
#ifndef lint
-static char copyright[] =
+static const char copyright[] =
"@(#) Copyright (c) 1983, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
+/*
static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/28/95";
+*/
+static const char rcsid[] =
+ "$Id$";
#endif /* not lint */
/*
@@ -68,97 +72,121 @@ static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/28/95";
extern uid_t uid, euid;
-static void abortpr __P((int));
-static void cleanpr __P((void));
-static void disablepr __P((void));
+static void abortpr __P((struct printer *, int));
static int doarg __P((char *));
static int doselect __P((struct dirent *));
-static void enablepr __P((void));
-static void prstat __P((void));
-static void putmsg __P((int, char **));
+static void putmsg __P((struct printer *, int, char **));
static int sortq __P((const void *, const void *));
-static void startpr __P((int));
-static void stoppr __P((void));
+static void startpr __P((struct printer *, int));
static int touch __P((struct queue *));
static void unlinkf __P((char *));
-static void upstat __P((char *));
+static void upstat __P((struct printer *, char *));
/*
- * kill an existing daemon and disable printing.
+ * generic framework for commands which operate on all or a specified
+ * set of printers
*/
void
-doabort(argc, argv)
+generic(doit, argc, argv)
+ void (*doit) __P((struct printer *));
int argc;
char *argv[];
{
- register int c, status;
- register char *cp1, *cp2;
- char prbuf[100];
+ int status, more;
+ struct printer myprinter, *pp = &myprinter;
if (argc == 1) {
- printf("Usage: abort {all | printer ...}\n");
- return;
- }
- if (argc == 2 && !strcmp(argv[1], "all")) {
- printer = prbuf;
- while (cgetnext(&bp, printcapdb) > 0) {
- cp1 = prbuf;
- cp2 = bp;
- while ((c = *cp2++) && c != '|' && c != ':' &&
- (cp1 - prbuf) < sizeof(prbuf))
- *cp1++ = c;
- *cp1 = '\0';
- abortpr(1);
+ printf("Usage: %s {all | printer ...}\n", argv[0]);
+ return;
+ }
+ if (argc == 2 && strcmp(argv[1], "all") == 0) {
+ more = firstprinter(pp, &status);
+ if (status)
+ goto looperr;
+ while (more) {
+ (*doit)(pp);
+ do {
+ more = nextprinter(pp, &status);
+looperr:
+ switch (status) {
+ case PCAPERR_TCOPEN:
+ printf("warning: %s: unresolved "
+ "tc= reference(s) ",
+ pp->printer);
+ case PCAPERR_SUCCESS:
+ break;
+ default:
+ fatal(pp, pcaperr(status));
+ }
+ } while (more && status);
}
return;
}
while (--argc) {
- printer = *++argv;
- if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
- printf("cannot open printer description file\n");
+ ++argv;
+ init_printer(pp);
+ status = getprintcap(*argv, pp);
+ switch(status) {
+ default:
+ fatal(pp, pcaperr(status));
+ case PCAPERR_NOTFOUND:
+ printf("unknown printer %s\n", *argv);
continue;
- } else if (status == -1) {
- printf("unknown printer %s\n", printer);
- continue;
- } else if (status == -3)
- fatal("potential reference loop detected in printcap file");
- abortpr(1);
+ case PCAPERR_TCOPEN:
+ printf("warning: %s: unresolved tc= reference(s)\n",
+ *argv);
+ break;
+ case PCAPERR_SUCCESS:
+ break;
+ }
+ (*doit)(pp);
}
}
+/*
+ * kill an existing daemon and disable printing.
+ */
+void
+doabort(pp)
+ struct printer *pp;
+{
+ abortpr(pp, 1);
+}
+
static void
-abortpr(dis)
+abortpr(pp, dis)
+ struct printer *pp;
int dis;
{
register FILE *fp;
struct stat stbuf;
int pid, fd;
+ char lf[MAXPATHLEN];
- if (cgetstr(bp, "sd", &SD) == -1)
- SD = _PATH_DEFSPOOL;
- if (cgetstr(bp, "lo", &LO) == -1)
- LO = DEFLOCK;
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
- printf("%s:\n", printer);
+ lock_file_name(pp, lf, sizeof lf);
+ printf("%s:\n", pp->printer);
/*
* Turn on the owner execute bit of the lock file to disable printing.
*/
if (dis) {
seteuid(euid);
- if (stat(line, &stbuf) >= 0) {
- if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
- printf("\tcannot disable printing\n");
+ if (stat(lf, &stbuf) >= 0) {
+ if (chmod(lf, stbuf.st_mode | LFM_PRINT_DIS) < 0)
+ printf("\tcannot disable printing: %s\n",
+ strerror(errno));
else {
- upstat("printing disabled\n");
+ upstat(pp, "printing disabled\n");
printf("\tprinting disabled\n");
}
} else if (errno == ENOENT) {
- if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
- printf("\tcannot create lock file\n");
+ if ((fd = open(lf, O_WRONLY|O_CREAT,
+ LOCK_FILE_MODE | LFM_PRINT_DIS)) < 0)
+ printf("\tcannot create lock file: %s\n",
+ strerror(errno));
else {
(void) close(fd);
- upstat("printing disabled\n");
+ upstat(pp, "printing disabled\n");
printf("\tprinting disabled\n");
printf("\tno daemon to abort\n");
}
@@ -171,7 +199,7 @@ abortpr(dis)
/*
* Kill the current daemon to stop printing now.
*/
- if ((fp = fopen(line, "r")) == NULL) {
+ if ((fp = fopen(lf, "r")) == NULL) {
printf("\tcannot open lock file\n");
goto out;
}
@@ -196,19 +224,18 @@ out:
* Write a message into the status file.
*/
static void
-upstat(msg)
+upstat(pp, msg)
+ struct printer *pp;
char *msg;
{
register int fd;
char statfile[MAXPATHLEN];
- if (cgetstr(bp, "st", &ST) == -1)
- ST = DEFSTAT;
- (void) snprintf(statfile, sizeof(statfile), "%s/%s", SD, ST);
+ status_file_name(pp, statfile, sizeof statfile);
umask(0);
- fd = open(statfile, O_WRONLY|O_CREAT, 0664);
- if (fd < 0 || flock(fd, LOCK_EX) < 0) {
- printf("\tcannot create status file\n");
+ fd = open(statfile, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE);
+ if (fd < 0) {
+ printf("\tcannot create status file: %s\n", strerror(errno));
return;
}
(void) ftruncate(fd, 0);
@@ -219,50 +246,6 @@ upstat(msg)
(void) close(fd);
}
-/*
- * Remove all spool files and temporaries from the spooling area.
- */
-void
-clean(argc, argv)
- int argc;
- char *argv[];
-{
- register int c, status;
- register char *cp1, *cp2;
- char prbuf[100];
-
- if (argc == 1) {
- printf("Usage: clean {all | printer ...}\n");
- return;
- }
- if (argc == 2 && !strcmp(argv[1], "all")) {
- printer = prbuf;
- while (cgetnext(&bp, printcapdb) > 0) {
- cp1 = prbuf;
- cp2 = bp;
- while ((c = *cp2++) && c != '|' && c != ':' &&
- (cp1 - prbuf) < sizeof(prbuf))
- *cp1++ = c;
- *cp1 = '\0';
- cleanpr();
- }
- return;
- }
- while (--argc) {
- printer = *++argv;
- if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
- printf("cannot open printer description file\n");
- continue;
- } else if (status == -1) {
- printf("unknown printer %s\n", printer);
- continue;
- } else if (status == -3)
- fatal("potential reference loop detected in printcap file");
-
- cleanpr();
- }
-}
-
static int
doselect(d)
struct dirent *d;
@@ -301,26 +284,31 @@ sortq(a, b)
}
/*
+ * Remove all spool files and temporaries from the spooling area.
+ * Or, perhaps:
* Remove incomplete jobs from spooling area.
*/
-static void
-cleanpr()
+void
+clean(pp)
+ struct printer *pp;
{
register int i, n;
register char *cp, *cp1, *lp;
struct dirent **queue;
int nitems;
- if (cgetstr(bp, "sd", &SD) == -1)
- SD = _PATH_DEFSPOOL;
- printf("%s:\n", printer);
+ printf("%s:\n", pp->printer);
- for (lp = line, cp = SD; (lp - line) < sizeof(line) && (*lp++ = *cp++);)
- ;
+ lp = line;
+ cp = pp->spool_dir;
+ while (lp < &line[sizeof(line) - 1]) {
+ if ((*lp++ = *cp++) == 0)
+ break;
+ }
lp[-1] = '/';
seteuid(euid);
- nitems = scandir(SD, &queue, doselect, sortq);
+ nitems = scandir(pp->spool_dir, &queue, doselect, sortq);
seteuid(uid);
if (nitems < 0) {
printf("\tcannot examine spool directory\n");
@@ -374,64 +362,21 @@ unlinkf(name)
* Enable queuing to the printer (allow lpr's).
*/
void
-enable(argc, argv)
- int argc;
- char *argv[];
-{
- register int c, status;
- register char *cp1, *cp2;
- char prbuf[100];
-
- if (argc == 1) {
- printf("Usage: enable {all | printer ...}\n");
- return;
- }
- if (argc == 2 && !strcmp(argv[1], "all")) {
- printer = prbuf;
- while (cgetnext(&bp, printcapdb) > 0) {
- cp1 = prbuf;
- cp2 = bp;
- while ((c = *cp2++) && c != '|' && c != ':' &&
- (cp1 - prbuf) < sizeof(prbuf))
- *cp1++ = c;
- *cp1 = '\0';
- enablepr();
- }
- return;
- }
- while (--argc) {
- printer = *++argv;
- if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
- printf("cannot open printer description file\n");
- continue;
- } else if (status == -1) {
- printf("unknown printer %s\n", printer);
- continue;
- } else if (status == -3)
- fatal("potential reference loop detected in printcap file");
-
- enablepr();
- }
-}
-
-static void
-enablepr()
+enable(pp)
+ struct printer *pp;
{
struct stat stbuf;
+ char lf[MAXPATHLEN];
- if (cgetstr(bp, "sd", &SD) == -1)
- SD = _PATH_DEFSPOOL;
- if (cgetstr(bp, "lo", &LO) == -1)
- LO = DEFLOCK;
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
- printf("%s:\n", printer);
+ lock_file_name(pp, lf, sizeof lf);
+ printf("%s:\n", pp->printer);
/*
* Turn off the group execute bit of the lock file to enable queuing.
*/
seteuid(euid);
- if (stat(line, &stbuf) >= 0) {
- if (chmod(line, stbuf.st_mode & 0767) < 0)
+ if (stat(lf, &stbuf) >= 0) {
+ if (chmod(lf, stbuf.st_mode & ~LFM_QUEUE_DIS) < 0)
printf("\tcannot enable queuing\n");
else
printf("\tqueuing enabled\n");
@@ -443,70 +388,30 @@ enablepr()
* Disable queuing.
*/
void
-disable(argc, argv)
- int argc;
- char *argv[];
-{
- register int c, status;
- register char *cp1, *cp2;
- char prbuf[100];
-
- if (argc == 1) {
- printf("Usage: disable {all | printer ...}\n");
- return;
- }
- if (argc == 2 && !strcmp(argv[1], "all")) {
- printer = prbuf;
- while (cgetnext(&bp, printcapdb) > 0) {
- cp1 = prbuf;
- cp2 = bp;
- while ((c = *cp2++) && c != '|' && c != ':' &&
- (cp1 - prbuf) < sizeof(prbuf))
- *cp1++ = c;
- *cp1 = '\0';
- disablepr();
- }
- return;
- }
- while (--argc) {
- printer = *++argv;
- if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
- printf("cannot open printer description file\n");
- continue;
- } else if (status == -1) {
- printf("unknown printer %s\n", printer);
- continue;
- } else if (status == -3)
- fatal("potential reference loop detected in printcap file");
-
- disablepr();
- }
-}
-
-static void
-disablepr()
+disable(pp)
+ struct printer *pp;
{
register int fd;
struct stat stbuf;
-
- if (cgetstr(bp, "sd", &SD) == -1)
- SD = _PATH_DEFSPOOL;
- if (cgetstr(bp, "lo", &LO) == -1)
- LO = DEFLOCK;
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
- printf("%s:\n", printer);
+ char lf[MAXPATHLEN];
+
+ lock_file_name(pp, lf, sizeof lf);
+ printf("%s:\n", pp->printer);
/*
* Turn on the group execute bit of the lock file to disable queuing.
*/
seteuid(euid);
- if (stat(line, &stbuf) >= 0) {
- if (chmod(line, (stbuf.st_mode & 0777) | 010) < 0)
- printf("\tcannot disable queuing\n");
+ if (stat(lf, &stbuf) >= 0) {
+ if (chmod(lf, stbuf.st_mode | LFM_QUEUE_DIS) < 0)
+ printf("\tcannot disable queuing: %s\n",
+ strerror(errno));
else
printf("\tqueuing disabled\n");
} else if (errno == ENOENT) {
- if ((fd = open(line, O_WRONLY|O_CREAT, 0670)) < 0)
- printf("\tcannot create lock file\n");
+ if ((fd = open(lf, O_WRONLY|O_CREAT,
+ LOCK_FILE_MODE | LFM_QUEUE_DIS)) < 0)
+ printf("\tcannot create lock file: %s\n",
+ strerror(errno));
else {
(void) close(fd);
printf("\tqueuing disabled\n");
@@ -525,71 +430,83 @@ down(argc, argv)
int argc;
char *argv[];
{
- register int c, status;
- register char *cp1, *cp2;
- char prbuf[100];
+ int status, more;
+ struct printer myprinter, *pp = &myprinter;
if (argc == 1) {
printf("Usage: down {all | printer} [message ...]\n");
return;
}
if (!strcmp(argv[1], "all")) {
- printer = prbuf;
- while (cgetnext(&bp, printcapdb) > 0) {
- cp1 = prbuf;
- cp2 = bp;
- while ((c = *cp2++) && c != '|' && c != ':' &&
- (cp1 - prbuf) < sizeof(prbuf))
- *cp1++ = c;
- *cp1 = '\0';
- putmsg(argc - 2, argv + 2);
+ more = firstprinter(pp, &status);
+ if (status)
+ goto looperr;
+ while (more) {
+ putmsg(pp, argc - 2, argv + 2);
+ do {
+ more = nextprinter(pp, &status);
+looperr:
+ switch (status) {
+ case PCAPERR_TCOPEN:
+ printf("warning: %s: unresolved "
+ "tc= reference(s) ",
+ pp->printer);
+ case PCAPERR_SUCCESS:
+ break;
+ default:
+ fatal(pp, pcaperr(status));
+ }
+ } while (more && status);
}
return;
}
- printer = argv[1];
- if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
- printf("cannot open printer description file\n");
- return;
- } else if (status == -1) {
- printf("unknown printer %s\n", printer);
+ init_printer(pp);
+ status = getprintcap(argv[1], pp);
+ switch(status) {
+ default:
+ fatal(pp, pcaperr(status));
+ case PCAPERR_NOTFOUND:
+ printf("unknown printer %s\n", argv[1]);
return;
- } else if (status == -3)
- fatal("potential reference loop detected in printcap file");
-
- putmsg(argc - 2, argv + 2);
+ case PCAPERR_TCOPEN:
+ printf("warning: %s: unresolved tc= reference(s)", argv[1]);
+ break;
+ case PCAPERR_SUCCESS:
+ break;
+ }
+ putmsg(pp, argc - 2, argv + 2);
}
static void
-putmsg(argc, argv)
+putmsg(pp, argc, argv)
+ struct printer *pp;
int argc;
char **argv;
{
register int fd;
register char *cp1, *cp2;
char buf[1024];
+ char file[MAXPATHLEN];
struct stat stbuf;
- if (cgetstr(bp, "sd", &SD) == -1)
- SD = _PATH_DEFSPOOL;
- if (cgetstr(bp, "lo", &LO) == -1)
- LO = DEFLOCK;
- if (cgetstr(bp, "st", &ST) == -1)
- ST = DEFSTAT;
- printf("%s:\n", printer);
+ printf("%s:\n", pp->printer);
/*
- * Turn on the group execute bit of the lock file to disable queuing and
+ * Turn on the group execute bit of the lock file to disable queuing;
* turn on the owner execute bit of the lock file to disable printing.
*/
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
+ lock_file_name(pp, file, sizeof file);
seteuid(euid);
- if (stat(line, &stbuf) >= 0) {
- if (chmod(line, (stbuf.st_mode & 0777) | 0110) < 0)
- printf("\tcannot disable queuing\n");
+ if (stat(file, &stbuf) >= 0) {
+ if (chmod(file, stbuf.st_mode|LFM_PRINT_DIS|LFM_QUEUE_DIS) < 0)
+ printf("\tcannot disable queuing: %s\n",
+ strerror(errno));
else
printf("\tprinter and queuing disabled\n");
} else if (errno == ENOENT) {
- if ((fd = open(line, O_WRONLY|O_CREAT, 0770)) < 0)
- printf("\tcannot create lock file\n");
+ if ((fd = open(file, O_WRONLY|O_CREAT,
+ LOCK_FILE_MODE|LFM_PRINT_DIS|LFM_QUEUE_DIS)) < 0)
+ printf("\tcannot create lock file: %s\n",
+ strerror(errno));
else {
(void) close(fd);
printf("\tprinter and queuing disabled\n");
@@ -601,10 +518,10 @@ putmsg(argc, argv)
/*
* Write the message into the status file.
*/
- (void) snprintf(line, sizeof(line), "%s/%s", SD, ST);
- fd = open(line, O_WRONLY|O_CREAT, 0664);
- if (fd < 0 || flock(fd, LOCK_EX) < 0) {
- printf("\tcannot create status file\n");
+ status_file_name(pp, file, sizeof file);
+ fd = open(file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE);
+ if (fd < 0) {
+ printf("\tcannot create status file: %s\n", strerror(errno));
seteuid(uid);
return;
}
@@ -643,116 +560,47 @@ quit(argc, argv)
* Kill and restart the daemon.
*/
void
-restart(argc, argv)
- int argc;
- char *argv[];
+restart(pp)
+ struct printer *pp;
{
- register int c, status;
- register char *cp1, *cp2;
- char prbuf[100];
-
- if (argc == 1) {
- printf("Usage: restart {all | printer ...}\n");
- return;
- }
- if (argc == 2 && !strcmp(argv[1], "all")) {
- printer = prbuf;
- while (cgetnext(&bp, printcapdb) > 0) {
- cp1 = prbuf;
- cp2 = bp;
- while ((c = *cp2++) && c != '|' && c != ':' &&
- (cp1 - prbuf) < sizeof(prbuf))
- *cp1++ = c;
- *cp1 = '\0';
- abortpr(0);
- startpr(0);
- }
- return;
- }
- while (--argc) {
- printer = *++argv;
- if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
- printf("cannot open printer description file\n");
- continue;
- } else if (status == -1) {
- printf("unknown printer %s\n", printer);
- continue;
- } else if (status == -3)
- fatal("potential reference loop detected in printcap file");
-
- abortpr(0);
- startpr(0);
- }
+ abortpr(pp, 0);
+ startpr(pp, 0);
}
/*
* Enable printing on the specified printer and startup the daemon.
*/
void
-startcmd(argc, argv)
- int argc;
- char *argv[];
+startcmd(pp)
+ struct printer *pp;
{
- register int c, status;
- register char *cp1, *cp2;
- char prbuf[100];
-
- if (argc == 1) {
- printf("Usage: start {all | printer ...}\n");
- return;
- }
- if (argc == 2 && !strcmp(argv[1], "all")) {
- printer = prbuf;
- while (cgetnext(&bp, printcapdb) > 0) {
- cp1 = prbuf;
- cp2 = bp;
- while ((c = *cp2++) && c != '|' && c != ':' &&
- (cp1 - prbuf) < sizeof(prbuf))
- *cp1++ = c;
- *cp1 = '\0';
- startpr(1);
- }
- return;
- }
- while (--argc) {
- printer = *++argv;
- if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
- printf("cannot open printer description file\n");
- continue;
- } else if (status == -1) {
- printf("unknown printer %s\n", printer);
- continue;
- } else if (status == -3)
- fatal("potential reference loop detected in printcap file");
-
- startpr(1);
- }
+ startpr(pp, 1);
}
static void
-startpr(enable)
+startpr(pp, enable)
+ struct printer *pp;
int enable;
{
struct stat stbuf;
+ char lf[MAXPATHLEN];
- if (cgetstr(bp, "sd", &SD) == -1)
- SD = _PATH_DEFSPOOL;
- if (cgetstr(bp, "lo", &LO) == -1)
- LO = DEFLOCK;
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
- printf("%s:\n", printer);
+ lock_file_name(pp, lf, sizeof lf);
+ printf("%s:\n", pp->printer);
/*
* Turn off the owner execute bit of the lock file to enable printing.
*/
seteuid(euid);
- if (enable && stat(line, &stbuf) >= 0) {
- if (chmod(line, stbuf.st_mode & (enable==2 ? 0666 : 0677)) < 0)
+ if (enable && stat(lf, &stbuf) >= 0) {
+ mode_t bits = (enable == 2 ? 0
+ : (LFM_PRINT_DIS | LFM_QUEUE_DIS));
+ if (chmod(lf, stbuf.st_mode & (LOCK_FILE_MODE | bits)) < 0)
printf("\tcannot enable printing\n");
else
printf("\tprinting enabled\n");
}
- if (!startdaemon(printer))
+ if (!startdaemon(pp))
printf("\tcouldn't start daemon\n");
else
printf("\tdaemon started\n");
@@ -760,74 +608,32 @@ startpr(enable)
}
/*
- * Print the status of each queue listed or all the queues.
- */
-void
-status(argc, argv)
- int argc;
- char *argv[];
-{
- register int c, status;
- register char *cp1, *cp2;
- char prbuf[100];
-
- if (argc == 1 || argc == 2 && !strcmp(argv[1], "all")) {
- printer = prbuf;
- while (cgetnext(&bp, printcapdb) > 0) {
- cp1 = prbuf;
- cp2 = bp;
- while ((c = *cp2++) && c != '|' && c != ':' &&
- (cp1 - prbuf) < sizeof(prbuf))
- *cp1++ = c;
- *cp1 = '\0';
- prstat();
- }
- return;
- }
- while (--argc) {
- printer = *++argv;
- if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
- printf("cannot open printer description file\n");
- continue;
- } else if (status == -1) {
- printf("unknown printer %s\n", printer);
- continue;
- } else if (status == -3)
- fatal("potential reference loop detected in printcap file");
-
- prstat();
- }
-}
-
-/*
* Print the status of the printer queue.
*/
-static void
-prstat()
+void
+status(pp)
+ struct printer *pp;
{
struct stat stbuf;
register int fd, i;
register struct dirent *dp;
DIR *dirp;
+ char file[MAXPATHLEN];
- if (cgetstr(bp, "sd", &SD) == -1)
- SD = _PATH_DEFSPOOL;
- if (cgetstr(bp, "lo", &LO) == -1)
- LO = DEFLOCK;
- if (cgetstr(bp, "st", &ST) == -1)
- ST = DEFSTAT;
- printf("%s:\n", printer);
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
- if (stat(line, &stbuf) >= 0) {
+ printf("%s:\n", pp->printer);
+ lock_file_name(pp, file, sizeof file);
+ if (stat(file, &stbuf) >= 0) {
printf("\tqueuing is %s\n",
- (stbuf.st_mode & 010) ? "disabled" : "enabled");
+ ((stbuf.st_mode & LFM_QUEUE_DIS) ? "disabled"
+ : "enabled"));
printf("\tprinting is %s\n",
- (stbuf.st_mode & 0100) ? "disabled" : "enabled");
+ ((stbuf.st_mode & LFM_PRINT_DIS) ? "disabled"
+ : "enabled"));
} else {
printf("\tqueuing is enabled\n");
printf("\tprinting is enabled\n");
}
- if ((dirp = opendir(SD)) == NULL) {
+ if ((dirp = opendir(pp->spool_dir)) == NULL) {
printf("\tcannot examine spool directory\n");
return;
}
@@ -838,12 +644,12 @@ prstat()
}
closedir(dirp);
if (i == 0)
- printf("\tno entries\n");
+ printf("\tno entries in spool area\n");
else if (i == 1)
printf("\t1 entry in spool area\n");
else
printf("\t%d entries in spool area\n", i);
- fd = open(line, O_RDONLY);
+ fd = open(file, O_RDONLY);
if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
(void) close(fd); /* unlocks as well */
printf("\tprinter idle\n");
@@ -851,10 +657,9 @@ prstat()
}
(void) close(fd);
/* print out the contents of the status file, if it exists */
- (void) snprintf(line, sizeof(line), "%s/%s", SD, ST);
- fd = open(line, O_RDONLY);
+ status_file_name(pp, file, sizeof file);
+ fd = open(file, O_RDONLY|O_SHLOCK);
if (fd >= 0) {
- (void) flock(fd, LOCK_SH);
(void) fstat(fd, &stbuf);
if (stbuf.st_size > 0) {
putchar('\t');
@@ -870,76 +675,36 @@ prstat()
* printing.
*/
void
-stop(argc, argv)
- int argc;
- char *argv[];
-{
- register int c, status;
- register char *cp1, *cp2;
- char prbuf[100];
-
- if (argc == 1) {
- printf("Usage: stop {all | printer ...}\n");
- return;
- }
- if (argc == 2 && !strcmp(argv[1], "all")) {
- printer = prbuf;
- while (cgetnext(&bp, printcapdb) > 0) {
- cp1 = prbuf;
- cp2 = bp;
- while ((c = *cp2++) && c != '|' && c != ':' &&
- (cp1 - prbuf) < sizeof(prbuf))
- *cp1++ = c;
- *cp1 = '\0';
- stoppr();
- }
- return;
- }
- while (--argc) {
- printer = *++argv;
- if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
- printf("cannot open printer description file\n");
- continue;
- } else if (status == -1) {
- printf("unknown printer %s\n", printer);
- continue;
- } else if (status == -3)
- fatal("potential reference loop detected in printcap file");
-
- stoppr();
- }
-}
-
-static void
-stoppr()
+stop(pp)
+ struct printer *pp;
{
register int fd;
struct stat stbuf;
+ char lf[MAXPATHLEN];
- if (cgetstr(bp, "sd", &SD) == -1)
- SD = _PATH_DEFSPOOL;
- if (cgetstr(bp, "lo", &LO) == -1)
- LO = DEFLOCK;
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
- printf("%s:\n", printer);
+ lock_file_name(pp, lf, sizeof lf);
+ printf("%s:\n", pp->printer);
/*
* Turn on the owner execute bit of the lock file to disable printing.
*/
seteuid(euid);
- if (stat(line, &stbuf) >= 0) {
- if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
- printf("\tcannot disable printing\n");
+ if (stat(lf, &stbuf) >= 0) {
+ if (chmod(lf, stbuf.st_mode | LFM_PRINT_DIS) < 0)
+ printf("\tcannot disable printing: %s\n",
+ strerror(errno));
else {
- upstat("printing disabled\n");
+ upstat(pp, "printing disabled\n");
printf("\tprinting disabled\n");
}
} else if (errno == ENOENT) {
- if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
- printf("\tcannot create lock file\n");
+ if ((fd = open(lf, O_WRONLY|O_CREAT,
+ LOCK_FILE_MODE | LFM_PRINT_DIS)) < 0)
+ printf("\tcannot create lock file: %s\n",
+ strerror(errno));
else {
(void) close(fd);
- upstat("printing disabled\n");
+ upstat(pp, "printing disabled\n");
printf("\tprinting disabled\n");
}
} else
@@ -962,6 +727,7 @@ topq(argc, argv)
register int i;
struct stat stbuf;
int status, changed;
+ struct printer myprinter, *pp = &myprinter;
if (argc < 3) {
printf("Usage: topq printer [jobnum ...] [user ...]\n");
@@ -969,30 +735,30 @@ topq(argc, argv)
}
--argc;
- printer = *++argv;
- status = cgetent(&bp, printcapdb, printer);
- if (status == -2) {
- printf("cannot open printer description file\n");
- return;
- } else if (status == -1) {
- printf("%s: unknown printer\n", printer);
- return;
- } else if (status == -3)
- fatal("potential reference loop detected in printcap file");
-
- if (cgetstr(bp, "sd", &SD) == -1)
- SD = _PATH_DEFSPOOL;
- if (cgetstr(bp, "lo", &LO) == -1)
- LO = DEFLOCK;
- printf("%s:\n", printer);
+ ++argv;
+ init_printer(pp);
+ status = getprintcap(*argv, pp);
+ switch(status) {
+ default:
+ fatal(pp, pcaperr(status));
+ case PCAPERR_NOTFOUND:
+ printf("unknown printer %s\n", *argv);
+ return;
+ case PCAPERR_TCOPEN:
+ printf("warning: %s: unresolved tc= reference(s)", *argv);
+ break;
+ case PCAPERR_SUCCESS:
+ break;
+ }
+ printf("%s:\n", pp->printer);
seteuid(euid);
- if (chdir(SD) < 0) {
- printf("\tcannot chdir to %s\n", SD);
+ if (chdir(pp->spool_dir) < 0) {
+ printf("\tcannot chdir to %s\n", pp->spool_dir);
goto out;
}
seteuid(uid);
- nitems = getq(&queue);
+ nitems = getq(pp, &queue);
if (nitems == 0)
return;
changed = 0;
@@ -1016,8 +782,8 @@ topq(argc, argv)
* get lpd to rebuild the queue after the current job.
*/
seteuid(euid);
- if (changed && stat(LO, &stbuf) >= 0)
- (void) chmod(LO, (stbuf.st_mode & 0777) | 01);
+ if (changed && stat(pp->lock_file, &stbuf) >= 0)
+ (void) chmod(pp->lock_file, stbuf.st_mode | LFM_RESET_QUE);
out:
seteuid(uid);
@@ -1119,42 +885,8 @@ doarg(job)
* Enable everything and start printer (undo `down').
*/
void
-up(argc, argv)
- int argc;
- char *argv[];
+up(pp)
+ struct printer *pp;
{
- register int c, status;
- register char *cp1, *cp2;
- char prbuf[100];
-
- if (argc == 1) {
- printf("Usage: up {all | printer ...}\n");
- return;
- }
- if (argc == 2 && !strcmp(argv[1], "all")) {
- printer = prbuf;
- while (cgetnext(&bp, printcapdb) > 0) {
- cp1 = prbuf;
- cp2 = bp;
- while ((c = *cp2++) && c != '|' && c != ':' &&
- (cp1 - prbuf) < sizeof(prbuf))
- *cp1++ = c;
- *cp1 = '\0';
- startpr(2);
- }
- return;
- }
- while (--argc) {
- printer = *++argv;
- if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
- printf("cannot open printer description file\n");
- continue;
- } else if (status == -1) {
- printf("unknown printer %s\n", printer);
- continue;
- } else if (status == -3)
- fatal("potential reference loop detected in printcap file");
-
- startpr(2);
- }
+ startpr(pp, 2);
}
diff --git a/usr.sbin/lpr/lpc/cmdtab.c b/usr.sbin/lpr/lpc/cmdtab.c
index 7619791..b4720a8 100644
--- a/usr.sbin/lpr/lpc/cmdtab.c
+++ b/usr.sbin/lpr/lpc/cmdtab.c
@@ -32,7 +32,11 @@
*/
#ifndef lint
+/*
static char sccsid[] = "@(#)cmdtab.c 8.1 (Berkeley) 6/6/93";
+*/
+static const char rcsid[] =
+ "$Id$";
#endif /* not lint */
#include <sys/cdefs.h>
@@ -58,20 +62,20 @@ char topqhelp[] = "put job at top of printer queue";
char uphelp[] = "enable everything and restart spooling daemon";
struct cmd cmdtab[] = {
- { "abort", aborthelp, doabort, 1 },
- { "clean", cleanhelp, clean, 1 },
- { "enable", enablehelp, enable, 1 },
+ { "abort", aborthelp, 0, 1, doabort },
+ { "clean", cleanhelp, 0, 1, clean },
+ { "enable", enablehelp, 0, 1, enable },
{ "exit", quithelp, quit, 0 },
- { "disable", disablehelp, disable, 1 },
+ { "disable", disablehelp, 0, 1, disable },
{ "down", downhelp, down, 1 },
{ "help", helphelp, help, 0 },
{ "quit", quithelp, quit, 0 },
- { "restart", restarthelp, restart, 0 },
- { "start", starthelp, startcmd, 1 },
- { "status", statushelp, status, 0 },
- { "stop", stophelp, stop, 1 },
+ { "restart", restarthelp, 0, 0, restart },
+ { "start", starthelp, 0, 1, startcmd },
+ { "status", statushelp, 0, 0, status },
+ { "stop", stophelp, 0, 1, stop },
{ "topq", topqhelp, topq, 1 },
- { "up", uphelp, up, 1 },
+ { "up", uphelp, 0, 1, up },
{ "?", helphelp, help, 0 },
{ 0 },
};
diff --git a/usr.sbin/lpr/lpc/extern.h b/usr.sbin/lpr/lpc/extern.h
index 1b807b1..c61c474 100644
--- a/usr.sbin/lpr/lpc/extern.h
+++ b/usr.sbin/lpr/lpc/extern.h
@@ -39,19 +39,20 @@
__BEGIN_DECLS
-void clean __P((int, char **));
-void disable __P((int, char **));
-void doabort __P((int, char **));
+void clean __P((struct printer *));
+void disable __P((struct printer *));
+void doabort __P((struct printer *));
void down __P((int, char **));
-void enable __P((int, char **));
+void enable __P((struct printer *));
+void generic __P((void (*) __P((struct printer *)), int, char **));
void help __P((int, char **));
void quit __P((int, char **));
-void restart __P((int, char **));
-void startcmd __P((int, char **));
-void status __P((int, char **));
-void stop __P((int, char **));
+void restart __P((struct printer *));
+void startcmd __P((struct printer *));
+void status __P((struct printer *));
+void stop __P((struct printer *));
void topq __P((int, char **));
-void up __P((int, char **));
+void up __P((struct printer *));
__END_DECLS
extern int NCMDS;
diff --git a/usr.sbin/lpr/lpc/lpc.c b/usr.sbin/lpr/lpc/lpc.c
index bb07d46..9a9b986 100644
--- a/usr.sbin/lpr/lpc/lpc.c
+++ b/usr.sbin/lpr/lpc/lpc.c
@@ -43,13 +43,14 @@ static const char copyright[] =
static char sccsid[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95";
#endif
static const char rcsid[] =
- "$Id$";
+ "$Id: lpc.c,v 1.5 1997/09/24 06:47:46 charnier Exp $";
#endif /* not lint */
#include <sys/param.h>
#include <ctype.h>
#include <dirent.h>
+#include <err.h>
#include <grp.h>
#include <setjmp.h>
#include <signal.h>
@@ -58,7 +59,7 @@ static const char rcsid[] =
#include <syslog.h>
#include <string.h>
#include <unistd.h>
-#include <sys/param.h>
+
#include "lp.h"
#include "lpc.h"
#include "extern.h"
@@ -116,7 +117,10 @@ main(argc, argv)
printf("?Privileged command\n");
exit(1);
}
- (*c->c_handler)(argc, argv);
+ if (c->c_generic != 0)
+ generic(c->c_generic, argc, argv);
+ else
+ (*c->c_handler)(argc, argv);
exit(0);
}
fromatty = isatty(fileno(stdin));
@@ -172,7 +176,10 @@ cmdscanner(top)
printf("?Privileged command\n");
continue;
}
- (*c->c_handler)(margc, margv);
+ if (c->c_generic != 0)
+ generic(c->c_generic, margc, margv);
+ else
+ (*c->c_handler)(margc, margv);
}
longjmp(toplevel, 0);
}
diff --git a/usr.sbin/lpr/lpc/lpc.h b/usr.sbin/lpr/lpc/lpc.h
index 5e71203..eca7b91 100644
--- a/usr.sbin/lpr/lpc/lpc.h
+++ b/usr.sbin/lpr/lpc/lpc.h
@@ -36,10 +36,13 @@
/*
* Line printer control program.
*/
+struct printer;
+
struct cmd {
char *c_name; /* command name */
char *c_help; /* help message */
/* routine to do the work */
void (*c_handler) __P((int, char *[]));
int c_priv; /* privileged command */
+ void (*c_generic) __P((struct printer *)); /* generic command */
};
OpenPOWER on IntegriCloud