common code for checking if rm==localhost
[unix-history] / usr / src / usr.sbin / lpr / common_source / displayq.c
index ce86140..e08c274 100644 (file)
@@ -1,9 +1,30 @@
-/*     displayq.c      4.7     83/06/17        */
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)displayq.c 5.11 (Berkeley) %G%";
+#endif /* not lint */
+
 /*
  * Routines to display the state of the queue.
  */
 
 #include "lp.h"
 /*
  * Routines to display the state of the queue.
  */
 
 #include "lp.h"
+#include "pathnames.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 */
@@ -17,18 +38,17 @@ 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 */
 
-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 */
+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 */
+char   file[132];      /* print file name */
 
 
-static char    *head0 = "Rank   Owner      Job  Files";
-static char    *head1 = "Total Size\n";
+char   *head0 = "Rank   Owner      Job  Files";
+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.
@@ -38,9 +58,11 @@ displayq(format)
 {
        register struct queue *q;
        register int i, nitems, fd;
 {
        register struct queue *q;
        register int i, nitems, fd;
+       register char   *cp;
        struct queue **queue;
        struct stat statb;
        FILE *fp;
        struct queue **queue;
        struct stat statb;
        FILE *fp;
+       char c;
 
        lflag = format;
        totsize = 0;
 
        lflag = format;
        totsize = 0;
@@ -51,124 +73,152 @@ displayq(format)
        else if (i == 0)
                fatal("unknown printer");
        if ((LP = pgetstr("lp", &bp)) == NULL)
        else if (i == 0)
                fatal("unknown printer");
        if ((LP = pgetstr("lp", &bp)) == NULL)
-               LP = DEFDEVLP;
+               LP = _PATH_DEFDEVLP;
        if ((RP = pgetstr("rp", &bp)) == NULL)
                RP = DEFLP;
        if ((SD = pgetstr("sd", &bp)) == NULL)
        if ((RP = pgetstr("rp", &bp)) == NULL)
                RP = DEFLP;
        if ((SD = pgetstr("sd", &bp)) == NULL)
-               SD = DEFSPOOL;
+               SD = _PATH_DEFSPOOL;
        if ((LO = pgetstr("lo", &bp)) == NULL)
                LO = DEFLOCK;
        if ((ST = pgetstr("st", &bp)) == NULL)
                ST = DEFSTAT;
        RM = pgetstr("rm", &bp);
        if ((LO = pgetstr("lo", &bp)) == NULL)
                LO = DEFLOCK;
        if ((ST = pgetstr("st", &bp)) == NULL)
                ST = DEFSTAT;
        RM = pgetstr("rm", &bp);
+       if (cp = checkremote())
+               printf("Warning: %s\n", cp);
 
        /*
 
        /*
-        * If there is no local printer, then print the queue on
-        * the remote machine and then what's in the queue here.
-        * Note that a file in transit may not show up in either queue.
-        */
-       if (*LP == '\0') {
-               register char *cp;
-               char c;
-
-               sendtorem++;
-               (void) sprintf(line, "%c%s", format + '\3', RP);
-               cp = line;
-               for (i = 0; i < requests; i++) {
-                       cp += strlen(cp);
-                       (void) sprintf(cp, " %d", requ[i]);
-               }
-               for (i = 0; i < users; i++) {
-                       cp += strlen(cp);
-                       *cp++ = ' ';
-                       strcpy(cp, user[i]);
-               }
-               strcat(line, "\n");
-               fd = getport(RM);
-               if (fd < 0) {
-                       if (from != host)
-                               printf("%s: ", host);
-                       printf("connection to %s is down\n", RM);
-               } else {
-                       i = strlen(line);
-                       if (write(fd, line, i) != i)
-                               fatal("Lost connection");
-                       while ((i = read(fd, line, sizeof(line))) > 0)
-                               (void) fwrite(line, 1, i, stdout);
-                       (void) close(fd);
-               }
-       }
-       /*
+        * Print out local queue
         * Find all the control files in the spooling directory
         */
        if (chdir(SD) < 0)
                fatal("cannot chdir to spooling directory");
        if ((nitems = getq(&queue)) < 0)
                fatal("cannot examine spooling area\n");
         * Find all the control files in the spooling directory
         */
        if (chdir(SD) < 0)
                fatal("cannot chdir to spooling directory");
        if ((nitems = getq(&queue)) < 0)
                fatal("cannot examine spooling area\n");
-       if (stat(LO, &statb) >= 0 && (statb.st_mode & 010)) {
-               if (sendtorem)
-                       printf("\n%s: ", host);
-               printf("Warning: %s queue is turned off\n", printer);
-       }
-       if (nitems == 0) {
-               if (!sendtorem)
-                       printf("no entries\n");
-               return(0);
+       if (stat(LO, &statb) >= 0) {
+               if (statb.st_mode & 0100) {
+                       if (sendtorem)
+                               printf("%s: ", host);
+                       printf("Warning: %s is down: ", printer);
+                       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');
+               }
+               if (statb.st_mode & 010) {
+                       if (sendtorem)
+                               printf("%s: ", host);
+                       printf("Warning: %s queue is turned off\n", printer);
+               }
        }
        }
-       fp = fopen(LO, "r");
-       if (fp == NULL || flock(fileno(fp), LOCK_SH|LOCK_NB) == 0) {
-               if (fp != NULL)
-                       fclose(fp);     /* unlocks as well */
-               garbage = nitems;
-               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");
-       } else {
-               register char *cp = current;
 
 
-               /* skip daemon pid */
-               while ((*cp = getc(fp)) != EOF && *cp != '\n');
-               /* read current file name */
-               while ((*cp = getc(fp)) != EOF && *cp != '\n')
-                       cp++;
-               *cp = '\0';
-               fclose(fp);
+       if (nitems) {
+               fp = fopen(LO, "r");
+               if (fp == NULL)
+                       warn();
+               else {
+                       register char *cp;
+
+                       /* get daemon pid */
+                       cp = current;
+                       while ((*cp = getc(fp)) != EOF && *cp != '\n')
+                               cp++;
+                       *cp = '\0';
+                       i = atoi(current);
+                       if (i <= 0 || 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("%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);
+               }
                /*
                /*
-                * Print the status file to show what the daemon is doing.
+                * Now, examine the control files and print out the jobs to
+                * be done for each user.
                 */
                 */
-               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');
+               if (!lflag)
+                       header();
+               for (i = 0; i < nitems; i++) {
+                       q = queue[i];
+                       inform(q->q_name);
+                       free(q);
+               }
+               free(queue);
+       }
+       if (!sendtorem) {
+               if (nitems == 0)
+                       puts("no entries");
+               return;
        }
        }
+
        /*
        /*
-        * Now, examine the control files and print out the jobs to
-        * be done for each user.
+        * Print foreign queue
+        * Note that a file in transit may show up in either queue.
         */
         */
-       if (!lflag)
-               header();
-       for (i = 0; i < nitems; i++) {
-               q = queue[i];
-               inform(q->q_name);
-               free(q);
+       if (nitems)
+               putchar('\n');
+       (void) sprintf(line, "%c%s", format + '\3', RP);
+       cp = line;
+       for (i = 0; i < requests; i++) {
+               cp += strlen(cp);
+               (void) sprintf(cp, " %d", requ[i]);
        }
        }
-       free(queue);
-       return(nitems-garbage);
+       for (i = 0; i < users; i++) {
+               cp += strlen(cp);
+               *cp++ = ' ';
+               (void) strcpy(cp, user[i]);
+       }
+       strcat(line, "\n");
+       fd = getport(RM);
+       if (fd < 0) {
+               if (from != host)
+                       printf("%s: ", host);
+               printf("connection to %s is down\n", RM);
+       }
+       else {
+               i = strlen(line);
+               if (write(fd, line, i) != i)
+                       fatal("Lost connection");
+               while ((i = read(fd, line, sizeof(line))) > 0)
+                       (void) fwrite(line, 1, i, stdout);
+               (void) close(fd);
+       }
+}
+
+/*
+ * Print a warning message if there is no daemon present.
+ */
+warn()
+{
+       if (sendtorem)
+               printf("\n%s: ", host);
+       puts("Warning: no daemon present");
+       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);
@@ -177,7 +227,6 @@ header()
        printf(head1);
 }
 
        printf(head1);
 }
 
-static
 inform(cf)
        char *cf;
 {
 inform(cf)
        char *cf;
 {
@@ -223,7 +272,7 @@ inform(cf)
                        if (line[0] < 'a' || line[0] > 'z')
                                continue;
                        if (j == 0 || strcmp(file, line+1) != 0)
                        if (line[0] < 'a' || line[0] > 'z')
                                continue;
                        if (j == 0 || strcmp(file, line+1) != 0)
-                               strcpy(file, line+1);
+                               (void) strcpy(file, line+1);
                        j++;
                        continue;
                case 'N':
                        j++;
                        continue;
                case 'N':
@@ -235,12 +284,11 @@ inform(cf)
        fclose(cfp);
        if (!lflag) {
                blankfill(SIZCOL);
        fclose(cfp);
        if (!lflag) {
                blankfill(SIZCOL);
-               printf("%D bytes\n", totsize);
+               printf("%ld bytes\n", totsize);
                totsize = 0;
        }
 }
 
                totsize = 0;
        }
 }
 
-static
 inlist(name, file)
        char *name, *file;
 {
 inlist(name, file)
        char *name, *file;
 {
@@ -266,7 +314,6 @@ inlist(name, file)
        return(0);
 }
 
        return(0);
 }
 
-static
 show(nfile, file, copies)
        register char *nfile, *file;
 {
 show(nfile, file, copies)
        register char *nfile, *file;
 {
@@ -281,7 +328,6 @@ 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;
 {
@@ -292,7 +338,6 @@ 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;
 {
@@ -324,7 +369,6 @@ 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;
 {
@@ -336,7 +380,7 @@ ldump(nfile, file, copies)
        else
                printf("%-32s", nfile);
        if (*file && !stat(file, &lbuf))
        else
                printf("%-32s", nfile);
        if (*file && !stat(file, &lbuf))
-               printf(" %D bytes", lbuf.st_size);
+               printf(" %ld bytes", lbuf.st_size);
        else
                printf(" ??? bytes");
        putchar('\n');
        else
                printf(" ??? bytes");
        putchar('\n');
@@ -346,7 +390,6 @@ 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];
@@ -359,7 +402,7 @@ prank(n)
                col += 6;
                return;
        }
                col += 6;
                return;
        }
-       if ((n/10) == 1)
+       if ((n/10)%10 == 1)
                (void) sprintf(line, "%dth", n);
        else
                (void) sprintf(line, "%d%s", n, r[n%10]);
                (void) sprintf(line, "%dth", n);
        else
                (void) sprintf(line, "%d%s", n, r[n%10]);