Home | History | Annotate | Line # | Download | only in lpc
cmds.c revision 1.16
      1  1.16      agc /*	$NetBSD: cmds.c,v 1.16 2003/08/07 11:25:26 agc Exp $	*/
      2   1.1      cgd /*
      3   1.4      cgd  * Copyright (c) 1983, 1993
      4   1.4      cgd  *	The Regents of the University of California.  All rights reserved.
      5   1.4      cgd  *
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15  1.16      agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1      cgd  *    may be used to endorse or promote products derived from this software
     17   1.1      cgd  *    without specific prior written permission.
     18   1.1      cgd  *
     19   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1      cgd  * SUCH DAMAGE.
     30   1.1      cgd  */
     31   1.1      cgd 
     32  1.12      mrg #include <sys/cdefs.h>
     33   1.1      cgd #ifndef lint
     34  1.12      mrg __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     35  1.12      mrg 	The Regents of the University of California.  All rights reserved.\n");
     36  1.10    mikel #if 0
     37  1.11      mrg static char sccsid[] = "@(#)cmds.c	8.2 (Berkeley) 4/28/95";
     38  1.10    mikel #else
     39  1.16      agc __RCSID("$NetBSD: cmds.c,v 1.16 2003/08/07 11:25:26 agc Exp $");
     40  1.10    mikel #endif
     41   1.1      cgd #endif /* not lint */
     42   1.1      cgd 
     43   1.1      cgd /*
     44   1.1      cgd  * lpc -- line printer control program -- commands:
     45   1.1      cgd  */
     46   1.1      cgd 
     47   1.4      cgd #include <sys/param.h>
     48   1.4      cgd #include <sys/time.h>
     49   1.4      cgd #include <sys/stat.h>
     50  1.11      mrg #include <sys/file.h>
     51   1.4      cgd 
     52   1.4      cgd #include <signal.h>
     53   1.4      cgd #include <fcntl.h>
     54   1.4      cgd #include <errno.h>
     55   1.4      cgd #include <dirent.h>
     56   1.4      cgd #include <unistd.h>
     57   1.4      cgd #include <stdlib.h>
     58   1.4      cgd #include <stdio.h>
     59   1.4      cgd #include <ctype.h>
     60   1.4      cgd #include <string.h>
     61   1.1      cgd #include "lp.h"
     62   1.4      cgd #include "lp.local.h"
     63   1.4      cgd #include "lpc.h"
     64   1.4      cgd #include "extern.h"
     65   1.1      cgd #include "pathnames.h"
     66   1.1      cgd 
     67   1.5  hpeyerl extern uid_t	uid, euid;
     68   1.5  hpeyerl 
     69  1.14      wiz static void	abortpr(int);
     70  1.14      wiz static void	cleanpr(void);
     71  1.14      wiz static void	disablepr(void);
     72  1.14      wiz static int	doarg(char *);
     73  1.14      wiz static int	doselect(const struct dirent *);
     74  1.14      wiz static void	enablepr(void);
     75  1.14      wiz static void	prstat(void);
     76  1.14      wiz static void	putmsg(int, char **);
     77  1.14      wiz static int	sortq(const void *, const void *);
     78  1.14      wiz static void	startpr(int);
     79  1.14      wiz static void	stoppr(void);
     80  1.14      wiz static int	touch(struct queue *);
     81  1.14      wiz static void	unlinkf(char *);
     82  1.14      wiz static void	upstat(char *);
     83   1.4      cgd 
     84   1.1      cgd /*
     85   1.1      cgd  * kill an existing daemon and disable printing.
     86   1.1      cgd  */
     87   1.4      cgd void
     88  1.14      wiz doabort(int argc, char *argv[])
     89   1.1      cgd {
     90  1.12      mrg 	int c, status;
     91  1.12      mrg 	char *cp1, *cp2;
     92   1.1      cgd 	char prbuf[100];
     93   1.1      cgd 
     94   1.1      cgd 	if (argc == 1) {
     95   1.1      cgd 		printf("Usage: abort {all | printer ...}\n");
     96   1.1      cgd 		return;
     97   1.1      cgd 	}
     98   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
     99   1.1      cgd 		printer = prbuf;
    100   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    101   1.1      cgd 			cp1 = prbuf;
    102   1.4      cgd 			cp2 = bp;
    103  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    104  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    105   1.1      cgd 				*cp1++ = c;
    106   1.1      cgd 			*cp1 = '\0';
    107   1.1      cgd 			abortpr(1);
    108   1.1      cgd 		}
    109   1.1      cgd 		return;
    110   1.1      cgd 	}
    111   1.1      cgd 	while (--argc) {
    112   1.1      cgd 		printer = *++argv;
    113   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    114   1.1      cgd 			printf("cannot open printer description file\n");
    115   1.1      cgd 			continue;
    116   1.4      cgd 		} else if (status == -1) {
    117   1.1      cgd 			printf("unknown printer %s\n", printer);
    118   1.1      cgd 			continue;
    119   1.4      cgd 		} else if (status == -3)
    120   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    121   1.1      cgd 		abortpr(1);
    122   1.1      cgd 	}
    123   1.1      cgd }
    124   1.1      cgd 
    125   1.4      cgd static void
    126  1.14      wiz abortpr(int dis)
    127   1.1      cgd {
    128  1.12      mrg 	FILE *fp;
    129   1.1      cgd 	struct stat stbuf;
    130  1.12      mrg 	int pid, fd;
    131   1.1      cgd 
    132   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    133   1.1      cgd 		SD = _PATH_DEFSPOOL;
    134   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    135   1.1      cgd 		LO = DEFLOCK;
    136   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    137   1.1      cgd 	printf("%s:\n", printer);
    138   1.1      cgd 
    139   1.1      cgd 	/*
    140   1.1      cgd 	 * Turn on the owner execute bit of the lock file to disable printing.
    141   1.1      cgd 	 */
    142   1.1      cgd 	if (dis) {
    143   1.5  hpeyerl 		seteuid(euid);
    144   1.1      cgd 		if (stat(line, &stbuf) >= 0) {
    145   1.1      cgd 			if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
    146   1.1      cgd 				printf("\tcannot disable printing\n");
    147   1.1      cgd 			else {
    148   1.1      cgd 				upstat("printing disabled\n");
    149   1.1      cgd 				printf("\tprinting disabled\n");
    150   1.1      cgd 			}
    151   1.1      cgd 		} else if (errno == ENOENT) {
    152   1.1      cgd 			if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
    153   1.1      cgd 				printf("\tcannot create lock file\n");
    154   1.1      cgd 			else {
    155   1.8      mrg 				(void)close(fd);
    156   1.1      cgd 				upstat("printing disabled\n");
    157   1.1      cgd 				printf("\tprinting disabled\n");
    158   1.1      cgd 				printf("\tno daemon to abort\n");
    159   1.1      cgd 			}
    160   1.5  hpeyerl 			goto out;
    161   1.1      cgd 		} else {
    162   1.1      cgd 			printf("\tcannot stat lock file\n");
    163   1.5  hpeyerl 			goto out;
    164   1.1      cgd 		}
    165   1.1      cgd 	}
    166   1.1      cgd 	/*
    167   1.1      cgd 	 * Kill the current daemon to stop printing now.
    168   1.1      cgd 	 */
    169   1.1      cgd 	if ((fp = fopen(line, "r")) == NULL) {
    170   1.1      cgd 		printf("\tcannot open lock file\n");
    171   1.5  hpeyerl 		goto out;
    172   1.1      cgd 	}
    173   1.1      cgd 	if (!getline(fp) || flock(fileno(fp), LOCK_SH|LOCK_NB) == 0) {
    174   1.8      mrg 		(void)fclose(fp);	/* unlocks as well */
    175   1.1      cgd 		printf("\tno daemon to abort\n");
    176   1.5  hpeyerl 		goto out;
    177   1.1      cgd 	}
    178   1.8      mrg 	(void)fclose(fp);
    179  1.12      mrg 	if (kill(pid = atoi(line), SIGTERM) < 0) {
    180  1.12      mrg 		if (errno == ESRCH)
    181  1.12      mrg 			printf("\tno daemon to abort\n");
    182  1.12      mrg 		else
    183  1.12      mrg 			printf("\tWarning: daemon (pid %d) not killed\n", pid);
    184  1.12      mrg 	} else
    185   1.1      cgd 		printf("\tdaemon (pid %d) killed\n", pid);
    186   1.5  hpeyerl out:
    187   1.5  hpeyerl 	seteuid(uid);
    188   1.1      cgd }
    189   1.1      cgd 
    190   1.1      cgd /*
    191   1.1      cgd  * Write a message into the status file.
    192   1.1      cgd  */
    193   1.4      cgd static void
    194  1.14      wiz upstat(char *msg)
    195   1.1      cgd {
    196  1.12      mrg 	int fd;
    197  1.12      mrg 	char statfile[MAXPATHLEN];
    198   1.1      cgd 
    199   1.4      cgd 	if (cgetstr(bp, "st", &ST) == -1)
    200   1.1      cgd 		ST = DEFSTAT;
    201   1.8      mrg 	(void)snprintf(statfile, sizeof(statfile), "%s/%s", SD, ST);
    202   1.1      cgd 	umask(0);
    203   1.1      cgd 	fd = open(statfile, O_WRONLY|O_CREAT, 0664);
    204   1.1      cgd 	if (fd < 0 || flock(fd, LOCK_EX) < 0) {
    205   1.1      cgd 		printf("\tcannot create status file\n");
    206   1.1      cgd 		return;
    207   1.1      cgd 	}
    208   1.8      mrg 	(void)ftruncate(fd, 0);
    209   1.1      cgd 	if (msg == (char *)NULL)
    210   1.8      mrg 		(void)write(fd, "\n", 1);
    211   1.1      cgd 	else
    212   1.8      mrg 		(void)write(fd, msg, strlen(msg));
    213   1.8      mrg 	(void)close(fd);
    214   1.1      cgd }
    215   1.1      cgd 
    216   1.1      cgd /*
    217   1.1      cgd  * Remove all spool files and temporaries from the spooling area.
    218   1.1      cgd  */
    219   1.4      cgd void
    220  1.14      wiz clean(int argc, char *argv[])
    221   1.1      cgd {
    222  1.12      mrg 	int c, status;
    223  1.12      mrg 	char *cp1, *cp2;
    224   1.1      cgd 	char prbuf[100];
    225   1.1      cgd 
    226   1.1      cgd 	if (argc == 1) {
    227   1.1      cgd 		printf("Usage: clean {all | printer ...}\n");
    228   1.1      cgd 		return;
    229   1.1      cgd 	}
    230   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
    231   1.1      cgd 		printer = prbuf;
    232   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    233   1.1      cgd 			cp1 = prbuf;
    234   1.4      cgd 			cp2 = bp;
    235  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    236  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    237   1.1      cgd 				*cp1++ = c;
    238   1.1      cgd 			*cp1 = '\0';
    239   1.1      cgd 			cleanpr();
    240   1.1      cgd 		}
    241   1.1      cgd 		return;
    242   1.1      cgd 	}
    243   1.1      cgd 	while (--argc) {
    244   1.1      cgd 		printer = *++argv;
    245   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    246   1.1      cgd 			printf("cannot open printer description file\n");
    247   1.1      cgd 			continue;
    248   1.4      cgd 		} else if (status == -1) {
    249   1.1      cgd 			printf("unknown printer %s\n", printer);
    250   1.1      cgd 			continue;
    251   1.4      cgd 		} else if (status == -3)
    252   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    253   1.4      cgd 
    254   1.1      cgd 		cleanpr();
    255   1.1      cgd 	}
    256   1.1      cgd }
    257   1.1      cgd 
    258   1.4      cgd static int
    259  1.14      wiz doselect(const struct dirent *d)
    260   1.1      cgd {
    261   1.1      cgd 	int c = d->d_name[0];
    262   1.1      cgd 
    263   1.1      cgd 	if ((c == 't' || c == 'c' || c == 'd') && d->d_name[1] == 'f')
    264   1.1      cgd 		return(1);
    265   1.1      cgd 	return(0);
    266   1.1      cgd }
    267   1.1      cgd 
    268   1.1      cgd /*
    269   1.1      cgd  * Comparison routine for scandir. Sort by job number and machine, then
    270   1.1      cgd  * by `cf', `tf', or `df', then by the sequence letter A-Z, a-z.
    271   1.1      cgd  */
    272   1.4      cgd static int
    273  1.14      wiz sortq(const void *a, const void *b)
    274   1.1      cgd {
    275  1.13      mrg 	const struct dirent **d1, **d2;
    276   1.1      cgd 	int c1, c2;
    277   1.1      cgd 
    278  1.13      mrg 	d1 = (const struct dirent **)a;
    279  1.13      mrg 	d2 = (const struct dirent **)b;
    280   1.9    mikel 	if ((c1 = strcmp((*d1)->d_name + 3, (*d2)->d_name + 3)) != 0)
    281   1.1      cgd 		return(c1);
    282   1.1      cgd 	c1 = (*d1)->d_name[0];
    283   1.1      cgd 	c2 = (*d2)->d_name[0];
    284   1.1      cgd 	if (c1 == c2)
    285   1.1      cgd 		return((*d1)->d_name[2] - (*d2)->d_name[2]);
    286   1.1      cgd 	if (c1 == 'c')
    287   1.1      cgd 		return(-1);
    288   1.1      cgd 	if (c1 == 'd' || c2 == 'c')
    289   1.1      cgd 		return(1);
    290   1.1      cgd 	return(-1);
    291   1.1      cgd }
    292   1.1      cgd 
    293   1.1      cgd /*
    294   1.1      cgd  * Remove incomplete jobs from spooling area.
    295   1.1      cgd  */
    296   1.4      cgd static void
    297  1.14      wiz cleanpr(void)
    298   1.1      cgd {
    299  1.12      mrg 	int i, n;
    300  1.15   itojun 	char *cp, *cp1, *lp, *ep;
    301   1.3      jtc 	struct dirent **queue;
    302   1.1      cgd 	int nitems;
    303   1.1      cgd 
    304   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    305   1.1      cgd 		SD = _PATH_DEFSPOOL;
    306   1.1      cgd 	printf("%s:\n", printer);
    307   1.1      cgd 
    308   1.8      mrg 	/* XXX depends on SD being non nul */
    309  1.15   itojun 	ep = line + sizeof(line);
    310  1.12      mrg 	for (lp = line, cp = SD; (lp - line) < sizeof(line) &&
    311  1.12      mrg 	    (*lp++ = *cp++) != '\0'; )
    312   1.1      cgd 		;
    313   1.1      cgd 	lp[-1] = '/';
    314   1.1      cgd 
    315   1.5  hpeyerl 	seteuid(euid);
    316   1.4      cgd 	nitems = scandir(SD, &queue, doselect, sortq);
    317   1.5  hpeyerl 	seteuid(uid);
    318   1.1      cgd 	if (nitems < 0) {
    319   1.1      cgd 		printf("\tcannot examine spool directory\n");
    320   1.1      cgd 		return;
    321   1.1      cgd 	}
    322   1.1      cgd 	if (nitems == 0)
    323   1.1      cgd 		return;
    324   1.1      cgd 	i = 0;
    325   1.1      cgd 	do {
    326   1.1      cgd 		cp = queue[i]->d_name;
    327   1.1      cgd 		if (*cp == 'c') {
    328   1.1      cgd 			n = 0;
    329   1.1      cgd 			while (i + 1 < nitems) {
    330   1.1      cgd 				cp1 = queue[i + 1]->d_name;
    331   1.1      cgd 				if (*cp1 != 'd' || strcmp(cp + 3, cp1 + 3))
    332   1.1      cgd 					break;
    333   1.1      cgd 				i++;
    334   1.1      cgd 				n++;
    335   1.1      cgd 			}
    336   1.1      cgd 			if (n == 0) {
    337  1.15   itojun 				strlcpy(lp, cp, ep - lp);
    338   1.1      cgd 				unlinkf(line);
    339   1.1      cgd 			}
    340   1.1      cgd 		} else {
    341   1.1      cgd 			/*
    342   1.1      cgd 			 * Must be a df with no cf (otherwise, it would have
    343   1.1      cgd 			 * been skipped above) or a tf file (which can always
    344   1.1      cgd 			 * be removed).
    345   1.1      cgd 			 */
    346  1.15   itojun 			strlcpy(lp, cp, ep - lp);
    347   1.1      cgd 			unlinkf(line);
    348   1.1      cgd 		}
    349   1.1      cgd      	} while (++i < nitems);
    350   1.1      cgd }
    351   1.1      cgd 
    352   1.4      cgd static void
    353  1.14      wiz unlinkf(char *name)
    354   1.1      cgd {
    355   1.5  hpeyerl 	seteuid(euid);
    356   1.1      cgd 	if (unlink(name) < 0)
    357   1.1      cgd 		printf("\tcannot remove %s\n", name);
    358   1.1      cgd 	else
    359   1.1      cgd 		printf("\tremoved %s\n", name);
    360   1.5  hpeyerl 	seteuid(uid);
    361   1.1      cgd }
    362   1.1      cgd 
    363   1.1      cgd /*
    364   1.1      cgd  * Enable queuing to the printer (allow lpr's).
    365   1.1      cgd  */
    366   1.4      cgd void
    367  1.14      wiz enable(int argc, char *argv[])
    368   1.1      cgd {
    369  1.12      mrg 	int c, status;
    370  1.12      mrg 	char *cp1, *cp2;
    371   1.1      cgd 	char prbuf[100];
    372   1.1      cgd 
    373   1.1      cgd 	if (argc == 1) {
    374   1.1      cgd 		printf("Usage: enable {all | printer ...}\n");
    375   1.1      cgd 		return;
    376   1.1      cgd 	}
    377   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
    378   1.1      cgd 		printer = prbuf;
    379   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    380   1.1      cgd 			cp1 = prbuf;
    381   1.4      cgd 			cp2 = bp;
    382  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    383  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    384   1.1      cgd 				*cp1++ = c;
    385   1.1      cgd 			*cp1 = '\0';
    386   1.1      cgd 			enablepr();
    387   1.1      cgd 		}
    388   1.1      cgd 		return;
    389   1.1      cgd 	}
    390   1.1      cgd 	while (--argc) {
    391   1.1      cgd 		printer = *++argv;
    392   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    393   1.1      cgd 			printf("cannot open printer description file\n");
    394   1.1      cgd 			continue;
    395   1.4      cgd 		} else if (status == -1) {
    396   1.1      cgd 			printf("unknown printer %s\n", printer);
    397   1.1      cgd 			continue;
    398   1.4      cgd 		} else if (status == -3)
    399   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    400   1.4      cgd 
    401   1.1      cgd 		enablepr();
    402   1.1      cgd 	}
    403   1.1      cgd }
    404   1.1      cgd 
    405   1.4      cgd static void
    406  1.14      wiz enablepr(void)
    407   1.1      cgd {
    408   1.1      cgd 	struct stat stbuf;
    409   1.1      cgd 
    410   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    411   1.1      cgd 		SD = _PATH_DEFSPOOL;
    412   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    413   1.1      cgd 		LO = DEFLOCK;
    414   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    415   1.1      cgd 	printf("%s:\n", printer);
    416   1.1      cgd 
    417   1.1      cgd 	/*
    418   1.1      cgd 	 * Turn off the group execute bit of the lock file to enable queuing.
    419   1.1      cgd 	 */
    420   1.5  hpeyerl 	seteuid(euid);
    421   1.1      cgd 	if (stat(line, &stbuf) >= 0) {
    422   1.1      cgd 		if (chmod(line, stbuf.st_mode & 0767) < 0)
    423   1.1      cgd 			printf("\tcannot enable queuing\n");
    424   1.1      cgd 		else
    425   1.1      cgd 			printf("\tqueuing enabled\n");
    426   1.1      cgd 	}
    427   1.5  hpeyerl 	seteuid(uid);
    428   1.1      cgd }
    429   1.1      cgd 
    430   1.1      cgd /*
    431   1.1      cgd  * Disable queuing.
    432   1.1      cgd  */
    433   1.4      cgd void
    434  1.14      wiz disable(int argc, char *argv[])
    435   1.1      cgd {
    436  1.12      mrg 	int c, status;
    437  1.12      mrg 	char *cp1, *cp2;
    438   1.1      cgd 	char prbuf[100];
    439   1.1      cgd 
    440   1.1      cgd 	if (argc == 1) {
    441   1.1      cgd 		printf("Usage: disable {all | printer ...}\n");
    442   1.1      cgd 		return;
    443   1.1      cgd 	}
    444   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
    445   1.1      cgd 		printer = prbuf;
    446   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    447   1.1      cgd 			cp1 = prbuf;
    448   1.4      cgd 			cp2 = bp;
    449  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    450  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    451   1.1      cgd 				*cp1++ = c;
    452   1.1      cgd 			*cp1 = '\0';
    453   1.1      cgd 			disablepr();
    454   1.1      cgd 		}
    455   1.1      cgd 		return;
    456   1.1      cgd 	}
    457   1.1      cgd 	while (--argc) {
    458   1.1      cgd 		printer = *++argv;
    459   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    460   1.1      cgd 			printf("cannot open printer description file\n");
    461   1.1      cgd 			continue;
    462   1.4      cgd 		} else if (status == -1) {
    463   1.1      cgd 			printf("unknown printer %s\n", printer);
    464   1.1      cgd 			continue;
    465   1.4      cgd 		} else if (status == -3)
    466   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    467   1.4      cgd 
    468   1.1      cgd 		disablepr();
    469   1.1      cgd 	}
    470   1.1      cgd }
    471   1.1      cgd 
    472   1.4      cgd static void
    473  1.14      wiz disablepr(void)
    474   1.1      cgd {
    475  1.12      mrg 	int fd;
    476   1.1      cgd 	struct stat stbuf;
    477   1.1      cgd 
    478   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    479   1.1      cgd 		SD = _PATH_DEFSPOOL;
    480   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    481   1.1      cgd 		LO = DEFLOCK;
    482   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    483   1.1      cgd 	printf("%s:\n", printer);
    484   1.1      cgd 	/*
    485   1.1      cgd 	 * Turn on the group execute bit of the lock file to disable queuing.
    486   1.1      cgd 	 */
    487   1.5  hpeyerl 	seteuid(euid);
    488   1.1      cgd 	if (stat(line, &stbuf) >= 0) {
    489   1.1      cgd 		if (chmod(line, (stbuf.st_mode & 0777) | 010) < 0)
    490   1.1      cgd 			printf("\tcannot disable queuing\n");
    491   1.1      cgd 		else
    492   1.1      cgd 			printf("\tqueuing disabled\n");
    493   1.1      cgd 	} else if (errno == ENOENT) {
    494   1.1      cgd 		if ((fd = open(line, O_WRONLY|O_CREAT, 0670)) < 0)
    495   1.1      cgd 			printf("\tcannot create lock file\n");
    496   1.1      cgd 		else {
    497   1.8      mrg 			(void)close(fd);
    498   1.1      cgd 			printf("\tqueuing disabled\n");
    499   1.1      cgd 		}
    500   1.1      cgd 	} else
    501   1.1      cgd 		printf("\tcannot stat lock file\n");
    502   1.5  hpeyerl 	seteuid(uid);
    503   1.1      cgd }
    504   1.1      cgd 
    505   1.1      cgd /*
    506   1.1      cgd  * Disable queuing and printing and put a message into the status file
    507   1.1      cgd  * (reason for being down).
    508   1.1      cgd  */
    509   1.4      cgd void
    510  1.14      wiz down(int argc, char *argv[])
    511   1.1      cgd {
    512  1.12      mrg 	int c, status;
    513  1.12      mrg 	char *cp1, *cp2;
    514   1.1      cgd 	char prbuf[100];
    515   1.1      cgd 
    516   1.1      cgd 	if (argc == 1) {
    517   1.1      cgd 		printf("Usage: down {all | printer} [message ...]\n");
    518   1.1      cgd 		return;
    519   1.1      cgd 	}
    520   1.1      cgd 	if (!strcmp(argv[1], "all")) {
    521   1.1      cgd 		printer = prbuf;
    522   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    523   1.1      cgd 			cp1 = prbuf;
    524   1.4      cgd 			cp2 = bp;
    525  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    526  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    527   1.1      cgd 				*cp1++ = c;
    528   1.1      cgd 			*cp1 = '\0';
    529   1.1      cgd 			putmsg(argc - 2, argv + 2);
    530   1.1      cgd 		}
    531   1.1      cgd 		return;
    532   1.1      cgd 	}
    533   1.1      cgd 	printer = argv[1];
    534   1.4      cgd 	if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    535   1.1      cgd 		printf("cannot open printer description file\n");
    536   1.1      cgd 		return;
    537   1.4      cgd 	} else if (status == -1) {
    538   1.1      cgd 		printf("unknown printer %s\n", printer);
    539   1.1      cgd 		return;
    540   1.4      cgd 	} else if (status == -3)
    541   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    542   1.4      cgd 
    543   1.1      cgd 	putmsg(argc - 2, argv + 2);
    544   1.1      cgd }
    545   1.1      cgd 
    546   1.4      cgd static void
    547  1.14      wiz putmsg(int argc, char **argv)
    548   1.1      cgd {
    549  1.12      mrg 	int fd;
    550  1.12      mrg 	char *cp1, *cp2;
    551   1.1      cgd 	char buf[1024];
    552   1.1      cgd 	struct stat stbuf;
    553   1.1      cgd 
    554   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    555   1.1      cgd 		SD = _PATH_DEFSPOOL;
    556   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    557   1.1      cgd 		LO = DEFLOCK;
    558   1.4      cgd 	if (cgetstr(bp, "st", &ST) == -1)
    559   1.1      cgd 		ST = DEFSTAT;
    560   1.1      cgd 	printf("%s:\n", printer);
    561   1.1      cgd 	/*
    562   1.1      cgd 	 * Turn on the group execute bit of the lock file to disable queuing and
    563   1.1      cgd 	 * turn on the owner execute bit of the lock file to disable printing.
    564   1.1      cgd 	 */
    565   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    566   1.5  hpeyerl 	seteuid(euid);
    567   1.1      cgd 	if (stat(line, &stbuf) >= 0) {
    568   1.1      cgd 		if (chmod(line, (stbuf.st_mode & 0777) | 0110) < 0)
    569   1.1      cgd 			printf("\tcannot disable queuing\n");
    570   1.1      cgd 		else
    571   1.1      cgd 			printf("\tprinter and queuing disabled\n");
    572   1.1      cgd 	} else if (errno == ENOENT) {
    573   1.1      cgd 		if ((fd = open(line, O_WRONLY|O_CREAT, 0770)) < 0)
    574   1.1      cgd 			printf("\tcannot create lock file\n");
    575   1.1      cgd 		else {
    576   1.8      mrg 			(void)close(fd);
    577   1.1      cgd 			printf("\tprinter and queuing disabled\n");
    578   1.1      cgd 		}
    579   1.5  hpeyerl 		seteuid(uid);
    580   1.1      cgd 		return;
    581   1.1      cgd 	} else
    582   1.1      cgd 		printf("\tcannot stat lock file\n");
    583   1.1      cgd 	/*
    584   1.1      cgd 	 * Write the message into the status file.
    585   1.1      cgd 	 */
    586   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, ST);
    587   1.1      cgd 	fd = open(line, O_WRONLY|O_CREAT, 0664);
    588   1.1      cgd 	if (fd < 0 || flock(fd, LOCK_EX) < 0) {
    589   1.1      cgd 		printf("\tcannot create status file\n");
    590   1.5  hpeyerl 		seteuid(uid);
    591   1.1      cgd 		return;
    592   1.1      cgd 	}
    593   1.5  hpeyerl 	seteuid(uid);
    594   1.8      mrg 	(void)ftruncate(fd, 0);
    595   1.1      cgd 	if (argc <= 0) {
    596   1.8      mrg 		(void)write(fd, "\n", 1);
    597   1.8      mrg 		(void)close(fd);
    598   1.1      cgd 		return;
    599   1.1      cgd 	}
    600   1.1      cgd 	cp1 = buf;
    601   1.1      cgd 	while (--argc >= 0) {
    602   1.1      cgd 		cp2 = *argv++;
    603  1.12      mrg 		while ((cp1 - buf) < sizeof(buf) && (*cp1++ = *cp2++))
    604   1.1      cgd 			;
    605   1.1      cgd 		cp1[-1] = ' ';
    606   1.1      cgd 	}
    607   1.1      cgd 	cp1[-1] = '\n';
    608   1.1      cgd 	*cp1 = '\0';
    609   1.8      mrg 	(void)write(fd, buf, strlen(buf));
    610   1.8      mrg 	(void)close(fd);
    611   1.1      cgd }
    612   1.1      cgd 
    613   1.1      cgd /*
    614   1.1      cgd  * Exit lpc
    615   1.1      cgd  */
    616   1.4      cgd void
    617  1.14      wiz quit(int argc, char *argv[])
    618   1.1      cgd {
    619   1.1      cgd 	exit(0);
    620   1.1      cgd }
    621   1.1      cgd 
    622   1.1      cgd /*
    623   1.1      cgd  * Kill and restart the daemon.
    624   1.1      cgd  */
    625   1.4      cgd void
    626  1.14      wiz restart(int argc, char *argv[])
    627   1.1      cgd {
    628  1.12      mrg 	int c, status;
    629  1.12      mrg 	char *cp1, *cp2;
    630   1.1      cgd 	char prbuf[100];
    631   1.1      cgd 
    632   1.1      cgd 	if (argc == 1) {
    633   1.1      cgd 		printf("Usage: restart {all | printer ...}\n");
    634   1.1      cgd 		return;
    635   1.1      cgd 	}
    636   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
    637   1.1      cgd 		printer = prbuf;
    638   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    639   1.1      cgd 			cp1 = prbuf;
    640   1.4      cgd 			cp2 = bp;
    641  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    642  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    643   1.1      cgd 				*cp1++ = c;
    644   1.1      cgd 			*cp1 = '\0';
    645   1.1      cgd 			abortpr(0);
    646   1.1      cgd 			startpr(0);
    647   1.1      cgd 		}
    648   1.1      cgd 		return;
    649   1.1      cgd 	}
    650   1.1      cgd 	while (--argc) {
    651   1.1      cgd 		printer = *++argv;
    652   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    653   1.1      cgd 			printf("cannot open printer description file\n");
    654   1.1      cgd 			continue;
    655   1.4      cgd 		} else if (status == -1) {
    656   1.1      cgd 			printf("unknown printer %s\n", printer);
    657   1.1      cgd 			continue;
    658   1.4      cgd 		} else if (status == -3)
    659   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    660   1.4      cgd 
    661   1.1      cgd 		abortpr(0);
    662   1.1      cgd 		startpr(0);
    663   1.1      cgd 	}
    664   1.1      cgd }
    665   1.1      cgd 
    666   1.1      cgd /*
    667   1.1      cgd  * Enable printing on the specified printer and startup the daemon.
    668   1.1      cgd  */
    669   1.4      cgd void
    670  1.14      wiz startcmd(int argc, char *argv[])
    671   1.1      cgd {
    672  1.12      mrg 	int c, status;
    673  1.12      mrg 	char *cp1, *cp2;
    674   1.1      cgd 	char prbuf[100];
    675   1.1      cgd 
    676   1.1      cgd 	if (argc == 1) {
    677   1.1      cgd 		printf("Usage: start {all | printer ...}\n");
    678   1.1      cgd 		return;
    679   1.1      cgd 	}
    680   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
    681   1.1      cgd 		printer = prbuf;
    682   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    683   1.1      cgd 			cp1 = prbuf;
    684   1.4      cgd 			cp2 = bp;
    685  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    686  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    687   1.1      cgd 				*cp1++ = c;
    688   1.1      cgd 			*cp1 = '\0';
    689   1.1      cgd 			startpr(1);
    690   1.1      cgd 		}
    691   1.1      cgd 		return;
    692   1.1      cgd 	}
    693   1.1      cgd 	while (--argc) {
    694   1.1      cgd 		printer = *++argv;
    695   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    696   1.1      cgd 			printf("cannot open printer description file\n");
    697   1.1      cgd 			continue;
    698   1.4      cgd 		} else if (status == -1) {
    699   1.1      cgd 			printf("unknown printer %s\n", printer);
    700   1.1      cgd 			continue;
    701   1.4      cgd 		} else if (status == -3)
    702   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    703   1.4      cgd 
    704   1.1      cgd 		startpr(1);
    705   1.1      cgd 	}
    706   1.1      cgd }
    707   1.1      cgd 
    708   1.4      cgd static void
    709  1.14      wiz startpr(int enable)
    710   1.1      cgd {
    711   1.1      cgd 	struct stat stbuf;
    712   1.1      cgd 
    713   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    714   1.1      cgd 		SD = _PATH_DEFSPOOL;
    715   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    716   1.1      cgd 		LO = DEFLOCK;
    717   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    718   1.1      cgd 	printf("%s:\n", printer);
    719   1.1      cgd 
    720   1.1      cgd 	/*
    721   1.1      cgd 	 * Turn off the owner execute bit of the lock file to enable printing.
    722   1.1      cgd 	 */
    723   1.5  hpeyerl 	seteuid(euid);
    724   1.1      cgd 	if (enable && stat(line, &stbuf) >= 0) {
    725   1.1      cgd 		if (chmod(line, stbuf.st_mode & (enable==2 ? 0666 : 0677)) < 0)
    726   1.1      cgd 			printf("\tcannot enable printing\n");
    727   1.1      cgd 		else
    728   1.1      cgd 			printf("\tprinting enabled\n");
    729   1.1      cgd 	}
    730   1.1      cgd 	if (!startdaemon(printer))
    731   1.1      cgd 		printf("\tcouldn't start daemon\n");
    732   1.1      cgd 	else
    733   1.1      cgd 		printf("\tdaemon started\n");
    734   1.5  hpeyerl 	seteuid(uid);
    735   1.1      cgd }
    736   1.1      cgd 
    737   1.1      cgd /*
    738   1.1      cgd  * Print the status of each queue listed or all the queues.
    739   1.1      cgd  */
    740   1.4      cgd void
    741  1.14      wiz status(int argc, char *argv[])
    742   1.1      cgd {
    743  1.12      mrg 	int c, status;
    744  1.12      mrg 	char *cp1, *cp2;
    745   1.1      cgd 	char prbuf[100];
    746   1.1      cgd 
    747  1.12      mrg 	if (argc == 1 || (argc == 2 && strcmp(argv[1], "all") == 0)) {
    748   1.1      cgd 		printer = prbuf;
    749   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    750   1.1      cgd 			cp1 = prbuf;
    751   1.4      cgd 			cp2 = bp;
    752  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    753  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    754   1.1      cgd 				*cp1++ = c;
    755   1.1      cgd 			*cp1 = '\0';
    756   1.1      cgd 			prstat();
    757   1.1      cgd 		}
    758   1.1      cgd 		return;
    759   1.1      cgd 	}
    760   1.1      cgd 	while (--argc) {
    761   1.1      cgd 		printer = *++argv;
    762   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    763   1.1      cgd 			printf("cannot open printer description file\n");
    764   1.1      cgd 			continue;
    765   1.4      cgd 		} else if (status == -1) {
    766   1.1      cgd 			printf("unknown printer %s\n", printer);
    767   1.1      cgd 			continue;
    768   1.4      cgd 		} else if (status == -3)
    769   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    770   1.4      cgd 
    771   1.1      cgd 		prstat();
    772   1.1      cgd 	}
    773   1.1      cgd }
    774   1.1      cgd 
    775   1.1      cgd /*
    776   1.1      cgd  * Print the status of the printer queue.
    777   1.1      cgd  */
    778   1.4      cgd static void
    779  1.14      wiz prstat(void)
    780   1.1      cgd {
    781   1.1      cgd 	struct stat stbuf;
    782  1.12      mrg 	int fd, i;
    783  1.12      mrg 	struct dirent *dp;
    784   1.1      cgd 	DIR *dirp;
    785   1.1      cgd 
    786   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    787   1.1      cgd 		SD = _PATH_DEFSPOOL;
    788   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    789   1.1      cgd 		LO = DEFLOCK;
    790   1.4      cgd 	if (cgetstr(bp, "st", &ST) == -1)
    791   1.1      cgd 		ST = DEFSTAT;
    792   1.1      cgd 	printf("%s:\n", printer);
    793   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    794   1.1      cgd 	if (stat(line, &stbuf) >= 0) {
    795   1.1      cgd 		printf("\tqueuing is %s\n",
    796   1.1      cgd 			(stbuf.st_mode & 010) ? "disabled" : "enabled");
    797   1.1      cgd 		printf("\tprinting is %s\n",
    798   1.1      cgd 			(stbuf.st_mode & 0100) ? "disabled" : "enabled");
    799   1.1      cgd 	} else {
    800   1.1      cgd 		printf("\tqueuing is enabled\n");
    801   1.1      cgd 		printf("\tprinting is enabled\n");
    802   1.1      cgd 	}
    803   1.1      cgd 	if ((dirp = opendir(SD)) == NULL) {
    804   1.1      cgd 		printf("\tcannot examine spool directory\n");
    805   1.1      cgd 		return;
    806   1.1      cgd 	}
    807   1.1      cgd 	i = 0;
    808   1.1      cgd 	while ((dp = readdir(dirp)) != NULL) {
    809   1.1      cgd 		if (*dp->d_name == 'c' && dp->d_name[1] == 'f')
    810   1.1      cgd 			i++;
    811   1.1      cgd 	}
    812   1.1      cgd 	closedir(dirp);
    813   1.1      cgd 	if (i == 0)
    814   1.1      cgd 		printf("\tno entries\n");
    815   1.1      cgd 	else if (i == 1)
    816   1.1      cgd 		printf("\t1 entry in spool area\n");
    817   1.1      cgd 	else
    818   1.1      cgd 		printf("\t%d entries in spool area\n", i);
    819   1.1      cgd 	fd = open(line, O_RDONLY);
    820   1.1      cgd 	if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
    821   1.8      mrg 		(void)close(fd);	/* unlocks as well */
    822   1.7  hpeyerl 		printf("\tprinter idle\n");
    823   1.1      cgd 		return;
    824   1.1      cgd 	}
    825   1.8      mrg 	(void)close(fd);
    826   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, ST);
    827   1.1      cgd 	fd = open(line, O_RDONLY);
    828   1.1      cgd 	if (fd >= 0) {
    829   1.8      mrg 		(void)flock(fd, LOCK_SH);
    830  1.12      mrg 		(void)fstat(fd, &stbuf);
    831  1.12      mrg 		if (stbuf.st_size > 0) {
    832  1.12      mrg 			putchar('\t');
    833  1.12      mrg 			while ((i = read(fd, line, sizeof(line))) > 0)
    834  1.12      mrg 				(void)fwrite(line, 1, i, stdout);
    835  1.12      mrg 		}
    836   1.8      mrg 		(void)close(fd);	/* unlocks as well */
    837   1.1      cgd 	}
    838   1.1      cgd }
    839   1.1      cgd 
    840   1.1      cgd /*
    841   1.1      cgd  * Stop the specified daemon after completing the current job and disable
    842   1.1      cgd  * printing.
    843   1.1      cgd  */
    844   1.4      cgd void
    845  1.14      wiz stop(int argc, char *argv[])
    846   1.1      cgd {
    847  1.12      mrg 	int c, status;
    848  1.12      mrg 	char *cp1, *cp2;
    849   1.1      cgd 	char prbuf[100];
    850   1.1      cgd 
    851   1.1      cgd 	if (argc == 1) {
    852   1.1      cgd 		printf("Usage: stop {all | printer ...}\n");
    853   1.1      cgd 		return;
    854   1.1      cgd 	}
    855   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
    856   1.1      cgd 		printer = prbuf;
    857   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    858   1.1      cgd 			cp1 = prbuf;
    859   1.4      cgd 			cp2 = bp;
    860  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    861  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    862   1.1      cgd 				*cp1++ = c;
    863   1.1      cgd 			*cp1 = '\0';
    864   1.1      cgd 			stoppr();
    865   1.1      cgd 		}
    866   1.1      cgd 		return;
    867   1.1      cgd 	}
    868   1.1      cgd 	while (--argc) {
    869   1.1      cgd 		printer = *++argv;
    870   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    871   1.1      cgd 			printf("cannot open printer description file\n");
    872   1.1      cgd 			continue;
    873   1.4      cgd 		} else if (status == -1) {
    874   1.1      cgd 			printf("unknown printer %s\n", printer);
    875   1.1      cgd 			continue;
    876   1.4      cgd 		} else if (status == -3)
    877   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    878   1.4      cgd 
    879   1.1      cgd 		stoppr();
    880   1.1      cgd 	}
    881   1.1      cgd }
    882   1.1      cgd 
    883   1.4      cgd static void
    884  1.14      wiz stoppr(void)
    885   1.1      cgd {
    886  1.12      mrg 	int fd;
    887   1.1      cgd 	struct stat stbuf;
    888   1.1      cgd 
    889   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    890   1.1      cgd 		SD = _PATH_DEFSPOOL;
    891   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    892   1.1      cgd 		LO = DEFLOCK;
    893   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    894   1.1      cgd 	printf("%s:\n", printer);
    895   1.1      cgd 
    896   1.1      cgd 	/*
    897   1.1      cgd 	 * Turn on the owner execute bit of the lock file to disable printing.
    898   1.1      cgd 	 */
    899   1.5  hpeyerl 	seteuid(euid);
    900   1.1      cgd 	if (stat(line, &stbuf) >= 0) {
    901   1.1      cgd 		if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
    902   1.1      cgd 			printf("\tcannot disable printing\n");
    903   1.1      cgd 		else {
    904   1.1      cgd 			upstat("printing disabled\n");
    905   1.1      cgd 			printf("\tprinting disabled\n");
    906   1.1      cgd 		}
    907   1.1      cgd 	} else if (errno == ENOENT) {
    908   1.1      cgd 		if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
    909   1.1      cgd 			printf("\tcannot create lock file\n");
    910   1.1      cgd 		else {
    911   1.8      mrg 			(void)close(fd);
    912   1.1      cgd 			upstat("printing disabled\n");
    913   1.1      cgd 			printf("\tprinting disabled\n");
    914   1.1      cgd 		}
    915   1.1      cgd 	} else
    916   1.1      cgd 		printf("\tcannot stat lock file\n");
    917   1.5  hpeyerl 	seteuid(uid);
    918   1.1      cgd }
    919   1.1      cgd 
    920   1.1      cgd struct	queue **queue;
    921   1.1      cgd int	nitems;
    922   1.1      cgd time_t	mtime;
    923   1.1      cgd 
    924   1.1      cgd /*
    925   1.1      cgd  * Put the specified jobs at the top of printer queue.
    926   1.1      cgd  */
    927   1.4      cgd void
    928  1.14      wiz topq(int argc, char *argv[])
    929   1.1      cgd {
    930  1.12      mrg 	int i;
    931   1.1      cgd 	struct stat stbuf;
    932   1.1      cgd 	int status, changed;
    933   1.1      cgd 
    934   1.1      cgd 	if (argc < 3) {
    935   1.1      cgd 		printf("Usage: topq printer [jobnum ...] [user ...]\n");
    936   1.1      cgd 		return;
    937   1.1      cgd 	}
    938   1.1      cgd 
    939   1.1      cgd 	--argc;
    940   1.1      cgd 	printer = *++argv;
    941   1.4      cgd 	status = cgetent(&bp, printcapdb, printer);
    942   1.4      cgd 	if (status == -2) {
    943   1.1      cgd 		printf("cannot open printer description file\n");
    944   1.1      cgd 		return;
    945   1.4      cgd 	} else if (status == -1) {
    946   1.1      cgd 		printf("%s: unknown printer\n", printer);
    947   1.1      cgd 		return;
    948   1.4      cgd 	} else if (status == -3)
    949   1.4      cgd 		fatal("potential reference loop detected in printcap file");
    950   1.4      cgd 
    951   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    952   1.1      cgd 		SD = _PATH_DEFSPOOL;
    953   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    954   1.1      cgd 		LO = DEFLOCK;
    955   1.1      cgd 	printf("%s:\n", printer);
    956   1.1      cgd 
    957   1.5  hpeyerl 	seteuid(euid);
    958   1.1      cgd 	if (chdir(SD) < 0) {
    959   1.1      cgd 		printf("\tcannot chdir to %s\n", SD);
    960   1.5  hpeyerl 		goto out;
    961   1.1      cgd 	}
    962   1.5  hpeyerl 	seteuid(uid);
    963   1.1      cgd 	nitems = getq(&queue);
    964   1.1      cgd 	if (nitems == 0)
    965   1.1      cgd 		return;
    966   1.1      cgd 	changed = 0;
    967   1.1      cgd 	mtime = queue[0]->q_time;
    968   1.1      cgd 	for (i = argc; --i; ) {
    969   1.1      cgd 		if (doarg(argv[i]) == 0) {
    970   1.1      cgd 			printf("\tjob %s is not in the queue\n", argv[i]);
    971   1.1      cgd 			continue;
    972   1.1      cgd 		} else
    973   1.1      cgd 			changed++;
    974   1.1      cgd 	}
    975   1.1      cgd 	for (i = 0; i < nitems; i++)
    976   1.1      cgd 		free(queue[i]);
    977   1.1      cgd 	free(queue);
    978   1.1      cgd 	if (!changed) {
    979   1.1      cgd 		printf("\tqueue order unchanged\n");
    980   1.1      cgd 		return;
    981   1.1      cgd 	}
    982   1.1      cgd 	/*
    983   1.1      cgd 	 * Turn on the public execute bit of the lock file to
    984   1.1      cgd 	 * get lpd to rebuild the queue after the current job.
    985   1.1      cgd 	 */
    986   1.5  hpeyerl 	seteuid(euid);
    987   1.1      cgd 	if (changed && stat(LO, &stbuf) >= 0)
    988   1.8      mrg 		(void)chmod(LO, (stbuf.st_mode & 0777) | 01);
    989   1.5  hpeyerl 
    990   1.5  hpeyerl out:
    991   1.5  hpeyerl 	seteuid(uid);
    992   1.1      cgd }
    993   1.1      cgd 
    994   1.1      cgd /*
    995   1.1      cgd  * Reposition the job by changing the modification time of
    996   1.1      cgd  * the control file.
    997   1.1      cgd  */
    998   1.4      cgd static int
    999  1.14      wiz touch(struct queue *q)
   1000   1.1      cgd {
   1001   1.1      cgd 	struct timeval tvp[2];
   1002   1.5  hpeyerl 	int ret;
   1003   1.1      cgd 
   1004   1.1      cgd 	tvp[0].tv_sec = tvp[1].tv_sec = --mtime;
   1005   1.1      cgd 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
   1006   1.5  hpeyerl 	seteuid(euid);
   1007   1.5  hpeyerl 	ret = utimes(q->q_name, tvp);
   1008   1.5  hpeyerl 	seteuid(uid);
   1009   1.5  hpeyerl 	return (ret);
   1010   1.1      cgd }
   1011   1.1      cgd 
   1012   1.1      cgd /*
   1013   1.1      cgd  * Checks if specified job name is in the printer's queue.
   1014   1.1      cgd  * Returns:  negative (-1) if argument name is not in the queue.
   1015   1.1      cgd  */
   1016   1.4      cgd static int
   1017  1.14      wiz doarg(char *job)
   1018   1.1      cgd {
   1019  1.12      mrg 	struct queue **qq;
   1020  1.12      mrg 	int jobnum, n;
   1021  1.12      mrg 	char *cp, *machine;
   1022   1.1      cgd 	int cnt = 0;
   1023   1.1      cgd 	FILE *fp;
   1024   1.1      cgd 
   1025   1.1      cgd 	/*
   1026   1.1      cgd 	 * Look for a job item consisting of system name, colon, number
   1027   1.1      cgd 	 * (example: ucbarpa:114)
   1028   1.1      cgd 	 */
   1029  1.12      mrg 	if ((cp = strchr(job, ':')) != NULL) {
   1030   1.1      cgd 		machine = job;
   1031   1.1      cgd 		*cp++ = '\0';
   1032   1.1      cgd 		job = cp;
   1033   1.1      cgd 	} else
   1034   1.1      cgd 		machine = NULL;
   1035   1.1      cgd 
   1036   1.1      cgd 	/*
   1037   1.1      cgd 	 * Check for job specified by number (example: 112 or 235ucbarpa).
   1038   1.1      cgd 	 */
   1039   1.1      cgd 	if (isdigit(*job)) {
   1040   1.1      cgd 		jobnum = 0;
   1041   1.1      cgd 		do
   1042   1.1      cgd 			jobnum = jobnum * 10 + (*job++ - '0');
   1043   1.1      cgd 		while (isdigit(*job));
   1044   1.1      cgd 		for (qq = queue + nitems; --qq >= queue; ) {
   1045   1.1      cgd 			n = 0;
   1046   1.1      cgd 			for (cp = (*qq)->q_name+3; isdigit(*cp); )
   1047   1.1      cgd 				n = n * 10 + (*cp++ - '0');
   1048   1.1      cgd 			if (jobnum != n)
   1049   1.1      cgd 				continue;
   1050   1.1      cgd 			if (*job && strcmp(job, cp) != 0)
   1051   1.1      cgd 				continue;
   1052   1.1      cgd 			if (machine != NULL && strcmp(machine, cp) != 0)
   1053   1.1      cgd 				continue;
   1054   1.1      cgd 			if (touch(*qq) == 0) {
   1055   1.1      cgd 				printf("\tmoved %s\n", (*qq)->q_name);
   1056   1.1      cgd 				cnt++;
   1057   1.1      cgd 			}
   1058   1.1      cgd 		}
   1059   1.1      cgd 		return(cnt);
   1060   1.1      cgd 	}
   1061   1.1      cgd 	/*
   1062   1.1      cgd 	 * Process item consisting of owner's name (example: henry).
   1063   1.1      cgd 	 */
   1064   1.1      cgd 	for (qq = queue + nitems; --qq >= queue; ) {
   1065   1.5  hpeyerl 		seteuid(euid);
   1066   1.5  hpeyerl 		fp = fopen((*qq)->q_name, "r");
   1067   1.5  hpeyerl 		seteuid(uid);
   1068   1.5  hpeyerl 		if (fp == NULL)
   1069   1.1      cgd 			continue;
   1070   1.1      cgd 		while (getline(fp) > 0)
   1071   1.1      cgd 			if (line[0] == 'P')
   1072   1.1      cgd 				break;
   1073   1.8      mrg 		(void)fclose(fp);
   1074   1.1      cgd 		if (line[0] != 'P' || strcmp(job, line+1) != 0)
   1075   1.1      cgd 			continue;
   1076   1.1      cgd 		if (touch(*qq) == 0) {
   1077   1.1      cgd 			printf("\tmoved %s\n", (*qq)->q_name);
   1078   1.1      cgd 			cnt++;
   1079   1.1      cgd 		}
   1080   1.1      cgd 	}
   1081   1.1      cgd 	return(cnt);
   1082   1.1      cgd }
   1083   1.1      cgd 
   1084   1.1      cgd /*
   1085   1.1      cgd  * Enable everything and start printer (undo `down').
   1086   1.1      cgd  */
   1087   1.4      cgd void
   1088  1.14      wiz up(int argc, char *argv[])
   1089   1.1      cgd {
   1090  1.12      mrg 	int c, status;
   1091  1.12      mrg 	char *cp1, *cp2;
   1092   1.1      cgd 	char prbuf[100];
   1093   1.1      cgd 
   1094   1.1      cgd 	if (argc == 1) {
   1095   1.1      cgd 		printf("Usage: up {all | printer ...}\n");
   1096   1.1      cgd 		return;
   1097   1.1      cgd 	}
   1098   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
   1099   1.1      cgd 		printer = prbuf;
   1100   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
   1101   1.1      cgd 			cp1 = prbuf;
   1102   1.4      cgd 			cp2 = bp;
   1103  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
   1104  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
   1105   1.1      cgd 				*cp1++ = c;
   1106   1.1      cgd 			*cp1 = '\0';
   1107   1.1      cgd 			startpr(2);
   1108   1.1      cgd 		}
   1109   1.1      cgd 		return;
   1110   1.1      cgd 	}
   1111   1.1      cgd 	while (--argc) {
   1112   1.1      cgd 		printer = *++argv;
   1113   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
   1114   1.1      cgd 			printf("cannot open printer description file\n");
   1115   1.1      cgd 			continue;
   1116   1.4      cgd 		} else if (status == -1) {
   1117   1.1      cgd 			printf("unknown printer %s\n", printer);
   1118   1.1      cgd 			continue;
   1119   1.4      cgd 		} else if (status == -3)
   1120   1.4      cgd 			fatal("potential reference loop detected in printcap file");
   1121   1.4      cgd 
   1122   1.1      cgd 		startpr(2);
   1123   1.1      cgd 	}
   1124   1.1      cgd }
   1125