122427Sdist /* 222427Sdist * Copyright (c) 1983 Regents of the University of California. 334203Sbostic * All rights reserved. 434203Sbostic * 534203Sbostic * Redistribution and use in source and binary forms are permitted 634936Sbostic * provided that the above copyright notice and this paragraph are 734936Sbostic * duplicated in all such forms and that any documentation, 834936Sbostic * advertising materials, and other materials related to such 934936Sbostic * distribution and use acknowledge that the software was developed 1034936Sbostic * by the University of California, Berkeley. The name of the 1134936Sbostic * University may not be used to endorse or promote products derived 1234936Sbostic * from this software without specific prior written permission. 1334936Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434936Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534936Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1622427Sdist */ 1722427Sdist 1813952Ssam #ifndef lint 19*38736Stef static char sccsid[] = "@(#)displayq.c 5.11 (Berkeley) 08/22/89"; 2034203Sbostic #endif /* not lint */ 2113952Ssam 2212113Sralph /* 2312113Sralph * Routines to display the state of the queue. 2412113Sralph */ 2512113Sralph 2612113Sralph #include "lp.h" 2737968Sbostic #include "pathnames.h" 2812113Sralph 2913170Sralph #define JOBCOL 40 /* column for job # in -l format */ 3013170Sralph #define OWNCOL 7 /* start of Owner column in normal */ 3113170Sralph #define SIZCOL 62 /* start of Size column in normal */ 3212113Sralph 3312113Sralph /* 3412113Sralph * Stuff for handling job specifications 3512113Sralph */ 3612113Sralph extern char *user[]; /* users to process */ 3712113Sralph extern int users; /* # of users in user array */ 3812113Sralph extern int requ[]; /* job number of spool entries */ 3912113Sralph extern int requests; /* # of spool requests */ 4012113Sralph 4116758Sralph int lflag; /* long output option */ 4216758Sralph char current[40]; /* current file being printed */ 4316758Sralph int garbage; /* # of garbage cf files */ 4416758Sralph int rank; /* order to be printed (-1=none, 0=active) */ 4516758Sralph long totsize; /* total print job size in bytes */ 4616758Sralph int first; /* first file in ``files'' column? */ 4716758Sralph int col; /* column on screen */ 4816758Sralph char file[132]; /* print file name */ 4912113Sralph 5016758Sralph char *head0 = "Rank Owner Job Files"; 5116758Sralph char *head1 = "Total Size\n"; 5212113Sralph 5312113Sralph /* 5412113Sralph * Display the current state of the queue. Format = 1 if long format. 5512113Sralph */ 5612113Sralph displayq(format) 5712113Sralph int format; 5812113Sralph { 5912113Sralph register struct queue *q; 6012113Sralph register int i, nitems, fd; 6130995Sbostic register char *cp; 6212113Sralph struct queue **queue; 6312113Sralph struct stat statb; 6412113Sralph FILE *fp; 6530995Sbostic char c; 6612113Sralph 6712113Sralph lflag = format; 6812113Sralph totsize = 0; 6912113Sralph rank = -1; 7012113Sralph 7112113Sralph if ((i = pgetent(line, printer)) < 0) 7212113Sralph fatal("cannot open printer description file"); 7312113Sralph else if (i == 0) 7412113Sralph fatal("unknown printer"); 7512113Sralph if ((LP = pgetstr("lp", &bp)) == NULL) 7637968Sbostic LP = _PATH_DEFDEVLP; 7712113Sralph if ((RP = pgetstr("rp", &bp)) == NULL) 7812434Sralph RP = DEFLP; 7912113Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 8037968Sbostic SD = _PATH_DEFSPOOL; 8112113Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 8212113Sralph LO = DEFLOCK; 8312113Sralph if ((ST = pgetstr("st", &bp)) == NULL) 8412113Sralph ST = DEFSTAT; 8512113Sralph RM = pgetstr("rm", &bp); 86*38736Stef if (cp = checkremote()) 87*38736Stef printf("Warning: %s\n", cp); 8812113Sralph 8912113Sralph /* 9030995Sbostic * Print out local queue 9112113Sralph * Find all the control files in the spooling directory 9212113Sralph */ 9312113Sralph if (chdir(SD) < 0) 9412113Sralph fatal("cannot chdir to spooling directory"); 9512113Sralph if ((nitems = getq(&queue)) < 0) 9612113Sralph fatal("cannot examine spooling area\n"); 9716205Sralph if (stat(LO, &statb) >= 0) { 9816205Sralph if (statb.st_mode & 0100) { 9916205Sralph if (sendtorem) 10016205Sralph printf("%s: ", host); 10116205Sralph printf("Warning: %s is down: ", printer); 10216205Sralph fd = open(ST, O_RDONLY); 10316205Sralph if (fd >= 0) { 10416205Sralph (void) flock(fd, LOCK_SH); 10516205Sralph while ((i = read(fd, line, sizeof(line))) > 0) 10616205Sralph (void) fwrite(line, 1, i, stdout); 10716205Sralph (void) close(fd); /* unlocks as well */ 10816205Sralph } else 10916205Sralph putchar('\n'); 11016205Sralph } 11116205Sralph if (statb.st_mode & 010) { 11216205Sralph if (sendtorem) 11316205Sralph printf("%s: ", host); 11416205Sralph printf("Warning: %s queue is turned off\n", printer); 11516205Sralph } 11612740Sralph } 11712113Sralph 11830995Sbostic if (nitems) { 11930995Sbostic fp = fopen(LO, "r"); 12030995Sbostic if (fp == NULL) 12113441Sralph warn(); 12213441Sralph else { 12330995Sbostic register char *cp; 12430995Sbostic 12530995Sbostic /* get daemon pid */ 12613441Sralph cp = current; 12713441Sralph while ((*cp = getc(fp)) != EOF && *cp != '\n') 12813441Sralph cp++; 12913441Sralph *cp = '\0'; 13030995Sbostic i = atoi(current); 13130995Sbostic if (i <= 0 || kill(i, 0) < 0) 13230995Sbostic warn(); 13330995Sbostic else { 13430995Sbostic /* read current file name */ 13530995Sbostic cp = current; 13630995Sbostic while ((*cp = getc(fp)) != EOF && *cp != '\n') 13730995Sbostic cp++; 13830995Sbostic *cp = '\0'; 13930995Sbostic /* 14030995Sbostic * Print the status file. 14130995Sbostic */ 14230995Sbostic if (sendtorem) 14330995Sbostic printf("%s: ", host); 14430995Sbostic fd = open(ST, O_RDONLY); 14530995Sbostic if (fd >= 0) { 14630995Sbostic (void) flock(fd, LOCK_SH); 14730995Sbostic while ((i = read(fd, line, sizeof(line))) > 0) 14830995Sbostic (void) fwrite(line, 1, i, stdout); 14930995Sbostic (void) close(fd); /* unlocks as well */ 15030995Sbostic } else 15130995Sbostic putchar('\n'); 15230995Sbostic } 15330995Sbostic (void) fclose(fp); 15413441Sralph } 15530995Sbostic /* 15630995Sbostic * Now, examine the control files and print out the jobs to 15730995Sbostic * be done for each user. 15830995Sbostic */ 15930995Sbostic if (!lflag) 16030995Sbostic header(); 16130995Sbostic for (i = 0; i < nitems; i++) { 16230995Sbostic q = queue[i]; 16330995Sbostic inform(q->q_name); 16430995Sbostic free(q); 16530995Sbostic } 16630995Sbostic free(queue); 16712113Sralph } 16831678Skarels if (!sendtorem) { 16931678Skarels if (nitems == 0) 17031678Skarels puts("no entries"); 17130995Sbostic return; 17230995Sbostic } 17330995Sbostic 17412113Sralph /* 17530995Sbostic * Print foreign queue 17630995Sbostic * Note that a file in transit may show up in either queue. 17712113Sralph */ 17830995Sbostic if (nitems) 17930995Sbostic putchar('\n'); 18030995Sbostic (void) sprintf(line, "%c%s", format + '\3', RP); 18130995Sbostic cp = line; 18230995Sbostic for (i = 0; i < requests; i++) { 18330995Sbostic cp += strlen(cp); 18430995Sbostic (void) sprintf(cp, " %d", requ[i]); 18512113Sralph } 18630995Sbostic for (i = 0; i < users; i++) { 18730995Sbostic cp += strlen(cp); 18830995Sbostic *cp++ = ' '; 18930995Sbostic (void) strcpy(cp, user[i]); 19030995Sbostic } 19130995Sbostic strcat(line, "\n"); 19230995Sbostic fd = getport(RM); 19330995Sbostic if (fd < 0) { 19430995Sbostic if (from != host) 19530995Sbostic printf("%s: ", host); 19630995Sbostic printf("connection to %s is down\n", RM); 19730995Sbostic } 19830995Sbostic else { 19930995Sbostic i = strlen(line); 20030995Sbostic if (write(fd, line, i) != i) 20130995Sbostic fatal("Lost connection"); 20230995Sbostic while ((i = read(fd, line, sizeof(line))) > 0) 20330995Sbostic (void) fwrite(line, 1, i, stdout); 20430995Sbostic (void) close(fd); 20530995Sbostic } 20612113Sralph } 20712113Sralph 20812113Sralph /* 20913441Sralph * Print a warning message if there is no daemon present. 21013441Sralph */ 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 */ 22212113Sralph header() 22312113Sralph { 22412113Sralph printf(head0); 22512113Sralph col = strlen(head0)+1; 22612113Sralph blankfill(SIZCOL); 22712113Sralph printf(head1); 22812113Sralph } 22912113Sralph 23012113Sralph inform(cf) 23112113Sralph char *cf; 23212113Sralph { 23312113Sralph register int j, k; 23412113Sralph register char *cp; 23512113Sralph FILE *cfp; 23612113Sralph 23712113Sralph /* 23812113Sralph * There's a chance the control file has gone away 23912113Sralph * in the meantime; if this is the case just keep going 24012113Sralph */ 24112113Sralph if ((cfp = fopen(cf, "r")) == NULL) 24212113Sralph return; 24312113Sralph 24412113Sralph if (rank < 0) 24512113Sralph rank = 0; 24612113Sralph if (sendtorem || garbage || strcmp(cf, current)) 24712113Sralph rank++; 24812113Sralph j = 0; 24912113Sralph while (getline(cfp)) { 25012113Sralph switch (line[0]) { 25112113Sralph case 'P': /* Was this file specified in the user's list? */ 25212113Sralph if (!inlist(line+1, cf)) { 25312113Sralph fclose(cfp); 25412113Sralph return; 25512113Sralph } 25612113Sralph if (lflag) { 25712113Sralph printf("\n%s: ", line+1); 25812113Sralph col = strlen(line+1) + 2; 25912113Sralph prank(rank); 26012113Sralph blankfill(JOBCOL); 26112113Sralph printf(" [job %s]\n", cf+3); 26212113Sralph } else { 26312113Sralph col = 0; 26412113Sralph prank(rank); 26512113Sralph blankfill(OWNCOL); 26612113Sralph printf("%-10s %-3d ", line+1, atoi(cf+3)); 26712113Sralph col += 16; 26812113Sralph first = 1; 26912113Sralph } 27012113Sralph continue; 27112113Sralph default: /* some format specifer and file name? */ 27212113Sralph if (line[0] < 'a' || line[0] > 'z') 27312113Sralph continue; 27412113Sralph if (j == 0 || strcmp(file, line+1) != 0) 27530995Sbostic (void) strcpy(file, line+1); 27612113Sralph j++; 27712113Sralph continue; 27812113Sralph case 'N': 27912113Sralph show(line+1, file, j); 28012113Sralph file[0] = '\0'; 28112113Sralph j = 0; 28212113Sralph } 28312113Sralph } 28412113Sralph fclose(cfp); 28512113Sralph if (!lflag) { 28612113Sralph blankfill(SIZCOL); 28734587Sbostic printf("%ld bytes\n", totsize); 28812113Sralph totsize = 0; 28912113Sralph } 29012113Sralph } 29112113Sralph 29212113Sralph inlist(name, file) 29312113Sralph char *name, *file; 29412113Sralph { 29512113Sralph register int *r, n; 29612113Sralph register char **u, *cp; 29712113Sralph 29812113Sralph if (users == 0 && requests == 0) 29912113Sralph return(1); 30012113Sralph /* 30112113Sralph * Check to see if it's in the user list 30212113Sralph */ 30312113Sralph for (u = user; u < &user[users]; u++) 30412113Sralph if (!strcmp(*u, name)) 30512113Sralph return(1); 30612113Sralph /* 30712113Sralph * Check the request list 30812113Sralph */ 30912113Sralph for (n = 0, cp = file+3; isdigit(*cp); ) 31012113Sralph n = n * 10 + (*cp++ - '0'); 31112113Sralph for (r = requ; r < &requ[requests]; r++) 31212113Sralph if (*r == n && !strcmp(cp, from)) 31312113Sralph return(1); 31412113Sralph return(0); 31512113Sralph } 31612113Sralph 31712113Sralph show(nfile, file, copies) 31812113Sralph register char *nfile, *file; 31912113Sralph { 32012113Sralph if (strcmp(nfile, " ") == 0) 32112113Sralph nfile = "(standard input)"; 32212113Sralph if (lflag) 32312113Sralph ldump(nfile, file, copies); 32412113Sralph else 32512113Sralph dump(nfile, file, copies); 32612113Sralph } 32712113Sralph 32812113Sralph /* 32912113Sralph * Fill the line with blanks to the specified column 33012113Sralph */ 33112113Sralph blankfill(n) 33212113Sralph register int n; 33312113Sralph { 33412113Sralph while (col++ < n) 33512113Sralph putchar(' '); 33612113Sralph } 33712113Sralph 33812113Sralph /* 33912113Sralph * Give the abbreviated dump of the file names 34012113Sralph */ 34112113Sralph dump(nfile, file, copies) 34212113Sralph char *nfile, *file; 34312113Sralph { 34412113Sralph register short n, fill; 34512113Sralph struct stat lbuf; 34612113Sralph 34712113Sralph /* 34812113Sralph * Print as many files as will fit 34912113Sralph * (leaving room for the total size) 35012113Sralph */ 35112113Sralph fill = first ? 0 : 2; /* fill space for ``, '' */ 35212113Sralph if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) { 35312113Sralph if (col < SIZCOL) { 35412113Sralph printf(" ..."), col += 4; 35512113Sralph blankfill(SIZCOL); 35612113Sralph } 35712113Sralph } else { 35812113Sralph if (first) 35912113Sralph first = 0; 36012113Sralph else 36112113Sralph printf(", "); 36212113Sralph printf("%s", nfile); 36312113Sralph col += n+fill; 36412113Sralph } 36512113Sralph if (*file && !stat(file, &lbuf)) 36612113Sralph totsize += copies * lbuf.st_size; 36712113Sralph } 36812113Sralph 36912113Sralph /* 37012113Sralph * Print the long info about the file 37112113Sralph */ 37212113Sralph ldump(nfile, file, copies) 37312113Sralph char *nfile, *file; 37412113Sralph { 37512113Sralph struct stat lbuf; 37612113Sralph 37712113Sralph putchar('\t'); 37812113Sralph if (copies > 1) 37912113Sralph printf("%-2d copies of %-19s", copies, nfile); 38012113Sralph else 38112113Sralph printf("%-32s", nfile); 38212113Sralph if (*file && !stat(file, &lbuf)) 38334587Sbostic printf(" %ld bytes", lbuf.st_size); 38412113Sralph else 38512113Sralph printf(" ??? bytes"); 38612113Sralph putchar('\n'); 38712113Sralph } 38812113Sralph 38912113Sralph /* 39012113Sralph * Print the job's rank in the queue, 39112113Sralph * update col for screen management 39212113Sralph */ 39312113Sralph prank(n) 39412113Sralph { 39512113Sralph char line[100]; 39612113Sralph static char *r[] = { 39712113Sralph "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" 39812113Sralph }; 39912113Sralph 40012113Sralph if (n == 0) { 40112113Sralph printf("active"); 40212113Sralph col += 6; 40312113Sralph return; 40412113Sralph } 40538491Sbostic if ((n/10)%10 == 1) 40612113Sralph (void) sprintf(line, "%dth", n); 40712113Sralph else 40812113Sralph (void) sprintf(line, "%d%s", n, r[n%10]); 40912113Sralph col += strlen(line); 41012113Sralph printf("%s", line); 41112113Sralph } 412