lpq.c revision 1.7
1/* $NetBSD: lpq.c,v 1.7 1997/10/05 15:12:18 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 42static char sccsid[] = "@(#)lpq.c 8.3 (Berkeley) 5/10/95"; 43#else 44__RCSID("$NetBSD: lpq.c,v 1.7 1997/10/05 15:12:18 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 72int requ[MAXREQUESTS]; /* job number of spool entries */ 73int requests; /* # of spool requests */ 74char *user[MAXUSERS]; /* users to process */ 75int users; /* # of users in user array */ 76uid_t uid, euid; 77 78static int ckqueue __P((char *)); 79static void usage __P((void)); 80int main __P((int, char *[])); 81 82int 83main(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 openlog("lpd", 0, LOG_LPR); 99 100 aflag = lflag = 0; 101 while ((ch = getopt(argc, argv, "alP:")) != -1) 102 switch((char)ch) { 103 case 'a': 104 ++aflag; 105 break; 106 case 'l': /* long output */ 107 ++lflag; 108 break; 109 case 'P': /* printer name */ 110 printer = optarg; 111 break; 112 case '?': 113 default: 114 usage(); 115 } 116 117 if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL) 118 printer = DEFLP; 119 120 for (argc -= optind, argv += optind; argc; --argc, ++argv) 121 if (isdigit(argv[0][0])) { 122 if (requests >= MAXREQUESTS) 123 fatal("too many requests"); 124 requ[requests++] = atoi(*argv); 125 } 126 else { 127 if (users >= MAXUSERS) 128 fatal("too many users"); 129 user[users++] = *argv; 130 } 131 132 if (aflag) { 133 while (cgetnext(&buf, printcapdb) > 0) { 134 if (ckqueue(buf) <= 0) { 135 free(buf); 136 continue; /* no jobs */ 137 } 138 for (cp = buf; *cp; cp++) 139 if (*cp == '|' || *cp == ':') { 140 *cp = '\0'; 141 break; 142 } 143 printer = buf; 144 printf("%s:\n", printer); 145 displayq(lflag); 146 free(buf); 147 printf("\n"); 148 } 149 } else 150 displayq(lflag); 151 exit(0); 152} 153 154static int 155ckqueue(cap) 156 char *cap; 157{ 158 struct dirent *d; 159 DIR *dirp; 160 char *spooldir; 161 162 if (cgetstr(cap, "sd", &spooldir) == -1) 163 spooldir = _PATH_DEFSPOOL; 164 if ((dirp = opendir(spooldir)) == NULL) 165 return (-1); 166 while ((d = readdir(dirp)) != NULL) { 167 if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 168 continue; /* daemon control files only */ 169 closedir(dirp); 170 return (1); /* found something */ 171 } 172 closedir(dirp); 173 return (0); 174} 175 176static void 177usage() 178{ 179 puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]"); 180 exit(1); 181} 182