1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1983, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #if 0 40 #ifndef lint 41 static char sccsid[] = "@(#)lpq.c 8.3 (Berkeley) 5/10/95"; 42 #endif /* not lint */ 43 #endif 44 45 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ 46 __FBSDID("$FreeBSD$"); 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 <ctype.h> 61 #include <dirent.h> 62 #include <err.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <syslog.h> 66 #include <unistd.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 77 uid_t uid, euid; 78 79 static int ckqueue(const struct printer *_pp); 80 static void usage(void); 81 int main(int _argc, char **_argv); 82 83 int 84 main(int argc, char **argv) 85 { 86 int ch, aflag, lflag; 87 const char *printer; 88 struct printer myprinter, *pp = &myprinter; 89 90 printer = NULL; 91 euid = geteuid(); 92 uid = getuid(); 93 PRIV_END 94 progname = *argv; 95 if (gethostname(local_host, sizeof(local_host))) 96 err(1, "gethostname"); 97 openlog("lpd", 0, LOG_LPR); 98 99 aflag = lflag = 0; 100 while ((ch = getopt(argc, argv, "alP:")) != -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 '?': 112 default: 113 usage(); 114 } 115 116 if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL) 117 printer = DEFLP; 118 119 for (argc -= optind, argv += optind; argc; --argc, ++argv) 120 if (isdigit(argv[0][0])) { 121 if (requests >= MAXREQUESTS) 122 fatal(0, "too many requests"); 123 requ[requests++] = atoi(*argv); 124 } 125 else { 126 if (users >= MAXUSERS) 127 fatal(0, "too many users"); 128 user[users++] = *argv; 129 } 130 131 if (aflag) { 132 int more, status; 133 134 more = firstprinter(pp, &status); 135 if (status) 136 goto looperr; 137 while (more) { 138 if (ckqueue(pp) > 0) { 139 printf("%s:\n", pp->printer); 140 displayq(pp, lflag); 141 printf("\n"); 142 } 143 do { 144 more = nextprinter(pp, &status); 145 looperr: 146 switch (status) { 147 case PCAPERR_TCOPEN: 148 printf("warning: %s: unresolved " 149 "tc= reference(s) ", 150 pp->printer); 151 case PCAPERR_SUCCESS: 152 break; 153 default: 154 fatal(pp, "%s", pcaperr(status)); 155 } 156 } while (more && status); 157 } 158 } else { 159 int status; 160 161 init_printer(pp); 162 status = getprintcap(printer, pp); 163 if (status < 0) 164 fatal(pp, "%s", pcaperr(status)); 165 166 displayq(pp, lflag); 167 } 168 exit(0); 169 } 170 171 static int 172 ckqueue(const struct printer *pp) 173 { 174 register struct dirent *d; 175 DIR *dirp; 176 char *spooldir; 177 178 spooldir = pp->spool_dir; 179 if ((dirp = opendir(spooldir)) == NULL) 180 return (-1); 181 while ((d = readdir(dirp)) != NULL) { 182 if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 183 continue; /* daemon control files only */ 184 closedir(dirp); 185 return (1); /* found something */ 186 } 187 closedir(dirp); 188 return (0); 189 } 190 191 static void 192 usage(void) 193 { 194 fprintf(stderr, 195 "usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]\n"); 196 exit(1); 197 } 198