lpq.c revision 1.8
11.8Smrg/* $NetBSD: lpq.c,v 1.8 1998/07/06 07:03:28 mrg Exp $ */ 21.7Smrg 31.1Scgd/* 41.3Scgd * Copyright (c) 1983, 1993 51.3Scgd * The Regents of the University of California. All rights reserved. 61.3Scgd * 71.1Scgd * 81.1Scgd * Redistribution and use in source and binary forms, with or without 91.1Scgd * modification, are permitted provided that the following conditions 101.1Scgd * are met: 111.1Scgd * 1. Redistributions of source code must retain the above copyright 121.1Scgd * notice, this list of conditions and the following disclaimer. 131.1Scgd * 2. Redistributions in binary form must reproduce the above copyright 141.1Scgd * notice, this list of conditions and the following disclaimer in the 151.1Scgd * documentation and/or other materials provided with the distribution. 161.1Scgd * 3. All advertising materials mentioning features or use of this software 171.1Scgd * must display the following acknowledgement: 181.1Scgd * This product includes software developed by the University of 191.1Scgd * California, Berkeley and its contributors. 201.1Scgd * 4. Neither the name of the University nor the names of its contributors 211.1Scgd * may be used to endorse or promote products derived from this software 221.1Scgd * without specific prior written permission. 231.1Scgd * 241.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271.1Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341.1Scgd * SUCH DAMAGE. 351.1Scgd */ 361.1Scgd 371.7Smrg#include <sys/cdefs.h> 381.1Scgd#ifndef lint 391.7Smrg__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\ 401.7Smrg The Regents of the University of California. All rights reserved.\n"); 411.7Smrg#if 0 421.6Smrgstatic char sccsid[] = "@(#)lpq.c 8.3 (Berkeley) 5/10/95"; 431.7Smrg#else 441.8Smrg__RCSID("$NetBSD: lpq.c,v 1.8 1998/07/06 07:03:28 mrg Exp $"); 451.7Smrg#endif 461.1Scgd#endif /* not lint */ 471.1Scgd 481.1Scgd/* 491.1Scgd * Spool Queue examination program 501.1Scgd * 511.6Smrg * lpq [-a] [-l] [-Pprinter] [user...] [job...] 521.1Scgd * 531.6Smrg * -a show all non-null queues on the local machine 541.1Scgd * -l long output 551.1Scgd * -P used to identify printer as per lpr/lprm 561.1Scgd */ 571.1Scgd 581.3Scgd#include <sys/param.h> 591.3Scgd 601.3Scgd#include <syslog.h> 611.3Scgd#include <dirent.h> 621.3Scgd#include <unistd.h> 631.3Scgd#include <stdlib.h> 641.3Scgd#include <stdio.h> 651.3Scgd#include <ctype.h> 661.7Smrg#include <err.h> 671.7Smrg 681.1Scgd#include "lp.h" 691.3Scgd#include "lp.local.h" 701.6Smrg#include "pathnames.h" 711.1Scgd 721.3Scgdint requ[MAXREQUESTS]; /* job number of spool entries */ 731.3Scgdint requests; /* # of spool requests */ 741.1Scgdchar *user[MAXUSERS]; /* users to process */ 751.3Scgdint users; /* # of users in user array */ 761.4Shpeyerluid_t uid, euid; 771.4Shpeyerl 781.6Smrgstatic int ckqueue __P((char *)); 791.7Smrgstatic void usage __P((void)); 801.7Smrgint main __P((int, char *[])); 811.1Scgd 821.3Scgdint 831.1Scgdmain(argc, argv) 841.7Smrg int argc; 851.7Smrg char **argv; 861.1Scgd{ 871.1Scgd extern char *optarg; 881.1Scgd extern int optind; 891.6Smrg int ch, aflag, lflag; 901.6Smrg char *buf, *cp; 911.1Scgd 921.4Shpeyerl euid = geteuid(); 931.4Shpeyerl uid = getuid(); 941.4Shpeyerl seteuid(uid); 951.1Scgd name = *argv; 961.7Smrg if (gethostname(host, sizeof(host))) 971.7Smrg err(1, "lpq: gethostname"); 981.8Smrg host[sizeof(host) - 1] = '\0'; 991.1Scgd openlog("lpd", 0, LOG_LPR); 1001.1Scgd 1011.6Smrg aflag = lflag = 0; 1021.7Smrg while ((ch = getopt(argc, argv, "alP:")) != -1) 1031.1Scgd switch((char)ch) { 1041.6Smrg case 'a': 1051.6Smrg ++aflag; 1061.6Smrg break; 1071.1Scgd case 'l': /* long output */ 1081.1Scgd ++lflag; 1091.1Scgd break; 1101.1Scgd case 'P': /* printer name */ 1111.1Scgd printer = optarg; 1121.1Scgd break; 1131.1Scgd case '?': 1141.1Scgd default: 1151.1Scgd usage(); 1161.1Scgd } 1171.1Scgd 1181.6Smrg if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL) 1191.1Scgd printer = DEFLP; 1201.1Scgd 1211.1Scgd for (argc -= optind, argv += optind; argc; --argc, ++argv) 1221.1Scgd if (isdigit(argv[0][0])) { 1231.1Scgd if (requests >= MAXREQUESTS) 1241.1Scgd fatal("too many requests"); 1251.1Scgd requ[requests++] = atoi(*argv); 1261.1Scgd } 1271.1Scgd else { 1281.1Scgd if (users >= MAXUSERS) 1291.1Scgd fatal("too many users"); 1301.1Scgd user[users++] = *argv; 1311.1Scgd } 1321.1Scgd 1331.6Smrg if (aflag) { 1341.6Smrg while (cgetnext(&buf, printcapdb) > 0) { 1351.6Smrg if (ckqueue(buf) <= 0) { 1361.6Smrg free(buf); 1371.6Smrg continue; /* no jobs */ 1381.6Smrg } 1391.6Smrg for (cp = buf; *cp; cp++) 1401.6Smrg if (*cp == '|' || *cp == ':') { 1411.6Smrg *cp = '\0'; 1421.6Smrg break; 1431.6Smrg } 1441.6Smrg printer = buf; 1451.6Smrg printf("%s:\n", printer); 1461.6Smrg displayq(lflag); 1471.6Smrg free(buf); 1481.6Smrg printf("\n"); 1491.6Smrg } 1501.6Smrg } else 1511.6Smrg displayq(lflag); 1521.1Scgd exit(0); 1531.1Scgd} 1541.1Scgd 1551.6Smrgstatic int 1561.6Smrgckqueue(cap) 1571.6Smrg char *cap; 1581.6Smrg{ 1591.7Smrg struct dirent *d; 1601.6Smrg DIR *dirp; 1611.6Smrg char *spooldir; 1621.6Smrg 1631.6Smrg if (cgetstr(cap, "sd", &spooldir) == -1) 1641.6Smrg spooldir = _PATH_DEFSPOOL; 1651.6Smrg if ((dirp = opendir(spooldir)) == NULL) 1661.6Smrg return (-1); 1671.6Smrg while ((d = readdir(dirp)) != NULL) { 1681.6Smrg if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 1691.6Smrg continue; /* daemon control files only */ 1701.6Smrg closedir(dirp); 1711.6Smrg return (1); /* found something */ 1721.6Smrg } 1731.6Smrg closedir(dirp); 1741.6Smrg return (0); 1751.6Smrg} 1761.6Smrg 1771.7Smrgstatic void 1781.1Scgdusage() 1791.1Scgd{ 1801.6Smrg puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]"); 1811.1Scgd exit(1); 1821.1Scgd} 183