lpq.c revision 1.6
1/* $NetBSD: lpq.c,v 1.6 1997/10/05 11:52:38 mrg Exp $ */ 2/* 3 * Copyright (c) 1983, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36#ifndef lint 37static char copyright[] = 38"@(#) Copyright (c) 1983, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40#endif /* not lint */ 41 42#ifndef lint 43static char sccsid[] = "@(#)lpq.c 8.3 (Berkeley) 5/10/95"; 44#endif /* not lint */ 45 46/* 47 * Spool Queue examination program 48 * 49 * lpq [-a] [-l] [-Pprinter] [user...] [job...] 50 * 51 * -a show all non-null queues on the local machine 52 * -l long output 53 * -P used to identify printer as per lpr/lprm 54 */ 55 56#include <sys/param.h> 57 58#include <syslog.h> 59#include <dirent.h> 60#include <unistd.h> 61#include <stdlib.h> 62#include <stdio.h> 63#include <ctype.h> 64#include "lp.h" 65#include "lp.local.h" 66#include "pathnames.h" 67 68int requ[MAXREQUESTS]; /* job number of spool entries */ 69int requests; /* # of spool requests */ 70char *user[MAXUSERS]; /* users to process */ 71int users; /* # of users in user array */ 72uid_t uid, euid; 73 74static int ckqueue __P((char *)); 75void usage __P((void)); 76 77int 78main(argc, argv) 79 register int argc; 80 register char **argv; 81{ 82 extern char *optarg; 83 extern int optind; 84 int ch, aflag, lflag; 85 char *buf, *cp; 86 87 euid = geteuid(); 88 uid = getuid(); 89 seteuid(uid); 90 name = *argv; 91 if (gethostname(host, sizeof(host))) { 92 perror("lpq: gethostname"); 93 exit(1); 94 } 95 openlog("lpd", 0, LOG_LPR); 96 97 aflag = lflag = 0; 98 while ((ch = getopt(argc, argv, "alP:")) != EOF) 99 switch((char)ch) { 100 case 'a': 101 ++aflag; 102 break; 103 case 'l': /* long output */ 104 ++lflag; 105 break; 106 case 'P': /* printer name */ 107 printer = optarg; 108 break; 109 case '?': 110 default: 111 usage(); 112 } 113 114 if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL) 115 printer = DEFLP; 116 117 for (argc -= optind, argv += optind; argc; --argc, ++argv) 118 if (isdigit(argv[0][0])) { 119 if (requests >= MAXREQUESTS) 120 fatal("too many requests"); 121 requ[requests++] = atoi(*argv); 122 } 123 else { 124 if (users >= MAXUSERS) 125 fatal("too many users"); 126 user[users++] = *argv; 127 } 128 129 if (aflag) { 130 while (cgetnext(&buf, printcapdb) > 0) { 131 if (ckqueue(buf) <= 0) { 132 free(buf); 133 continue; /* no jobs */ 134 } 135 for (cp = buf; *cp; cp++) 136 if (*cp == '|' || *cp == ':') { 137 *cp = '\0'; 138 break; 139 } 140 printer = buf; 141 printf("%s:\n", printer); 142 displayq(lflag); 143 free(buf); 144 printf("\n"); 145 } 146 } else 147 displayq(lflag); 148 exit(0); 149} 150 151static int 152ckqueue(cap) 153 char *cap; 154{ 155 register struct dirent *d; 156 DIR *dirp; 157 char *spooldir; 158 159 if (cgetstr(cap, "sd", &spooldir) == -1) 160 spooldir = _PATH_DEFSPOOL; 161 if ((dirp = opendir(spooldir)) == NULL) 162 return (-1); 163 while ((d = readdir(dirp)) != NULL) { 164 if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 165 continue; /* daemon control files only */ 166 closedir(dirp); 167 return (1); /* found something */ 168 } 169 closedir(dirp); 170 return (0); 171} 172 173void 174usage() 175{ 176 puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]"); 177 exit(1); 178} 179