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