1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/* $OpenBSD: lp_displayq.c,v 1.1.1.1 2018/04/27 16:14:36 eric Exp $ */
/*
* Copyright (c) 2017 Eric Faurot <eric@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lp.h"
#include "log.h"
static void dolong(int, struct lp_printer *, struct lp_jobfilter *, int,
const char *, int);
static void doshort(int, struct lp_printer *, struct lp_jobfilter *, int,
const char *, int);
void
lp_displayq(int ofd, struct lp_printer *lp, int lng, struct lp_jobfilter *jf)
{
struct lp_queue q;
pid_t currpid;
char status[256], currjob[PATH_MAX];
int i, active, qstate;
/* Warn about the queue state if needed. */
if (lp_getqueuestate(lp, 0, &qstate) == -1)
log_warnx("cannot get queue state");
else {
if (qstate & LPQ_PRINTER_DOWN) {
if (lp->lp_type == PRN_LPR)
dprintf(ofd, "%s: ", lpd_hostname);
dprintf(ofd, "Warning: %s is down\n", lp->lp_name);
}
if (qstate & LPQ_QUEUE_OFF) {
if (lp->lp_type == PRN_LPR)
dprintf(ofd, "%s: ", lpd_hostname);
dprintf(ofd, "Warning: %s queue is turned off\n",
lp->lp_name);
}
}
/* Read queue content. */
if ((lp_readqueue(lp, &q)) == -1) {
log_warnx("cannot read queue");
if (lp->lp_type == PRN_LPR)
dprintf(ofd, "%s: ", lpd_hostname);
dprintf(ofd, "Warning: cannot read queue\n");
}
/* Display current printer status. */
if (lp_getcurrtask(lp, &currpid, currjob, sizeof(currjob)) == -1)
log_warnx("cannot get current task");
if (currpid) {
if (lp->lp_type == PRN_LPR)
dprintf(ofd, "%s: ", lpd_hostname);
if (lp_getstatus(lp, status, sizeof(status)) == -1) {
log_warnx("cannot read printer status");
dprintf(ofd, "Warning: cannot read status file\n");
}
else
dprintf(ofd, "%s\n", status);
}
/* Display queue content. */
if (q.count == 0) {
if (lp->lp_type != PRN_LPR)
dprintf(ofd, "no entries\n");
}
else {
if (currpid == 0) {
if (lp->lp_type == PRN_LPR)
dprintf(ofd, "%s: ", lpd_hostname);
dprintf(ofd, "Warning: no daemon present\n");
}
if (!lng) {
dprintf(ofd, "Rank Owner Job Files");
dprintf(ofd, "%43s\n", "Total Size");
}
for (i = 0; i < q.count; i++) {
active = !strcmp(q.cfname[i], currjob);
if (lng)
dolong(ofd, lp, jf, i+1, q.cfname[i], active);
else
doshort(ofd, lp, jf, i+1, q.cfname[i], active);
}
}
lp_clearqueue(&q);
}
static int
checklists(const char *cfname, struct lp_jobfilter *jf, const char *person)
{
int i;
if (jf->nuser == 0 && jf->njob == 0)
return 1;
/* Check if user is in user list. */
for (i = 0; i < jf->nuser; i++)
if (!strcmp(jf->users[i], person))
return 1;
/* Skip if hostnames don't match. */
if (strcmp(LP_JOBHOST(cfname), jf->hostfrom))
return 0;
/* Check for matching jobnum. */
for (i = 0; i < jf->njob; i++)
if (jf->jobs[i] == LP_JOBNUM(cfname))
return 1;
return 0;
}
static const char *
rankstr(int rank, int active)
{
static char buf[16];
const char *sfx;
if (active)
return "active";
sfx = "th";
switch (rank % 10) {
case 1:
if ((rank / 10) % 10 != 1)
sfx = "st";
break;
case 2:
sfx = "nd";
break;
case 3:
sfx = "rd";
}
snprintf(buf, sizeof(buf), "%d%s", rank, sfx);
return buf;
}
static void
doshort(int ofd, struct lp_printer *lp, struct lp_jobfilter *jf, int rank,
const char *cfname, int active)
{
struct stat st;
FILE *fp;
const char *fname;
char dfname[PATH_MAX], names[80], *line = NULL;
ssize_t len;
size_t linesz = 0, totalsz = 0;
int copies = 0;
fp = lp_fopen(lp, cfname);
if (fp == NULL) {
log_warn("cannot open %s", cfname);
return;
}
names[0] = '\0';
while ((len = getline(&line, &linesz, fp)) != -1) {
if (line[len-1] == '\n')
line[len - 1] = '\0';
switch (line[0]) {
case 'P':
if (!checklists(cfname, jf, line + 1))
goto end;
dprintf(ofd, "%-7s%-11s%-4i ", rankstr(rank, active),
line + 1, LP_JOBNUM(cfname));
break;
case 'N':
fname = line + 1;
if (!strcmp(fname, " "))
fname = "(standard input)";
if (names[0])
(void)strlcat(names, ", ", sizeof(names));
(void)strlcat(names, fname, sizeof(names));
if (lp_stat(lp, dfname, &st) == -1)
log_warn("cannot stat %s", dfname);
else
totalsz += copies * st.st_size;
copies = 0;
break;
default:
if (line[0] < 'a' || line[0] > 'z')
continue;
if (copies++ == 0)
(void)strlcpy(dfname, line+1, sizeof(dfname));
break;
}
}
dprintf(ofd, "%-37s %lld bytes\n", names, (long long)totalsz);
end:
free(line);
}
static void
dolong(int ofd, struct lp_printer *lp, struct lp_jobfilter *jf, int rank,
const char *cfname, int active)
{
struct stat st;
FILE *fp;
const char *fname;
char dfname[PATH_MAX], names[80], buf[64], *line = NULL;
ssize_t len;
size_t linesz = 0;
int copies = 0;
fp = lp_fopen(lp, cfname);
if (fp == NULL) {
log_warn("cannot open %s", cfname);
return;
}
names[0] = '\0';
while ((len = getline(&line, &linesz, fp)) != -1) {
if (line[len-1] == '\n')
line[len - 1] = '\0';
switch (line[0]) {
case 'P':
if (!checklists(cfname, jf, line + 1))
goto end;
snprintf(buf, sizeof(buf), "%s: %s", line+1,
rankstr(rank, active));
dprintf(ofd, "\n%-41s[job %s]\n", buf, cfname + 3);
break;
case 'N':
fname = line + 1;
if (!strcmp(fname, " "))
fname = "(standard input)";
if (copies > 1)
dprintf(ofd, "\t%-2d copies of %-19s", copies,
fname);
else
dprintf(ofd, "\t%-32s", fname);
if (lp_stat(lp, dfname, &st) == -1) {
log_warn("cannot stat %s", dfname);
dprintf(ofd, " ??? bytes\n");
}
else
dprintf(ofd, " %lld bytes\n",
(long long)st.st_size);
copies = 0;
break;
default:
if (line[0] < 'a' || line[0] > 'z')
continue;
if (copies++ == 0)
(void)strlcpy(dfname, line+1, sizeof(dfname));
break;
}
}
end:
free(line);
}
|