113952Ssam #ifndef lint 2*16205Sralph static char sccsid[] = "@(#)displayq.c 4.11 (Berkeley) 03/19/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 2312878Sralph static int lflag; /* long output option */ 2412878Sralph static char current[40]; /* current file being printed */ 2512878Sralph static int garbage; /* # of garbage cf files */ 2612878Sralph static int rank; /* order to be printed (-1=none, 0=active) */ 2712878Sralph static long totsize; /* total print job size in bytes */ 2812878Sralph static int first; /* first file in ``files'' column? */ 2912878Sralph static int col; /* column on screen */ 3012878Sralph static int sendtorem; /* are we sending to a remote? */ 3112878Sralph static char file[132]; /* print file name */ 3212113Sralph 3312878Sralph static char *head0 = "Rank Owner Job Files"; 3412878Sralph static 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"); 111*16205Sralph if (stat(LO, &statb) >= 0) { 112*16205Sralph if ((statb.st_mode & 0110) && sendtorem) 113*16205Sralph printf("\n"); 114*16205Sralph if (statb.st_mode & 0100) { 115*16205Sralph if (sendtorem) 116*16205Sralph printf("%s: ", host); 117*16205Sralph printf("Warning: %s is down: ", printer); 118*16205Sralph fd = open(ST, O_RDONLY); 119*16205Sralph if (fd >= 0) { 120*16205Sralph (void) flock(fd, LOCK_SH); 121*16205Sralph while ((i = read(fd, line, sizeof(line))) > 0) 122*16205Sralph (void) fwrite(line, 1, i, stdout); 123*16205Sralph (void) close(fd); /* unlocks as well */ 124*16205Sralph } else 125*16205Sralph putchar('\n'); 126*16205Sralph } 127*16205Sralph if (statb.st_mode & 010) { 128*16205Sralph if (sendtorem) 129*16205Sralph printf("%s: ", host); 130*16205Sralph printf("Warning: %s queue is turned off\n", printer); 131*16205Sralph } 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); 196*16205Sralph printf("Warning: no daemon present\n"); 19713441Sralph current[0] = '\0'; 19813441Sralph } 19913441Sralph 20013441Sralph /* 20112113Sralph * Print the header for the short listing format 20212113Sralph */ 20312878Sralph static 20412113Sralph header() 20512113Sralph { 20612113Sralph printf(head0); 20712113Sralph col = strlen(head0)+1; 20812113Sralph blankfill(SIZCOL); 20912113Sralph printf(head1); 21012113Sralph } 21112113Sralph 21212878Sralph static 21312113Sralph inform(cf) 21412113Sralph char *cf; 21512113Sralph { 21612113Sralph register int j, k; 21712113Sralph register char *cp; 21812113Sralph FILE *cfp; 21912113Sralph 22012113Sralph /* 22112113Sralph * There's a chance the control file has gone away 22212113Sralph * in the meantime; if this is the case just keep going 22312113Sralph */ 22412113Sralph if ((cfp = fopen(cf, "r")) == NULL) 22512113Sralph return; 22612113Sralph 22712113Sralph if (rank < 0) 22812113Sralph rank = 0; 22912113Sralph if (sendtorem || garbage || strcmp(cf, current)) 23012113Sralph rank++; 23112113Sralph j = 0; 23212113Sralph while (getline(cfp)) { 23312113Sralph switch (line[0]) { 23412113Sralph case 'P': /* Was this file specified in the user's list? */ 23512113Sralph if (!inlist(line+1, cf)) { 23612113Sralph fclose(cfp); 23712113Sralph return; 23812113Sralph } 23912113Sralph if (lflag) { 24012113Sralph printf("\n%s: ", line+1); 24112113Sralph col = strlen(line+1) + 2; 24212113Sralph prank(rank); 24312113Sralph blankfill(JOBCOL); 24412113Sralph printf(" [job %s]\n", cf+3); 24512113Sralph } else { 24612113Sralph col = 0; 24712113Sralph prank(rank); 24812113Sralph blankfill(OWNCOL); 24912113Sralph printf("%-10s %-3d ", line+1, atoi(cf+3)); 25012113Sralph col += 16; 25112113Sralph first = 1; 25212113Sralph } 25312113Sralph continue; 25412113Sralph default: /* some format specifer and file name? */ 25512113Sralph if (line[0] < 'a' || line[0] > 'z') 25612113Sralph continue; 25712113Sralph if (j == 0 || strcmp(file, line+1) != 0) 25812113Sralph strcpy(file, line+1); 25912113Sralph j++; 26012113Sralph continue; 26112113Sralph case 'N': 26212113Sralph show(line+1, file, j); 26312113Sralph file[0] = '\0'; 26412113Sralph j = 0; 26512113Sralph } 26612113Sralph } 26712113Sralph fclose(cfp); 26812113Sralph if (!lflag) { 26912113Sralph blankfill(SIZCOL); 27012113Sralph printf("%D bytes\n", totsize); 27112113Sralph totsize = 0; 27212113Sralph } 27312113Sralph } 27412113Sralph 27512878Sralph static 27612113Sralph inlist(name, file) 27712113Sralph char *name, *file; 27812113Sralph { 27912113Sralph register int *r, n; 28012113Sralph register char **u, *cp; 28112113Sralph 28212113Sralph if (users == 0 && requests == 0) 28312113Sralph return(1); 28412113Sralph /* 28512113Sralph * Check to see if it's in the user list 28612113Sralph */ 28712113Sralph for (u = user; u < &user[users]; u++) 28812113Sralph if (!strcmp(*u, name)) 28912113Sralph return(1); 29012113Sralph /* 29112113Sralph * Check the request list 29212113Sralph */ 29312113Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 29412113Sralph n = n * 10 + (*cp++ - '0'); 29512113Sralph for (r = requ; r < &requ[requests]; r++) 29612113Sralph if (*r == n && !strcmp(cp, from)) 29712113Sralph return(1); 29812113Sralph return(0); 29912113Sralph } 30012113Sralph 30112878Sralph static 30212113Sralph show(nfile, file, copies) 30312113Sralph register char *nfile, *file; 30412113Sralph { 30512113Sralph if (strcmp(nfile, " ") == 0) 30612113Sralph nfile = "(standard input)"; 30712113Sralph if (lflag) 30812113Sralph ldump(nfile, file, copies); 30912113Sralph else 31012113Sralph dump(nfile, file, copies); 31112113Sralph } 31212113Sralph 31312113Sralph /* 31412113Sralph * Fill the line with blanks to the specified column 31512113Sralph */ 31612878Sralph static 31712113Sralph blankfill(n) 31812113Sralph register int n; 31912113Sralph { 32012113Sralph while (col++ < n) 32112113Sralph putchar(' '); 32212113Sralph } 32312113Sralph 32412113Sralph /* 32512113Sralph * Give the abbreviated dump of the file names 32612113Sralph */ 32712878Sralph static 32812113Sralph dump(nfile, file, copies) 32912113Sralph char *nfile, *file; 33012113Sralph { 33112113Sralph register short n, fill; 33212113Sralph struct stat lbuf; 33312113Sralph 33412113Sralph /* 33512113Sralph * Print as many files as will fit 33612113Sralph * (leaving room for the total size) 33712113Sralph */ 33812113Sralph fill = first ? 0 : 2; /* fill space for ``, '' */ 33912113Sralph if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) { 34012113Sralph if (col < SIZCOL) { 34112113Sralph printf(" ..."), col += 4; 34212113Sralph blankfill(SIZCOL); 34312113Sralph } 34412113Sralph } else { 34512113Sralph if (first) 34612113Sralph first = 0; 34712113Sralph else 34812113Sralph printf(", "); 34912113Sralph printf("%s", nfile); 35012113Sralph col += n+fill; 35112113Sralph } 35212113Sralph if (*file && !stat(file, &lbuf)) 35312113Sralph totsize += copies * lbuf.st_size; 35412113Sralph } 35512113Sralph 35612113Sralph /* 35712113Sralph * Print the long info about the file 35812113Sralph */ 35912878Sralph static 36012113Sralph ldump(nfile, file, copies) 36112113Sralph char *nfile, *file; 36212113Sralph { 36312113Sralph struct stat lbuf; 36412113Sralph 36512113Sralph putchar('\t'); 36612113Sralph if (copies > 1) 36712113Sralph printf("%-2d copies of %-19s", copies, nfile); 36812113Sralph else 36912113Sralph printf("%-32s", nfile); 37012113Sralph if (*file && !stat(file, &lbuf)) 37112113Sralph printf(" %D bytes", lbuf.st_size); 37212113Sralph else 37312113Sralph printf(" ??? bytes"); 37412113Sralph putchar('\n'); 37512113Sralph } 37612113Sralph 37712113Sralph /* 37812113Sralph * Print the job's rank in the queue, 37912113Sralph * update col for screen management 38012113Sralph */ 38112878Sralph static 38212113Sralph prank(n) 38312113Sralph { 38412113Sralph char line[100]; 38512113Sralph static char *r[] = { 38612113Sralph "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" 38712113Sralph }; 38812113Sralph 38912113Sralph if (n == 0) { 39012113Sralph printf("active"); 39112113Sralph col += 6; 39212113Sralph return; 39312113Sralph } 39412113Sralph if ((n/10) == 1) 39512113Sralph (void) sprintf(line, "%dth", n); 39612113Sralph else 39712113Sralph (void) sprintf(line, "%d%s", n, r[n%10]); 39812113Sralph col += strlen(line); 39912113Sralph printf("%s", line); 40012113Sralph } 401