2011-08-31 20:24:54 +04:00
|
|
|
/* $NetBSD: cmds.c,v 1.24 2011/08/31 16:24:59 plunky Exp $ */
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-05-18 05:25:37 +04:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:25:11 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-05 19:11:58 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
2008-07-21 17:36:57 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
1997-07-10 10:09:27 +04:00
|
|
|
#if 0
|
1997-10-05 15:52:17 +04:00
|
|
|
static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/28/95";
|
1997-07-10 10:09:27 +04:00
|
|
|
#else
|
2011-08-31 20:24:54 +04:00
|
|
|
__RCSID("$NetBSD: cmds.c,v 1.24 2011/08/31 16:24:59 plunky Exp $");
|
1997-07-10 10:09:27 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lpc -- line printer control program -- commands:
|
|
|
|
*/
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
#include <sys/param.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/time.h>
|
1994-05-18 05:25:37 +04:00
|
|
|
#include <sys/stat.h>
|
1997-10-05 15:52:17 +04:00
|
|
|
#include <sys/file.h>
|
1994-05-18 05:25:37 +04:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "lp.h"
|
|
|
|
#include "lp.local.h"
|
|
|
|
#include "lpc.h"
|
|
|
|
#include "extern.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "pathnames.h"
|
|
|
|
|
1995-10-03 18:02:17 +03:00
|
|
|
extern uid_t uid, euid;
|
|
|
|
|
2002-07-14 19:27:58 +04:00
|
|
|
static void abortpr(int);
|
|
|
|
static void cleanpr(void);
|
|
|
|
static void disablepr(void);
|
2005-11-28 06:26:06 +03:00
|
|
|
static int doarg(const char *);
|
2002-07-14 19:27:58 +04:00
|
|
|
static int doselect(const 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 *);
|
2005-11-28 06:26:06 +03:00
|
|
|
static void unlinkf(const char *);
|
|
|
|
static void upstat(const char *);
|
|
|
|
static int getcapdesc(void);
|
|
|
|
static void getcaps(void);
|
1994-05-18 05:25:37 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* kill an existing daemon and disable printing.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
doabort(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
int c;
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp1, *cp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
char prbuf[100];
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("Usage: abort {all | printer ...}\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (argc == 2 && !strcmp(argv[1], "all")) {
|
|
|
|
printer = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
while (cgetnext(&bp, printcapdb) > 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cp1 = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
cp2 = bp;
|
1997-10-05 19:11:58 +04:00
|
|
|
while ((c = *cp2++) && c != '|' && c != ':' &&
|
2009-01-18 12:57:26 +03:00
|
|
|
(size_t)(cp1 - prbuf) < sizeof(prbuf))
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp1++ = c;
|
|
|
|
*cp1 = '\0';
|
|
|
|
abortpr(1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (--argc) {
|
|
|
|
printer = *++argv;
|
2005-11-28 06:26:06 +03:00
|
|
|
if (!getcapdesc())
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
abortpr(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
2002-07-14 19:27:58 +04:00
|
|
|
abortpr(int dis)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
FILE *fp;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stat stbuf;
|
1997-10-05 19:11:58 +04:00
|
|
|
int pid, fd;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
getcaps();
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("%s:\n", printer);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn on the owner execute bit of the lock file to disable printing.
|
|
|
|
*/
|
|
|
|
if (dis) {
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (stat(line, &stbuf) >= 0) {
|
|
|
|
if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
|
|
|
|
printf("\tcannot disable printing\n");
|
|
|
|
else {
|
|
|
|
upstat("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");
|
|
|
|
else {
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)close(fd);
|
1993-03-21 12:45:37 +03:00
|
|
|
upstat("printing disabled\n");
|
|
|
|
printf("\tprinting disabled\n");
|
|
|
|
printf("\tno daemon to abort\n");
|
|
|
|
}
|
1995-10-03 18:02:17 +03:00
|
|
|
goto out;
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
|
|
|
printf("\tcannot stat lock file\n");
|
1995-10-03 18:02:17 +03:00
|
|
|
goto out;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Kill the current daemon to stop printing now.
|
|
|
|
*/
|
|
|
|
if ((fp = fopen(line, "r")) == NULL) {
|
|
|
|
printf("\tcannot open lock file\n");
|
1995-10-03 18:02:17 +03:00
|
|
|
goto out;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2009-07-13 23:05:39 +04:00
|
|
|
if (!get_line(fp) || flock(fileno(fp), LOCK_SH|LOCK_NB) == 0) {
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)fclose(fp); /* unlocks as well */
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("\tno daemon to abort\n");
|
1995-10-03 18:02:17 +03:00
|
|
|
goto out;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)fclose(fp);
|
1997-10-05 19:11:58 +04:00
|
|
|
if (kill(pid = atoi(line), SIGTERM) < 0) {
|
|
|
|
if (errno == ESRCH)
|
|
|
|
printf("\tno daemon to abort\n");
|
|
|
|
else
|
|
|
|
printf("\tWarning: daemon (pid %d) not killed\n", pid);
|
|
|
|
} else
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("\tdaemon (pid %d) killed\n", pid);
|
1995-10-03 18:02:17 +03:00
|
|
|
out:
|
|
|
|
seteuid(uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a message into the status file.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
2005-11-28 06:26:06 +03:00
|
|
|
upstat(const char *msg)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
int fd;
|
|
|
|
char statfile[MAXPATHLEN];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
getcaps();
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)snprintf(statfile, sizeof(statfile), "%s/%s", SD, ST);
|
1993-03-21 12:45:37 +03:00
|
|
|
umask(0);
|
|
|
|
fd = open(statfile, O_WRONLY|O_CREAT, 0664);
|
|
|
|
if (fd < 0 || flock(fd, LOCK_EX) < 0) {
|
|
|
|
printf("\tcannot create status file\n");
|
2006-03-22 01:49:43 +03:00
|
|
|
if (fd >= 0)
|
|
|
|
(void)close(fd);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)ftruncate(fd, 0);
|
2011-08-31 20:24:54 +04:00
|
|
|
if (msg == NULL)
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)write(fd, "\n", 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)write(fd, msg, strlen(msg));
|
|
|
|
(void)close(fd);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove all spool files and temporaries from the spooling area.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
clean(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
int c;
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp1, *cp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
char prbuf[100];
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("Usage: clean {all | printer ...}\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (argc == 2 && !strcmp(argv[1], "all")) {
|
|
|
|
printer = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
while (cgetnext(&bp, printcapdb) > 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cp1 = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
cp2 = bp;
|
1997-10-05 19:11:58 +04:00
|
|
|
while ((c = *cp2++) && c != '|' && c != ':' &&
|
2009-01-18 12:57:26 +03:00
|
|
|
(size_t)(cp1 - prbuf) < sizeof(prbuf))
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp1++ = c;
|
|
|
|
*cp1 = '\0';
|
|
|
|
cleanpr();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (--argc) {
|
|
|
|
printer = *++argv;
|
2005-11-28 06:26:06 +03:00
|
|
|
if (!getcapdesc())
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
cleanpr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static int
|
2002-07-14 19:27:58 +04:00
|
|
|
doselect(const struct dirent *d)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int c = d->d_name[0];
|
|
|
|
|
|
|
|
if ((c == 't' || c == 'c' || c == 'd') && d->d_name[1] == 'f')
|
|
|
|
return(1);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static int
|
2002-07-14 19:27:58 +04:00
|
|
|
sortq(const void *a, const void *b)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
const struct dirent *const *d1, *const *d2;
|
1993-03-21 12:45:37 +03:00
|
|
|
int c1, c2;
|
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
d1 = (const struct dirent *const *)a;
|
|
|
|
d2 = (const struct dirent *const *)b;
|
1997-07-10 09:38:54 +04:00
|
|
|
if ((c1 = strcmp((*d1)->d_name + 3, (*d2)->d_name + 3)) != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
return(c1);
|
|
|
|
c1 = (*d1)->d_name[0];
|
|
|
|
c2 = (*d2)->d_name[0];
|
|
|
|
if (c1 == c2)
|
|
|
|
return((*d1)->d_name[2] - (*d2)->d_name[2]);
|
|
|
|
if (c1 == 'c')
|
|
|
|
return(-1);
|
|
|
|
if (c1 == 'd' || c2 == 'c')
|
|
|
|
return(1);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove incomplete jobs from spooling area.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
2002-07-14 19:27:58 +04:00
|
|
|
cleanpr(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
int i, n;
|
2005-11-28 06:26:06 +03:00
|
|
|
char *cp1, *lp, *ep;
|
|
|
|
const char *cp;
|
1993-12-08 03:47:01 +03:00
|
|
|
struct dirent **queue;
|
1993-03-21 12:45:37 +03:00
|
|
|
int nitems;
|
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
getcaps();
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("%s:\n", printer);
|
|
|
|
|
1996-12-09 12:57:40 +03:00
|
|
|
/* XXX depends on SD being non nul */
|
2003-05-18 00:46:42 +04:00
|
|
|
ep = line + sizeof(line);
|
2009-01-18 12:57:26 +03:00
|
|
|
for (lp = line, cp = SD; (size_t)(lp - line) < sizeof(line) &&
|
1997-10-05 19:11:58 +04:00
|
|
|
(*lp++ = *cp++) != '\0'; )
|
1993-03-21 12:45:37 +03:00
|
|
|
;
|
|
|
|
lp[-1] = '/';
|
|
|
|
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
1994-05-18 05:25:37 +04:00
|
|
|
nitems = scandir(SD, &queue, doselect, sortq);
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (nitems < 0) {
|
|
|
|
printf("\tcannot examine spool directory\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (nitems == 0)
|
|
|
|
return;
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
cp = queue[i]->d_name;
|
|
|
|
if (*cp == 'c') {
|
|
|
|
n = 0;
|
|
|
|
while (i + 1 < nitems) {
|
|
|
|
cp1 = queue[i + 1]->d_name;
|
|
|
|
if (*cp1 != 'd' || strcmp(cp + 3, cp1 + 3))
|
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (n == 0) {
|
2003-05-18 00:46:42 +04:00
|
|
|
strlcpy(lp, cp, ep - lp);
|
1993-03-21 12:45:37 +03:00
|
|
|
unlinkf(line);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Must be a df with no cf (otherwise, it would have
|
|
|
|
* been skipped above) or a tf file (which can always
|
|
|
|
* be removed).
|
|
|
|
*/
|
2003-05-18 00:46:42 +04:00
|
|
|
strlcpy(lp, cp, ep - lp);
|
1993-03-21 12:45:37 +03:00
|
|
|
unlinkf(line);
|
|
|
|
}
|
|
|
|
} while (++i < nitems);
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
2005-11-28 06:26:06 +03:00
|
|
|
unlinkf(const char *name)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (unlink(name) < 0)
|
|
|
|
printf("\tcannot remove %s\n", name);
|
|
|
|
else
|
|
|
|
printf("\tremoved %s\n", name);
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable queuing to the printer (allow lpr's).
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
enable(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
int c;
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp1, *cp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
char prbuf[100];
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("Usage: enable {all | printer ...}\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (argc == 2 && !strcmp(argv[1], "all")) {
|
|
|
|
printer = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
while (cgetnext(&bp, printcapdb) > 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cp1 = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
cp2 = bp;
|
1997-10-05 19:11:58 +04:00
|
|
|
while ((c = *cp2++) && c != '|' && c != ':' &&
|
2009-01-18 12:57:26 +03:00
|
|
|
(size_t)(cp1 - prbuf) < sizeof(prbuf))
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp1++ = c;
|
|
|
|
*cp1 = '\0';
|
|
|
|
enablepr();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (--argc) {
|
|
|
|
printer = *++argv;
|
2005-11-28 06:26:06 +03:00
|
|
|
if (!getcapdesc())
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
enablepr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
static int
|
|
|
|
getcapdesc(void)
|
|
|
|
{
|
|
|
|
int st;
|
|
|
|
if ((st = cgetent(&bp, printcapdb, printer)) == -2) {
|
|
|
|
printf("cannot open printer description file\n");
|
|
|
|
return 0;
|
|
|
|
} else if (st == -1) {
|
|
|
|
printf("unknown printer %s\n", printer);
|
|
|
|
return 0;
|
|
|
|
} else if (st == -3)
|
|
|
|
fatal("potential reference loop detected in printcap file");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
2002-07-14 19:27:58 +04:00
|
|
|
enablepr(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct stat stbuf;
|
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
getcaps();
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("%s:\n", printer);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn off the group execute bit of the lock file to enable queuing.
|
|
|
|
*/
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (stat(line, &stbuf) >= 0) {
|
|
|
|
if (chmod(line, stbuf.st_mode & 0767) < 0)
|
|
|
|
printf("\tcannot enable queuing\n");
|
|
|
|
else
|
|
|
|
printf("\tqueuing enabled\n");
|
|
|
|
}
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable queuing.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
disable(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
int c;
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp1, *cp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
char prbuf[100];
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("Usage: disable {all | printer ...}\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (argc == 2 && !strcmp(argv[1], "all")) {
|
|
|
|
printer = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
while (cgetnext(&bp, printcapdb) > 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cp1 = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
cp2 = bp;
|
1997-10-05 19:11:58 +04:00
|
|
|
while ((c = *cp2++) && c != '|' && c != ':' &&
|
2009-01-18 12:57:26 +03:00
|
|
|
(size_t)(cp1 - prbuf) < sizeof(prbuf))
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp1++ = c;
|
|
|
|
*cp1 = '\0';
|
|
|
|
disablepr();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (--argc) {
|
|
|
|
printer = *++argv;
|
2005-11-28 06:26:06 +03:00
|
|
|
if (!getcapdesc())
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
disablepr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
2002-07-14 19:27:58 +04:00
|
|
|
disablepr(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
int fd;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stat stbuf;
|
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
getcaps();
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("%s:\n", printer);
|
|
|
|
/*
|
|
|
|
* Turn on the group execute bit of the lock file to disable queuing.
|
|
|
|
*/
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (stat(line, &stbuf) >= 0) {
|
|
|
|
if (chmod(line, (stbuf.st_mode & 0777) | 010) < 0)
|
|
|
|
printf("\tcannot disable queuing\n");
|
|
|
|
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");
|
|
|
|
else {
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)close(fd);
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("\tqueuing disabled\n");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
printf("\tcannot stat lock file\n");
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable queuing and printing and put a message into the status file
|
|
|
|
* (reason for being down).
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
down(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
int c;
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp1, *cp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
char prbuf[100];
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("Usage: down {all | printer} [message ...]\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[1], "all")) {
|
|
|
|
printer = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
while (cgetnext(&bp, printcapdb) > 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cp1 = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
cp2 = bp;
|
1997-10-05 19:11:58 +04:00
|
|
|
while ((c = *cp2++) && c != '|' && c != ':' &&
|
2009-01-18 12:57:26 +03:00
|
|
|
(size_t)(cp1 - prbuf) < sizeof(prbuf))
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp1++ = c;
|
|
|
|
*cp1 = '\0';
|
|
|
|
putmsg(argc - 2, argv + 2);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printer = argv[1];
|
2005-11-28 06:26:06 +03:00
|
|
|
if (!getcapdesc())
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
putmsg(argc - 2, argv + 2);
|
|
|
|
}
|
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
static void
|
|
|
|
getcaps(void)
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
SD = cgetstr(bp, "sd", &cp) == -1 ? _PATH_DEFSPOOL : cp;
|
|
|
|
LO = cgetstr(bp, "lo", &cp) == -1 ? DEFLOCK : cp;
|
|
|
|
ST = cgetstr(bp, "st", &cp) == -1 ? DEFSTAT : cp;
|
|
|
|
(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
2002-07-14 19:27:58 +04:00
|
|
|
putmsg(int argc, char **argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
int fd;
|
|
|
|
char *cp1, *cp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
char buf[1024];
|
|
|
|
struct stat stbuf;
|
|
|
|
|
|
|
|
printf("%s:\n", printer);
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (stat(line, &stbuf) >= 0) {
|
|
|
|
if (chmod(line, (stbuf.st_mode & 0777) | 0110) < 0)
|
|
|
|
printf("\tcannot disable queuing\n");
|
|
|
|
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");
|
|
|
|
else {
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)close(fd);
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("\tprinter and queuing disabled\n");
|
|
|
|
}
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
} else
|
|
|
|
printf("\tcannot stat lock file\n");
|
|
|
|
/*
|
|
|
|
* Write the message into the status file.
|
|
|
|
*/
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)snprintf(line, sizeof(line), "%s/%s", SD, ST);
|
1993-03-21 12:45:37 +03:00
|
|
|
fd = open(line, O_WRONLY|O_CREAT, 0664);
|
|
|
|
if (fd < 0 || flock(fd, LOCK_EX) < 0) {
|
|
|
|
printf("\tcannot create status file\n");
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(uid);
|
2011-01-04 12:43:39 +03:00
|
|
|
if (fd >= 0)
|
|
|
|
(void)close(fd);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(uid);
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)ftruncate(fd, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (argc <= 0) {
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)write(fd, "\n", 1);
|
|
|
|
(void)close(fd);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
cp1 = buf;
|
|
|
|
while (--argc >= 0) {
|
|
|
|
cp2 = *argv++;
|
2009-01-18 12:57:26 +03:00
|
|
|
while ((size_t)(cp1 - buf) < sizeof(buf) && (*cp1++ = *cp2++))
|
1993-03-21 12:45:37 +03:00
|
|
|
;
|
|
|
|
cp1[-1] = ' ';
|
|
|
|
}
|
|
|
|
cp1[-1] = '\n';
|
|
|
|
*cp1 = '\0';
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)write(fd, buf, strlen(buf));
|
|
|
|
(void)close(fd);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exit lpc
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
quit(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Kill and restart the daemon.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
restart(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
int c;
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp1, *cp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
char prbuf[100];
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("Usage: restart {all | printer ...}\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (argc == 2 && !strcmp(argv[1], "all")) {
|
|
|
|
printer = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
while (cgetnext(&bp, printcapdb) > 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cp1 = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
cp2 = bp;
|
1997-10-05 19:11:58 +04:00
|
|
|
while ((c = *cp2++) && c != '|' && c != ':' &&
|
2009-01-18 12:57:26 +03:00
|
|
|
(size_t)(cp1 - prbuf) < sizeof(prbuf))
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp1++ = c;
|
|
|
|
*cp1 = '\0';
|
|
|
|
abortpr(0);
|
|
|
|
startpr(0);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (--argc) {
|
|
|
|
printer = *++argv;
|
2005-11-28 06:26:06 +03:00
|
|
|
if (!getcapdesc())
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
abortpr(0);
|
|
|
|
startpr(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable printing on the specified printer and startup the daemon.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
startcmd(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
int c;
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp1, *cp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
char prbuf[100];
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("Usage: start {all | printer ...}\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (argc == 2 && !strcmp(argv[1], "all")) {
|
|
|
|
printer = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
while (cgetnext(&bp, printcapdb) > 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cp1 = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
cp2 = bp;
|
1997-10-05 19:11:58 +04:00
|
|
|
while ((c = *cp2++) && c != '|' && c != ':' &&
|
2009-01-18 12:57:26 +03:00
|
|
|
(size_t)(cp1 - prbuf) < sizeof(prbuf))
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp1++ = c;
|
|
|
|
*cp1 = '\0';
|
|
|
|
startpr(1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (--argc) {
|
|
|
|
printer = *++argv;
|
2005-11-28 06:26:06 +03:00
|
|
|
if (!getcapdesc())
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
startpr(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
2005-11-28 06:26:06 +03:00
|
|
|
startpr(int ena)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct stat stbuf;
|
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
getcaps();
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("%s:\n", printer);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn off the owner execute bit of the lock file to enable printing.
|
|
|
|
*/
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
2005-11-28 06:26:06 +03:00
|
|
|
if (ena && stat(line, &stbuf) >= 0) {
|
|
|
|
if (chmod(line, stbuf.st_mode & (ena == 2 ? 0666 : 0677)) < 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("\tcannot enable printing\n");
|
|
|
|
else
|
|
|
|
printf("\tprinting enabled\n");
|
|
|
|
}
|
|
|
|
if (!startdaemon(printer))
|
|
|
|
printf("\tcouldn't start daemon\n");
|
|
|
|
else
|
|
|
|
printf("\tdaemon started\n");
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print the status of each queue listed or all the queues.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
status(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
int c;
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp1, *cp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
char prbuf[100];
|
|
|
|
|
1997-10-05 19:11:58 +04:00
|
|
|
if (argc == 1 || (argc == 2 && strcmp(argv[1], "all") == 0)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
printer = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
while (cgetnext(&bp, printcapdb) > 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cp1 = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
cp2 = bp;
|
1997-10-05 19:11:58 +04:00
|
|
|
while ((c = *cp2++) && c != '|' && c != ':' &&
|
2009-01-18 12:57:26 +03:00
|
|
|
(size_t)(cp1 - prbuf) < sizeof(prbuf))
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp1++ = c;
|
|
|
|
*cp1 = '\0';
|
|
|
|
prstat();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (--argc) {
|
|
|
|
printer = *++argv;
|
2005-11-28 06:26:06 +03:00
|
|
|
if (!getcapdesc())
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
prstat();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print the status of the printer queue.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
2002-07-14 19:27:58 +04:00
|
|
|
prstat(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct stat stbuf;
|
1997-10-05 19:11:58 +04:00
|
|
|
int fd, i;
|
|
|
|
struct dirent *dp;
|
1993-03-21 12:45:37 +03:00
|
|
|
DIR *dirp;
|
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
getcaps();
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("%s:\n", printer);
|
|
|
|
if (stat(line, &stbuf) >= 0) {
|
|
|
|
printf("\tqueuing is %s\n",
|
|
|
|
(stbuf.st_mode & 010) ? "disabled" : "enabled");
|
|
|
|
printf("\tprinting is %s\n",
|
|
|
|
(stbuf.st_mode & 0100) ? "disabled" : "enabled");
|
|
|
|
} else {
|
|
|
|
printf("\tqueuing is enabled\n");
|
|
|
|
printf("\tprinting is enabled\n");
|
|
|
|
}
|
|
|
|
if ((dirp = opendir(SD)) == NULL) {
|
|
|
|
printf("\tcannot examine spool directory\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
|
|
|
if (*dp->d_name == 'c' && dp->d_name[1] == 'f')
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
if (i == 0)
|
|
|
|
printf("\tno entries\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);
|
|
|
|
if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
|
2006-03-22 01:49:43 +03:00
|
|
|
if (fd >= 0)
|
|
|
|
(void)close(fd); /* unlocks as well */
|
1996-01-14 20:25:21 +03:00
|
|
|
printf("\tprinter idle\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)close(fd);
|
|
|
|
(void)snprintf(line, sizeof(line), "%s/%s", SD, ST);
|
1993-03-21 12:45:37 +03:00
|
|
|
fd = open(line, O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)flock(fd, LOCK_SH);
|
1997-10-05 19:11:58 +04:00
|
|
|
(void)fstat(fd, &stbuf);
|
|
|
|
if (stbuf.st_size > 0) {
|
|
|
|
putchar('\t');
|
|
|
|
while ((i = read(fd, line, sizeof(line))) > 0)
|
|
|
|
(void)fwrite(line, 1, i, stdout);
|
|
|
|
}
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)close(fd); /* unlocks as well */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stop the specified daemon after completing the current job and disable
|
|
|
|
* printing.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
stop(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
int c;
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp1, *cp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
char prbuf[100];
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("Usage: stop {all | printer ...}\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (argc == 2 && !strcmp(argv[1], "all")) {
|
|
|
|
printer = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
while (cgetnext(&bp, printcapdb) > 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cp1 = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
cp2 = bp;
|
1997-10-05 19:11:58 +04:00
|
|
|
while ((c = *cp2++) && c != '|' && c != ':' &&
|
2009-01-18 12:57:26 +03:00
|
|
|
(size_t)(cp1 - prbuf) < sizeof(prbuf))
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp1++ = c;
|
|
|
|
*cp1 = '\0';
|
|
|
|
stoppr();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (--argc) {
|
|
|
|
printer = *++argv;
|
2005-11-28 06:26:06 +03:00
|
|
|
if (!getcapdesc())
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
stoppr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
2002-07-14 19:27:58 +04:00
|
|
|
stoppr(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
int fd;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stat stbuf;
|
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
getcaps();
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("%s:\n", printer);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn on the owner execute bit of the lock file to disable printing.
|
|
|
|
*/
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (stat(line, &stbuf) >= 0) {
|
|
|
|
if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
|
|
|
|
printf("\tcannot disable printing\n");
|
|
|
|
else {
|
|
|
|
upstat("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");
|
|
|
|
else {
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)close(fd);
|
1993-03-21 12:45:37 +03:00
|
|
|
upstat("printing disabled\n");
|
|
|
|
printf("\tprinting disabled\n");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
printf("\tcannot stat lock file\n");
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct queue **queue;
|
|
|
|
int nitems;
|
|
|
|
time_t mtime;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put the specified jobs at the top of printer queue.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
topq(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
int i;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stat stbuf;
|
2005-11-28 06:26:06 +03:00
|
|
|
int changed;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
printf("Usage: topq printer [jobnum ...] [user ...]\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
--argc;
|
|
|
|
printer = *++argv;
|
2005-11-28 06:26:06 +03:00
|
|
|
if (!getcapdesc())
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
1994-05-18 05:25:37 +04:00
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
getcaps();
|
1993-03-21 12:45:37 +03:00
|
|
|
printf("%s:\n", printer);
|
|
|
|
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (chdir(SD) < 0) {
|
|
|
|
printf("\tcannot chdir to %s\n", SD);
|
1995-10-03 18:02:17 +03:00
|
|
|
goto out;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
nitems = getq(&queue);
|
|
|
|
if (nitems == 0)
|
|
|
|
return;
|
|
|
|
changed = 0;
|
|
|
|
mtime = queue[0]->q_time;
|
|
|
|
for (i = argc; --i; ) {
|
|
|
|
if (doarg(argv[i]) == 0) {
|
|
|
|
printf("\tjob %s is not in the queue\n", argv[i]);
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
changed++;
|
|
|
|
}
|
|
|
|
for (i = 0; i < nitems; i++)
|
|
|
|
free(queue[i]);
|
|
|
|
free(queue);
|
|
|
|
if (!changed) {
|
|
|
|
printf("\tqueue order unchanged\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Turn on the public execute bit of the lock file to
|
|
|
|
* get lpd to rebuild the queue after the current job.
|
|
|
|
*/
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (changed && stat(LO, &stbuf) >= 0)
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)chmod(LO, (stbuf.st_mode & 0777) | 01);
|
1995-10-03 18:02:17 +03:00
|
|
|
|
|
|
|
out:
|
|
|
|
seteuid(uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reposition the job by changing the modification time of
|
|
|
|
* the control file.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static int
|
2002-07-14 19:27:58 +04:00
|
|
|
touch(struct queue *q)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct timeval tvp[2];
|
1995-10-03 18:02:17 +03:00
|
|
|
int ret;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
tvp[0].tv_sec = tvp[1].tv_sec = --mtime;
|
|
|
|
tvp[0].tv_usec = tvp[1].tv_usec = 0;
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
|
|
|
ret = utimes(q->q_name, tvp);
|
|
|
|
seteuid(uid);
|
|
|
|
return (ret);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks if specified job name is in the printer's queue.
|
|
|
|
* Returns: negative (-1) if argument name is not in the queue.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static int
|
2005-11-28 06:26:06 +03:00
|
|
|
doarg(const char *job)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
struct queue **qq;
|
|
|
|
int jobnum, n;
|
2005-11-28 06:26:06 +03:00
|
|
|
char *cp;
|
|
|
|
const char *machine;
|
1993-03-21 12:45:37 +03:00
|
|
|
int cnt = 0;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for a job item consisting of system name, colon, number
|
|
|
|
* (example: ucbarpa:114)
|
|
|
|
*/
|
1997-10-05 19:11:58 +04:00
|
|
|
if ((cp = strchr(job, ':')) != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
machine = job;
|
|
|
|
*cp++ = '\0';
|
|
|
|
job = cp;
|
|
|
|
} else
|
|
|
|
machine = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for job specified by number (example: 112 or 235ucbarpa).
|
|
|
|
*/
|
2004-10-30 12:44:25 +04:00
|
|
|
if (isdigit((unsigned char)*job)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
jobnum = 0;
|
|
|
|
do
|
|
|
|
jobnum = jobnum * 10 + (*job++ - '0');
|
2004-10-30 12:44:25 +04:00
|
|
|
while (isdigit((unsigned char)*job));
|
1993-03-21 12:45:37 +03:00
|
|
|
for (qq = queue + nitems; --qq >= queue; ) {
|
|
|
|
n = 0;
|
2004-10-30 12:44:25 +04:00
|
|
|
for (cp = (*qq)->q_name+3; isdigit((unsigned char)*cp); )
|
1993-03-21 12:45:37 +03:00
|
|
|
n = n * 10 + (*cp++ - '0');
|
|
|
|
if (jobnum != n)
|
|
|
|
continue;
|
|
|
|
if (*job && strcmp(job, cp) != 0)
|
|
|
|
continue;
|
|
|
|
if (machine != NULL && strcmp(machine, cp) != 0)
|
|
|
|
continue;
|
|
|
|
if (touch(*qq) == 0) {
|
|
|
|
printf("\tmoved %s\n", (*qq)->q_name);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(cnt);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Process item consisting of owner's name (example: henry).
|
|
|
|
*/
|
|
|
|
for (qq = queue + nitems; --qq >= queue; ) {
|
1995-10-03 18:02:17 +03:00
|
|
|
seteuid(euid);
|
|
|
|
fp = fopen((*qq)->q_name, "r");
|
|
|
|
seteuid(uid);
|
|
|
|
if (fp == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
2009-07-13 23:05:39 +04:00
|
|
|
while (get_line(fp) > 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
if (line[0] == 'P')
|
|
|
|
break;
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)fclose(fp);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (line[0] != 'P' || strcmp(job, line+1) != 0)
|
|
|
|
continue;
|
|
|
|
if (touch(*qq) == 0) {
|
|
|
|
printf("\tmoved %s\n", (*qq)->q_name);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable everything and start printer (undo `down').
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
void
|
2002-07-14 19:27:58 +04:00
|
|
|
up(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
int c;
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp1, *cp2;
|
1993-03-21 12:45:37 +03:00
|
|
|
char prbuf[100];
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("Usage: up {all | printer ...}\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (argc == 2 && !strcmp(argv[1], "all")) {
|
|
|
|
printer = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
while (cgetnext(&bp, printcapdb) > 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cp1 = prbuf;
|
1994-05-18 05:25:37 +04:00
|
|
|
cp2 = bp;
|
1997-10-05 19:11:58 +04:00
|
|
|
while ((c = *cp2++) && c != '|' && c != ':' &&
|
2009-01-18 12:57:26 +03:00
|
|
|
(size_t)(cp1 - prbuf) < sizeof(prbuf))
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp1++ = c;
|
|
|
|
*cp1 = '\0';
|
|
|
|
startpr(2);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (--argc) {
|
|
|
|
printer = *++argv;
|
2005-11-28 06:26:06 +03:00
|
|
|
if (!getcapdesc())
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
startpr(2);
|
|
|
|
}
|
|
|
|
}
|