Home | History | Annotate | Line # | Download | only in lpc
cmds.c revision 1.14
      1  1.14      wiz /*	$NetBSD: cmds.c,v 1.14 2002/07/14 15:27:59 wiz 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.14      wiz __RCSID("$NetBSD: cmds.c,v 1.14 2002/07/14 15:27:59 wiz 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.12      mrg 	char *cp, *cp1, *lp;
    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.12      mrg 	for (lp = line, cp = SD; (lp - line) < sizeof(line) &&
    314  1.12      mrg 	    (*lp++ = *cp++) != '\0'; )
    315   1.1      cgd 		;
    316   1.1      cgd 	lp[-1] = '/';
    317   1.1      cgd 
    318   1.5  hpeyerl 	seteuid(euid);
    319   1.4      cgd 	nitems = scandir(SD, &queue, doselect, sortq);
    320   1.5  hpeyerl 	seteuid(uid);
    321   1.1      cgd 	if (nitems < 0) {
    322   1.1      cgd 		printf("\tcannot examine spool directory\n");
    323   1.1      cgd 		return;
    324   1.1      cgd 	}
    325   1.1      cgd 	if (nitems == 0)
    326   1.1      cgd 		return;
    327   1.1      cgd 	i = 0;
    328   1.1      cgd 	do {
    329   1.1      cgd 		cp = queue[i]->d_name;
    330   1.1      cgd 		if (*cp == 'c') {
    331   1.1      cgd 			n = 0;
    332   1.1      cgd 			while (i + 1 < nitems) {
    333   1.1      cgd 				cp1 = queue[i + 1]->d_name;
    334   1.1      cgd 				if (*cp1 != 'd' || strcmp(cp + 3, cp1 + 3))
    335   1.1      cgd 					break;
    336   1.1      cgd 				i++;
    337   1.1      cgd 				n++;
    338   1.1      cgd 			}
    339   1.1      cgd 			if (n == 0) {
    340   1.8      mrg 				strncpy(lp, cp, sizeof(line) - strlen(line) - 1);
    341   1.8      mrg 				line[sizeof(line) - 1] = '\0';
    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.8      mrg 			strncpy(lp, cp, sizeof(line) - strlen(line) - 1);
    351   1.8      mrg 			line[sizeof(line) - 1] = '\0';
    352   1.1      cgd 			unlinkf(line);
    353   1.1      cgd 		}
    354   1.1      cgd      	} while (++i < nitems);
    355   1.1      cgd }
    356   1.1      cgd 
    357   1.4      cgd static void
    358  1.14      wiz unlinkf(char *name)
    359   1.1      cgd {
    360   1.5  hpeyerl 	seteuid(euid);
    361   1.1      cgd 	if (unlink(name) < 0)
    362   1.1      cgd 		printf("\tcannot remove %s\n", name);
    363   1.1      cgd 	else
    364   1.1      cgd 		printf("\tremoved %s\n", name);
    365   1.5  hpeyerl 	seteuid(uid);
    366   1.1      cgd }
    367   1.1      cgd 
    368   1.1      cgd /*
    369   1.1      cgd  * Enable queuing to the printer (allow lpr's).
    370   1.1      cgd  */
    371   1.4      cgd void
    372  1.14      wiz enable(int argc, char *argv[])
    373   1.1      cgd {
    374  1.12      mrg 	int c, status;
    375  1.12      mrg 	char *cp1, *cp2;
    376   1.1      cgd 	char prbuf[100];
    377   1.1      cgd 
    378   1.1      cgd 	if (argc == 1) {
    379   1.1      cgd 		printf("Usage: enable {all | printer ...}\n");
    380   1.1      cgd 		return;
    381   1.1      cgd 	}
    382   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
    383   1.1      cgd 		printer = prbuf;
    384   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    385   1.1      cgd 			cp1 = prbuf;
    386   1.4      cgd 			cp2 = bp;
    387  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    388  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    389   1.1      cgd 				*cp1++ = c;
    390   1.1      cgd 			*cp1 = '\0';
    391   1.1      cgd 			enablepr();
    392   1.1      cgd 		}
    393   1.1      cgd 		return;
    394   1.1      cgd 	}
    395   1.1      cgd 	while (--argc) {
    396   1.1      cgd 		printer = *++argv;
    397   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    398   1.1      cgd 			printf("cannot open printer description file\n");
    399   1.1      cgd 			continue;
    400   1.4      cgd 		} else if (status == -1) {
    401   1.1      cgd 			printf("unknown printer %s\n", printer);
    402   1.1      cgd 			continue;
    403   1.4      cgd 		} else if (status == -3)
    404   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    405   1.4      cgd 
    406   1.1      cgd 		enablepr();
    407   1.1      cgd 	}
    408   1.1      cgd }
    409   1.1      cgd 
    410   1.4      cgd static void
    411  1.14      wiz enablepr(void)
    412   1.1      cgd {
    413   1.1      cgd 	struct stat stbuf;
    414   1.1      cgd 
    415   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    416   1.1      cgd 		SD = _PATH_DEFSPOOL;
    417   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    418   1.1      cgd 		LO = DEFLOCK;
    419   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    420   1.1      cgd 	printf("%s:\n", printer);
    421   1.1      cgd 
    422   1.1      cgd 	/*
    423   1.1      cgd 	 * Turn off the group execute bit of the lock file to enable queuing.
    424   1.1      cgd 	 */
    425   1.5  hpeyerl 	seteuid(euid);
    426   1.1      cgd 	if (stat(line, &stbuf) >= 0) {
    427   1.1      cgd 		if (chmod(line, stbuf.st_mode & 0767) < 0)
    428   1.1      cgd 			printf("\tcannot enable queuing\n");
    429   1.1      cgd 		else
    430   1.1      cgd 			printf("\tqueuing enabled\n");
    431   1.1      cgd 	}
    432   1.5  hpeyerl 	seteuid(uid);
    433   1.1      cgd }
    434   1.1      cgd 
    435   1.1      cgd /*
    436   1.1      cgd  * Disable queuing.
    437   1.1      cgd  */
    438   1.4      cgd void
    439  1.14      wiz disable(int argc, char *argv[])
    440   1.1      cgd {
    441  1.12      mrg 	int c, status;
    442  1.12      mrg 	char *cp1, *cp2;
    443   1.1      cgd 	char prbuf[100];
    444   1.1      cgd 
    445   1.1      cgd 	if (argc == 1) {
    446   1.1      cgd 		printf("Usage: disable {all | printer ...}\n");
    447   1.1      cgd 		return;
    448   1.1      cgd 	}
    449   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
    450   1.1      cgd 		printer = prbuf;
    451   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    452   1.1      cgd 			cp1 = prbuf;
    453   1.4      cgd 			cp2 = bp;
    454  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    455  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    456   1.1      cgd 				*cp1++ = c;
    457   1.1      cgd 			*cp1 = '\0';
    458   1.1      cgd 			disablepr();
    459   1.1      cgd 		}
    460   1.1      cgd 		return;
    461   1.1      cgd 	}
    462   1.1      cgd 	while (--argc) {
    463   1.1      cgd 		printer = *++argv;
    464   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    465   1.1      cgd 			printf("cannot open printer description file\n");
    466   1.1      cgd 			continue;
    467   1.4      cgd 		} else if (status == -1) {
    468   1.1      cgd 			printf("unknown printer %s\n", printer);
    469   1.1      cgd 			continue;
    470   1.4      cgd 		} else if (status == -3)
    471   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    472   1.4      cgd 
    473   1.1      cgd 		disablepr();
    474   1.1      cgd 	}
    475   1.1      cgd }
    476   1.1      cgd 
    477   1.4      cgd static void
    478  1.14      wiz disablepr(void)
    479   1.1      cgd {
    480  1.12      mrg 	int fd;
    481   1.1      cgd 	struct stat stbuf;
    482   1.1      cgd 
    483   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    484   1.1      cgd 		SD = _PATH_DEFSPOOL;
    485   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    486   1.1      cgd 		LO = DEFLOCK;
    487   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    488   1.1      cgd 	printf("%s:\n", printer);
    489   1.1      cgd 	/*
    490   1.1      cgd 	 * Turn on the group execute bit of the lock file to disable queuing.
    491   1.1      cgd 	 */
    492   1.5  hpeyerl 	seteuid(euid);
    493   1.1      cgd 	if (stat(line, &stbuf) >= 0) {
    494   1.1      cgd 		if (chmod(line, (stbuf.st_mode & 0777) | 010) < 0)
    495   1.1      cgd 			printf("\tcannot disable queuing\n");
    496   1.1      cgd 		else
    497   1.1      cgd 			printf("\tqueuing disabled\n");
    498   1.1      cgd 	} else if (errno == ENOENT) {
    499   1.1      cgd 		if ((fd = open(line, O_WRONLY|O_CREAT, 0670)) < 0)
    500   1.1      cgd 			printf("\tcannot create lock file\n");
    501   1.1      cgd 		else {
    502   1.8      mrg 			(void)close(fd);
    503   1.1      cgd 			printf("\tqueuing disabled\n");
    504   1.1      cgd 		}
    505   1.1      cgd 	} else
    506   1.1      cgd 		printf("\tcannot stat lock file\n");
    507   1.5  hpeyerl 	seteuid(uid);
    508   1.1      cgd }
    509   1.1      cgd 
    510   1.1      cgd /*
    511   1.1      cgd  * Disable queuing and printing and put a message into the status file
    512   1.1      cgd  * (reason for being down).
    513   1.1      cgd  */
    514   1.4      cgd void
    515  1.14      wiz down(int argc, char *argv[])
    516   1.1      cgd {
    517  1.12      mrg 	int c, status;
    518  1.12      mrg 	char *cp1, *cp2;
    519   1.1      cgd 	char prbuf[100];
    520   1.1      cgd 
    521   1.1      cgd 	if (argc == 1) {
    522   1.1      cgd 		printf("Usage: down {all | printer} [message ...]\n");
    523   1.1      cgd 		return;
    524   1.1      cgd 	}
    525   1.1      cgd 	if (!strcmp(argv[1], "all")) {
    526   1.1      cgd 		printer = prbuf;
    527   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    528   1.1      cgd 			cp1 = prbuf;
    529   1.4      cgd 			cp2 = bp;
    530  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    531  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    532   1.1      cgd 				*cp1++ = c;
    533   1.1      cgd 			*cp1 = '\0';
    534   1.1      cgd 			putmsg(argc - 2, argv + 2);
    535   1.1      cgd 		}
    536   1.1      cgd 		return;
    537   1.1      cgd 	}
    538   1.1      cgd 	printer = argv[1];
    539   1.4      cgd 	if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    540   1.1      cgd 		printf("cannot open printer description file\n");
    541   1.1      cgd 		return;
    542   1.4      cgd 	} else if (status == -1) {
    543   1.1      cgd 		printf("unknown printer %s\n", printer);
    544   1.1      cgd 		return;
    545   1.4      cgd 	} else if (status == -3)
    546   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    547   1.4      cgd 
    548   1.1      cgd 	putmsg(argc - 2, argv + 2);
    549   1.1      cgd }
    550   1.1      cgd 
    551   1.4      cgd static void
    552  1.14      wiz putmsg(int argc, char **argv)
    553   1.1      cgd {
    554  1.12      mrg 	int fd;
    555  1.12      mrg 	char *cp1, *cp2;
    556   1.1      cgd 	char buf[1024];
    557   1.1      cgd 	struct stat stbuf;
    558   1.1      cgd 
    559   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    560   1.1      cgd 		SD = _PATH_DEFSPOOL;
    561   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    562   1.1      cgd 		LO = DEFLOCK;
    563   1.4      cgd 	if (cgetstr(bp, "st", &ST) == -1)
    564   1.1      cgd 		ST = DEFSTAT;
    565   1.1      cgd 	printf("%s:\n", printer);
    566   1.1      cgd 	/*
    567   1.1      cgd 	 * Turn on the group execute bit of the lock file to disable queuing and
    568   1.1      cgd 	 * turn on the owner execute bit of the lock file to disable printing.
    569   1.1      cgd 	 */
    570   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    571   1.5  hpeyerl 	seteuid(euid);
    572   1.1      cgd 	if (stat(line, &stbuf) >= 0) {
    573   1.1      cgd 		if (chmod(line, (stbuf.st_mode & 0777) | 0110) < 0)
    574   1.1      cgd 			printf("\tcannot disable queuing\n");
    575   1.1      cgd 		else
    576   1.1      cgd 			printf("\tprinter and queuing disabled\n");
    577   1.1      cgd 	} else if (errno == ENOENT) {
    578   1.1      cgd 		if ((fd = open(line, O_WRONLY|O_CREAT, 0770)) < 0)
    579   1.1      cgd 			printf("\tcannot create lock file\n");
    580   1.1      cgd 		else {
    581   1.8      mrg 			(void)close(fd);
    582   1.1      cgd 			printf("\tprinter and queuing disabled\n");
    583   1.1      cgd 		}
    584   1.5  hpeyerl 		seteuid(uid);
    585   1.1      cgd 		return;
    586   1.1      cgd 	} else
    587   1.1      cgd 		printf("\tcannot stat lock file\n");
    588   1.1      cgd 	/*
    589   1.1      cgd 	 * Write the message into the status file.
    590   1.1      cgd 	 */
    591   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, ST);
    592   1.1      cgd 	fd = open(line, O_WRONLY|O_CREAT, 0664);
    593   1.1      cgd 	if (fd < 0 || flock(fd, LOCK_EX) < 0) {
    594   1.1      cgd 		printf("\tcannot create status file\n");
    595   1.5  hpeyerl 		seteuid(uid);
    596   1.1      cgd 		return;
    597   1.1      cgd 	}
    598   1.5  hpeyerl 	seteuid(uid);
    599   1.8      mrg 	(void)ftruncate(fd, 0);
    600   1.1      cgd 	if (argc <= 0) {
    601   1.8      mrg 		(void)write(fd, "\n", 1);
    602   1.8      mrg 		(void)close(fd);
    603   1.1      cgd 		return;
    604   1.1      cgd 	}
    605   1.1      cgd 	cp1 = buf;
    606   1.1      cgd 	while (--argc >= 0) {
    607   1.1      cgd 		cp2 = *argv++;
    608  1.12      mrg 		while ((cp1 - buf) < sizeof(buf) && (*cp1++ = *cp2++))
    609   1.1      cgd 			;
    610   1.1      cgd 		cp1[-1] = ' ';
    611   1.1      cgd 	}
    612   1.1      cgd 	cp1[-1] = '\n';
    613   1.1      cgd 	*cp1 = '\0';
    614   1.8      mrg 	(void)write(fd, buf, strlen(buf));
    615   1.8      mrg 	(void)close(fd);
    616   1.1      cgd }
    617   1.1      cgd 
    618   1.1      cgd /*
    619   1.1      cgd  * Exit lpc
    620   1.1      cgd  */
    621   1.4      cgd void
    622  1.14      wiz quit(int argc, char *argv[])
    623   1.1      cgd {
    624   1.1      cgd 	exit(0);
    625   1.1      cgd }
    626   1.1      cgd 
    627   1.1      cgd /*
    628   1.1      cgd  * Kill and restart the daemon.
    629   1.1      cgd  */
    630   1.4      cgd void
    631  1.14      wiz restart(int argc, char *argv[])
    632   1.1      cgd {
    633  1.12      mrg 	int c, status;
    634  1.12      mrg 	char *cp1, *cp2;
    635   1.1      cgd 	char prbuf[100];
    636   1.1      cgd 
    637   1.1      cgd 	if (argc == 1) {
    638   1.1      cgd 		printf("Usage: restart {all | printer ...}\n");
    639   1.1      cgd 		return;
    640   1.1      cgd 	}
    641   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
    642   1.1      cgd 		printer = prbuf;
    643   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    644   1.1      cgd 			cp1 = prbuf;
    645   1.4      cgd 			cp2 = bp;
    646  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    647  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    648   1.1      cgd 				*cp1++ = c;
    649   1.1      cgd 			*cp1 = '\0';
    650   1.1      cgd 			abortpr(0);
    651   1.1      cgd 			startpr(0);
    652   1.1      cgd 		}
    653   1.1      cgd 		return;
    654   1.1      cgd 	}
    655   1.1      cgd 	while (--argc) {
    656   1.1      cgd 		printer = *++argv;
    657   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    658   1.1      cgd 			printf("cannot open printer description file\n");
    659   1.1      cgd 			continue;
    660   1.4      cgd 		} else if (status == -1) {
    661   1.1      cgd 			printf("unknown printer %s\n", printer);
    662   1.1      cgd 			continue;
    663   1.4      cgd 		} else if (status == -3)
    664   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    665   1.4      cgd 
    666   1.1      cgd 		abortpr(0);
    667   1.1      cgd 		startpr(0);
    668   1.1      cgd 	}
    669   1.1      cgd }
    670   1.1      cgd 
    671   1.1      cgd /*
    672   1.1      cgd  * Enable printing on the specified printer and startup the daemon.
    673   1.1      cgd  */
    674   1.4      cgd void
    675  1.14      wiz startcmd(int argc, char *argv[])
    676   1.1      cgd {
    677  1.12      mrg 	int c, status;
    678  1.12      mrg 	char *cp1, *cp2;
    679   1.1      cgd 	char prbuf[100];
    680   1.1      cgd 
    681   1.1      cgd 	if (argc == 1) {
    682   1.1      cgd 		printf("Usage: start {all | printer ...}\n");
    683   1.1      cgd 		return;
    684   1.1      cgd 	}
    685   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
    686   1.1      cgd 		printer = prbuf;
    687   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    688   1.1      cgd 			cp1 = prbuf;
    689   1.4      cgd 			cp2 = bp;
    690  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    691  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    692   1.1      cgd 				*cp1++ = c;
    693   1.1      cgd 			*cp1 = '\0';
    694   1.1      cgd 			startpr(1);
    695   1.1      cgd 		}
    696   1.1      cgd 		return;
    697   1.1      cgd 	}
    698   1.1      cgd 	while (--argc) {
    699   1.1      cgd 		printer = *++argv;
    700   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    701   1.1      cgd 			printf("cannot open printer description file\n");
    702   1.1      cgd 			continue;
    703   1.4      cgd 		} else if (status == -1) {
    704   1.1      cgd 			printf("unknown printer %s\n", printer);
    705   1.1      cgd 			continue;
    706   1.4      cgd 		} else if (status == -3)
    707   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    708   1.4      cgd 
    709   1.1      cgd 		startpr(1);
    710   1.1      cgd 	}
    711   1.1      cgd }
    712   1.1      cgd 
    713   1.4      cgd static void
    714  1.14      wiz startpr(int enable)
    715   1.1      cgd {
    716   1.1      cgd 	struct stat stbuf;
    717   1.1      cgd 
    718   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    719   1.1      cgd 		SD = _PATH_DEFSPOOL;
    720   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    721   1.1      cgd 		LO = DEFLOCK;
    722   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    723   1.1      cgd 	printf("%s:\n", printer);
    724   1.1      cgd 
    725   1.1      cgd 	/*
    726   1.1      cgd 	 * Turn off the owner execute bit of the lock file to enable printing.
    727   1.1      cgd 	 */
    728   1.5  hpeyerl 	seteuid(euid);
    729   1.1      cgd 	if (enable && stat(line, &stbuf) >= 0) {
    730   1.1      cgd 		if (chmod(line, stbuf.st_mode & (enable==2 ? 0666 : 0677)) < 0)
    731   1.1      cgd 			printf("\tcannot enable printing\n");
    732   1.1      cgd 		else
    733   1.1      cgd 			printf("\tprinting enabled\n");
    734   1.1      cgd 	}
    735   1.1      cgd 	if (!startdaemon(printer))
    736   1.1      cgd 		printf("\tcouldn't start daemon\n");
    737   1.1      cgd 	else
    738   1.1      cgd 		printf("\tdaemon started\n");
    739   1.5  hpeyerl 	seteuid(uid);
    740   1.1      cgd }
    741   1.1      cgd 
    742   1.1      cgd /*
    743   1.1      cgd  * Print the status of each queue listed or all the queues.
    744   1.1      cgd  */
    745   1.4      cgd void
    746  1.14      wiz status(int argc, char *argv[])
    747   1.1      cgd {
    748  1.12      mrg 	int c, status;
    749  1.12      mrg 	char *cp1, *cp2;
    750   1.1      cgd 	char prbuf[100];
    751   1.1      cgd 
    752  1.12      mrg 	if (argc == 1 || (argc == 2 && strcmp(argv[1], "all") == 0)) {
    753   1.1      cgd 		printer = prbuf;
    754   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    755   1.1      cgd 			cp1 = prbuf;
    756   1.4      cgd 			cp2 = bp;
    757  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    758  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    759   1.1      cgd 				*cp1++ = c;
    760   1.1      cgd 			*cp1 = '\0';
    761   1.1      cgd 			prstat();
    762   1.1      cgd 		}
    763   1.1      cgd 		return;
    764   1.1      cgd 	}
    765   1.1      cgd 	while (--argc) {
    766   1.1      cgd 		printer = *++argv;
    767   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    768   1.1      cgd 			printf("cannot open printer description file\n");
    769   1.1      cgd 			continue;
    770   1.4      cgd 		} else if (status == -1) {
    771   1.1      cgd 			printf("unknown printer %s\n", printer);
    772   1.1      cgd 			continue;
    773   1.4      cgd 		} else if (status == -3)
    774   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    775   1.4      cgd 
    776   1.1      cgd 		prstat();
    777   1.1      cgd 	}
    778   1.1      cgd }
    779   1.1      cgd 
    780   1.1      cgd /*
    781   1.1      cgd  * Print the status of the printer queue.
    782   1.1      cgd  */
    783   1.4      cgd static void
    784  1.14      wiz prstat(void)
    785   1.1      cgd {
    786   1.1      cgd 	struct stat stbuf;
    787  1.12      mrg 	int fd, i;
    788  1.12      mrg 	struct dirent *dp;
    789   1.1      cgd 	DIR *dirp;
    790   1.1      cgd 
    791   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    792   1.1      cgd 		SD = _PATH_DEFSPOOL;
    793   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    794   1.1      cgd 		LO = DEFLOCK;
    795   1.4      cgd 	if (cgetstr(bp, "st", &ST) == -1)
    796   1.1      cgd 		ST = DEFSTAT;
    797   1.1      cgd 	printf("%s:\n", printer);
    798   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    799   1.1      cgd 	if (stat(line, &stbuf) >= 0) {
    800   1.1      cgd 		printf("\tqueuing is %s\n",
    801   1.1      cgd 			(stbuf.st_mode & 010) ? "disabled" : "enabled");
    802   1.1      cgd 		printf("\tprinting is %s\n",
    803   1.1      cgd 			(stbuf.st_mode & 0100) ? "disabled" : "enabled");
    804   1.1      cgd 	} else {
    805   1.1      cgd 		printf("\tqueuing is enabled\n");
    806   1.1      cgd 		printf("\tprinting is enabled\n");
    807   1.1      cgd 	}
    808   1.1      cgd 	if ((dirp = opendir(SD)) == NULL) {
    809   1.1      cgd 		printf("\tcannot examine spool directory\n");
    810   1.1      cgd 		return;
    811   1.1      cgd 	}
    812   1.1      cgd 	i = 0;
    813   1.1      cgd 	while ((dp = readdir(dirp)) != NULL) {
    814   1.1      cgd 		if (*dp->d_name == 'c' && dp->d_name[1] == 'f')
    815   1.1      cgd 			i++;
    816   1.1      cgd 	}
    817   1.1      cgd 	closedir(dirp);
    818   1.1      cgd 	if (i == 0)
    819   1.1      cgd 		printf("\tno entries\n");
    820   1.1      cgd 	else if (i == 1)
    821   1.1      cgd 		printf("\t1 entry in spool area\n");
    822   1.1      cgd 	else
    823   1.1      cgd 		printf("\t%d entries in spool area\n", i);
    824   1.1      cgd 	fd = open(line, O_RDONLY);
    825   1.1      cgd 	if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
    826   1.8      mrg 		(void)close(fd);	/* unlocks as well */
    827   1.7  hpeyerl 		printf("\tprinter idle\n");
    828   1.1      cgd 		return;
    829   1.1      cgd 	}
    830   1.8      mrg 	(void)close(fd);
    831   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, ST);
    832   1.1      cgd 	fd = open(line, O_RDONLY);
    833   1.1      cgd 	if (fd >= 0) {
    834   1.8      mrg 		(void)flock(fd, LOCK_SH);
    835  1.12      mrg 		(void)fstat(fd, &stbuf);
    836  1.12      mrg 		if (stbuf.st_size > 0) {
    837  1.12      mrg 			putchar('\t');
    838  1.12      mrg 			while ((i = read(fd, line, sizeof(line))) > 0)
    839  1.12      mrg 				(void)fwrite(line, 1, i, stdout);
    840  1.12      mrg 		}
    841   1.8      mrg 		(void)close(fd);	/* unlocks as well */
    842   1.1      cgd 	}
    843   1.1      cgd }
    844   1.1      cgd 
    845   1.1      cgd /*
    846   1.1      cgd  * Stop the specified daemon after completing the current job and disable
    847   1.1      cgd  * printing.
    848   1.1      cgd  */
    849   1.4      cgd void
    850  1.14      wiz stop(int argc, char *argv[])
    851   1.1      cgd {
    852  1.12      mrg 	int c, status;
    853  1.12      mrg 	char *cp1, *cp2;
    854   1.1      cgd 	char prbuf[100];
    855   1.1      cgd 
    856   1.1      cgd 	if (argc == 1) {
    857   1.1      cgd 		printf("Usage: stop {all | printer ...}\n");
    858   1.1      cgd 		return;
    859   1.1      cgd 	}
    860   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
    861   1.1      cgd 		printer = prbuf;
    862   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
    863   1.1      cgd 			cp1 = prbuf;
    864   1.4      cgd 			cp2 = bp;
    865  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
    866  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
    867   1.1      cgd 				*cp1++ = c;
    868   1.1      cgd 			*cp1 = '\0';
    869   1.1      cgd 			stoppr();
    870   1.1      cgd 		}
    871   1.1      cgd 		return;
    872   1.1      cgd 	}
    873   1.1      cgd 	while (--argc) {
    874   1.1      cgd 		printer = *++argv;
    875   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
    876   1.1      cgd 			printf("cannot open printer description file\n");
    877   1.1      cgd 			continue;
    878   1.4      cgd 		} else if (status == -1) {
    879   1.1      cgd 			printf("unknown printer %s\n", printer);
    880   1.1      cgd 			continue;
    881   1.4      cgd 		} else if (status == -3)
    882   1.4      cgd 			fatal("potential reference loop detected in printcap file");
    883   1.4      cgd 
    884   1.1      cgd 		stoppr();
    885   1.1      cgd 	}
    886   1.1      cgd }
    887   1.1      cgd 
    888   1.4      cgd static void
    889  1.14      wiz stoppr(void)
    890   1.1      cgd {
    891  1.12      mrg 	int fd;
    892   1.1      cgd 	struct stat stbuf;
    893   1.1      cgd 
    894   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    895   1.1      cgd 		SD = _PATH_DEFSPOOL;
    896   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    897   1.1      cgd 		LO = DEFLOCK;
    898   1.8      mrg 	(void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
    899   1.1      cgd 	printf("%s:\n", printer);
    900   1.1      cgd 
    901   1.1      cgd 	/*
    902   1.1      cgd 	 * Turn on the owner execute bit of the lock file to disable printing.
    903   1.1      cgd 	 */
    904   1.5  hpeyerl 	seteuid(euid);
    905   1.1      cgd 	if (stat(line, &stbuf) >= 0) {
    906   1.1      cgd 		if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
    907   1.1      cgd 			printf("\tcannot disable printing\n");
    908   1.1      cgd 		else {
    909   1.1      cgd 			upstat("printing disabled\n");
    910   1.1      cgd 			printf("\tprinting disabled\n");
    911   1.1      cgd 		}
    912   1.1      cgd 	} else if (errno == ENOENT) {
    913   1.1      cgd 		if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
    914   1.1      cgd 			printf("\tcannot create lock file\n");
    915   1.1      cgd 		else {
    916   1.8      mrg 			(void)close(fd);
    917   1.1      cgd 			upstat("printing disabled\n");
    918   1.1      cgd 			printf("\tprinting disabled\n");
    919   1.1      cgd 		}
    920   1.1      cgd 	} else
    921   1.1      cgd 		printf("\tcannot stat lock file\n");
    922   1.5  hpeyerl 	seteuid(uid);
    923   1.1      cgd }
    924   1.1      cgd 
    925   1.1      cgd struct	queue **queue;
    926   1.1      cgd int	nitems;
    927   1.1      cgd time_t	mtime;
    928   1.1      cgd 
    929   1.1      cgd /*
    930   1.1      cgd  * Put the specified jobs at the top of printer queue.
    931   1.1      cgd  */
    932   1.4      cgd void
    933  1.14      wiz topq(int argc, char *argv[])
    934   1.1      cgd {
    935  1.12      mrg 	int i;
    936   1.1      cgd 	struct stat stbuf;
    937   1.1      cgd 	int status, changed;
    938   1.1      cgd 
    939   1.1      cgd 	if (argc < 3) {
    940   1.1      cgd 		printf("Usage: topq printer [jobnum ...] [user ...]\n");
    941   1.1      cgd 		return;
    942   1.1      cgd 	}
    943   1.1      cgd 
    944   1.1      cgd 	--argc;
    945   1.1      cgd 	printer = *++argv;
    946   1.4      cgd 	status = cgetent(&bp, printcapdb, printer);
    947   1.4      cgd 	if (status == -2) {
    948   1.1      cgd 		printf("cannot open printer description file\n");
    949   1.1      cgd 		return;
    950   1.4      cgd 	} else if (status == -1) {
    951   1.1      cgd 		printf("%s: unknown printer\n", printer);
    952   1.1      cgd 		return;
    953   1.4      cgd 	} else if (status == -3)
    954   1.4      cgd 		fatal("potential reference loop detected in printcap file");
    955   1.4      cgd 
    956   1.4      cgd 	if (cgetstr(bp, "sd", &SD) == -1)
    957   1.1      cgd 		SD = _PATH_DEFSPOOL;
    958   1.4      cgd 	if (cgetstr(bp, "lo", &LO) == -1)
    959   1.1      cgd 		LO = DEFLOCK;
    960   1.1      cgd 	printf("%s:\n", printer);
    961   1.1      cgd 
    962   1.5  hpeyerl 	seteuid(euid);
    963   1.1      cgd 	if (chdir(SD) < 0) {
    964   1.1      cgd 		printf("\tcannot chdir to %s\n", SD);
    965   1.5  hpeyerl 		goto out;
    966   1.1      cgd 	}
    967   1.5  hpeyerl 	seteuid(uid);
    968   1.1      cgd 	nitems = getq(&queue);
    969   1.1      cgd 	if (nitems == 0)
    970   1.1      cgd 		return;
    971   1.1      cgd 	changed = 0;
    972   1.1      cgd 	mtime = queue[0]->q_time;
    973   1.1      cgd 	for (i = argc; --i; ) {
    974   1.1      cgd 		if (doarg(argv[i]) == 0) {
    975   1.1      cgd 			printf("\tjob %s is not in the queue\n", argv[i]);
    976   1.1      cgd 			continue;
    977   1.1      cgd 		} else
    978   1.1      cgd 			changed++;
    979   1.1      cgd 	}
    980   1.1      cgd 	for (i = 0; i < nitems; i++)
    981   1.1      cgd 		free(queue[i]);
    982   1.1      cgd 	free(queue);
    983   1.1      cgd 	if (!changed) {
    984   1.1      cgd 		printf("\tqueue order unchanged\n");
    985   1.1      cgd 		return;
    986   1.1      cgd 	}
    987   1.1      cgd 	/*
    988   1.1      cgd 	 * Turn on the public execute bit of the lock file to
    989   1.1      cgd 	 * get lpd to rebuild the queue after the current job.
    990   1.1      cgd 	 */
    991   1.5  hpeyerl 	seteuid(euid);
    992   1.1      cgd 	if (changed && stat(LO, &stbuf) >= 0)
    993   1.8      mrg 		(void)chmod(LO, (stbuf.st_mode & 0777) | 01);
    994   1.5  hpeyerl 
    995   1.5  hpeyerl out:
    996   1.5  hpeyerl 	seteuid(uid);
    997   1.1      cgd }
    998   1.1      cgd 
    999   1.1      cgd /*
   1000   1.1      cgd  * Reposition the job by changing the modification time of
   1001   1.1      cgd  * the control file.
   1002   1.1      cgd  */
   1003   1.4      cgd static int
   1004  1.14      wiz touch(struct queue *q)
   1005   1.1      cgd {
   1006   1.1      cgd 	struct timeval tvp[2];
   1007   1.5  hpeyerl 	int ret;
   1008   1.1      cgd 
   1009   1.1      cgd 	tvp[0].tv_sec = tvp[1].tv_sec = --mtime;
   1010   1.1      cgd 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
   1011   1.5  hpeyerl 	seteuid(euid);
   1012   1.5  hpeyerl 	ret = utimes(q->q_name, tvp);
   1013   1.5  hpeyerl 	seteuid(uid);
   1014   1.5  hpeyerl 	return (ret);
   1015   1.1      cgd }
   1016   1.1      cgd 
   1017   1.1      cgd /*
   1018   1.1      cgd  * Checks if specified job name is in the printer's queue.
   1019   1.1      cgd  * Returns:  negative (-1) if argument name is not in the queue.
   1020   1.1      cgd  */
   1021   1.4      cgd static int
   1022  1.14      wiz doarg(char *job)
   1023   1.1      cgd {
   1024  1.12      mrg 	struct queue **qq;
   1025  1.12      mrg 	int jobnum, n;
   1026  1.12      mrg 	char *cp, *machine;
   1027   1.1      cgd 	int cnt = 0;
   1028   1.1      cgd 	FILE *fp;
   1029   1.1      cgd 
   1030   1.1      cgd 	/*
   1031   1.1      cgd 	 * Look for a job item consisting of system name, colon, number
   1032   1.1      cgd 	 * (example: ucbarpa:114)
   1033   1.1      cgd 	 */
   1034  1.12      mrg 	if ((cp = strchr(job, ':')) != NULL) {
   1035   1.1      cgd 		machine = job;
   1036   1.1      cgd 		*cp++ = '\0';
   1037   1.1      cgd 		job = cp;
   1038   1.1      cgd 	} else
   1039   1.1      cgd 		machine = NULL;
   1040   1.1      cgd 
   1041   1.1      cgd 	/*
   1042   1.1      cgd 	 * Check for job specified by number (example: 112 or 235ucbarpa).
   1043   1.1      cgd 	 */
   1044   1.1      cgd 	if (isdigit(*job)) {
   1045   1.1      cgd 		jobnum = 0;
   1046   1.1      cgd 		do
   1047   1.1      cgd 			jobnum = jobnum * 10 + (*job++ - '0');
   1048   1.1      cgd 		while (isdigit(*job));
   1049   1.1      cgd 		for (qq = queue + nitems; --qq >= queue; ) {
   1050   1.1      cgd 			n = 0;
   1051   1.1      cgd 			for (cp = (*qq)->q_name+3; isdigit(*cp); )
   1052   1.1      cgd 				n = n * 10 + (*cp++ - '0');
   1053   1.1      cgd 			if (jobnum != n)
   1054   1.1      cgd 				continue;
   1055   1.1      cgd 			if (*job && strcmp(job, cp) != 0)
   1056   1.1      cgd 				continue;
   1057   1.1      cgd 			if (machine != NULL && strcmp(machine, cp) != 0)
   1058   1.1      cgd 				continue;
   1059   1.1      cgd 			if (touch(*qq) == 0) {
   1060   1.1      cgd 				printf("\tmoved %s\n", (*qq)->q_name);
   1061   1.1      cgd 				cnt++;
   1062   1.1      cgd 			}
   1063   1.1      cgd 		}
   1064   1.1      cgd 		return(cnt);
   1065   1.1      cgd 	}
   1066   1.1      cgd 	/*
   1067   1.1      cgd 	 * Process item consisting of owner's name (example: henry).
   1068   1.1      cgd 	 */
   1069   1.1      cgd 	for (qq = queue + nitems; --qq >= queue; ) {
   1070   1.5  hpeyerl 		seteuid(euid);
   1071   1.5  hpeyerl 		fp = fopen((*qq)->q_name, "r");
   1072   1.5  hpeyerl 		seteuid(uid);
   1073   1.5  hpeyerl 		if (fp == NULL)
   1074   1.1      cgd 			continue;
   1075   1.1      cgd 		while (getline(fp) > 0)
   1076   1.1      cgd 			if (line[0] == 'P')
   1077   1.1      cgd 				break;
   1078   1.8      mrg 		(void)fclose(fp);
   1079   1.1      cgd 		if (line[0] != 'P' || strcmp(job, line+1) != 0)
   1080   1.1      cgd 			continue;
   1081   1.1      cgd 		if (touch(*qq) == 0) {
   1082   1.1      cgd 			printf("\tmoved %s\n", (*qq)->q_name);
   1083   1.1      cgd 			cnt++;
   1084   1.1      cgd 		}
   1085   1.1      cgd 	}
   1086   1.1      cgd 	return(cnt);
   1087   1.1      cgd }
   1088   1.1      cgd 
   1089   1.1      cgd /*
   1090   1.1      cgd  * Enable everything and start printer (undo `down').
   1091   1.1      cgd  */
   1092   1.4      cgd void
   1093  1.14      wiz up(int argc, char *argv[])
   1094   1.1      cgd {
   1095  1.12      mrg 	int c, status;
   1096  1.12      mrg 	char *cp1, *cp2;
   1097   1.1      cgd 	char prbuf[100];
   1098   1.1      cgd 
   1099   1.1      cgd 	if (argc == 1) {
   1100   1.1      cgd 		printf("Usage: up {all | printer ...}\n");
   1101   1.1      cgd 		return;
   1102   1.1      cgd 	}
   1103   1.1      cgd 	if (argc == 2 && !strcmp(argv[1], "all")) {
   1104   1.1      cgd 		printer = prbuf;
   1105   1.4      cgd 		while (cgetnext(&bp, printcapdb) > 0) {
   1106   1.1      cgd 			cp1 = prbuf;
   1107   1.4      cgd 			cp2 = bp;
   1108  1.12      mrg 			while ((c = *cp2++) && c != '|' && c != ':' &&
   1109  1.12      mrg 			    (cp1 - prbuf) < sizeof(prbuf))
   1110   1.1      cgd 				*cp1++ = c;
   1111   1.1      cgd 			*cp1 = '\0';
   1112   1.1      cgd 			startpr(2);
   1113   1.1      cgd 		}
   1114   1.1      cgd 		return;
   1115   1.1      cgd 	}
   1116   1.1      cgd 	while (--argc) {
   1117   1.1      cgd 		printer = *++argv;
   1118   1.4      cgd 		if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
   1119   1.1      cgd 			printf("cannot open printer description file\n");
   1120   1.1      cgd 			continue;
   1121   1.4      cgd 		} else if (status == -1) {
   1122   1.1      cgd 			printf("unknown printer %s\n", printer);
   1123   1.1      cgd 			continue;
   1124   1.4      cgd 		} else if (status == -3)
   1125   1.4      cgd 			fatal("potential reference loop detected in printcap file");
   1126   1.4      cgd 
   1127   1.1      cgd 		startpr(2);
   1128   1.1      cgd 	}
   1129   1.1      cgd }
   1130