xref: /csrg-svn/usr.sbin/lpr/lpq/lpq.c (revision 69010)
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*69010Stef static char sccsid[] = "@(#)lpq.c	8.2 (Berkeley) 04/28/95";
1734203Sbostic #endif /* not lint */
1822431Sdist 
1912108Sralph /*
2012108Sralph  * Spool Queue examination program
2112108Sralph  *
22*69010Stef  * lpq [-a] [-l] [-Pprinter] [user...] [job...]
2312108Sralph  *
24*69010Stef  * -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"
39*69010Stef #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 
4655476Sbostic void usage __P((void));
4755476Sbostic 
4855476Sbostic int
4912108Sralph main(argc, argv)
5030994Sbostic 	register int	argc;
5130994Sbostic 	register char	**argv;
5212108Sralph {
5330994Sbostic 	extern char	*optarg;
5430994Sbostic 	extern int	optind;
55*69010Stef 	int	ch, aflag, lflag;
56*69010Stef 	char	*buf, *cp;
5712108Sralph 
5830994Sbostic 	name = *argv;
5930994Sbostic 	if (gethostname(host, sizeof(host))) {
6030994Sbostic 		perror("lpq: gethostname");
6130994Sbostic 		exit(1);
6230994Sbostic 	}
6325495Seric 	openlog("lpd", 0, LOG_LPR);
6412431Sralph 
65*69010Stef 	aflag = lflag = 0;
66*69010Stef 	while ((ch = getopt(argc, argv, "alP:")) != EOF)
6730994Sbostic 		switch((char)ch) {
68*69010Stef 		case 'a':
69*69010Stef 			++aflag;
70*69010Stef 			break;
7130994Sbostic 		case 'l':			/* long output */
7230994Sbostic 			++lflag;
7330994Sbostic 			break;
7430994Sbostic 		case 'P':		/* printer name */
7530994Sbostic 			printer = optarg;
7630994Sbostic 			break;
7730994Sbostic 		case '?':
7830994Sbostic 		default:
7930994Sbostic 			usage();
8030994Sbostic 		}
8112108Sralph 
82*69010Stef 	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
8312108Sralph 		printer = DEFLP;
8412108Sralph 
8530994Sbostic 	for (argc -= optind, argv += optind; argc; --argc, ++argv)
8630994Sbostic 		if (isdigit(argv[0][0])) {
8730994Sbostic 			if (requests >= MAXREQUESTS)
8830994Sbostic 				fatal("too many requests");
8930994Sbostic 			requ[requests++] = atoi(*argv);
9012108Sralph 		}
9130994Sbostic 		else {
9230994Sbostic 			if (users >= MAXUSERS)
9330994Sbostic 				fatal("too many users");
9430994Sbostic 			user[users++] = *argv;
9530994Sbostic 		}
9630994Sbostic 
97*69010Stef 	if (aflag) {
98*69010Stef 		while (cgetnext(&buf, printcapdb) > 0) {
99*69010Stef 			if (ckqueue() <= 0) {
100*69010Stef 				free(buf);
101*69010Stef 				continue;	/* no jobs */
102*69010Stef 			}
103*69010Stef 			for (cp = buf; *cp; cp++)
104*69010Stef 				if (*cp == '|' || *cp == ':') {
105*69010Stef 					*cp = '\0';
106*69010Stef 					break;
107*69010Stef 				}
108*69010Stef 			printer = buf;
109*69010Stef 			printf("%s:\n", printer);
110*69010Stef 			displayq(lflag);
111*69010Stef 			free(buf);
112*69010Stef 			printf("\n");
113*69010Stef 		}
114*69010Stef 	} else
115*69010Stef 		displayq(lflag);
11616103Sralph 	exit(0);
11712108Sralph }
11812108Sralph 
119*69010Stef ckqueue()
120*69010Stef {
121*69010Stef 	register struct dirent *d;
122*69010Stef 	DIR *dirp;
123*69010Stef 	char *spooldir;
124*69010Stef 
125*69010Stef 	if (cgetstr(bp, "sd", &spooldir) == -1)
126*69010Stef 		spooldir = _PATH_DEFSPOOL;
127*69010Stef 	if ((dirp = opendir(spooldir)) == NULL)
128*69010Stef 		return (-1);
129*69010Stef 	while ((d = readdir(dirp)) != NULL) {
130*69010Stef 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
131*69010Stef 			continue;	/* daemon control files only */
132*69010Stef 		closedir(dirp);
133*69010Stef 		return (1);		/* found something */
134*69010Stef 	}
135*69010Stef 	closedir(dirp);
136*69010Stef 	return (0);
137*69010Stef }
138*69010Stef 
13955476Sbostic void
14012108Sralph usage()
14112108Sralph {
142*69010Stef 	puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]");
14312108Sralph 	exit(1);
14412108Sralph }
145