122431Sdist /*
261845Sbostic * Copyright (c) 1983, 1993
361845Sbostic * The Regents of the University of California. All rights reserved.
434203Sbostic *
556125Selan *
656252Selan * %sccs.include.redist.c%
722431Sdist */
822431Sdist
913953Ssam #ifndef lint
1056266Selan static char copyright[] =
1161845Sbostic "@(#) Copyright (c) 1983, 1993\n\
1261845Sbostic The Regents of the University of California. All rights reserved.\n";
1334203Sbostic #endif /* not lint */
1413953Ssam
1522431Sdist #ifndef lint
16*69332Sbostic static char sccsid[] = "@(#)lpq.c 8.3 (Berkeley) 05/10/95";
1734203Sbostic #endif /* not lint */
1822431Sdist
1912108Sralph /*
2012108Sralph * Spool Queue examination program
2112108Sralph *
2269010Stef * lpq [-a] [-l] [-Pprinter] [user...] [job...]
2312108Sralph *
2469010Stef * -a show all non-null queues on the local machine
2530994Sbostic * -l long output
2612108Sralph * -P used to identify printer as per lpr/lprm
2712108Sralph */
2812108Sralph
2955476Sbostic #include <sys/param.h>
3055476Sbostic
3155476Sbostic #include <syslog.h>
3255476Sbostic #include <dirent.h>
3355476Sbostic #include <unistd.h>
3455476Sbostic #include <stdlib.h>
3555476Sbostic #include <stdio.h>
3655476Sbostic #include <ctype.h>
3712108Sralph #include "lp.h"
3855476Sbostic #include "lp.local.h"
3969010Stef #include "pathnames.h"
4012108Sralph
4156125Selan int requ[MAXREQUESTS]; /* job number of spool entries */
4256125Selan int requests; /* # of spool requests */
4312108Sralph char *user[MAXUSERS]; /* users to process */
4456125Selan int users; /* # of users in user array */
4512108Sralph
46*69332Sbostic static int ckqueue __P((char *));
4755476Sbostic void usage __P((void));
4855476Sbostic
4955476Sbostic int
main(argc,argv)5012108Sralph main(argc, argv)
5130994Sbostic register int argc;
5230994Sbostic register char **argv;
5312108Sralph {
5430994Sbostic extern char *optarg;
5530994Sbostic extern int optind;
5669010Stef int ch, aflag, lflag;
5769010Stef char *buf, *cp;
5812108Sralph
5930994Sbostic name = *argv;
6030994Sbostic if (gethostname(host, sizeof(host))) {
6130994Sbostic perror("lpq: gethostname");
6230994Sbostic exit(1);
6330994Sbostic }
6425495Seric openlog("lpd", 0, LOG_LPR);
6512431Sralph
6669010Stef aflag = lflag = 0;
6769010Stef while ((ch = getopt(argc, argv, "alP:")) != EOF)
6830994Sbostic switch((char)ch) {
6969010Stef case 'a':
7069010Stef ++aflag;
7169010Stef break;
7230994Sbostic case 'l': /* long output */
7330994Sbostic ++lflag;
7430994Sbostic break;
7530994Sbostic case 'P': /* printer name */
7630994Sbostic printer = optarg;
7730994Sbostic break;
7830994Sbostic case '?':
7930994Sbostic default:
8030994Sbostic usage();
8130994Sbostic }
8212108Sralph
8369010Stef if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
8412108Sralph printer = DEFLP;
8512108Sralph
8630994Sbostic for (argc -= optind, argv += optind; argc; --argc, ++argv)
8730994Sbostic if (isdigit(argv[0][0])) {
8830994Sbostic if (requests >= MAXREQUESTS)
8930994Sbostic fatal("too many requests");
9030994Sbostic requ[requests++] = atoi(*argv);
9112108Sralph }
9230994Sbostic else {
9330994Sbostic if (users >= MAXUSERS)
9430994Sbostic fatal("too many users");
9530994Sbostic user[users++] = *argv;
9630994Sbostic }
9730994Sbostic
9869010Stef if (aflag) {
9969010Stef while (cgetnext(&buf, printcapdb) > 0) {
100*69332Sbostic if (ckqueue(buf) <= 0) {
10169010Stef free(buf);
10269010Stef continue; /* no jobs */
10369010Stef }
10469010Stef for (cp = buf; *cp; cp++)
10569010Stef if (*cp == '|' || *cp == ':') {
10669010Stef *cp = '\0';
10769010Stef break;
10869010Stef }
10969010Stef printer = buf;
11069010Stef printf("%s:\n", printer);
11169010Stef displayq(lflag);
11269010Stef free(buf);
11369010Stef printf("\n");
11469010Stef }
11569010Stef } else
11669010Stef displayq(lflag);
11716103Sralph exit(0);
11812108Sralph }
11912108Sralph
120*69332Sbostic static int
ckqueue(cap)121*69332Sbostic ckqueue(cap)
122*69332Sbostic char *cap;
12369010Stef {
12469010Stef register struct dirent *d;
12569010Stef DIR *dirp;
12669010Stef char *spooldir;
12769010Stef
128*69332Sbostic if (cgetstr(cap, "sd", &spooldir) == -1)
12969010Stef spooldir = _PATH_DEFSPOOL;
13069010Stef if ((dirp = opendir(spooldir)) == NULL)
13169010Stef return (-1);
13269010Stef while ((d = readdir(dirp)) != NULL) {
13369010Stef if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
13469010Stef continue; /* daemon control files only */
13569010Stef closedir(dirp);
13669010Stef return (1); /* found something */
13769010Stef }
13869010Stef closedir(dirp);
13969010Stef return (0);
14069010Stef }
14169010Stef
14255476Sbostic void
usage()14312108Sralph usage()
14412108Sralph {
14569010Stef puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]");
14612108Sralph exit(1);
14712108Sralph }
148