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. 140b561052SJoerg Wunsch * 3. All advertising materials mentioning features or use of this software 150b561052SJoerg Wunsch * must display the following acknowledgement: 160b561052SJoerg Wunsch * This product includes software developed by the University of 170b561052SJoerg Wunsch * California, Berkeley and its contributors. 180b561052SJoerg Wunsch * 4. Neither the name of the University nor the names of its contributors 190b561052SJoerg Wunsch * may be used to endorse or promote products derived from this software 200b561052SJoerg Wunsch * without specific prior written permission. 210b561052SJoerg Wunsch * 220b561052SJoerg Wunsch * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 230b561052SJoerg Wunsch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 240b561052SJoerg Wunsch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 250b561052SJoerg Wunsch * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 260b561052SJoerg Wunsch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 270b561052SJoerg Wunsch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 280b561052SJoerg Wunsch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 290b561052SJoerg Wunsch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 300b561052SJoerg Wunsch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 310b561052SJoerg Wunsch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 320b561052SJoerg Wunsch * SUCH DAMAGE. 330b561052SJoerg Wunsch */ 340b561052SJoerg Wunsch 350b561052SJoerg Wunsch #ifndef lint 364a1a0dbeSGarrett Wollman static const char copyright[] = 370b561052SJoerg Wunsch "@(#) Copyright (c) 1983, 1993\n\ 380b561052SJoerg Wunsch The Regents of the University of California. All rights reserved.\n"; 390b561052SJoerg Wunsch #endif /* not lint */ 400b561052SJoerg Wunsch 418e36ed92SGarance A Drosehn #if 0 420b561052SJoerg Wunsch #ifndef lint 430b561052SJoerg Wunsch static char sccsid[] = "@(#)lpq.c 8.3 (Berkeley) 5/10/95"; 440b561052SJoerg Wunsch #endif /* not lint */ 458e36ed92SGarance A Drosehn #endif 468e36ed92SGarance A Drosehn 478e36ed92SGarance A Drosehn #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ 488e36ed92SGarance A Drosehn __FBSDID("$FreeBSD$"); 490b561052SJoerg Wunsch 500b561052SJoerg Wunsch /* 510b561052SJoerg Wunsch * Spool Queue examination program 520b561052SJoerg Wunsch * 530b561052SJoerg Wunsch * lpq [-a] [-l] [-Pprinter] [user...] [job...] 540b561052SJoerg Wunsch * 550b561052SJoerg Wunsch * -a show all non-null queues on the local machine 560b561052SJoerg Wunsch * -l long output 570b561052SJoerg Wunsch * -P used to identify printer as per lpr/lprm 580b561052SJoerg Wunsch */ 590b561052SJoerg Wunsch 600b561052SJoerg Wunsch #include <sys/param.h> 610b561052SJoerg Wunsch 620b561052SJoerg Wunsch #include <ctype.h> 634a1a0dbeSGarrett Wollman #include <dirent.h> 644a1a0dbeSGarrett Wollman #include <err.h> 654a1a0dbeSGarrett Wollman #include <stdio.h> 664a1a0dbeSGarrett Wollman #include <stdlib.h> 674a1a0dbeSGarrett Wollman #include <syslog.h> 684a1a0dbeSGarrett Wollman #include <unistd.h> 694a1a0dbeSGarrett Wollman 700b561052SJoerg Wunsch #include "lp.h" 710b561052SJoerg Wunsch #include "lp.local.h" 720b561052SJoerg Wunsch #include "pathnames.h" 730b561052SJoerg Wunsch 740b561052SJoerg Wunsch int requ[MAXREQUESTS]; /* job number of spool entries */ 750b561052SJoerg Wunsch int requests; /* # of spool requests */ 760b561052SJoerg Wunsch char *user[MAXUSERS]; /* users to process */ 770b561052SJoerg Wunsch int users; /* # of users in user array */ 780b561052SJoerg Wunsch 79360d4ad5SWarner Losh uid_t uid, euid; 80360d4ad5SWarner Losh 81ba7a1ad7SGarance A Drosehn static int ckqueue(const struct printer *_pp); 82ba7a1ad7SGarance A Drosehn static void usage(void); 83ba7a1ad7SGarance A Drosehn int main(int _argc, char **_argv); 840b561052SJoerg Wunsch 850b561052SJoerg Wunsch int 86ba7a1ad7SGarance A Drosehn main(int argc, char **argv) 870b561052SJoerg Wunsch { 880b561052SJoerg Wunsch int ch, aflag, lflag; 89ba7a1ad7SGarance A Drosehn const char *printer; 904a1a0dbeSGarrett Wollman struct printer myprinter, *pp = &myprinter; 910b561052SJoerg Wunsch 924a1a0dbeSGarrett Wollman printer = NULL; 93360d4ad5SWarner Losh euid = geteuid(); 94360d4ad5SWarner Losh uid = getuid(); 95360d4ad5SWarner Losh seteuid(uid); 9631058a75SGarance A Drosehn progname = *argv; 97cc3fd56fSGarance A Drosehn if (gethostname(local_host, sizeof(local_host))) 989b3fe531SPhilippe Charnier err(1, "gethostname"); 990b561052SJoerg Wunsch openlog("lpd", 0, LOG_LPR); 1000b561052SJoerg Wunsch 1010b561052SJoerg Wunsch aflag = lflag = 0; 1026c3f552aSWarner Losh while ((ch = getopt(argc, argv, "alP:")) != -1) 1030b561052SJoerg Wunsch switch((char)ch) { 1040b561052SJoerg Wunsch case 'a': 1050b561052SJoerg Wunsch ++aflag; 1060b561052SJoerg Wunsch break; 1070b561052SJoerg Wunsch case 'l': /* long output */ 1080b561052SJoerg Wunsch ++lflag; 1090b561052SJoerg Wunsch break; 1100b561052SJoerg Wunsch case 'P': /* printer name */ 1110b561052SJoerg Wunsch printer = optarg; 1120b561052SJoerg Wunsch break; 1130b561052SJoerg Wunsch case '?': 1140b561052SJoerg Wunsch default: 1150b561052SJoerg Wunsch usage(); 1160b561052SJoerg Wunsch } 1170b561052SJoerg Wunsch 1180b561052SJoerg Wunsch if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL) 1190b561052SJoerg Wunsch printer = DEFLP; 1200b561052SJoerg Wunsch 1210b561052SJoerg Wunsch for (argc -= optind, argv += optind; argc; --argc, ++argv) 1220b561052SJoerg Wunsch if (isdigit(argv[0][0])) { 1230b561052SJoerg Wunsch if (requests >= MAXREQUESTS) 1244a1a0dbeSGarrett Wollman fatal(0, "too many requests"); 1250b561052SJoerg Wunsch requ[requests++] = atoi(*argv); 1260b561052SJoerg Wunsch } 1270b561052SJoerg Wunsch else { 1280b561052SJoerg Wunsch if (users >= MAXUSERS) 1294a1a0dbeSGarrett Wollman fatal(0, "too many users"); 1300b561052SJoerg Wunsch user[users++] = *argv; 1310b561052SJoerg Wunsch } 1320b561052SJoerg Wunsch 1330b561052SJoerg Wunsch if (aflag) { 1344a1a0dbeSGarrett Wollman int more, status; 1354a1a0dbeSGarrett Wollman 1364a1a0dbeSGarrett Wollman more = firstprinter(pp, &status); 1374a1a0dbeSGarrett Wollman if (status) 1384a1a0dbeSGarrett Wollman goto looperr; 1394a1a0dbeSGarrett Wollman while (more) { 1404a1a0dbeSGarrett Wollman if (ckqueue(pp) > 0) { 1414a1a0dbeSGarrett Wollman printf("%s:\n", pp->printer); 1424a1a0dbeSGarrett Wollman displayq(pp, lflag); 1430b561052SJoerg Wunsch printf("\n"); 1440b561052SJoerg Wunsch } 1454a1a0dbeSGarrett Wollman do { 1464a1a0dbeSGarrett Wollman more = nextprinter(pp, &status); 1474a1a0dbeSGarrett Wollman looperr: 1484a1a0dbeSGarrett Wollman switch (status) { 1494a1a0dbeSGarrett Wollman case PCAPERR_TCOPEN: 1504a1a0dbeSGarrett Wollman printf("warning: %s: unresolved " 1514a1a0dbeSGarrett Wollman "tc= reference(s) ", 1524a1a0dbeSGarrett Wollman pp->printer); 1534a1a0dbeSGarrett Wollman case PCAPERR_SUCCESS: 1544a1a0dbeSGarrett Wollman break; 1554a1a0dbeSGarrett Wollman default: 1566d39e1b7SGarance A Drosehn fatal(pp, "%s", pcaperr(status)); 1574a1a0dbeSGarrett Wollman } 1584a1a0dbeSGarrett Wollman } while (more && status); 1594a1a0dbeSGarrett Wollman } 1604a1a0dbeSGarrett Wollman } else { 1614a1a0dbeSGarrett Wollman int status; 1624a1a0dbeSGarrett Wollman 1634a1a0dbeSGarrett Wollman init_printer(pp); 1644a1a0dbeSGarrett Wollman status = getprintcap(printer, pp); 1654a1a0dbeSGarrett Wollman if (status < 0) 1666d39e1b7SGarance A Drosehn fatal(pp, "%s", pcaperr(status)); 1674a1a0dbeSGarrett Wollman 1684a1a0dbeSGarrett Wollman displayq(pp, lflag); 1694a1a0dbeSGarrett Wollman } 1700b561052SJoerg Wunsch exit(0); 1710b561052SJoerg Wunsch } 1720b561052SJoerg Wunsch 1730b561052SJoerg Wunsch static int 174ba7a1ad7SGarance A Drosehn ckqueue(const struct printer *pp) 1750b561052SJoerg Wunsch { 1760b561052SJoerg Wunsch register struct dirent *d; 1770b561052SJoerg Wunsch DIR *dirp; 1780b561052SJoerg Wunsch char *spooldir; 1790b561052SJoerg Wunsch 1804a1a0dbeSGarrett Wollman spooldir = pp->spool_dir; 1810b561052SJoerg Wunsch if ((dirp = opendir(spooldir)) == NULL) 1820b561052SJoerg Wunsch return (-1); 1830b561052SJoerg Wunsch while ((d = readdir(dirp)) != NULL) { 1840b561052SJoerg Wunsch if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 1850b561052SJoerg Wunsch continue; /* daemon control files only */ 1860b561052SJoerg Wunsch closedir(dirp); 1870b561052SJoerg Wunsch return (1); /* found something */ 1880b561052SJoerg Wunsch } 1890b561052SJoerg Wunsch closedir(dirp); 1900b561052SJoerg Wunsch return (0); 1910b561052SJoerg Wunsch } 1920b561052SJoerg Wunsch 1939b3fe531SPhilippe Charnier static void 194ba7a1ad7SGarance A Drosehn usage(void) 1950b561052SJoerg Wunsch { 1969b3fe531SPhilippe Charnier fprintf(stderr, 1979b3fe531SPhilippe Charnier "usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]\n"); 1980b561052SJoerg Wunsch exit(1); 1990b561052SJoerg Wunsch } 200