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