xref: /original-bsd/usr.sbin/lpr/lpq/lpq.c (revision 00695d63)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char copyright[] =
11 "@(#) Copyright (c) 1983, 1993\n\
12 	The Regents of the University of California.  All rights reserved.\n";
13 #endif /* not lint */
14 
15 #ifndef lint
16 static char sccsid[] = "@(#)lpq.c	8.2 (Berkeley) 04/28/95";
17 #endif /* not lint */
18 
19 /*
20  * Spool Queue examination program
21  *
22  * lpq [-a] [-l] [-Pprinter] [user...] [job...]
23  *
24  * -a show all non-null queues on the local machine
25  * -l long output
26  * -P used to identify printer as per lpr/lprm
27  */
28 
29 #include <sys/param.h>
30 
31 #include <syslog.h>
32 #include <dirent.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <ctype.h>
37 #include "lp.h"
38 #include "lp.local.h"
39 #include "pathnames.h"
40 
41 int	 requ[MAXREQUESTS];	/* job number of spool entries */
42 int	 requests;		/* # of spool requests */
43 char	*user[MAXUSERS];	/* users to process */
44 int	 users;			/* # of users in user array */
45 
46 void usage __P((void));
47 
48 int
49 main(argc, argv)
50 	register int	argc;
51 	register char	**argv;
52 {
53 	extern char	*optarg;
54 	extern int	optind;
55 	int	ch, aflag, lflag;
56 	char	*buf, *cp;
57 
58 	name = *argv;
59 	if (gethostname(host, sizeof(host))) {
60 		perror("lpq: gethostname");
61 		exit(1);
62 	}
63 	openlog("lpd", 0, LOG_LPR);
64 
65 	aflag = lflag = 0;
66 	while ((ch = getopt(argc, argv, "alP:")) != EOF)
67 		switch((char)ch) {
68 		case 'a':
69 			++aflag;
70 			break;
71 		case 'l':			/* long output */
72 			++lflag;
73 			break;
74 		case 'P':		/* printer name */
75 			printer = optarg;
76 			break;
77 		case '?':
78 		default:
79 			usage();
80 		}
81 
82 	if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
83 		printer = DEFLP;
84 
85 	for (argc -= optind, argv += optind; argc; --argc, ++argv)
86 		if (isdigit(argv[0][0])) {
87 			if (requests >= MAXREQUESTS)
88 				fatal("too many requests");
89 			requ[requests++] = atoi(*argv);
90 		}
91 		else {
92 			if (users >= MAXUSERS)
93 				fatal("too many users");
94 			user[users++] = *argv;
95 		}
96 
97 	if (aflag) {
98 		while (cgetnext(&buf, printcapdb) > 0) {
99 			if (ckqueue() <= 0) {
100 				free(buf);
101 				continue;	/* no jobs */
102 			}
103 			for (cp = buf; *cp; cp++)
104 				if (*cp == '|' || *cp == ':') {
105 					*cp = '\0';
106 					break;
107 				}
108 			printer = buf;
109 			printf("%s:\n", printer);
110 			displayq(lflag);
111 			free(buf);
112 			printf("\n");
113 		}
114 	} else
115 		displayq(lflag);
116 	exit(0);
117 }
118 
119 ckqueue()
120 {
121 	register struct dirent *d;
122 	DIR *dirp;
123 	char *spooldir;
124 
125 	if (cgetstr(bp, "sd", &spooldir) == -1)
126 		spooldir = _PATH_DEFSPOOL;
127 	if ((dirp = opendir(spooldir)) == NULL)
128 		return (-1);
129 	while ((d = readdir(dirp)) != NULL) {
130 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
131 			continue;	/* daemon control files only */
132 		closedir(dirp);
133 		return (1);		/* found something */
134 	}
135 	closedir(dirp);
136 	return (0);
137 }
138 
139 void
140 usage()
141 {
142 	puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]");
143 	exit(1);
144 }
145