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