1 /* $NetBSD: lpq.c,v 1.10 2000/04/14 06:26:53 simonb 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.10 2000/04/14 06:26:53 simonb 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 __P((char *)); 79 static void usage __P((void)); 80 int main __P((int, char *[])); 81 82 int 83 main(argc, argv) 84 int argc; 85 char **argv; 86 { 87 int ch, aflag, lflag; 88 char *buf, *cp; 89 90 euid = geteuid(); 91 uid = getuid(); 92 seteuid(uid); 93 name = *argv; 94 if (gethostname(host, sizeof(host))) 95 err(1, "lpq: gethostname"); 96 host[sizeof(host) - 1] = '\0'; 97 openlog("lpd", 0, LOG_LPR); 98 99 aflag = lflag = 0; 100 while ((ch = getopt(argc, argv, "alP:w:")) != -1) 101 switch((char)ch) { 102 case 'a': 103 ++aflag; 104 break; 105 case 'l': /* long output */ 106 ++lflag; 107 break; 108 case 'P': /* printer name */ 109 printer = optarg; 110 break; 111 case 'w': 112 wait_time = atoi(optarg); 113 if (wait_time < 0) 114 errx(1, "wait time must be postive: %s", 115 optarg); 116 if (wait_time < 30) 117 warnx("warning: wait time less than 30 seconds"); 118 break; 119 case '?': 120 default: 121 usage(); 122 } 123 124 if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL) 125 printer = DEFLP; 126 127 for (argc -= optind, argv += optind; argc; --argc, ++argv) 128 if (isdigit(argv[0][0])) { 129 if (requests >= MAXREQUESTS) 130 fatal("too many requests"); 131 requ[requests++] = atoi(*argv); 132 } 133 else { 134 if (users >= MAXUSERS) 135 fatal("too many users"); 136 user[users++] = *argv; 137 } 138 139 if (aflag) { 140 while (cgetnext(&buf, printcapdb) > 0) { 141 if (ckqueue(buf) <= 0) { 142 free(buf); 143 continue; /* no jobs */ 144 } 145 for (cp = buf; *cp; cp++) 146 if (*cp == '|' || *cp == ':') { 147 *cp = '\0'; 148 break; 149 } 150 printer = buf; 151 printf("%s:\n", printer); 152 displayq(lflag); 153 free(buf); 154 printf("\n"); 155 } 156 } else 157 displayq(lflag); 158 exit(0); 159 } 160 161 static int 162 ckqueue(cap) 163 char *cap; 164 { 165 struct dirent *d; 166 DIR *dirp; 167 char *spooldir; 168 169 if (cgetstr(cap, "sd", &spooldir) == -1) 170 spooldir = _PATH_DEFSPOOL; 171 if ((dirp = opendir(spooldir)) == NULL) 172 return (-1); 173 while ((d = readdir(dirp)) != NULL) { 174 if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 175 continue; /* daemon control files only */ 176 closedir(dirp); 177 return (1); /* found something */ 178 } 179 closedir(dirp); 180 return (0); 181 } 182 183 static void 184 usage() 185 { 186 puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]"); 187 exit(1); 188 } 189