122427Sdist /* 222427Sdist * Copyright (c) 1983 Regents of the University of California. 334203Sbostic * All rights reserved. 434203Sbostic * 542801Sbostic * %sccs.include.redist.c% 622427Sdist */ 722427Sdist 813952Ssam #ifndef lint 9*55470Sbostic static char sccsid[] = "@(#)displayq.c 5.14 (Berkeley) 07/21/92"; 1034203Sbostic #endif /* not lint */ 1113952Ssam 12*55470Sbostic #include <sys/param.h> 13*55470Sbostic #include <sys/stat.h> 1412113Sralph 15*55470Sbostic #include <signal.h> 16*55470Sbostic #include <fcntl.h> 17*55470Sbostic #include <dirent.h> 18*55470Sbostic #include <unistd.h> 19*55470Sbostic #include <stdio.h> 20*55470Sbostic #include <stdlib.h> 21*55470Sbostic #include <string.h> 22*55470Sbostic #include <ctype.h> 2312113Sralph #include "lp.h" 24*55470Sbostic #include "lp.local.h" 2537968Sbostic #include "pathnames.h" 2612113Sralph 27*55470Sbostic /* 28*55470Sbostic * Routines to display the state of the queue. 29*55470Sbostic */ 3013170Sralph #define JOBCOL 40 /* column for job # in -l format */ 3113170Sralph #define OWNCOL 7 /* start of Owner column in normal */ 3213170Sralph #define SIZCOL 62 /* start of Size column in normal */ 3312113Sralph 3412113Sralph /* 3512113Sralph * Stuff for handling job specifications 3612113Sralph */ 3712113Sralph extern char *user[]; /* users to process */ 3812113Sralph extern int users; /* # of users in user array */ 3912113Sralph extern int requ[]; /* job number of spool entries */ 4012113Sralph extern int requests; /* # of spool requests */ 4112113Sralph 4216758Sralph int lflag; /* long output option */ 4316758Sralph char current[40]; /* current file being printed */ 4416758Sralph int garbage; /* # of garbage cf files */ 4516758Sralph int rank; /* order to be printed (-1=none, 0=active) */ 4616758Sralph long totsize; /* total print job size in bytes */ 4716758Sralph int first; /* first file in ``files'' column? */ 4816758Sralph int col; /* column on screen */ 4916758Sralph char file[132]; /* print file name */ 5012113Sralph 5116758Sralph char *head0 = "Rank Owner Job Files"; 5216758Sralph char *head1 = "Total Size\n"; 5312113Sralph 5412113Sralph /* 5512113Sralph * Display the current state of the queue. Format = 1 if long format. 5612113Sralph */ 57*55470Sbostic void 5812113Sralph displayq(format) 5912113Sralph int format; 6012113Sralph { 6112113Sralph register struct queue *q; 6212113Sralph register int i, nitems, fd; 6330995Sbostic register char *cp; 6412113Sralph struct queue **queue; 6512113Sralph struct stat statb; 6612113Sralph FILE *fp; 6712113Sralph 6812113Sralph lflag = format; 6912113Sralph totsize = 0; 7012113Sralph rank = -1; 7112113Sralph 7212113Sralph if ((i = pgetent(line, printer)) < 0) 7312113Sralph fatal("cannot open printer description file"); 7412113Sralph else if (i == 0) 7512113Sralph fatal("unknown printer"); 7612113Sralph if ((LP = pgetstr("lp", &bp)) == NULL) 7737968Sbostic LP = _PATH_DEFDEVLP; 7812113Sralph if ((RP = pgetstr("rp", &bp)) == NULL) 7912434Sralph RP = DEFLP; 8012113Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 8137968Sbostic SD = _PATH_DEFSPOOL; 8212113Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 8312113Sralph LO = DEFLOCK; 8412113Sralph if ((ST = pgetstr("st", &bp)) == NULL) 8512113Sralph ST = DEFSTAT; 8612113Sralph RM = pgetstr("rm", &bp); 8738736Stef if (cp = checkremote()) 8838736Stef printf("Warning: %s\n", cp); 8912113Sralph 9012113Sralph /* 9130995Sbostic * Print out local queue 9212113Sralph * Find all the control files in the spooling directory 9312113Sralph */ 9412113Sralph if (chdir(SD) < 0) 9512113Sralph fatal("cannot chdir to spooling directory"); 9612113Sralph if ((nitems = getq(&queue)) < 0) 9712113Sralph fatal("cannot examine spooling area\n"); 9816205Sralph if (stat(LO, &statb) >= 0) { 9916205Sralph if (statb.st_mode & 0100) { 10016205Sralph if (sendtorem) 10116205Sralph printf("%s: ", host); 10216205Sralph printf("Warning: %s is down: ", printer); 10316205Sralph fd = open(ST, O_RDONLY); 10416205Sralph if (fd >= 0) { 10516205Sralph (void) flock(fd, LOCK_SH); 10616205Sralph while ((i = read(fd, line, sizeof(line))) > 0) 10716205Sralph (void) fwrite(line, 1, i, stdout); 10816205Sralph (void) close(fd); /* unlocks as well */ 10916205Sralph } else 11016205Sralph putchar('\n'); 11116205Sralph } 11216205Sralph if (statb.st_mode & 010) { 11316205Sralph if (sendtorem) 11416205Sralph printf("%s: ", host); 11516205Sralph printf("Warning: %s queue is turned off\n", printer); 11616205Sralph } 11712740Sralph } 11812113Sralph 11930995Sbostic if (nitems) { 12030995Sbostic fp = fopen(LO, "r"); 12130995Sbostic if (fp == NULL) 12213441Sralph warn(); 12313441Sralph else { 12430995Sbostic /* get daemon pid */ 12513441Sralph cp = current; 12613441Sralph while ((*cp = getc(fp)) != EOF && *cp != '\n') 12713441Sralph cp++; 12813441Sralph *cp = '\0'; 12930995Sbostic i = atoi(current); 13030995Sbostic if (i <= 0 || kill(i, 0) < 0) 13130995Sbostic warn(); 13230995Sbostic else { 13330995Sbostic /* read current file name */ 13430995Sbostic cp = current; 13530995Sbostic while ((*cp = getc(fp)) != EOF && *cp != '\n') 13630995Sbostic cp++; 13730995Sbostic *cp = '\0'; 13830995Sbostic /* 13930995Sbostic * Print the status file. 14030995Sbostic */ 14130995Sbostic if (sendtorem) 14230995Sbostic printf("%s: ", host); 14330995Sbostic fd = open(ST, O_RDONLY); 14430995Sbostic if (fd >= 0) { 14530995Sbostic (void) flock(fd, LOCK_SH); 14630995Sbostic while ((i = read(fd, line, sizeof(line))) > 0) 14730995Sbostic (void) fwrite(line, 1, i, stdout); 14830995Sbostic (void) close(fd); /* unlocks as well */ 14930995Sbostic } else 15030995Sbostic putchar('\n'); 15130995Sbostic } 15230995Sbostic (void) fclose(fp); 15313441Sralph } 15430995Sbostic /* 15530995Sbostic * Now, examine the control files and print out the jobs to 15630995Sbostic * be done for each user. 15730995Sbostic */ 15830995Sbostic if (!lflag) 15930995Sbostic header(); 16030995Sbostic for (i = 0; i < nitems; i++) { 16130995Sbostic q = queue[i]; 16230995Sbostic inform(q->q_name); 16330995Sbostic free(q); 16430995Sbostic } 16530995Sbostic free(queue); 16612113Sralph } 16731678Skarels if (!sendtorem) { 16831678Skarels if (nitems == 0) 16931678Skarels puts("no entries"); 17030995Sbostic return; 17130995Sbostic } 17230995Sbostic 17312113Sralph /* 17430995Sbostic * Print foreign queue 17530995Sbostic * Note that a file in transit may show up in either queue. 17612113Sralph */ 17730995Sbostic if (nitems) 17830995Sbostic putchar('\n'); 17930995Sbostic (void) sprintf(line, "%c%s", format + '\3', RP); 18030995Sbostic cp = line; 18130995Sbostic for (i = 0; i < requests; i++) { 18230995Sbostic cp += strlen(cp); 18330995Sbostic (void) sprintf(cp, " %d", requ[i]); 18412113Sralph } 18530995Sbostic for (i = 0; i < users; i++) { 18630995Sbostic cp += strlen(cp); 18730995Sbostic *cp++ = ' '; 18830995Sbostic (void) strcpy(cp, user[i]); 18930995Sbostic } 19030995Sbostic strcat(line, "\n"); 19130995Sbostic fd = getport(RM); 19230995Sbostic if (fd < 0) { 19330995Sbostic if (from != host) 19430995Sbostic printf("%s: ", host); 19530995Sbostic printf("connection to %s is down\n", RM); 19630995Sbostic } 19730995Sbostic else { 19830995Sbostic i = strlen(line); 19930995Sbostic if (write(fd, line, i) != i) 20030995Sbostic fatal("Lost connection"); 20130995Sbostic while ((i = read(fd, line, sizeof(line))) > 0) 20230995Sbostic (void) fwrite(line, 1, i, stdout); 20330995Sbostic (void) close(fd); 20430995Sbostic } 20512113Sralph } 20612113Sralph 20712113Sralph /* 20813441Sralph * Print a warning message if there is no daemon present. 20913441Sralph */ 210*55470Sbostic void 21113441Sralph warn() 21213441Sralph { 21313441Sralph if (sendtorem) 21413441Sralph printf("\n%s: ", host); 21530995Sbostic puts("Warning: no daemon present"); 21613441Sralph current[0] = '\0'; 21713441Sralph } 21813441Sralph 21913441Sralph /* 22012113Sralph * Print the header for the short listing format 22112113Sralph */ 222*55470Sbostic void 22312113Sralph header() 22412113Sralph { 22512113Sralph printf(head0); 22612113Sralph col = strlen(head0)+1; 22712113Sralph blankfill(SIZCOL); 22812113Sralph printf(head1); 22912113Sralph } 23012113Sralph 231*55470Sbostic void 23212113Sralph inform(cf) 23312113Sralph char *cf; 23412113Sralph { 235*55470Sbostic register int j; 23612113Sralph FILE *cfp; 23712113Sralph 23812113Sralph /* 23912113Sralph * There's a chance the control file has gone away 24012113Sralph * in the meantime; if this is the case just keep going 24112113Sralph */ 24212113Sralph if ((cfp = fopen(cf, "r")) == NULL) 24312113Sralph return; 24412113Sralph 24512113Sralph if (rank < 0) 24612113Sralph rank = 0; 24712113Sralph if (sendtorem || garbage || strcmp(cf, current)) 24812113Sralph rank++; 24912113Sralph j = 0; 25012113Sralph while (getline(cfp)) { 25112113Sralph switch (line[0]) { 25212113Sralph case 'P': /* Was this file specified in the user's list? */ 25312113Sralph if (!inlist(line+1, cf)) { 25412113Sralph fclose(cfp); 25512113Sralph return; 25612113Sralph } 25712113Sralph if (lflag) { 25812113Sralph printf("\n%s: ", line+1); 25912113Sralph col = strlen(line+1) + 2; 26012113Sralph prank(rank); 26112113Sralph blankfill(JOBCOL); 26212113Sralph printf(" [job %s]\n", cf+3); 26312113Sralph } else { 26412113Sralph col = 0; 26512113Sralph prank(rank); 26612113Sralph blankfill(OWNCOL); 26712113Sralph printf("%-10s %-3d ", line+1, atoi(cf+3)); 26812113Sralph col += 16; 26912113Sralph first = 1; 27012113Sralph } 27112113Sralph continue; 27212113Sralph default: /* some format specifer and file name? */ 27312113Sralph if (line[0] < 'a' || line[0] > 'z') 27412113Sralph continue; 27512113Sralph if (j == 0 || strcmp(file, line+1) != 0) 27630995Sbostic (void) strcpy(file, line+1); 27712113Sralph j++; 27812113Sralph continue; 27912113Sralph case 'N': 28012113Sralph show(line+1, file, j); 28112113Sralph file[0] = '\0'; 28212113Sralph j = 0; 28312113Sralph } 28412113Sralph } 28512113Sralph fclose(cfp); 28612113Sralph if (!lflag) { 28712113Sralph blankfill(SIZCOL); 28834587Sbostic printf("%ld bytes\n", totsize); 28912113Sralph totsize = 0; 29012113Sralph } 29112113Sralph } 29212113Sralph 293*55470Sbostic int 29412113Sralph inlist(name, file) 29512113Sralph char *name, *file; 29612113Sralph { 29712113Sralph register int *r, n; 29812113Sralph register char **u, *cp; 29912113Sralph 30012113Sralph if (users == 0 && requests == 0) 30112113Sralph return(1); 30212113Sralph /* 30312113Sralph * Check to see if it's in the user list 30412113Sralph */ 30512113Sralph for (u = user; u < &user[users]; u++) 30612113Sralph if (!strcmp(*u, name)) 30712113Sralph return(1); 30812113Sralph /* 30912113Sralph * Check the request list 31012113Sralph */ 31112113Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 31212113Sralph n = n * 10 + (*cp++ - '0'); 31312113Sralph for (r = requ; r < &requ[requests]; r++) 31412113Sralph if (*r == n && !strcmp(cp, from)) 31512113Sralph return(1); 31612113Sralph return(0); 31712113Sralph } 31812113Sralph 319*55470Sbostic void 32012113Sralph show(nfile, file, copies) 32112113Sralph register char *nfile, *file; 322*55470Sbostic int copies; 32312113Sralph { 32412113Sralph if (strcmp(nfile, " ") == 0) 32512113Sralph nfile = "(standard input)"; 32612113Sralph if (lflag) 32712113Sralph ldump(nfile, file, copies); 32812113Sralph else 32912113Sralph dump(nfile, file, copies); 33012113Sralph } 33112113Sralph 33212113Sralph /* 33312113Sralph * Fill the line with blanks to the specified column 33412113Sralph */ 335*55470Sbostic void 33612113Sralph blankfill(n) 33712113Sralph register int n; 33812113Sralph { 33912113Sralph while (col++ < n) 34012113Sralph putchar(' '); 34112113Sralph } 34212113Sralph 34312113Sralph /* 34412113Sralph * Give the abbreviated dump of the file names 34512113Sralph */ 346*55470Sbostic void 34712113Sralph dump(nfile, file, copies) 34812113Sralph char *nfile, *file; 349*55470Sbostic int copies; 35012113Sralph { 35112113Sralph register short n, fill; 35212113Sralph struct stat lbuf; 35312113Sralph 35412113Sralph /* 35512113Sralph * Print as many files as will fit 35612113Sralph * (leaving room for the total size) 35712113Sralph */ 35812113Sralph fill = first ? 0 : 2; /* fill space for ``, '' */ 35912113Sralph if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) { 36012113Sralph if (col < SIZCOL) { 36112113Sralph printf(" ..."), col += 4; 36212113Sralph blankfill(SIZCOL); 36312113Sralph } 36412113Sralph } else { 36512113Sralph if (first) 36612113Sralph first = 0; 36712113Sralph else 36812113Sralph printf(", "); 36912113Sralph printf("%s", nfile); 37012113Sralph col += n+fill; 37112113Sralph } 37212113Sralph if (*file && !stat(file, &lbuf)) 37312113Sralph totsize += copies * lbuf.st_size; 37412113Sralph } 37512113Sralph 37612113Sralph /* 37712113Sralph * Print the long info about the file 37812113Sralph */ 379*55470Sbostic void 38012113Sralph ldump(nfile, file, copies) 38112113Sralph char *nfile, *file; 382*55470Sbostic int copies; 38312113Sralph { 38412113Sralph struct stat lbuf; 38512113Sralph 38612113Sralph putchar('\t'); 38712113Sralph if (copies > 1) 38812113Sralph printf("%-2d copies of %-19s", copies, nfile); 38912113Sralph else 39012113Sralph printf("%-32s", nfile); 39112113Sralph if (*file && !stat(file, &lbuf)) 392*55470Sbostic printf(" %qd bytes", lbuf.st_size); 39312113Sralph else 39412113Sralph printf(" ??? bytes"); 39512113Sralph putchar('\n'); 39612113Sralph } 39712113Sralph 39812113Sralph /* 39912113Sralph * Print the job's rank in the queue, 40012113Sralph * update col for screen management 40112113Sralph */ 402*55470Sbostic void 40312113Sralph prank(n) 404*55470Sbostic int n; 40512113Sralph { 406*55470Sbostic char rline[100]; 40712113Sralph static char *r[] = { 40812113Sralph "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" 40912113Sralph }; 41012113Sralph 41112113Sralph if (n == 0) { 41212113Sralph printf("active"); 41312113Sralph col += 6; 41412113Sralph return; 41512113Sralph } 41638491Sbostic if ((n/10)%10 == 1) 417*55470Sbostic (void)snprintf(rline, sizeof(rline), "%dth", n); 41812113Sralph else 419*55470Sbostic (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]); 420*55470Sbostic col += strlen(rline); 421*55470Sbostic printf("%s", rline); 42212113Sralph } 423