1 /* $NetBSD: displayq.c,v 1.13 1997/10/05 16:45:43 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)displayq.c 8.4 (Berkeley) 4/28/95"; 40 #else 41 __RCSID("$NetBSD: displayq.c,v 1.13 1997/10/05 16:45:43 mrg Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/stat.h> 47 #include <sys/file.h> 48 49 #include <signal.h> 50 #include <fcntl.h> 51 #include <dirent.h> 52 #include <unistd.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <ctype.h> 57 #include "lp.h" 58 #include "lp.local.h" 59 #include "pathnames.h" 60 61 /* 62 * Routines to display the state of the queue. 63 */ 64 #define JOBCOL 40 /* column for job # in -l format */ 65 #define OWNCOL 7 /* start of Owner column in normal */ 66 #define SIZCOL 62 /* start of Size column in normal */ 67 68 /* 69 * Stuff for handling job specifications 70 */ 71 extern int requ[]; /* job number of spool entries */ 72 extern int requests; /* # of spool requests */ 73 extern char *user[]; /* users to process */ 74 extern int users; /* # of users in user array */ 75 76 extern uid_t uid, euid; 77 78 static int col; /* column on screen */ 79 static char current[40]; /* current file being printed */ 80 static char file[132]; /* print file name */ 81 static int first; /* first file in ``files'' column? */ 82 static int garbage; /* # of garbage cf files */ 83 static int lflag; /* long output option */ 84 static int rank; /* order to be printed (-1=none, 0=active) */ 85 static long totsize; /* total print job size in bytes */ 86 87 static char *head0 = "Rank Owner Job Files"; 88 static char *head1 = "Total Size\n"; 89 90 /* 91 * Display the current state of the queue. Format = 1 if long format. 92 */ 93 void 94 displayq(format) 95 int format; 96 { 97 struct queue *q; 98 int i, nitems, fd, ret; 99 char *cp; 100 struct queue **queue; 101 struct stat statb; 102 FILE *fp; 103 104 lflag = format; 105 totsize = 0; 106 rank = -1; 107 if ((i = cgetent(&bp, printcapdb, printer)) == -2) 108 fatal("can't open printer description file"); 109 else if (i == -1) 110 fatal("unknown printer"); 111 else if (i == -3) 112 fatal("potential reference loop detected in printcap file"); 113 if (cgetstr(bp, "lp", &LP) < 0) 114 LP = _PATH_DEFDEVLP; 115 if (cgetstr(bp, "rp", &RP) < 0) 116 RP = DEFLP; 117 if (cgetstr(bp, "sd", &SD) < 0) 118 SD = _PATH_DEFSPOOL; 119 if (cgetstr(bp,"lo", &LO) < 0) 120 LO = DEFLOCK; 121 if (cgetstr(bp, "st", &ST) < 0) 122 ST = DEFSTAT; 123 cgetstr(bp, "rm", &RM); 124 if ((cp = checkremote()) != NULL) 125 printf("Warning: %s\n", cp); 126 127 /* 128 * Print out local queue 129 * Find all the control files in the spooling directory 130 */ 131 seteuid(euid); 132 if (chdir(SD) < 0) 133 fatal("cannot chdir to spooling directory"); 134 seteuid(uid); 135 if ((nitems = getq(&queue)) < 0) 136 fatal("cannot examine spooling area\n"); 137 seteuid(euid); 138 ret = stat(LO, &statb); 139 seteuid(uid); 140 if (ret >= 0) { 141 if (statb.st_mode & 0100) { 142 if (remote) 143 printf("%s: ", host); 144 printf("Warning: %s is down: ", printer); 145 seteuid(euid); 146 fd = open(ST, O_RDONLY); 147 seteuid(uid); 148 if (fd >= 0) { 149 (void)flock(fd, LOCK_SH); 150 while ((i = read(fd, line, sizeof(line))) > 0) 151 (void)fwrite(line, 1, i, stdout); 152 (void)close(fd); /* unlocks as well */ 153 } else 154 putchar('\n'); 155 } 156 if (statb.st_mode & 010) { 157 if (remote) 158 printf("%s: ", host); 159 printf("Warning: %s queue is turned off\n", printer); 160 } 161 } 162 163 if (nitems) { 164 seteuid(euid); 165 fp = fopen(LO, "r"); 166 seteuid(uid); 167 if (fp == NULL) 168 nodaemon(); 169 else { 170 /* get daemon pid */ 171 cp = current; 172 while ((i = getc(fp)) != EOF && i != '\n') 173 *cp++ = i; 174 *cp = '\0'; 175 i = atoi(current); 176 if (i <= 0) { 177 ret = -1; 178 } else { 179 seteuid(euid); 180 ret = kill(i, 0); 181 seteuid(uid); 182 } 183 if (ret < 0) { 184 nodaemon(); 185 } else { 186 /* read current file name */ 187 cp = current; 188 while ((i = getc(fp)) != EOF && i != '\n') 189 *cp++ = i; 190 *cp = '\0'; 191 /* 192 * Print the status file. 193 */ 194 if (remote) 195 printf("%s: ", host); 196 seteuid(euid); 197 fd = open(ST, O_RDONLY); 198 seteuid(uid); 199 if (fd >= 0) { 200 (void)flock(fd, LOCK_SH); 201 while ((i = read(fd, line, sizeof(line))) > 0) 202 (void)fwrite(line, 1, i, stdout); 203 (void)close(fd); /* unlocks as well */ 204 } else 205 putchar('\n'); 206 } 207 (void)fclose(fp); 208 } 209 /* 210 * Now, examine the control files and print out the jobs to 211 * be done for each user. 212 */ 213 if (!lflag) 214 header(); 215 for (i = 0; i < nitems; i++) { 216 q = queue[i]; 217 inform(q->q_name); 218 free(q); 219 } 220 free(queue); 221 } 222 if (!remote) { 223 if (nitems == 0) 224 puts("no entries"); 225 return; 226 } 227 228 /* 229 * Print foreign queue 230 * Note that a file in transit may show up in either queue. 231 */ 232 if (nitems) 233 putchar('\n'); 234 (void)snprintf(line, sizeof(line), "%c%s", format + '\3', RP); 235 cp = line; 236 for (i = 0; i < requests && cp-line+10 < sizeof(line) - 1; i++) { 237 cp += strlen(cp); 238 (void)snprintf(cp, line - cp, " %d", requ[i]); 239 } 240 for (i = 0; i < users && cp - line + 1 + strlen(user[i]) < 241 sizeof(line) - 1; i++) { 242 cp += strlen(cp); 243 if (cp - line > sizeof(line) - 1) 244 break; 245 *cp++ = ' '; 246 (void)strncpy(cp, user[i], line - cp - 1); 247 } 248 (void)strncat(line, "\n", sizeof(line) - strlen(line) - 1); 249 fd = getport(RM, 0); 250 if (fd < 0) { 251 if (from != host) 252 printf("%s: ", host); 253 (void)printf("connection to %s is down\n", RM); 254 } 255 else { 256 i = strlen(line); 257 if (write(fd, line, i) != i) 258 fatal("Lost connection"); 259 while ((i = read(fd, line, sizeof(line))) > 0) 260 (void)fwrite(line, 1, i, stdout); 261 (void)close(fd); 262 } 263 } 264 265 /* 266 * Print a warning message if there is no daemon present. 267 */ 268 void 269 nodaemon() 270 { 271 if (remote) 272 printf("\n%s: ", host); 273 puts("Warning: no daemon present"); 274 current[0] = '\0'; 275 } 276 277 /* 278 * Print the header for the short listing format 279 */ 280 void 281 header() 282 { 283 printf(head0); 284 col = strlen(head0)+1; 285 blankfill(SIZCOL); 286 printf(head1); 287 } 288 289 void 290 inform(cf) 291 char *cf; 292 { 293 int j; 294 FILE *cfp; 295 296 /* 297 * There's a chance the control file has gone away 298 * in the meantime; if this is the case just keep going 299 */ 300 seteuid(euid); 301 if ((cfp = fopen(cf, "r")) == NULL) 302 return; 303 seteuid(uid); 304 305 if (rank < 0) 306 rank = 0; 307 if (remote || garbage || strcmp(cf, current)) 308 rank++; 309 j = 0; 310 while (getline(cfp)) { 311 switch (line[0]) { 312 case 'P': /* Was this file specified in the user's list? */ 313 if (!inlist(line+1, cf)) { 314 fclose(cfp); 315 return; 316 } 317 if (lflag) { 318 printf("\n%s: ", line+1); 319 col = strlen(line+1) + 2; 320 prank(rank); 321 blankfill(JOBCOL); 322 printf(" [job %s]\n", cf+3); 323 } else { 324 col = 0; 325 prank(rank); 326 blankfill(OWNCOL); 327 printf("%-10s %-3d ", line+1, atoi(cf+3)); 328 col += 16; 329 first = 1; 330 } 331 continue; 332 default: /* some format specifer and file name? */ 333 if (line[0] < 'a' || line[0] > 'z') 334 continue; 335 if (j == 0 || strcmp(file, line+1) != 0) { 336 (void)strncpy(file, line+1, sizeof(file) - 1); 337 file[sizeof(file) - 1] = '\0'; 338 } 339 j++; 340 continue; 341 case 'N': 342 show(line+1, file, j); 343 file[0] = '\0'; 344 j = 0; 345 } 346 } 347 fclose(cfp); 348 if (!lflag) { 349 blankfill(SIZCOL); 350 printf("%ld bytes\n", totsize); 351 totsize = 0; 352 } 353 } 354 355 int 356 inlist(name, file) 357 char *name, *file; 358 { 359 int *r, n; 360 char **u, *cp; 361 362 if (users == 0 && requests == 0) 363 return(1); 364 /* 365 * Check to see if it's in the user list 366 */ 367 for (u = user; u < &user[users]; u++) 368 if (!strcmp(*u, name)) 369 return(1); 370 /* 371 * Check the request list 372 */ 373 for (n = 0, cp = file+3; isdigit(*cp); ) 374 n = n * 10 + (*cp++ - '0'); 375 for (r = requ; r < &requ[requests]; r++) 376 if (*r == n && !strcmp(cp, from)) 377 return(1); 378 return(0); 379 } 380 381 void 382 show(nfile, file, copies) 383 char *nfile, *file; 384 int copies; 385 { 386 if (strcmp(nfile, " ") == 0) 387 nfile = "(standard input)"; 388 if (lflag) 389 ldump(nfile, file, copies); 390 else 391 dump(nfile, file, copies); 392 } 393 394 /* 395 * Fill the line with blanks to the specified column 396 */ 397 void 398 blankfill(n) 399 int n; 400 { 401 while (col++ < n) 402 putchar(' '); 403 } 404 405 /* 406 * Give the abbreviated dump of the file names 407 */ 408 void 409 dump(nfile, file, copies) 410 char *nfile, *file; 411 int copies; 412 { 413 short n, fill; 414 struct stat lbuf; 415 416 /* 417 * Print as many files as will fit 418 * (leaving room for the total size) 419 */ 420 fill = first ? 0 : 2; /* fill space for ``, '' */ 421 if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) { 422 if (col < SIZCOL) { 423 printf(" ..."), col += 4; 424 blankfill(SIZCOL); 425 } 426 } else { 427 if (first) 428 first = 0; 429 else 430 printf(", "); 431 printf("%s", nfile); 432 col += n+fill; 433 } 434 seteuid(euid); 435 if (*file && !stat(file, &lbuf)) 436 totsize += copies * lbuf.st_size; 437 seteuid(uid); 438 } 439 440 /* 441 * Print the long info about the file 442 */ 443 void 444 ldump(nfile, file, copies) 445 char *nfile, *file; 446 int copies; 447 { 448 struct stat lbuf; 449 450 putchar('\t'); 451 if (copies > 1) 452 printf("%-2d copies of %-19s", copies, nfile); 453 else 454 printf("%-32s", nfile); 455 if (*file && !stat(file, &lbuf)) 456 printf(" %qd bytes", (long long)lbuf.st_size); 457 else 458 printf(" ??? bytes"); 459 putchar('\n'); 460 } 461 462 /* 463 * Print the job's rank in the queue, 464 * update col for screen management 465 */ 466 void 467 prank(n) 468 int n; 469 { 470 char rline[100]; 471 static char *r[] = { 472 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" 473 }; 474 475 if (n == 0) { 476 printf("active"); 477 col += 6; 478 return; 479 } 480 if ((n/10)%10 == 1) 481 (void)snprintf(rline, sizeof(rline), "%dth", n); 482 else 483 (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]); 484 col += strlen(rline); 485 printf("%s", rline); 486 } 487