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