113952Ssam #ifndef lint 2*16758Sralph static char sccsid[] = "@(#)displayq.c 4.12 (Berkeley) 07/24/84"; 313952Ssam #endif 413952Ssam 512113Sralph /* 612113Sralph * Routines to display the state of the queue. 712113Sralph */ 812113Sralph 912113Sralph #include "lp.h" 1012113Sralph 1113170Sralph #define JOBCOL 40 /* column for job # in -l format */ 1213170Sralph #define OWNCOL 7 /* start of Owner column in normal */ 1313170Sralph #define SIZCOL 62 /* start of Size column in normal */ 1412113Sralph 1512113Sralph /* 1612113Sralph * Stuff for handling job specifications 1712113Sralph */ 1812113Sralph extern char *user[]; /* users to process */ 1912113Sralph extern int users; /* # of users in user array */ 2012113Sralph extern int requ[]; /* job number of spool entries */ 2112113Sralph extern int requests; /* # of spool requests */ 2212113Sralph 23*16758Sralph int lflag; /* long output option */ 24*16758Sralph char current[40]; /* current file being printed */ 25*16758Sralph int garbage; /* # of garbage cf files */ 26*16758Sralph int rank; /* order to be printed (-1=none, 0=active) */ 27*16758Sralph long totsize; /* total print job size in bytes */ 28*16758Sralph int first; /* first file in ``files'' column? */ 29*16758Sralph int col; /* column on screen */ 30*16758Sralph int sendtorem; /* are we sending to a remote? */ 31*16758Sralph char file[132]; /* print file name */ 3212113Sralph 33*16758Sralph char *head0 = "Rank Owner Job Files"; 34*16758Sralph char *head1 = "Total Size\n"; 3512113Sralph 3612113Sralph /* 3712113Sralph * Display the current state of the queue. Format = 1 if long format. 3812113Sralph */ 3912113Sralph displayq(format) 4012113Sralph int format; 4112113Sralph { 4212113Sralph register struct queue *q; 4312113Sralph register int i, nitems, fd; 4412113Sralph struct queue **queue; 4512113Sralph struct stat statb; 4612113Sralph FILE *fp; 4712113Sralph 4812113Sralph lflag = format; 4912113Sralph totsize = 0; 5012113Sralph rank = -1; 5112113Sralph 5212113Sralph if ((i = pgetent(line, printer)) < 0) 5312113Sralph fatal("cannot open printer description file"); 5412113Sralph else if (i == 0) 5512113Sralph fatal("unknown printer"); 5612113Sralph if ((LP = pgetstr("lp", &bp)) == NULL) 5712113Sralph LP = DEFDEVLP; 5812113Sralph if ((RP = pgetstr("rp", &bp)) == NULL) 5912434Sralph RP = DEFLP; 6012113Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 6112113Sralph SD = DEFSPOOL; 6212113Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 6312113Sralph LO = DEFLOCK; 6412113Sralph if ((ST = pgetstr("st", &bp)) == NULL) 6512113Sralph ST = DEFSTAT; 6612113Sralph RM = pgetstr("rm", &bp); 6712113Sralph 6812113Sralph /* 6912113Sralph * If there is no local printer, then print the queue on 7012113Sralph * the remote machine and then what's in the queue here. 7112113Sralph * Note that a file in transit may not show up in either queue. 7212113Sralph */ 7312113Sralph if (*LP == '\0') { 7412113Sralph register char *cp; 7512113Sralph char c; 7612113Sralph 7712113Sralph sendtorem++; 7812113Sralph (void) sprintf(line, "%c%s", format + '\3', RP); 7912113Sralph cp = line; 8012113Sralph for (i = 0; i < requests; i++) { 8112113Sralph cp += strlen(cp); 8212113Sralph (void) sprintf(cp, " %d", requ[i]); 8312113Sralph } 8412113Sralph for (i = 0; i < users; i++) { 8512113Sralph cp += strlen(cp); 8612113Sralph *cp++ = ' '; 8712113Sralph strcpy(cp, user[i]); 8812113Sralph } 8912113Sralph strcat(line, "\n"); 9012531Sralph fd = getport(RM); 9112113Sralph if (fd < 0) { 9212113Sralph if (from != host) 9312113Sralph printf("%s: ", host); 9412113Sralph printf("connection to %s is down\n", RM); 9512113Sralph } else { 9612113Sralph i = strlen(line); 9712113Sralph if (write(fd, line, i) != i) 9812113Sralph fatal("Lost connection"); 9912113Sralph while ((i = read(fd, line, sizeof(line))) > 0) 10012113Sralph (void) fwrite(line, 1, i, stdout); 10112113Sralph (void) close(fd); 10212113Sralph } 10312113Sralph } 10412113Sralph /* 10512113Sralph * Find all the control files in the spooling directory 10612113Sralph */ 10712113Sralph if (chdir(SD) < 0) 10812113Sralph fatal("cannot chdir to spooling directory"); 10912113Sralph if ((nitems = getq(&queue)) < 0) 11012113Sralph fatal("cannot examine spooling area\n"); 11116205Sralph if (stat(LO, &statb) >= 0) { 11216205Sralph if ((statb.st_mode & 0110) && sendtorem) 11316205Sralph printf("\n"); 11416205Sralph if (statb.st_mode & 0100) { 11516205Sralph if (sendtorem) 11616205Sralph printf("%s: ", host); 11716205Sralph printf("Warning: %s is down: ", printer); 11816205Sralph fd = open(ST, O_RDONLY); 11916205Sralph if (fd >= 0) { 12016205Sralph (void) flock(fd, LOCK_SH); 12116205Sralph while ((i = read(fd, line, sizeof(line))) > 0) 12216205Sralph (void) fwrite(line, 1, i, stdout); 12316205Sralph (void) close(fd); /* unlocks as well */ 12416205Sralph } else 12516205Sralph putchar('\n'); 12616205Sralph } 12716205Sralph if (statb.st_mode & 010) { 12816205Sralph if (sendtorem) 12916205Sralph printf("%s: ", host); 13016205Sralph printf("Warning: %s queue is turned off\n", printer); 13116205Sralph } 13212740Sralph } 13312740Sralph if (nitems == 0) { 13412740Sralph if (!sendtorem) 13512740Sralph printf("no entries\n"); 13612113Sralph return(0); 13712113Sralph } 13812113Sralph fp = fopen(LO, "r"); 13913441Sralph if (fp == NULL) 14013441Sralph warn(); 14113441Sralph else { 14213441Sralph register char *cp; 14312113Sralph 14413441Sralph /* get daemon pid */ 14513441Sralph cp = current; 14612113Sralph while ((*cp = getc(fp)) != EOF && *cp != '\n') 14712113Sralph cp++; 14812113Sralph *cp = '\0'; 14913441Sralph i = atoi(current); 15015908Sralph if (i <= 0 || kill(i, 0) < 0) 15113441Sralph warn(); 15213441Sralph else { 15313441Sralph /* read current file name */ 15413441Sralph cp = current; 15513441Sralph while ((*cp = getc(fp)) != EOF && *cp != '\n') 15613441Sralph cp++; 15713441Sralph *cp = '\0'; 15813441Sralph /* 15913441Sralph * Print the status file. 16013441Sralph */ 16113441Sralph if (sendtorem) 16213441Sralph printf("\n%s: ", host); 16313441Sralph fd = open(ST, O_RDONLY); 16413441Sralph if (fd >= 0) { 16513441Sralph (void) flock(fd, LOCK_SH); 16613441Sralph while ((i = read(fd, line, sizeof(line))) > 0) 16713441Sralph (void) fwrite(line, 1, i, stdout); 16813441Sralph (void) close(fd); /* unlocks as well */ 16913441Sralph } else 17013441Sralph putchar('\n'); 17113441Sralph } 17213441Sralph (void) fclose(fp); 17312113Sralph } 17412113Sralph /* 17512113Sralph * Now, examine the control files and print out the jobs to 17612113Sralph * be done for each user. 17712113Sralph */ 17812113Sralph if (!lflag) 17912113Sralph header(); 18012113Sralph for (i = 0; i < nitems; i++) { 18112113Sralph q = queue[i]; 18212113Sralph inform(q->q_name); 18312113Sralph free(q); 18412113Sralph } 18512113Sralph free(queue); 18612113Sralph return(nitems-garbage); 18712113Sralph } 18812113Sralph 18912113Sralph /* 19013441Sralph * Print a warning message if there is no daemon present. 19113441Sralph */ 19213441Sralph warn() 19313441Sralph { 19413441Sralph if (sendtorem) 19513441Sralph printf("\n%s: ", host); 19616205Sralph printf("Warning: no daemon present\n"); 19713441Sralph current[0] = '\0'; 19813441Sralph } 19913441Sralph 20013441Sralph /* 20112113Sralph * Print the header for the short listing format 20212113Sralph */ 20312113Sralph header() 20412113Sralph { 20512113Sralph printf(head0); 20612113Sralph col = strlen(head0)+1; 20712113Sralph blankfill(SIZCOL); 20812113Sralph printf(head1); 20912113Sralph } 21012113Sralph 21112113Sralph inform(cf) 21212113Sralph char *cf; 21312113Sralph { 21412113Sralph register int j, k; 21512113Sralph register char *cp; 21612113Sralph FILE *cfp; 21712113Sralph 21812113Sralph /* 21912113Sralph * There's a chance the control file has gone away 22012113Sralph * in the meantime; if this is the case just keep going 22112113Sralph */ 22212113Sralph if ((cfp = fopen(cf, "r")) == NULL) 22312113Sralph return; 22412113Sralph 22512113Sralph if (rank < 0) 22612113Sralph rank = 0; 22712113Sralph if (sendtorem || garbage || strcmp(cf, current)) 22812113Sralph rank++; 22912113Sralph j = 0; 23012113Sralph while (getline(cfp)) { 23112113Sralph switch (line[0]) { 23212113Sralph case 'P': /* Was this file specified in the user's list? */ 23312113Sralph if (!inlist(line+1, cf)) { 23412113Sralph fclose(cfp); 23512113Sralph return; 23612113Sralph } 23712113Sralph if (lflag) { 23812113Sralph printf("\n%s: ", line+1); 23912113Sralph col = strlen(line+1) + 2; 24012113Sralph prank(rank); 24112113Sralph blankfill(JOBCOL); 24212113Sralph printf(" [job %s]\n", cf+3); 24312113Sralph } else { 24412113Sralph col = 0; 24512113Sralph prank(rank); 24612113Sralph blankfill(OWNCOL); 24712113Sralph printf("%-10s %-3d ", line+1, atoi(cf+3)); 24812113Sralph col += 16; 24912113Sralph first = 1; 25012113Sralph } 25112113Sralph continue; 25212113Sralph default: /* some format specifer and file name? */ 25312113Sralph if (line[0] < 'a' || line[0] > 'z') 25412113Sralph continue; 25512113Sralph if (j == 0 || strcmp(file, line+1) != 0) 25612113Sralph strcpy(file, line+1); 25712113Sralph j++; 25812113Sralph continue; 25912113Sralph case 'N': 26012113Sralph show(line+1, file, j); 26112113Sralph file[0] = '\0'; 26212113Sralph j = 0; 26312113Sralph } 26412113Sralph } 26512113Sralph fclose(cfp); 26612113Sralph if (!lflag) { 26712113Sralph blankfill(SIZCOL); 26812113Sralph printf("%D bytes\n", totsize); 26912113Sralph totsize = 0; 27012113Sralph } 27112113Sralph } 27212113Sralph 27312113Sralph inlist(name, file) 27412113Sralph char *name, *file; 27512113Sralph { 27612113Sralph register int *r, n; 27712113Sralph register char **u, *cp; 27812113Sralph 27912113Sralph if (users == 0 && requests == 0) 28012113Sralph return(1); 28112113Sralph /* 28212113Sralph * Check to see if it's in the user list 28312113Sralph */ 28412113Sralph for (u = user; u < &user[users]; u++) 28512113Sralph if (!strcmp(*u, name)) 28612113Sralph return(1); 28712113Sralph /* 28812113Sralph * Check the request list 28912113Sralph */ 29012113Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 29112113Sralph n = n * 10 + (*cp++ - '0'); 29212113Sralph for (r = requ; r < &requ[requests]; r++) 29312113Sralph if (*r == n && !strcmp(cp, from)) 29412113Sralph return(1); 29512113Sralph return(0); 29612113Sralph } 29712113Sralph 29812113Sralph show(nfile, file, copies) 29912113Sralph register char *nfile, *file; 30012113Sralph { 30112113Sralph if (strcmp(nfile, " ") == 0) 30212113Sralph nfile = "(standard input)"; 30312113Sralph if (lflag) 30412113Sralph ldump(nfile, file, copies); 30512113Sralph else 30612113Sralph dump(nfile, file, copies); 30712113Sralph } 30812113Sralph 30912113Sralph /* 31012113Sralph * Fill the line with blanks to the specified column 31112113Sralph */ 31212113Sralph blankfill(n) 31312113Sralph register int n; 31412113Sralph { 31512113Sralph while (col++ < n) 31612113Sralph putchar(' '); 31712113Sralph } 31812113Sralph 31912113Sralph /* 32012113Sralph * Give the abbreviated dump of the file names 32112113Sralph */ 32212113Sralph dump(nfile, file, copies) 32312113Sralph char *nfile, *file; 32412113Sralph { 32512113Sralph register short n, fill; 32612113Sralph struct stat lbuf; 32712113Sralph 32812113Sralph /* 32912113Sralph * Print as many files as will fit 33012113Sralph * (leaving room for the total size) 33112113Sralph */ 33212113Sralph fill = first ? 0 : 2; /* fill space for ``, '' */ 33312113Sralph if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) { 33412113Sralph if (col < SIZCOL) { 33512113Sralph printf(" ..."), col += 4; 33612113Sralph blankfill(SIZCOL); 33712113Sralph } 33812113Sralph } else { 33912113Sralph if (first) 34012113Sralph first = 0; 34112113Sralph else 34212113Sralph printf(", "); 34312113Sralph printf("%s", nfile); 34412113Sralph col += n+fill; 34512113Sralph } 34612113Sralph if (*file && !stat(file, &lbuf)) 34712113Sralph totsize += copies * lbuf.st_size; 34812113Sralph } 34912113Sralph 35012113Sralph /* 35112113Sralph * Print the long info about the file 35212113Sralph */ 35312113Sralph ldump(nfile, file, copies) 35412113Sralph char *nfile, *file; 35512113Sralph { 35612113Sralph struct stat lbuf; 35712113Sralph 35812113Sralph putchar('\t'); 35912113Sralph if (copies > 1) 36012113Sralph printf("%-2d copies of %-19s", copies, nfile); 36112113Sralph else 36212113Sralph printf("%-32s", nfile); 36312113Sralph if (*file && !stat(file, &lbuf)) 36412113Sralph printf(" %D bytes", lbuf.st_size); 36512113Sralph else 36612113Sralph printf(" ??? bytes"); 36712113Sralph putchar('\n'); 36812113Sralph } 36912113Sralph 37012113Sralph /* 37112113Sralph * Print the job's rank in the queue, 37212113Sralph * update col for screen management 37312113Sralph */ 37412113Sralph prank(n) 37512113Sralph { 37612113Sralph char line[100]; 37712113Sralph static char *r[] = { 37812113Sralph "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" 37912113Sralph }; 38012113Sralph 38112113Sralph if (n == 0) { 38212113Sralph printf("active"); 38312113Sralph col += 6; 38412113Sralph return; 38512113Sralph } 38612113Sralph if ((n/10) == 1) 38712113Sralph (void) sprintf(line, "%dth", n); 38812113Sralph else 38912113Sralph (void) sprintf(line, "%d%s", n, r[n%10]); 39012113Sralph col += strlen(line); 39112113Sralph printf("%s", line); 39212113Sralph } 393