1 #ifndef lint 2 static char sccsid[] = "@(#)displayq.c 4.10 (Berkeley) 01/30/84"; 3 #endif 4 5 /* 6 * Routines to display the state of the queue. 7 */ 8 9 #include "lp.h" 10 11 #define JOBCOL 40 /* column for job # in -l format */ 12 #define OWNCOL 7 /* start of Owner column in normal */ 13 #define SIZCOL 62 /* start of Size column in normal */ 14 15 /* 16 * Stuff for handling job specifications 17 */ 18 extern char *user[]; /* users to process */ 19 extern int users; /* # of users in user array */ 20 extern int requ[]; /* job number of spool entries */ 21 extern int requests; /* # of spool requests */ 22 23 static int lflag; /* long output option */ 24 static char current[40]; /* current file being printed */ 25 static int garbage; /* # of garbage cf files */ 26 static int rank; /* order to be printed (-1=none, 0=active) */ 27 static long totsize; /* total print job size in bytes */ 28 static int first; /* first file in ``files'' column? */ 29 static int col; /* column on screen */ 30 static int sendtorem; /* are we sending to a remote? */ 31 static char file[132]; /* print file name */ 32 33 static char *head0 = "Rank Owner Job Files"; 34 static char *head1 = "Total Size\n"; 35 36 /* 37 * Display the current state of the queue. Format = 1 if long format. 38 */ 39 displayq(format) 40 int format; 41 { 42 register struct queue *q; 43 register int i, nitems, fd; 44 struct queue **queue; 45 struct stat statb; 46 FILE *fp; 47 48 lflag = format; 49 totsize = 0; 50 rank = -1; 51 52 if ((i = pgetent(line, printer)) < 0) 53 fatal("cannot open printer description file"); 54 else if (i == 0) 55 fatal("unknown printer"); 56 if ((LP = pgetstr("lp", &bp)) == NULL) 57 LP = DEFDEVLP; 58 if ((RP = pgetstr("rp", &bp)) == NULL) 59 RP = DEFLP; 60 if ((SD = pgetstr("sd", &bp)) == NULL) 61 SD = DEFSPOOL; 62 if ((LO = pgetstr("lo", &bp)) == NULL) 63 LO = DEFLOCK; 64 if ((ST = pgetstr("st", &bp)) == NULL) 65 ST = DEFSTAT; 66 RM = pgetstr("rm", &bp); 67 68 /* 69 * If there is no local printer, then print the queue on 70 * the remote machine and then what's in the queue here. 71 * Note that a file in transit may not show up in either queue. 72 */ 73 if (*LP == '\0') { 74 register char *cp; 75 char c; 76 77 sendtorem++; 78 (void) sprintf(line, "%c%s", format + '\3', RP); 79 cp = line; 80 for (i = 0; i < requests; i++) { 81 cp += strlen(cp); 82 (void) sprintf(cp, " %d", requ[i]); 83 } 84 for (i = 0; i < users; i++) { 85 cp += strlen(cp); 86 *cp++ = ' '; 87 strcpy(cp, user[i]); 88 } 89 strcat(line, "\n"); 90 fd = getport(RM); 91 if (fd < 0) { 92 if (from != host) 93 printf("%s: ", host); 94 printf("connection to %s is down\n", RM); 95 } else { 96 i = strlen(line); 97 if (write(fd, line, i) != i) 98 fatal("Lost connection"); 99 while ((i = read(fd, line, sizeof(line))) > 0) 100 (void) fwrite(line, 1, i, stdout); 101 (void) close(fd); 102 } 103 } 104 /* 105 * Find all the control files in the spooling directory 106 */ 107 if (chdir(SD) < 0) 108 fatal("cannot chdir to spooling directory"); 109 if ((nitems = getq(&queue)) < 0) 110 fatal("cannot examine spooling area\n"); 111 if (stat(LO, &statb) >= 0 && (statb.st_mode & 010)) { 112 if (sendtorem) 113 printf("\n%s: ", host); 114 printf("Warning: %s queue is turned off\n", printer); 115 } 116 if (nitems == 0) { 117 if (!sendtorem) 118 printf("no entries\n"); 119 return(0); 120 } 121 fp = fopen(LO, "r"); 122 if (fp == NULL) 123 warn(); 124 else { 125 register char *cp; 126 127 /* get daemon pid */ 128 cp = current; 129 while ((*cp = getc(fp)) != EOF && *cp != '\n') 130 cp++; 131 *cp = '\0'; 132 i = atoi(current); 133 if (i <= 0 || kill(i, 0) < 0) 134 warn(); 135 else { 136 /* read current file name */ 137 cp = current; 138 while ((*cp = getc(fp)) != EOF && *cp != '\n') 139 cp++; 140 *cp = '\0'; 141 /* 142 * Print the status file. 143 */ 144 if (sendtorem) 145 printf("\n%s: ", host); 146 fd = open(ST, O_RDONLY); 147 if (fd >= 0) { 148 (void) flock(fd, LOCK_SH); 149 while ((i = read(fd, line, sizeof(line))) > 0) 150 (void) fwrite(line, 1, i, stdout); 151 (void) close(fd); /* unlocks as well */ 152 } else 153 putchar('\n'); 154 } 155 (void) fclose(fp); 156 } 157 /* 158 * Now, examine the control files and print out the jobs to 159 * be done for each user. 160 */ 161 if (!lflag) 162 header(); 163 for (i = 0; i < nitems; i++) { 164 q = queue[i]; 165 inform(q->q_name); 166 free(q); 167 } 168 free(queue); 169 return(nitems-garbage); 170 } 171 172 /* 173 * Print a warning message if there is no daemon present. 174 */ 175 warn() 176 { 177 struct stat statb; 178 int i, fd; 179 180 if (sendtorem) 181 printf("\n%s: ", host); 182 if (stat(LO, &statb) >= 0 && (statb.st_mode & 0100)) { 183 printf("Warning: %s is down: ", printer); 184 fd = open(ST, O_RDONLY); 185 if (fd >= 0) { 186 (void) flock(fd, LOCK_SH); 187 while ((i = read(fd, line, sizeof(line))) > 0) 188 (void) fwrite(line, 1, i, stdout); 189 (void) close(fd); /* unlocks as well */ 190 } else 191 putchar('\n'); 192 } else 193 printf("Warning: no daemon present\n"); 194 current[0] = '\0'; 195 } 196 197 /* 198 * Print the header for the short listing format 199 */ 200 static 201 header() 202 { 203 printf(head0); 204 col = strlen(head0)+1; 205 blankfill(SIZCOL); 206 printf(head1); 207 } 208 209 static 210 inform(cf) 211 char *cf; 212 { 213 register int j, k; 214 register char *cp; 215 FILE *cfp; 216 217 /* 218 * There's a chance the control file has gone away 219 * in the meantime; if this is the case just keep going 220 */ 221 if ((cfp = fopen(cf, "r")) == NULL) 222 return; 223 224 if (rank < 0) 225 rank = 0; 226 if (sendtorem || garbage || strcmp(cf, current)) 227 rank++; 228 j = 0; 229 while (getline(cfp)) { 230 switch (line[0]) { 231 case 'P': /* Was this file specified in the user's list? */ 232 if (!inlist(line+1, cf)) { 233 fclose(cfp); 234 return; 235 } 236 if (lflag) { 237 printf("\n%s: ", line+1); 238 col = strlen(line+1) + 2; 239 prank(rank); 240 blankfill(JOBCOL); 241 printf(" [job %s]\n", cf+3); 242 } else { 243 col = 0; 244 prank(rank); 245 blankfill(OWNCOL); 246 printf("%-10s %-3d ", line+1, atoi(cf+3)); 247 col += 16; 248 first = 1; 249 } 250 continue; 251 default: /* some format specifer and file name? */ 252 if (line[0] < 'a' || line[0] > 'z') 253 continue; 254 if (j == 0 || strcmp(file, line+1) != 0) 255 strcpy(file, line+1); 256 j++; 257 continue; 258 case 'N': 259 show(line+1, file, j); 260 file[0] = '\0'; 261 j = 0; 262 } 263 } 264 fclose(cfp); 265 if (!lflag) { 266 blankfill(SIZCOL); 267 printf("%D bytes\n", totsize); 268 totsize = 0; 269 } 270 } 271 272 static 273 inlist(name, file) 274 char *name, *file; 275 { 276 register int *r, n; 277 register char **u, *cp; 278 279 if (users == 0 && requests == 0) 280 return(1); 281 /* 282 * Check to see if it's in the user list 283 */ 284 for (u = user; u < &user[users]; u++) 285 if (!strcmp(*u, name)) 286 return(1); 287 /* 288 * Check the request list 289 */ 290 for (n = 0, cp = file+3; isdigit(*cp); ) 291 n = n * 10 + (*cp++ - '0'); 292 for (r = requ; r < &requ[requests]; r++) 293 if (*r == n && !strcmp(cp, from)) 294 return(1); 295 return(0); 296 } 297 298 static 299 show(nfile, file, copies) 300 register char *nfile, *file; 301 { 302 if (strcmp(nfile, " ") == 0) 303 nfile = "(standard input)"; 304 if (lflag) 305 ldump(nfile, file, copies); 306 else 307 dump(nfile, file, copies); 308 } 309 310 /* 311 * Fill the line with blanks to the specified column 312 */ 313 static 314 blankfill(n) 315 register int n; 316 { 317 while (col++ < n) 318 putchar(' '); 319 } 320 321 /* 322 * Give the abbreviated dump of the file names 323 */ 324 static 325 dump(nfile, file, copies) 326 char *nfile, *file; 327 { 328 register short n, fill; 329 struct stat lbuf; 330 331 /* 332 * Print as many files as will fit 333 * (leaving room for the total size) 334 */ 335 fill = first ? 0 : 2; /* fill space for ``, '' */ 336 if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) { 337 if (col < SIZCOL) { 338 printf(" ..."), col += 4; 339 blankfill(SIZCOL); 340 } 341 } else { 342 if (first) 343 first = 0; 344 else 345 printf(", "); 346 printf("%s", nfile); 347 col += n+fill; 348 } 349 if (*file && !stat(file, &lbuf)) 350 totsize += copies * lbuf.st_size; 351 } 352 353 /* 354 * Print the long info about the file 355 */ 356 static 357 ldump(nfile, file, copies) 358 char *nfile, *file; 359 { 360 struct stat lbuf; 361 362 putchar('\t'); 363 if (copies > 1) 364 printf("%-2d copies of %-19s", copies, nfile); 365 else 366 printf("%-32s", nfile); 367 if (*file && !stat(file, &lbuf)) 368 printf(" %D bytes", lbuf.st_size); 369 else 370 printf(" ??? bytes"); 371 putchar('\n'); 372 } 373 374 /* 375 * Print the job's rank in the queue, 376 * update col for screen management 377 */ 378 static 379 prank(n) 380 { 381 char line[100]; 382 static char *r[] = { 383 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" 384 }; 385 386 if (n == 0) { 387 printf("active"); 388 col += 6; 389 return; 390 } 391 if ((n/10) == 1) 392 (void) sprintf(line, "%dth", n); 393 else 394 (void) sprintf(line, "%d%s", n, r[n%10]); 395 col += strlen(line); 396 printf("%s", line); 397 } 398