changed to use unix domain ipc.
[unix-history] / usr / src / usr.sbin / lpr / common_source / displayq.c
index 514c718..7bf2d4d 100644 (file)
@@ -1,12 +1,13 @@
-/*     displayq.c      4.1     83/04/29        */
+/*     displayq.c      4.8     83/06/29        */
 /*
  * Routines to display the state of the queue.
  */
 
 #include "lp.h"
 
 /*
  * Routines to display the state of the queue.
  */
 
 #include "lp.h"
 
-#define        JOBCOL          40      /* column for job # in -l format */
-#define OWNCOL         7       /* start of Owner column in normal */
+#define JOBCOL 40              /* column for job # in -l format */
+#define OWNCOL 7               /* start of Owner column in normal */
+#define SIZCOL 62              /* start of Size column in normal */
 
 /*
  * Stuff for handling job specifications
 
 /*
  * Stuff for handling job specifications
@@ -16,19 +17,18 @@ extern int  users;          /* # of users in user array */
 extern int     requ[];         /* job number of spool entries */
 extern int     requests;       /* # of spool requests */
 
 extern int     requ[];         /* job number of spool entries */
 extern int     requests;       /* # of spool requests */
 
-int    lflag;                  /* long output option */
-char   current[40];            /* current file being printed */
-int    garbage;                /* # of garbage cf files */
-int    rank;                   /* order to be printed (-1=none, 0=active) */
-long   totsize;                /* total print job size in bytes */
-int    first;                  /* first file in ``files'' column? */
-int    col;                    /* column on screen */
-int    SIZCOL = 62;            /* start of Size column in normal */
-int    sendtorem;              /* are we sending to a remote? */
-char   file[132];              /* print file name */
+static int     lflag;          /* long output option */
+static char    current[40];    /* current file being printed */
+static int     garbage;        /* # of garbage cf files */
+static int     rank;           /* order to be printed (-1=none, 0=active) */
+static long    totsize;        /* total print job size in bytes */
+static int     first;          /* first file in ``files'' column? */
+static int     col;            /* column on screen */
+static int     sendtorem;      /* are we sending to a remote? */
+static char    file[132];      /* print file name */
 
 
-char   *head0 = "Rank   Owner      Job  Files";
-char   *head1 = "Total Size\n";
+static char    *head0 = "Rank   Owner      Job  Files";
+static char    *head1 = "Total Size\n";
 
 /*
  * Display the current state of the queue. Format = 1 if long format.
 
 /*
  * Display the current state of the queue. Format = 1 if long format.
@@ -42,7 +42,6 @@ displayq(format)
        struct stat statb;
        FILE *fp;
 
        struct stat statb;
        FILE *fp;
 
-       name = "displayq";
        lflag = format;
        totsize = 0;
        rank = -1;
        lflag = format;
        totsize = 0;
        rank = -1;
@@ -54,7 +53,7 @@ displayq(format)
        if ((LP = pgetstr("lp", &bp)) == NULL)
                LP = DEFDEVLP;
        if ((RP = pgetstr("rp", &bp)) == NULL)
        if ((LP = pgetstr("lp", &bp)) == NULL)
                LP = DEFDEVLP;
        if ((RP = pgetstr("rp", &bp)) == NULL)
-               RP = printer;
+               RP = DEFLP;
        if ((SD = pgetstr("sd", &bp)) == NULL)
                SD = DEFSPOOL;
        if ((LO = pgetstr("lo", &bp)) == NULL)
        if ((SD = pgetstr("sd", &bp)) == NULL)
                SD = DEFSPOOL;
        if ((LO = pgetstr("lo", &bp)) == NULL)
@@ -85,7 +84,7 @@ displayq(format)
                        strcpy(cp, user[i]);
                }
                strcat(line, "\n");
                        strcpy(cp, user[i]);
                }
                strcat(line, "\n");
-               fd = getport();
+               fd = getport(RM);
                if (fd < 0) {
                        if (from != host)
                                printf("%s: ", host);
                if (fd < 0) {
                        if (from != host)
                                printf("%s: ", host);
@@ -106,44 +105,56 @@ displayq(format)
                fatal("cannot chdir to spooling directory");
        if ((nitems = getq(&queue)) < 0)
                fatal("cannot examine spooling area\n");
                fatal("cannot chdir to spooling directory");
        if ((nitems = getq(&queue)) < 0)
                fatal("cannot examine spooling area\n");
-       if (nitems == 0) {
+       if (stat(LO, &statb) >= 0 && (statb.st_mode & 010)) {
                if (sendtorem)
                        printf("\n%s: ", host);
                if (sendtorem)
                        printf("\n%s: ", host);
-               printf("no entries\n");
+               printf("Warning: %s queue is turned off\n", printer);
+       }
+       if (nitems == 0) {
+               if (!sendtorem)
+                       printf("no entries\n");
                return(0);
        }
        fp = fopen(LO, "r");
                return(0);
        }
        fp = fopen(LO, "r");
-       if (fp == NULL || flock(fileno(fp), FSHLOCK|FNBLOCK) == 0) {
-               if (fp != NULL)
-                       fclose(fp);
-               garbage = nitems;
-               if (*LP && stat(LP, &statb) >= 0 && (statb.st_mode & 0777) == 0)
-                       status("Warning: %s is down", printer);
-               else
-                       status("Warning: no daemon present");
-       } else {
-               register char *cp = current;
+       if (fp == NULL)
+               warn();
+       else {
+               register char *cp;
 
 
-               /* skip daemon pid */
-               while ((*cp = getc(fp)) != EOF && *cp != '\n');
-               /* read current file name */
+               /* get daemon pid */
+               cp = current;
                while ((*cp = getc(fp)) != EOF && *cp != '\n')
                        cp++;
                *cp = '\0';
                while ((*cp = getc(fp)) != EOF && *cp != '\n')
                        cp++;
                *cp = '\0';
-               fclose(fp);
+               i = atoi(current);
+               if (kill(i, 0) < 0)
+                       warn();
+               else {
+                       /* read current file name */
+                       cp = current;
+                       while ((*cp = getc(fp)) != EOF && *cp != '\n')
+                               cp++;
+                       *cp = '\0';
+                       /*
+                        * Print the status file.
+                        */
+                       if (sendtorem)
+                               printf("\n%s: ", host);
+                       fd = open(ST, O_RDONLY);
+                       if (fd >= 0) {
+                               (void) flock(fd, LOCK_SH);
+                               while ((i = read(fd, line, sizeof(line))) > 0)
+                                       (void) fwrite(line, 1, i, stdout);
+                               (void) close(fd);       /* unlocks as well */
+                       } else
+                               putchar('\n');
+               }
+               (void) fclose(fp);
        }
        /*
         * Now, examine the control files and print out the jobs to
         * be done for each user.
         */
        }
        /*
         * Now, examine the control files and print out the jobs to
         * be done for each user.
         */
-       if (sendtorem)
-               printf("\n%s: ", host);
-       if ((fd = open(ST, FRDONLY|FSHLOCK)) >= 0) {
-               while ((i = read(fd, line, sizeof(line))) > 0)
-                       (void) fwrite(line, 1, i, stdout);
-               (void) close(fd);
-       } else
-               putchar('\n');
        if (!lflag)
                header();
        for (i = 0; i < nitems; i++) {
        if (!lflag)
                header();
        for (i = 0; i < nitems; i++) {
@@ -155,9 +166,26 @@ displayq(format)
        return(nitems-garbage);
 }
 
        return(nitems-garbage);
 }
 
+/*
+ * Print a warning message if there is no daemon present.
+ */
+warn()
+{
+       struct stat statb;
+
+       if (sendtorem)
+               printf("\n%s: ", host);
+       if (stat(LO, &statb) >= 0 && (statb.st_mode & 0100))
+               printf("Warning: %s is down\n", printer);
+       else
+               printf("Warning: no daemon present\n");
+       current[0] = '\0';
+}
+
 /*
  * Print the header for the short listing format
  */
 /*
  * Print the header for the short listing format
  */
+static
 header()
 {
        printf(head0);
 header()
 {
        printf(head0);
@@ -166,6 +194,7 @@ header()
        printf(head1);
 }
 
        printf(head1);
 }
 
+static
 inform(cf)
        char *cf;
 {
 inform(cf)
        char *cf;
 {
@@ -228,6 +257,7 @@ inform(cf)
        }
 }
 
        }
 }
 
+static
 inlist(name, file)
        char *name, *file;
 {
 inlist(name, file)
        char *name, *file;
 {
@@ -253,6 +283,7 @@ inlist(name, file)
        return(0);
 }
 
        return(0);
 }
 
+static
 show(nfile, file, copies)
        register char *nfile, *file;
 {
 show(nfile, file, copies)
        register char *nfile, *file;
 {
@@ -267,6 +298,7 @@ show(nfile, file, copies)
 /*
  * Fill the line with blanks to the specified column
  */
 /*
  * Fill the line with blanks to the specified column
  */
+static
 blankfill(n)
        register int n;
 {
 blankfill(n)
        register int n;
 {
@@ -277,6 +309,7 @@ blankfill(n)
 /*
  * Give the abbreviated dump of the file names
  */
 /*
  * Give the abbreviated dump of the file names
  */
+static
 dump(nfile, file, copies)
        char *nfile, *file;
 {
 dump(nfile, file, copies)
        char *nfile, *file;
 {
@@ -308,6 +341,7 @@ dump(nfile, file, copies)
 /*
  * Print the long info about the file
  */
 /*
  * Print the long info about the file
  */
+static
 ldump(nfile, file, copies)
        char *nfile, *file;
 {
 ldump(nfile, file, copies)
        char *nfile, *file;
 {
@@ -329,6 +363,7 @@ ldump(nfile, file, copies)
  * Print the job's rank in the queue,
  *   update col for screen management
  */
  * Print the job's rank in the queue,
  *   update col for screen management
  */
+static
 prank(n)
 {
        char line[100];
 prank(n)
 {
        char line[100];