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