1*12878Sralph /* displayq.c 4.5 83/06/02 */ 212113Sralph /* 312113Sralph * Routines to display the state of the queue. 412113Sralph */ 512113Sralph 612113Sralph #include "lp.h" 712113Sralph 8*12878Sralph #define JOBCOL 40 /* column for job # in -l format */ 9*12878Sralph #define OWNCOL 7 /* start of Owner column in normal */ 10*12878Sralph #define SIZCOL 62 /* start of Size column in normal */ 1112113Sralph 1212113Sralph /* 1312113Sralph * Stuff for handling job specifications 1412113Sralph */ 1512113Sralph extern char *user[]; /* users to process */ 1612113Sralph extern int users; /* # of users in user array */ 1712113Sralph extern int requ[]; /* job number of spool entries */ 1812113Sralph extern int requests; /* # of spool requests */ 1912113Sralph 20*12878Sralph static int lflag; /* long output option */ 21*12878Sralph static char current[40]; /* current file being printed */ 22*12878Sralph static int garbage; /* # of garbage cf files */ 23*12878Sralph static int rank; /* order to be printed (-1=none, 0=active) */ 24*12878Sralph static long totsize; /* total print job size in bytes */ 25*12878Sralph static int first; /* first file in ``files'' column? */ 26*12878Sralph static int col; /* column on screen */ 27*12878Sralph static int sendtorem; /* are we sending to a remote? */ 28*12878Sralph static char file[132]; /* print file name */ 2912113Sralph 30*12878Sralph static char *head0 = "Rank Owner Job Files"; 31*12878Sralph static char *head1 = "Total Size\n"; 3212113Sralph 3312113Sralph /* 3412113Sralph * Display the current state of the queue. Format = 1 if long format. 3512113Sralph */ 3612113Sralph displayq(format) 3712113Sralph int format; 3812113Sralph { 3912113Sralph register struct queue *q; 4012113Sralph register int i, nitems, fd; 4112113Sralph struct queue **queue; 4212113Sralph struct stat statb; 4312113Sralph FILE *fp; 4412113Sralph 4512113Sralph lflag = format; 4612113Sralph totsize = 0; 4712113Sralph rank = -1; 4812113Sralph 4912113Sralph if ((i = pgetent(line, printer)) < 0) 5012113Sralph fatal("cannot open printer description file"); 5112113Sralph else if (i == 0) 5212113Sralph fatal("unknown printer"); 5312113Sralph if ((LP = pgetstr("lp", &bp)) == NULL) 5412113Sralph LP = DEFDEVLP; 5512113Sralph if ((RP = pgetstr("rp", &bp)) == NULL) 5612434Sralph RP = DEFLP; 5712113Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 5812113Sralph SD = DEFSPOOL; 5912113Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 6012113Sralph LO = DEFLOCK; 6112113Sralph if ((ST = pgetstr("st", &bp)) == NULL) 6212113Sralph ST = DEFSTAT; 6312113Sralph RM = pgetstr("rm", &bp); 6412113Sralph 6512113Sralph /* 6612113Sralph * If there is no local printer, then print the queue on 6712113Sralph * the remote machine and then what's in the queue here. 6812113Sralph * Note that a file in transit may not show up in either queue. 6912113Sralph */ 7012113Sralph if (*LP == '\0') { 7112113Sralph register char *cp; 7212113Sralph char c; 7312113Sralph 7412113Sralph sendtorem++; 7512113Sralph (void) sprintf(line, "%c%s", format + '\3', RP); 7612113Sralph cp = line; 7712113Sralph for (i = 0; i < requests; i++) { 7812113Sralph cp += strlen(cp); 7912113Sralph (void) sprintf(cp, " %d", requ[i]); 8012113Sralph } 8112113Sralph for (i = 0; i < users; i++) { 8212113Sralph cp += strlen(cp); 8312113Sralph *cp++ = ' '; 8412113Sralph strcpy(cp, user[i]); 8512113Sralph } 8612113Sralph strcat(line, "\n"); 8712531Sralph fd = getport(RM); 8812113Sralph if (fd < 0) { 8912113Sralph if (from != host) 9012113Sralph printf("%s: ", host); 9112113Sralph printf("connection to %s is down\n", RM); 9212113Sralph } else { 9312113Sralph i = strlen(line); 9412113Sralph if (write(fd, line, i) != i) 9512113Sralph fatal("Lost connection"); 9612113Sralph while ((i = read(fd, line, sizeof(line))) > 0) 9712113Sralph (void) fwrite(line, 1, i, stdout); 9812113Sralph (void) close(fd); 9912113Sralph } 10012113Sralph } 10112113Sralph /* 10212113Sralph * Find all the control files in the spooling directory 10312113Sralph */ 10412113Sralph if (chdir(SD) < 0) 10512113Sralph fatal("cannot chdir to spooling directory"); 10612113Sralph if ((nitems = getq(&queue)) < 0) 10712113Sralph fatal("cannot examine spooling area\n"); 10812740Sralph if (stat(LO, &statb) >= 0 && (statb.st_mode & 010)) { 10912113Sralph if (sendtorem) 11012113Sralph printf("\n%s: ", host); 11112740Sralph printf("Warning: %s queue is turned off\n", printer); 11212740Sralph } 11312740Sralph if (nitems == 0) { 11412740Sralph if (!sendtorem) 11512740Sralph printf("no entries\n"); 11612113Sralph return(0); 11712113Sralph } 11812113Sralph fp = fopen(LO, "r"); 11912113Sralph if (fp == NULL || flock(fileno(fp), FSHLOCK|FNBLOCK) == 0) { 12012113Sralph if (fp != NULL) 12112113Sralph fclose(fp); 12212113Sralph garbage = nitems; 12312740Sralph if (sendtorem) 12412740Sralph printf("\n%s: ", host); 12512434Sralph if (stat(LO, &statb) >= 0 && (statb.st_mode & 0100)) 12612434Sralph printf("Warning: %s is down\n", printer); 12712113Sralph else 12812434Sralph printf("Warning: no daemon present\n"); 12912113Sralph } else { 13012113Sralph register char *cp = current; 13112113Sralph 13212113Sralph /* skip daemon pid */ 13312113Sralph while ((*cp = getc(fp)) != EOF && *cp != '\n'); 13412113Sralph /* read current file name */ 13512113Sralph while ((*cp = getc(fp)) != EOF && *cp != '\n') 13612113Sralph cp++; 13712113Sralph *cp = '\0'; 13812113Sralph fclose(fp); 13912434Sralph /* 14012434Sralph * Print the status file to show what the daemon is doing. 14112434Sralph */ 14212434Sralph if (sendtorem) 14312434Sralph printf("\n%s: ", host); 14412434Sralph if ((fd = open(ST, FRDONLY|FSHLOCK)) >= 0) { 14512434Sralph while ((i = read(fd, line, sizeof(line))) > 0) 14612434Sralph (void) fwrite(line, 1, i, stdout); 14712434Sralph (void) close(fd); 14812434Sralph } else 14912434Sralph putchar('\n'); 15012113Sralph } 15112113Sralph /* 15212113Sralph * Now, examine the control files and print out the jobs to 15312113Sralph * be done for each user. 15412113Sralph */ 15512113Sralph if (!lflag) 15612113Sralph header(); 15712113Sralph for (i = 0; i < nitems; i++) { 15812113Sralph q = queue[i]; 15912113Sralph inform(q->q_name); 16012113Sralph free(q); 16112113Sralph } 16212113Sralph free(queue); 16312113Sralph return(nitems-garbage); 16412113Sralph } 16512113Sralph 16612113Sralph /* 16712113Sralph * Print the header for the short listing format 16812113Sralph */ 169*12878Sralph static 17012113Sralph header() 17112113Sralph { 17212113Sralph printf(head0); 17312113Sralph col = strlen(head0)+1; 17412113Sralph blankfill(SIZCOL); 17512113Sralph printf(head1); 17612113Sralph } 17712113Sralph 178*12878Sralph static 17912113Sralph inform(cf) 18012113Sralph char *cf; 18112113Sralph { 18212113Sralph register int j, k; 18312113Sralph register char *cp; 18412113Sralph FILE *cfp; 18512113Sralph 18612113Sralph /* 18712113Sralph * There's a chance the control file has gone away 18812113Sralph * in the meantime; if this is the case just keep going 18912113Sralph */ 19012113Sralph if ((cfp = fopen(cf, "r")) == NULL) 19112113Sralph return; 19212113Sralph 19312113Sralph if (rank < 0) 19412113Sralph rank = 0; 19512113Sralph if (sendtorem || garbage || strcmp(cf, current)) 19612113Sralph rank++; 19712113Sralph j = 0; 19812113Sralph while (getline(cfp)) { 19912113Sralph switch (line[0]) { 20012113Sralph case 'P': /* Was this file specified in the user's list? */ 20112113Sralph if (!inlist(line+1, cf)) { 20212113Sralph fclose(cfp); 20312113Sralph return; 20412113Sralph } 20512113Sralph if (lflag) { 20612113Sralph printf("\n%s: ", line+1); 20712113Sralph col = strlen(line+1) + 2; 20812113Sralph prank(rank); 20912113Sralph blankfill(JOBCOL); 21012113Sralph printf(" [job %s]\n", cf+3); 21112113Sralph } else { 21212113Sralph col = 0; 21312113Sralph prank(rank); 21412113Sralph blankfill(OWNCOL); 21512113Sralph printf("%-10s %-3d ", line+1, atoi(cf+3)); 21612113Sralph col += 16; 21712113Sralph first = 1; 21812113Sralph } 21912113Sralph continue; 22012113Sralph default: /* some format specifer and file name? */ 22112113Sralph if (line[0] < 'a' || line[0] > 'z') 22212113Sralph continue; 22312113Sralph if (j == 0 || strcmp(file, line+1) != 0) 22412113Sralph strcpy(file, line+1); 22512113Sralph j++; 22612113Sralph continue; 22712113Sralph case 'N': 22812113Sralph show(line+1, file, j); 22912113Sralph file[0] = '\0'; 23012113Sralph j = 0; 23112113Sralph } 23212113Sralph } 23312113Sralph fclose(cfp); 23412113Sralph if (!lflag) { 23512113Sralph blankfill(SIZCOL); 23612113Sralph printf("%D bytes\n", totsize); 23712113Sralph totsize = 0; 23812113Sralph } 23912113Sralph } 24012113Sralph 241*12878Sralph static 24212113Sralph inlist(name, file) 24312113Sralph char *name, *file; 24412113Sralph { 24512113Sralph register int *r, n; 24612113Sralph register char **u, *cp; 24712113Sralph 24812113Sralph if (users == 0 && requests == 0) 24912113Sralph return(1); 25012113Sralph /* 25112113Sralph * Check to see if it's in the user list 25212113Sralph */ 25312113Sralph for (u = user; u < &user[users]; u++) 25412113Sralph if (!strcmp(*u, name)) 25512113Sralph return(1); 25612113Sralph /* 25712113Sralph * Check the request list 25812113Sralph */ 25912113Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 26012113Sralph n = n * 10 + (*cp++ - '0'); 26112113Sralph for (r = requ; r < &requ[requests]; r++) 26212113Sralph if (*r == n && !strcmp(cp, from)) 26312113Sralph return(1); 26412113Sralph return(0); 26512113Sralph } 26612113Sralph 267*12878Sralph static 26812113Sralph show(nfile, file, copies) 26912113Sralph register char *nfile, *file; 27012113Sralph { 27112113Sralph if (strcmp(nfile, " ") == 0) 27212113Sralph nfile = "(standard input)"; 27312113Sralph if (lflag) 27412113Sralph ldump(nfile, file, copies); 27512113Sralph else 27612113Sralph dump(nfile, file, copies); 27712113Sralph } 27812113Sralph 27912113Sralph /* 28012113Sralph * Fill the line with blanks to the specified column 28112113Sralph */ 282*12878Sralph static 28312113Sralph blankfill(n) 28412113Sralph register int n; 28512113Sralph { 28612113Sralph while (col++ < n) 28712113Sralph putchar(' '); 28812113Sralph } 28912113Sralph 29012113Sralph /* 29112113Sralph * Give the abbreviated dump of the file names 29212113Sralph */ 293*12878Sralph static 29412113Sralph dump(nfile, file, copies) 29512113Sralph char *nfile, *file; 29612113Sralph { 29712113Sralph register short n, fill; 29812113Sralph struct stat lbuf; 29912113Sralph 30012113Sralph /* 30112113Sralph * Print as many files as will fit 30212113Sralph * (leaving room for the total size) 30312113Sralph */ 30412113Sralph fill = first ? 0 : 2; /* fill space for ``, '' */ 30512113Sralph if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) { 30612113Sralph if (col < SIZCOL) { 30712113Sralph printf(" ..."), col += 4; 30812113Sralph blankfill(SIZCOL); 30912113Sralph } 31012113Sralph } else { 31112113Sralph if (first) 31212113Sralph first = 0; 31312113Sralph else 31412113Sralph printf(", "); 31512113Sralph printf("%s", nfile); 31612113Sralph col += n+fill; 31712113Sralph } 31812113Sralph if (*file && !stat(file, &lbuf)) 31912113Sralph totsize += copies * lbuf.st_size; 32012113Sralph } 32112113Sralph 32212113Sralph /* 32312113Sralph * Print the long info about the file 32412113Sralph */ 325*12878Sralph static 32612113Sralph ldump(nfile, file, copies) 32712113Sralph char *nfile, *file; 32812113Sralph { 32912113Sralph struct stat lbuf; 33012113Sralph 33112113Sralph putchar('\t'); 33212113Sralph if (copies > 1) 33312113Sralph printf("%-2d copies of %-19s", copies, nfile); 33412113Sralph else 33512113Sralph printf("%-32s", nfile); 33612113Sralph if (*file && !stat(file, &lbuf)) 33712113Sralph printf(" %D bytes", lbuf.st_size); 33812113Sralph else 33912113Sralph printf(" ??? bytes"); 34012113Sralph putchar('\n'); 34112113Sralph } 34212113Sralph 34312113Sralph /* 34412113Sralph * Print the job's rank in the queue, 34512113Sralph * update col for screen management 34612113Sralph */ 347*12878Sralph static 34812113Sralph prank(n) 34912113Sralph { 35012113Sralph char line[100]; 35112113Sralph static char *r[] = { 35212113Sralph "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" 35312113Sralph }; 35412113Sralph 35512113Sralph if (n == 0) { 35612113Sralph printf("active"); 35712113Sralph col += 6; 35812113Sralph return; 35912113Sralph } 36012113Sralph if ((n/10) == 1) 36112113Sralph (void) sprintf(line, "%dth", n); 36212113Sralph else 36312113Sralph (void) sprintf(line, "%d%s", n, r[n%10]); 36412113Sralph col += strlen(line); 36512113Sralph printf("%s", line); 36612113Sralph } 367