consistency checking
[unix-history] / usr / src / usr.sbin / lpr / lpd / printjob.c
index 0cded7b..314e36d 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)printjob.c 4.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)printjob.c 4.15 (Berkeley) %G%";
 #endif
 
 /*
 #endif
 
 /*
@@ -24,10 +24,12 @@ static int  prchild;                /* id of pr process */
 static int     child;                  /* id of any filters */
 static int     ofilter;                /* id of output filter, if any */
 static int     tof;                    /* true if at top of form */
 static int     child;                  /* id of any filters */
 static int     ofilter;                /* id of output filter, if any */
 static int     tof;                    /* true if at top of form */
+static int     count;                  /* Number of files actually printed */
 static int     remote;                 /* true if sending files to remote */
 
 static int     remote;                 /* true if sending files to remote */
 
+static char    fromhost[32];           /* user's host machine */
 static char    logname[32];            /* user's login name */
 static char    logname[32];            /* user's login name */
-static char    jobname[32];            /* job or file name */
+static char    jobname[100];           /* job or file name */
 static char    class[32];              /* classification field */
 static char    width[10] = "-w";       /* page width in characters */
 static char    length[10] = "-l";      /* page length in lines */
 static char    class[32];              /* classification field */
 static char    width[10] = "-w";       /* page width in characters */
 static char    length[10] = "-l";      /* page length in lines */
@@ -53,7 +55,10 @@ printjob()
        dup(1);
        pid = getpid();                         /* for use with lprm */
        setpgrp(0, pid);
        dup(1);
        pid = getpid();                         /* for use with lprm */
        setpgrp(0, pid);
+       signal(SIGHUP, onintr);
        signal(SIGINT, onintr);
        signal(SIGINT, onintr);
+       signal(SIGQUIT, onintr);
+       signal(SIGTERM, onintr);
 
        /*
         * uses short form file names
 
        /*
         * uses short form file names
@@ -64,7 +69,7 @@ printjob()
        }
        if (stat(LO, &stb) == 0 && (stb.st_mode & 0100))
                exit(0);                /* printing disabled */
        }
        if (stat(LO, &stb) == 0 && (stb.st_mode & 0100))
                exit(0);                /* printing disabled */
-       lfd = open(LO, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+       lfd = open(LO, O_WRONLY|O_CREAT, 0644);
        if (lfd < 0) {
                log("cannot create %s", LO);
                exit(1);
        if (lfd < 0) {
                log("cannot create %s", LO);
                exit(1);
@@ -134,10 +139,9 @@ again:
                                break;
                        }
                }
                                break;
                        }
                }
-               /*
-                * Check to see if we should try reprinting the job.
-                */
-               if (i > 0) {
+               if (i == 0)             /* file ok and printed */
+                       count++;
+               else if (i > 0) {       /* try reprinting the job */
                        log("restarting");
                        if (ofilter > 0) {
                                kill(ofilter, SIGCONT); /* to be sure */
                        log("restarting");
                        if (ofilter > 0) {
                                kill(ofilter, SIGCONT); /* to be sure */
@@ -164,10 +168,12 @@ again:
        }
        if (nitems == 0) {              /* no more work to do */
        done:
        }
        if (nitems == 0) {              /* no more work to do */
        done:
-               if (!SF && !tof)
-                       (void) write(ofd, FF, strlen(FF));
-               if (TR != NULL)         /* output trailer */
-                       (void) write(ofd, TR, strlen(TR));
+               if (count > 0) {        /* Files actually printed */
+                       if (!SF && !tof)
+                               (void) write(ofd, FF, strlen(FF));
+                       if (TR != NULL)         /* output trailer */
+                               (void) write(ofd, TR, strlen(TR));
+               }
                exit(0);
        }
        goto again;
                exit(0);
        }
        goto again;
@@ -248,13 +254,13 @@ printit(file)
        while (getline(cfp))
                switch (line[0]) {
                case 'H':
        while (getline(cfp))
                switch (line[0]) {
                case 'H':
-                       strcpy(host, line+1);
+                       strcpy(fromhost, line+1);
                        if (class[0] == '\0')
                        if (class[0] == '\0')
-                               strcpy(class, line+1);
+                               strncpy(class, line+1, sizeof(class)-1);
                        continue;
 
                case 'P':
                        continue;
 
                case 'P':
-                       strcpy(logname, line+1);
+                       strncpy(logname, line+1, sizeof(logname)-1);
                        if (RS) {                       /* restricted */
                                if (getpwnam(logname) == (struct passwd *)0) {
                                        bombed = 2;
                        if (RS) {                       /* restricted */
                                if (getpwnam(logname) == (struct passwd *)0) {
                                        bombed = 2;
@@ -266,20 +272,20 @@ printit(file)
 
                case 'J':
                        if (line[1] != '\0')
 
                case 'J':
                        if (line[1] != '\0')
-                               strcpy(jobname, line+1);
+                               strncpy(jobname, line+1, sizeof(jobname)-1);
                        else
                                strcpy(jobname, " ");
                        continue;
 
                case 'C':
                        if (line[1] != '\0')
                        else
                                strcpy(jobname, " ");
                        continue;
 
                case 'C':
                        if (line[1] != '\0')
-                               strcpy(class, line+1);
+                               strncpy(class, line+1, sizeof(class)-1);
                        else if (class[0] == '\0')
                                gethostname(class, sizeof (class));
                        continue;
 
                case 'T':       /* header title for pr */
                        else if (class[0] == '\0')
                                gethostname(class, sizeof (class));
                        continue;
 
                case 'T':       /* header title for pr */
-                       strcpy(title, line+1);
+                       strncpy(title, line+1, sizeof(title)-1);
                        continue;
 
                case 'L':       /* identification line */
                        continue;
 
                case 'L':       /* identification line */
@@ -296,11 +302,11 @@ printit(file)
                        continue;
 
                case 'W':       /* page width */
                        continue;
 
                case 'W':       /* page width */
-                       strcpy(width+2, line+1);
+                       strncpy(width+2, line+1, sizeof(width)-3);
                        continue;
 
                case 'I':       /* indent amount */
                        continue;
 
                case 'I':       /* indent amount */
-                       strcpy(indent+2, line+1);
+                       strncpy(indent+2, line+1, sizeof(indent)-3);
                        continue;
 
                default:        /* some file to print */
                        continue;
 
                default:        /* some file to print */
@@ -337,7 +343,7 @@ pass2:
         */
        (void) fclose(cfp);
        (void) unlink(file);
         */
        (void) fclose(cfp);
        (void) unlink(file);
-       return(0);
+       return(bombed ? -1 : 0);
 }
 
 /*
 }
 
 /*
@@ -417,7 +423,7 @@ print(format, file)
                break;
        case 'l':       /* like 'f' but pass control characters */
                prog = IF;
                break;
        case 'l':       /* like 'f' but pass control characters */
                prog = IF;
-               av[1] = "-l";
+               av[1] = "-c";
                av[2] = width;
                av[3] = length;
                av[4] = indent;
                av[2] = width;
                av[3] = length;
                av[4] = indent;
@@ -480,7 +486,7 @@ print(format, file)
        av[n++] = "-n";
        av[n++] = logname;
        av[n++] = "-h";
        av[n++] = "-n";
        av[n++] = logname;
        av[n++] = "-h";
-       av[n++] = host;
+       av[n++] = fromhost;
        av[n++] = AF;
        av[n] = 0;
        fo = pfd;
        av[n++] = AF;
        av[n] = 0;
        fo = pfd;
@@ -806,12 +812,12 @@ sendmail(bombed)
                        cp++;
                else
                        cp = MAIL;
                        cp++;
                else
                        cp = MAIL;
-               sprintf(buf, "%s@%s", line+1, host);
+               sprintf(buf, "%s@%s", line+1, fromhost);
                execl(MAIL, cp, buf, 0);
                exit(0);
        } else if (stat > 0) {                          /* parent */
                dup2(p[1], 1);
                execl(MAIL, cp, buf, 0);
                exit(0);
        } else if (stat > 0) {                          /* parent */
                dup2(p[1], 1);
-               printf("To: %s\n", line+1);
+               printf("To: %s@%s\n", line+1, fromhost);
                printf("Subject: printer job\n\n");
                printf("Your printer job ");
                if (*jobname)
                printf("Subject: printer job\n\n");
                printf("Your printer job ");
                if (*jobname)
@@ -872,7 +878,7 @@ dofork(action)
 }
 
 /*
 }
 
 /*
- * Cleanup child processes when a SIGINT is caught.
+ * Cleanup child processes when a signal is caught.
  */
 static
 onintr()
  */
 static
 onintr()