1 /* $OpenBSD: lpq.c,v 1.7 1997/01/17 16:12:44 millert 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 #ifndef lint 38 static char copyright[] = 39 "@(#) Copyright (c) 1983, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "@(#)lpq.c 8.3 (Berkeley) 5/10/95"; 46 #else 47 static char rcsid[] = "$OpenBSD: lpq.c,v 1.7 1997/01/17 16:12:44 millert Exp $"; 48 #endif 49 #endif /* not lint */ 50 51 /* 52 * Spool Queue examination program 53 * 54 * lpq [-a] [-l] [-Pprinter] [user...] [job...] 55 * 56 * -a show all non-null queues on the local machine 57 * -l long output 58 * -P used to identify printer as per lpr/lprm 59 */ 60 61 #include <sys/param.h> 62 63 #include <syslog.h> 64 #include <dirent.h> 65 #include <unistd.h> 66 #include <stdlib.h> 67 #include <stdio.h> 68 #include <ctype.h> 69 #include "lp.h" 70 #include "lp.local.h" 71 #include "pathnames.h" 72 73 int requ[MAXREQUESTS]; /* job number of spool entries */ 74 int requests; /* # of spool requests */ 75 char *user[MAXUSERS]; /* users to process */ 76 int users; /* # of users in user array */ 77 78 uid_t uid, euid; 79 80 static int ckqueue __P((char *)); 81 void usage __P((void)); 82 83 int 84 main(argc, argv) 85 register int argc; 86 register char **argv; 87 { 88 extern char *optarg; 89 extern int optind; 90 int ch, aflag, lflag; 91 char *buf, *cp; 92 93 euid = geteuid(); 94 uid = getuid(); 95 seteuid(uid); 96 name = *argv; 97 if (gethostname(host, sizeof(host))) { 98 perror("lpq: gethostname"); 99 exit(1); 100 } 101 openlog("lpd", 0, LOG_LPR); 102 103 aflag = lflag = 0; 104 while ((ch = getopt(argc, argv, "alP:")) != -1) 105 switch((char)ch) { 106 case 'a': 107 ++aflag; 108 break; 109 case 'l': /* long output */ 110 ++lflag; 111 break; 112 case 'P': /* printer name */ 113 printer = optarg; 114 break; 115 case '?': 116 default: 117 usage(); 118 } 119 120 if (!aflag && printer == NULL) { 121 char *p; 122 123 printer = DEFLP; 124 if ((p = getenv("PRINTER")) != NULL) 125 printer = p; 126 } 127 128 for (argc -= optind, argv += optind; argc; --argc, ++argv) 129 if (isdigit(argv[0][0])) { 130 if (requests >= MAXREQUESTS) 131 fatal("too many requests"); 132 requ[requests++] = atoi(*argv); 133 } 134 else { 135 if (users >= MAXUSERS) 136 fatal("too many users"); 137 user[users++] = *argv; 138 } 139 140 if (aflag) { 141 while (cgetnext(&buf, printcapdb) > 0) { 142 if (ckqueue(buf) <= 0) { 143 free(buf); 144 continue; /* no jobs */ 145 } 146 for (cp = buf; *cp; cp++) 147 if (*cp == '|' || *cp == ':') { 148 *cp = '\0'; 149 break; 150 } 151 printer = buf; 152 printf("%s:\n", printer); 153 displayq(lflag); 154 free(buf); 155 printf("\n"); 156 } 157 } else 158 displayq(lflag); 159 exit(0); 160 } 161 162 static int 163 ckqueue(cap) 164 char *cap; 165 { 166 register struct dirent *d; 167 DIR *dirp; 168 char *spooldir; 169 170 if (cgetstr(cap, "sd", &spooldir) == -1) 171 spooldir = _PATH_DEFSPOOL; 172 if ((dirp = opendir(spooldir)) == NULL) 173 return (-1); 174 while ((d = readdir(dirp)) != NULL) { 175 if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 176 continue; /* daemon control files only */ 177 closedir(dirp); 178 return (1); /* found something */ 179 } 180 closedir(dirp); 181 return (0); 182 } 183 184 void 185 usage() 186 { 187 puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]"); 188 exit(1); 189 } 190