summaryrefslogtreecommitdiff
path: root/usr.sbin/lpr/lpc/cmds.c
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2002-05-20 23:13:51 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2002-05-20 23:13:51 +0000
commitb2a7fd194eceff2b87de78d7cec611dbe9852f58 (patch)
tree964c1ee0a29425901306badd36cf127d4a351291 /usr.sbin/lpr/lpc/cmds.c
parent1c6d6f81973745cbc85a424ec5f1931f07e8ebf7 (diff)
Pull in useful bits from NetBSD and make our lp* easier to diff and
do some minor cleanup of my own: o IPv6 support o ANSI function headers o use getopt() o synce usage() with man pages o passes -Wall on both 32bit and 64bit platforms o add an option to set the max number of children lpd will fork off o add an lpd option to bind to specific addresses instead of INADDR_ANY. o allow user to specify how long to wait for a connection to remote servers o more strlcpy() and snprintf() usage o Use FOO_FILENO constants instead of hard-coding 0-2 o Add some keeps to man the page SYNOPSIS to avoid options being split
Diffstat (limited to 'usr.sbin/lpr/lpc/cmds.c')
-rw-r--r--usr.sbin/lpr/lpc/cmds.c251
1 files changed, 111 insertions, 140 deletions
diff --git a/usr.sbin/lpr/lpc/cmds.c b/usr.sbin/lpr/lpc/cmds.c
index ac98baf23c4..86608fe3bda 100644
--- a/usr.sbin/lpr/lpc/cmds.c
+++ b/usr.sbin/lpr/lpc/cmds.c
@@ -1,4 +1,5 @@
-/* $OpenBSD: cmds.c,v 1.15 2002/02/16 21:28:03 millert Exp $ */
+/* $OpenBSD: cmds.c,v 1.16 2002/05/20 23:13:50 millert Exp $ */
+/* $NetBSD: cmds.c,v 1.12 1997/10/05 15:12:06 mrg Exp $ */
/*
* Copyright (c) 1983, 1993
@@ -44,7 +45,7 @@ static const char copyright[] =
#if 0
static const char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/28/95";
#else
-static const char rcsid[] = "$OpenBSD: cmds.c,v 1.15 2002/02/16 21:28:03 millert Exp $";
+static const char rcsid[] = "$OpenBSD: cmds.c,v 1.16 2002/05/20 23:13:50 millert Exp $";
#endif
#endif /* not lint */
@@ -74,28 +75,26 @@ static const char rcsid[] = "$OpenBSD: cmds.c,v 1.15 2002/02/16 21:28:03 millert
extern uid_t uid, euid;
-void abortpr(int);
-void cleanpr(void);
-void disablepr(void);
-int doarg(char *);
-int doselect(struct dirent *);
-void enablepr(void);
-void prstat(void);
-void putmsg(int, char **);
-int sortq(const void *, const void *);
-void startpr(int);
-void stoppr(void);
-int touch(struct queue *);
-void unlinkf(char *);
-void upstat(char *);
+static void abortpr(int);
+static void cleanpr(void);
+static void disablepr(void);
+static int doarg(char *);
+static int doselect(struct dirent *);
+static void enablepr(void);
+static void prstat(void);
+static void putmsg(int, char **);
+static int sortq(const void *, const void *);
+static void startpr(int);
+static void stoppr(void);
+static int touch(struct queue *);
+static void unlinkf(char *);
+static void upstat(char *);
/*
* kill an existing daemon and disable printing.
*/
void
-doabort(argc, argv)
- int argc;
- char *argv[];
+doabort(int argc, char **argv)
{
int c, status;
char *cp1, *cp2;
@@ -105,7 +104,7 @@ doabort(argc, argv)
printf("Usage: abort {all | printer ...}\n");
return;
}
- if (argc == 2 && !strcmp(argv[1], "all")) {
+ if (argc == 2 && strcmp(argv[1], "all") == 0) {
printer = prbuf;
while (cgetnext(&bp, printcapdb) > 0) {
cp1 = prbuf;
@@ -132,9 +131,8 @@ doabort(argc, argv)
}
}
-void
-abortpr(dis)
- int dis;
+static void
+abortpr(int dis)
{
FILE *fp;
struct stat stbuf;
@@ -144,7 +142,7 @@ abortpr(dis)
SD = _PATH_DEFSPOOL;
if (cgetstr(bp, "lo", &LO) == -1)
LO = DEFLOCK;
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
+ (void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
printf("%s:\n", printer);
/*
@@ -163,7 +161,7 @@ abortpr(dis)
if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
printf("\tcannot create lock file\n");
else {
- (void) close(fd);
+ (void)close(fd);
upstat("printing disabled\n");
printf("\tprinting disabled\n");
printf("\tno daemon to abort\n");
@@ -182,11 +180,11 @@ abortpr(dis)
goto out;
}
if (!getline(fp) || flock(fileno(fp), LOCK_SH|LOCK_NB) == 0) {
- (void) fclose(fp); /* unlocks as well */
+ (void)fclose(fp); /* unlocks as well */
printf("\tno daemon to abort\n");
goto out;
}
- (void) fclose(fp);
+ (void)fclose(fp);
if (kill(pid = atoi(line), SIGTERM) < 0) {
if (errno == ESRCH)
printf("\tno daemon to abort\n");
@@ -201,37 +199,34 @@ out:
/*
* Write a message into the status file.
*/
-void
-upstat(msg)
- char *msg;
+static void
+upstat(char *msg)
{
int fd;
char statfile[MAXPATHLEN];
if (cgetstr(bp, "st", &ST) == -1)
ST = DEFSTAT;
- (void) snprintf(statfile, sizeof(statfile), "%s/%s", SD, ST);
+ (void)snprintf(statfile, sizeof(statfile), "%s/%s", SD, ST);
umask(0);
fd = open(statfile, O_WRONLY|O_CREAT, 0664);
if (fd < 0 || flock(fd, LOCK_EX) < 0) {
printf("\tcannot create status file\n");
return;
}
- (void) ftruncate(fd, 0);
+ (void)ftruncate(fd, 0);
if (msg == (char *)NULL)
- (void) write(fd, "\n", 1);
+ (void)write(fd, "\n", 1);
else
- (void) write(fd, msg, strlen(msg));
- (void) close(fd);
+ (void)write(fd, msg, strlen(msg));
+ (void)close(fd);
}
/*
* Remove all spool files and temporaries from the spooling area.
*/
void
-clean(argc, argv)
- int argc;
- char *argv[];
+clean(int argc, char **argv)
{
int c, status;
char *cp1, *cp2;
@@ -241,7 +236,7 @@ clean(argc, argv)
printf("Usage: clean {all | printer ...}\n");
return;
}
- if (argc == 2 && !strcmp(argv[1], "all")) {
+ if (argc == 2 && strcmp(argv[1], "all") == 0) {
printer = prbuf;
while (cgetnext(&bp, printcapdb) > 0) {
cp1 = prbuf;
@@ -269,9 +264,8 @@ clean(argc, argv)
}
}
-int
-doselect(d)
- struct dirent *d;
+static int
+doselect(struct dirent *d)
{
int c = d->d_name[0];
@@ -284,16 +278,15 @@ doselect(d)
* Comparison routine for scandir. Sort by job number and machine, then
* by `cf', `tf', or `df', then by the sequence letter A-Z, a-z.
*/
-int
-sortq(a, b)
- const void *a, *b;
+static int
+sortq(const void *a, const void *b)
{
- struct dirent **d1, **d2;
+ const struct dirent **d1, **d2;
int c1, c2;
- d1 = (struct dirent **)a;
- d2 = (struct dirent **)b;
- if ((c1 = strcmp((*d1)->d_name + 3, (*d2)->d_name + 3)))
+ d1 = (const struct dirent **)a;
+ d2 = (const struct dirent **)b;
+ if ((c1 = strcmp((*d1)->d_name + 3, (*d2)->d_name + 3)) != 0)
return(c1);
c1 = (*d1)->d_name[0];
c2 = (*d2)->d_name[0];
@@ -309,8 +302,8 @@ sortq(a, b)
/*
* Remove incomplete jobs from spooling area.
*/
-void
-cleanpr()
+static void
+cleanpr(void)
{
int i, n;
char *cp, *cp1, *lp;
@@ -321,7 +314,9 @@ cleanpr()
SD = _PATH_DEFSPOOL;
printf("%s:\n", printer);
- for (lp = line, cp = SD; (lp - line) < sizeof(line) && (*lp++ = *cp++);)
+ /* XXX depends on SD being non-NUL */
+ for (lp = line, cp = SD; (lp - line) < sizeof(line) &&
+ (*lp++ = *cp++) != '\0'; )
;
lp[-1] = '/';
if (lp - line >= sizeof(line)) {
@@ -351,8 +346,8 @@ cleanpr()
n++;
}
if (n == 0) {
- if (strlcpy(lp, cp, sizeof(line) - (lp - line)) >=
- sizeof(line) - (lp - line))
+ if (strlcpy(lp, cp, sizeof(line) - (lp - line))
+ >= sizeof(line) - (lp - line))
printf("\tpath too long, %s/%s", SD, cp);
else
unlinkf(line);
@@ -372,9 +367,8 @@ cleanpr()
} while (++i < nitems);
}
-void
-unlinkf(name)
- char *name;
+static void
+unlinkf(char *name)
{
seteuid(euid);
if (unlink(name) < 0)
@@ -388,9 +382,7 @@ unlinkf(name)
* Enable queuing to the printer (allow lpr's).
*/
void
-enable(argc, argv)
- int argc;
- char *argv[];
+enable(int argc, char **argv)
{
int c, status;
char *cp1, *cp2;
@@ -400,7 +392,7 @@ enable(argc, argv)
printf("Usage: enable {all | printer ...}\n");
return;
}
- if (argc == 2 && !strcmp(argv[1], "all")) {
+ if (argc == 2 && strcmp(argv[1], "all") == 0) {
printer = prbuf;
while (cgetnext(&bp, printcapdb) > 0) {
cp1 = prbuf;
@@ -428,8 +420,8 @@ enable(argc, argv)
}
}
-void
-enablepr()
+static void
+enablepr(void)
{
struct stat stbuf;
@@ -437,7 +429,7 @@ enablepr()
SD = _PATH_DEFSPOOL;
if (cgetstr(bp, "lo", &LO) == -1)
LO = DEFLOCK;
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
+ (void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
printf("%s:\n", printer);
/*
@@ -457,9 +449,7 @@ enablepr()
* Disable queuing.
*/
void
-disable(argc, argv)
- int argc;
- char *argv[];
+disable(int argc, char **argv)
{
int c, status;
char *cp1, *cp2;
@@ -469,7 +459,7 @@ disable(argc, argv)
printf("Usage: disable {all | printer ...}\n");
return;
}
- if (argc == 2 && !strcmp(argv[1], "all")) {
+ if (argc == 2 && strcmp(argv[1], "all") == 0) {
printer = prbuf;
while (cgetnext(&bp, printcapdb) > 0) {
cp1 = prbuf;
@@ -497,8 +487,8 @@ disable(argc, argv)
}
}
-void
-disablepr()
+static void
+disablepr(void)
{
int fd;
struct stat stbuf;
@@ -507,7 +497,7 @@ disablepr()
SD = _PATH_DEFSPOOL;
if (cgetstr(bp, "lo", &LO) == -1)
LO = DEFLOCK;
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
+ (void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
printf("%s:\n", printer);
/*
* Turn on the group execute bit of the lock file to disable queuing.
@@ -522,7 +512,7 @@ disablepr()
if ((fd = open(line, O_WRONLY|O_CREAT, 0670)) < 0)
printf("\tcannot create lock file\n");
else {
- (void) close(fd);
+ (void)close(fd);
printf("\tqueuing disabled\n");
}
} else
@@ -535,9 +525,7 @@ disablepr()
* (reason for being down).
*/
void
-down(argc, argv)
- int argc;
- char *argv[];
+down(int argc, char **argv)
{
int c, status;
char *cp1, *cp2;
@@ -547,7 +535,7 @@ down(argc, argv)
printf("Usage: down {all | printer} [message ...]\n");
return;
}
- if (!strcmp(argv[1], "all")) {
+ if (strcmp(argv[1], "all") == 0) {
printer = prbuf;
while (cgetnext(&bp, printcapdb) > 0) {
cp1 = prbuf;
@@ -573,10 +561,8 @@ down(argc, argv)
putmsg(argc - 2, argv + 2);
}
-void
-putmsg(argc, argv)
- int argc;
- char **argv;
+static void
+putmsg(int argc, char **argv)
{
int fd;
char *cp1, *cp2;
@@ -594,7 +580,7 @@ putmsg(argc, argv)
* Turn on the group execute bit of the lock file to disable queuing and
* turn on the owner execute bit of the lock file to disable printing.
*/
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
+ (void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
seteuid(euid);
if (stat(line, &stbuf) >= 0) {
if (chmod(line, (stbuf.st_mode & 0777) | 0110) < 0)
@@ -605,7 +591,7 @@ putmsg(argc, argv)
if ((fd = open(line, O_WRONLY|O_CREAT, 0770)) < 0)
printf("\tcannot create lock file\n");
else {
- (void) close(fd);
+ (void)close(fd);
printf("\tprinter and queuing disabled\n");
}
seteuid(uid);
@@ -615,7 +601,7 @@ putmsg(argc, argv)
/*
* Write the message into the status file.
*/
- (void) snprintf(line, sizeof(line), "%s/%s", SD, ST);
+ (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");
@@ -623,10 +609,10 @@ putmsg(argc, argv)
return;
}
seteuid(uid);
- (void) ftruncate(fd, 0);
+ (void)ftruncate(fd, 0);
if (argc <= 0) {
- (void) write(fd, "\n", 1);
- (void) close(fd);
+ (void)write(fd, "\n", 1);
+ (void)close(fd);
return;
}
cp1 = buf;
@@ -638,17 +624,15 @@ putmsg(argc, argv)
}
cp1[-1] = '\n';
*cp1 = '\0';
- (void) write(fd, buf, strlen(buf));
- (void) close(fd);
+ (void)write(fd, buf, strlen(buf));
+ (void)close(fd);
}
/*
* Exit lpc
*/
void
-quit(argc, argv)
- int argc;
- char *argv[];
+quit(int argc, char **argv)
{
exit(0);
}
@@ -657,9 +641,7 @@ quit(argc, argv)
* Kill and restart the daemon.
*/
void
-restart(argc, argv)
- int argc;
- char *argv[];
+restart(int argc, char **argv)
{
int c, status;
char *cp1, *cp2;
@@ -669,7 +651,7 @@ restart(argc, argv)
printf("Usage: restart {all | printer ...}\n");
return;
}
- if (argc == 2 && !strcmp(argv[1], "all")) {
+ if (argc == 2 && strcmp(argv[1], "all") == 0) {
printer = prbuf;
while (cgetnext(&bp, printcapdb) > 0) {
cp1 = prbuf;
@@ -703,9 +685,7 @@ restart(argc, argv)
* Enable printing on the specified printer and startup the daemon.
*/
void
-startcmd(argc, argv)
- int argc;
- char *argv[];
+startcmd(int argc, char **argv)
{
int c, status;
char *cp1, *cp2;
@@ -715,7 +695,7 @@ startcmd(argc, argv)
printf("Usage: start {all | printer ...}\n");
return;
}
- if (argc == 2 && !strcmp(argv[1], "all")) {
+ if (argc == 2 && strcmp(argv[1], "all") == 0) {
printer = prbuf;
while (cgetnext(&bp, printcapdb) > 0) {
cp1 = prbuf;
@@ -743,9 +723,8 @@ startcmd(argc, argv)
}
}
-void
-startpr(enable)
- int enable;
+static void
+startpr(int enable)
{
struct stat stbuf;
@@ -753,7 +732,7 @@ startpr(enable)
SD = _PATH_DEFSPOOL;
if (cgetstr(bp, "lo", &LO) == -1)
LO = DEFLOCK;
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
+ (void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
printf("%s:\n", printer);
/*
@@ -777,15 +756,13 @@ startpr(enable)
* Print the status of each queue listed or all the queues.
*/
void
-status(argc, argv)
- int argc;
- char *argv[];
+status(int argc, char **argv)
{
int c, status;
char *cp1, *cp2;
char prbuf[100];
- if (argc == 1 || (argc == 2 && !strcmp(argv[1], "all"))) {
+ if (argc == 1 || (argc == 2 && strcmp(argv[1], "all") == 0)) {
printer = prbuf;
while (cgetnext(&bp, printcapdb) > 0) {
cp1 = prbuf;
@@ -816,8 +793,8 @@ status(argc, argv)
/*
* Print the status of the printer queue.
*/
-void
-prstat()
+static void
+prstat(void)
{
struct stat stbuf;
int fd, i;
@@ -831,7 +808,7 @@ prstat()
if (cgetstr(bp, "st", &ST) == -1)
ST = DEFSTAT;
printf("%s:\n", printer);
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
+ (void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
if (stat(line, &stbuf) >= 0) {
printf("\tqueuing is %s\n",
(stbuf.st_mode & 010) ? "disabled" : "enabled");
@@ -859,19 +836,21 @@ prstat()
printf("\t%d entries in spool area\n", i);
fd = open(line, O_RDONLY);
if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
- (void) close(fd); /* unlocks as well */
+ (void)close(fd); /* unlocks as well */
printf("\tprinter idle\n");
return;
}
- (void) close(fd);
- putchar('\t');
- (void) snprintf(line, sizeof(line), "%s/%s", SD, ST);
+ (void)close(fd);
+ (void)snprintf(line, sizeof(line), "%s/%s", SD, ST);
fd = open(line, O_RDONLY);
if (fd >= 0) {
- (void) flock(fd, LOCK_SH);
- while ((i = read(fd, line, sizeof(line))) > 0)
- (void) fwrite(line, 1, i, stdout);
- (void) close(fd); /* unlocks as well */
+ (void)flock(fd, LOCK_SH);
+ if (fstat(fd, &stbuf) == 0 && stbuf.st_size > 0) {
+ putchar('\t');
+ while ((i = read(fd, line, sizeof(line))) > 0)
+ (void)fwrite(line, 1, i, stdout);
+ }
+ (void)close(fd); /* unlocks as well */
}
}
@@ -880,9 +859,7 @@ prstat()
* printing.
*/
void
-stop(argc, argv)
- int argc;
- char *argv[];
+stop(int argc, char **argv)
{
int c, status;
char *cp1, *cp2;
@@ -892,7 +869,7 @@ stop(argc, argv)
printf("Usage: stop {all | printer ...}\n");
return;
}
- if (argc == 2 && !strcmp(argv[1], "all")) {
+ if (argc == 2 && strcmp(argv[1], "all") == 0) {
printer = prbuf;
while (cgetnext(&bp, printcapdb) > 0) {
cp1 = prbuf;
@@ -920,8 +897,8 @@ stop(argc, argv)
}
}
-void
-stoppr()
+static void
+stoppr(void)
{
int fd;
struct stat stbuf;
@@ -930,7 +907,7 @@ stoppr()
SD = _PATH_DEFSPOOL;
if (cgetstr(bp, "lo", &LO) == -1)
LO = DEFLOCK;
- (void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
+ (void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
printf("%s:\n", printer);
/*
@@ -948,7 +925,7 @@ stoppr()
if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
printf("\tcannot create lock file\n");
else {
- (void) close(fd);
+ (void)close(fd);
upstat("printing disabled\n");
printf("\tprinting disabled\n");
}
@@ -965,9 +942,7 @@ time_t mtime;
* Put the specified jobs at the top of printer queue.
*/
void
-topq(argc, argv)
- int argc;
- char *argv[];
+topq(int argc, char **argv)
{
int i;
struct stat stbuf;
@@ -1027,7 +1002,7 @@ topq(argc, argv)
*/
seteuid(euid);
if (changed && stat(LO, &stbuf) >= 0)
- (void) chmod(LO, (stbuf.st_mode & 0777) | 01);
+ (void)chmod(LO, (stbuf.st_mode & 0777) | 01);
out:
seteuid(uid);
@@ -1037,9 +1012,8 @@ out:
* Reposition the job by changing the modification time of
* the control file.
*/
-int
-touch(q)
- struct queue *q;
+static int
+touch(struct queue *q)
{
struct timeval tvp[2];
int ret;
@@ -1057,8 +1031,7 @@ touch(q)
* Returns: negative (-1) if argument name is not in the queue.
*/
int
-doarg(job)
- char *job;
+doarg(char *job)
{
struct queue **qq;
int jobnum, n;
@@ -1114,7 +1087,7 @@ doarg(job)
while (getline(fp) > 0)
if (line[0] == 'P')
break;
- (void) fclose(fp);
+ (void)fclose(fp);
if (line[0] != 'P' || strcmp(job, line+1) != 0)
continue;
if (touch(*qq) == 0) {
@@ -1129,9 +1102,7 @@ doarg(job)
* Enable everything and start printer (undo `down').
*/
void
-up(argc, argv)
- int argc;
- char *argv[];
+up(int argc, char **argv)
{
int c, status;
char *cp1, *cp2;
@@ -1141,7 +1112,7 @@ up(argc, argv)
printf("Usage: up {all | printer ...}\n");
return;
}
- if (argc == 2 && !strcmp(argv[1], "all")) {
+ if (argc == 2 && strcmp(argv[1], "all") == 0) {
printer = prbuf;
while (cgetnext(&bp, printcapdb) > 0) {
cp1 = prbuf;