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