Better portability to other systems.
[unix-history] / usr / src / usr.sbin / lpr / lpq / lpq.c
CommitLineData
d0aeaf5a 1/*
f7840ad0
KB
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
9d85c861 4 *
853a196e 5 *
c13032d6 6 * %sccs.include.redist.c%
d0aeaf5a
DF
7 */
8
9#ifndef lint
63fd8427 10static char copyright[] =
f7840ad0
KB
11"@(#) Copyright (c) 1983, 1993\n\
12 The Regents of the University of California. All rights reserved.\n";
9d85c861 13#endif /* not lint */
d0aeaf5a 14
5f84f8f0 15#ifndef lint
3333c35e 16static char sccsid[] = "@(#)lpq.c 8.2 (Berkeley) %G%";
9d85c861 17#endif /* not lint */
5f84f8f0 18
dd8e3649
RC
19/*
20 * Spool Queue examination program
21 *
3333c35e 22 * lpq [-a] [-l] [-Pprinter] [user...] [job...]
dd8e3649 23 *
3333c35e 24 * -a show all non-null queues on the local machine
1098f423 25 * -l long output
dd8e3649
RC
26 * -P used to identify printer as per lpr/lprm
27 */
28
6ce2d98b
KB
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>
dd8e3649 37#include "lp.h"
6ce2d98b 38#include "lp.local.h"
3333c35e 39#include "pathnames.h"
dd8e3649 40
853a196e
EA
41int requ[MAXREQUESTS]; /* job number of spool entries */
42int requests; /* # of spool requests */
dd8e3649 43char *user[MAXUSERS]; /* users to process */
853a196e 44int users; /* # of users in user array */
dd8e3649 45
6ce2d98b
KB
46void usage __P((void));
47
48int
dd8e3649 49main(argc, argv)
1098f423
KB
50 register int argc;
51 register char **argv;
dd8e3649 52{
1098f423
KB
53 extern char *optarg;
54 extern int optind;
3333c35e
TF
55 int ch, aflag, lflag;
56 char *buf, *cp;
1098f423
KB
57
58 name = *argv;
59 if (gethostname(host, sizeof(host))) {
60 perror("lpq: gethostname");
61 exit(1);
62 }
126fc76f 63 openlog("lpd", 0, LOG_LPR);
4d4caa50 64
3333c35e
TF
65 aflag = lflag = 0;
66 while ((ch = getopt(argc, argv, "alP:")) != EOF)
1098f423 67 switch((char)ch) {
3333c35e
TF
68 case 'a':
69 ++aflag;
70 break;
1098f423
KB
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();
dd8e3649 80 }
1098f423 81
3333c35e 82 if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
dd8e3649 83 printer = DEFLP;
dd8e3649 84
1098f423
KB
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;
dd8e3649 95 }
1098f423 96
3333c35e
TF
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);
1e7e612e 116 exit(0);
dd8e3649
RC
117}
118
3333c35e
TF
119ckqueue()
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
6ce2d98b 139void
dd8e3649
RC
140usage()
141{
3333c35e 142 puts("usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]");
dd8e3649
RC
143 exit(1);
144}