10b561052SJoerg Wunsch /* 20b561052SJoerg Wunsch * Copyright (c) 1983, 1993 30b561052SJoerg Wunsch * The Regents of the University of California. All rights reserved. 40b561052SJoerg Wunsch * 50b561052SJoerg Wunsch * 60b561052SJoerg Wunsch * Redistribution and use in source and binary forms, with or without 70b561052SJoerg Wunsch * modification, are permitted provided that the following conditions 80b561052SJoerg Wunsch * are met: 90b561052SJoerg Wunsch * 1. Redistributions of source code must retain the above copyright 100b561052SJoerg Wunsch * notice, this list of conditions and the following disclaimer. 110b561052SJoerg Wunsch * 2. Redistributions in binary form must reproduce the above copyright 120b561052SJoerg Wunsch * notice, this list of conditions and the following disclaimer in the 130b561052SJoerg Wunsch * documentation and/or other materials provided with the distribution. 14*fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 150b561052SJoerg Wunsch * may be used to endorse or promote products derived from this software 160b561052SJoerg Wunsch * without specific prior written permission. 170b561052SJoerg Wunsch * 180b561052SJoerg Wunsch * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 190b561052SJoerg Wunsch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 200b561052SJoerg Wunsch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 210b561052SJoerg Wunsch * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 220b561052SJoerg Wunsch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 230b561052SJoerg Wunsch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 240b561052SJoerg Wunsch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 250b561052SJoerg Wunsch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 260b561052SJoerg Wunsch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 270b561052SJoerg Wunsch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 280b561052SJoerg Wunsch * SUCH DAMAGE. 290b561052SJoerg Wunsch */ 300b561052SJoerg Wunsch 310b561052SJoerg Wunsch #ifndef lint 324a1a0dbeSGarrett Wollman static const char copyright[] = 330b561052SJoerg Wunsch "@(#) Copyright (c) 1983, 1993\n\ 340b561052SJoerg Wunsch The Regents of the University of California. All rights reserved.\n"; 350b561052SJoerg Wunsch #endif /* not lint */ 360b561052SJoerg Wunsch 378e36ed92SGarance A Drosehn #if 0 380b561052SJoerg Wunsch #ifndef lint 390b561052SJoerg Wunsch static char sccsid[] = "@(#)lpq.c 8.3 (Berkeley) 5/10/95"; 400b561052SJoerg Wunsch #endif /* not lint */ 418e36ed92SGarance A Drosehn #endif 428e36ed92SGarance A Drosehn 438e36ed92SGarance A Drosehn #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ 448e36ed92SGarance A Drosehn __FBSDID("$FreeBSD$"); 450b561052SJoerg Wunsch 460b561052SJoerg Wunsch /* 470b561052SJoerg Wunsch * Spool Queue examination program 480b561052SJoerg Wunsch * 490b561052SJoerg Wunsch * lpq [-a] [-l] [-Pprinter] [user...] [job...] 500b561052SJoerg Wunsch * 510b561052SJoerg Wunsch * -a show all non-null queues on the local machine 520b561052SJoerg Wunsch * -l long output 530b561052SJoerg Wunsch * -P used to identify printer as per lpr/lprm 540b561052SJoerg Wunsch */ 550b561052SJoerg Wunsch 560b561052SJoerg Wunsch #include <sys/param.h> 570b561052SJoerg Wunsch 580b561052SJoerg Wunsch #include <ctype.h> 594a1a0dbeSGarrett Wollman #include <dirent.h> 604a1a0dbeSGarrett Wollman #include <err.h> 614a1a0dbeSGarrett Wollman #include <stdio.h> 624a1a0dbeSGarrett Wollman #include <stdlib.h> 634a1a0dbeSGarrett Wollman #include <syslog.h> 644a1a0dbeSGarrett Wollman #include <unistd.h> 654a1a0dbeSGarrett Wollman 660b561052SJoerg Wunsch #include "lp.h" 670b561052SJoerg Wunsch #include "lp.local.h" 680b561052SJoerg Wunsch #include "pathnames.h" 690b561052SJoerg Wunsch 700b561052SJoerg Wunsch int requ[MAXREQUESTS]; /* job number of spool entries */ 710b561052SJoerg Wunsch int requests; /* # of spool requests */ 720b561052SJoerg Wunsch char *user[MAXUSERS]; /* users to process */ 730b561052SJoerg Wunsch int users; /* # of users in user array */ 740b561052SJoerg Wunsch 75360d4ad5SWarner Losh uid_t uid, euid; 76360d4ad5SWarner Losh 77ba7a1ad7SGarance A Drosehn static int ckqueue(const struct printer *_pp); 78ba7a1ad7SGarance A Drosehn static void usage(void); 79ba7a1ad7SGarance A Drosehn int main(int _argc, char **_argv); 800b561052SJoerg Wunsch 810b561052SJoerg Wunsch int 82ba7a1ad7SGarance A Drosehn main(int argc, char **argv) 830b561052SJoerg Wunsch { 840b561052SJoerg Wunsch int ch, aflag, lflag; 85ba7a1ad7SGarance A Drosehn const char *printer; 864a1a0dbeSGarrett Wollman struct printer myprinter, *pp = &myprinter; 870b561052SJoerg Wunsch 884a1a0dbeSGarrett Wollman printer = NULL; 89360d4ad5SWarner Losh euid = geteuid(); 90360d4ad5SWarner Losh uid = getuid(); 911d1d4a47SEitan Adler PRIV_END 9231058a75SGarance A Drosehn progname = *argv; 93cc3fd56fSGarance A Drosehn if (gethostname(local_host, sizeof(local_host))) 949b3fe531SPhilippe Charnier err(1, "gethostname"); 950b561052SJoerg Wunsch openlog("lpd", 0, LOG_LPR); 960b561052SJoerg Wunsch 970b561052SJoerg Wunsch aflag = lflag = 0; 986c3f552aSWarner Losh while ((ch = getopt(argc, argv, "alP:")) != -1) 990b561052SJoerg Wunsch switch((char)ch) { 1000b561052SJoerg Wunsch case 'a': 1010b561052SJoerg Wunsch ++aflag; 1020b561052SJoerg Wunsch break; 1030b561052SJoerg Wunsch case 'l': /* long output */ 1040b561052SJoerg Wunsch ++lflag; 1050b561052SJoerg Wunsch break; 1060b561052SJoerg Wunsch case 'P': /* printer name */ 1070b561052SJoerg Wunsch printer = optarg; 1080b561052SJoerg Wunsch break; 1090b561052SJoerg Wunsch case '?': 1100b561052SJoerg Wunsch default: 1110b561052SJoerg Wunsch usage(); 1120b561052SJoerg Wunsch } 1130b561052SJoerg Wunsch 1140b561052SJoerg Wunsch if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL) 1150b561052SJoerg Wunsch printer = DEFLP; 1160b561052SJoerg Wunsch 1170b561052SJoerg Wunsch for (argc -= optind, argv += optind; argc; --argc, ++argv) 1180b561052SJoerg Wunsch if (isdigit(argv[0][0])) { 1190b561052SJoerg Wunsch if (requests >= MAXREQUESTS) 1204a1a0dbeSGarrett Wollman fatal(0, "too many requests"); 1210b561052SJoerg Wunsch requ[requests++] = atoi(*argv); 1220b561052SJoerg Wunsch } 1230b561052SJoerg Wunsch else { 1240b561052SJoerg Wunsch if (users >= MAXUSERS) 1254a1a0dbeSGarrett Wollman fatal(0, "too many users"); 1260b561052SJoerg Wunsch user[users++] = *argv; 1270b561052SJoerg Wunsch } 1280b561052SJoerg Wunsch 1290b561052SJoerg Wunsch if (aflag) { 1304a1a0dbeSGarrett Wollman int more, status; 1314a1a0dbeSGarrett Wollman 1324a1a0dbeSGarrett Wollman more = firstprinter(pp, &status); 1334a1a0dbeSGarrett Wollman if (status) 1344a1a0dbeSGarrett Wollman goto looperr; 1354a1a0dbeSGarrett Wollman while (more) { 1364a1a0dbeSGarrett Wollman if (ckqueue(pp) > 0) { 1374a1a0dbeSGarrett Wollman printf("%s:\n", pp->printer); 1384a1a0dbeSGarrett Wollman displayq(pp, lflag); 1390b561052SJoerg Wunsch printf("\n"); 1400b561052SJoerg Wunsch } 1414a1a0dbeSGarrett Wollman do { 1424a1a0dbeSGarrett Wollman more = nextprinter(pp, &status); 1434a1a0dbeSGarrett Wollman looperr: 1444a1a0dbeSGarrett Wollman switch (status) { 1454a1a0dbeSGarrett Wollman case PCAPERR_TCOPEN: 1464a1a0dbeSGarrett Wollman printf("warning: %s: unresolved " 1474a1a0dbeSGarrett Wollman "tc= reference(s) ", 1484a1a0dbeSGarrett Wollman pp->printer); 1494a1a0dbeSGarrett Wollman case PCAPERR_SUCCESS: 1504a1a0dbeSGarrett Wollman break; 1514a1a0dbeSGarrett Wollman default: 1526d39e1b7SGarance A Drosehn fatal(pp, "%s", pcaperr(status)); 1534a1a0dbeSGarrett Wollman } 1544a1a0dbeSGarrett Wollman } while (more && status); 1554a1a0dbeSGarrett Wollman } 1564a1a0dbeSGarrett Wollman } else { 1574a1a0dbeSGarrett Wollman int status; 1584a1a0dbeSGarrett Wollman 1594a1a0dbeSGarrett Wollman init_printer(pp); 1604a1a0dbeSGarrett Wollman status = getprintcap(printer, pp); 1614a1a0dbeSGarrett Wollman if (status < 0) 1626d39e1b7SGarance A Drosehn fatal(pp, "%s", pcaperr(status)); 1634a1a0dbeSGarrett Wollman 1644a1a0dbeSGarrett Wollman displayq(pp, lflag); 1654a1a0dbeSGarrett Wollman } 1660b561052SJoerg Wunsch exit(0); 1670b561052SJoerg Wunsch } 1680b561052SJoerg Wunsch 1690b561052SJoerg Wunsch static int 170ba7a1ad7SGarance A Drosehn ckqueue(const struct printer *pp) 1710b561052SJoerg Wunsch { 1720b561052SJoerg Wunsch register struct dirent *d; 1730b561052SJoerg Wunsch DIR *dirp; 1740b561052SJoerg Wunsch char *spooldir; 1750b561052SJoerg Wunsch 1764a1a0dbeSGarrett Wollman spooldir = pp->spool_dir; 1770b561052SJoerg Wunsch if ((dirp = opendir(spooldir)) == NULL) 1780b561052SJoerg Wunsch return (-1); 1790b561052SJoerg Wunsch while ((d = readdir(dirp)) != NULL) { 1800b561052SJoerg Wunsch if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 1810b561052SJoerg Wunsch continue; /* daemon control files only */ 1820b561052SJoerg Wunsch closedir(dirp); 1830b561052SJoerg Wunsch return (1); /* found something */ 1840b561052SJoerg Wunsch } 1850b561052SJoerg Wunsch closedir(dirp); 1860b561052SJoerg Wunsch return (0); 1870b561052SJoerg Wunsch } 1880b561052SJoerg Wunsch 1899b3fe531SPhilippe Charnier static void 190ba7a1ad7SGarance A Drosehn usage(void) 1910b561052SJoerg Wunsch { 1929b3fe531SPhilippe Charnier fprintf(stderr, 1939b3fe531SPhilippe Charnier "usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]\n"); 1940b561052SJoerg Wunsch exit(1); 1950b561052SJoerg Wunsch } 196