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