Efficiency changes -- replace inline loops on getc() with calls
[unix-history] / usr / src / usr.bin / mail / send.c
index f19b4e3..69fb345 100644 (file)
@@ -1,10 +1,19 @@
-#
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char *sccsid = "@(#)send.c      5.2 (Berkeley) %G%";
+#endif not lint
 
 #include "rcv.h"
 #ifdef VMUNIX
 
 #include "rcv.h"
 #ifdef VMUNIX
-#include <wait.h>
+#include <sys/wait.h>
 #endif
 #include <ctype.h>
 #endif
 #include <ctype.h>
+#include <sys/stat.h>
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -12,8 +21,6 @@
  * Mail to others.
  */
 
  * Mail to others.
  */
 
-static char *SccsId = "@(#)send.c      2.6 %G%";
-
 /*
  * Send message described by the passed pointer to the
  * passed output buffer.  Return -1 on error, but normally
 /*
  * Send message described by the passed pointer to the
  * passed output buffer.  Return -1 on error, but normally
@@ -68,11 +75,14 @@ send(mailp, obuf, doign)
                                goto writeit;
                        }
                        /*
                                goto writeit;
                        }
                        /*
-                        * If this line is a continuation
-                        * of a previous header field, just echo it.
+                        * If this line is a continuation (via space or tab)
+                        * of a previous header field, just echo it
+                        * (unless the field should be ignored).
                         */
                         */
-                       if (isspace(line[0]) && infld)
+                       if (infld && (isspace(line[0]) || line[0] == '\t')) {
+                               if (doign && isign(field)) continue;
                                goto writeit;
                                goto writeit;
+                       }
                        infld = 0;
                        /*
                         * If we are no longer looking at real
                        infld = 0;
                        /*
                         * If we are no longer looking at real
@@ -236,6 +246,7 @@ mail1(hp)
        int pid, i, s, p, gotcha;
        char **namelist, *deliver;
        struct name *to, *np;
        int pid, i, s, p, gotcha;
        char **namelist, *deliver;
        struct name *to, *np;
+       struct stat sbuf;
        FILE *mtf, *postage;
        int remote = rflag != NOSTR || rmail;
        char **t;
        FILE *mtf, *postage;
        int remote = rflag != NOSTR || rmail;
        char **t;
@@ -251,15 +262,11 @@ mail1(hp)
        hp->h_seq = 1;
        if (hp->h_subject == NOSTR)
                hp->h_subject = sflag;
        hp->h_seq = 1;
        if (hp->h_subject == NOSTR)
                hp->h_subject = sflag;
-       if (fsize(mtf) == 0 && hp->h_subject == NOSTR) {
-               printf("No message !?!\n");
-               goto out;
-       }
        if (intty && value("askcc") != NOSTR)
                grabh(hp, GCC);
        else if (intty) {
                printf("EOT\n");
        if (intty && value("askcc") != NOSTR)
                grabh(hp, GCC);
        else if (intty) {
                printf("EOT\n");
-               flush();
+               fflush(stdout);
        }
 
        /*
        }
 
        /*
@@ -307,6 +314,9 @@ topdog:
        if (hp->h_seq > 0 && !remote) {
                fixhead(hp, to);
                if (fsize(mtf) == 0)
        if (hp->h_seq > 0 && !remote) {
                fixhead(hp, to);
                if (fsize(mtf) == 0)
+                   if (hp->h_subject == NOSTR)
+                       printf("No message, no subject; hope that's ok\n");
+                   else
                        printf("Null message body; hope that's ok\n");
                if ((mtf = infix(hp, mtf)) == NULL) {
                        fprintf(stderr, ". . . message lost, sorry.\n");
                        printf("Null message body; hope that's ok\n");
                if ((mtf = infix(hp, mtf)) == NULL) {
                        fprintf(stderr, ". . . message lost, sorry.\n");
@@ -337,7 +347,7 @@ topdog:
 #ifdef pdp11
        while (wait2(&s, WNOHANG) > 0)
 #endif
 #ifdef pdp11
        while (wait2(&s, WNOHANG) > 0)
 #endif
-#ifdef vax
+#if defined(vax) || defined(sun)
        while (wait3(&s, WNOHANG, 0) > 0)
 #endif
                ;
        while (wait3(&s, WNOHANG, 0) > 0)
 #endif
                ;
@@ -363,11 +373,12 @@ topdog:
 #endif
                for (i = SIGHUP; i <= SIGQUIT; i++)
                        sigset(i, SIG_IGN);
 #endif
                for (i = SIGHUP; i <= SIGQUIT; i++)
                        sigset(i, SIG_IGN);
-               if ((postage = fopen(POSTAGE, "a")) != NULL) {
-                       fprintf(postage, "%s %d %d\n", myname,
-                           count(to), fsize(mtf));
-                       fclose(postage);
-               }
+               if (!stat(POSTAGE, &sbuf))
+                       if ((postage = fopen(POSTAGE, "a")) != NULL) {
+                               fprintf(postage, "%s %d %d\n", myname,
+                                   count(to), fsize(mtf));
+                               fclose(postage);
+                       }
                s = fileno(mtf);
                for (i = 3; i < 15; i++)
                        if (i != s)
                s = fileno(mtf);
                for (i = 3; i < 15; i++)
                        if (i != s)
@@ -378,18 +389,18 @@ topdog:
 #ifdef CC
                submit(getpid());
 #endif CC
 #ifdef CC
                submit(getpid());
 #endif CC
-#ifdef DELIVERMAIL
+#ifdef SENDMAIL
                if ((deliver = value("sendmail")) == NOSTR)
                if ((deliver = value("sendmail")) == NOSTR)
-                       deliver = DELIVERMAIL;
+                       deliver = SENDMAIL;
                execv(deliver, namelist);
                execv(deliver, namelist);
-#endif DELIVERMAIL
+#endif SENDMAIL
                execv(MAIL, namelist);
                perror(MAIL);
                exit(1);
        }
 
 out:
                execv(MAIL, namelist);
                perror(MAIL);
                exit(1);
        }
 
 out:
-       if (remote) {
+       if (remote || (value("verbose") != NOSTR)) {
                while ((p = wait(&s)) != pid && p != -1)
                        ;
                if (s != 0)
                while ((p = wait(&s)) != pid && p != -1)
                        ;
                if (s != 0)
@@ -488,13 +499,13 @@ puthead(hp, fo, w)
 
        gotcha = 0;
        if (hp->h_to != NOSTR && w & GTO)
 
        gotcha = 0;
        if (hp->h_to != NOSTR && w & GTO)
-               fprintf(fo, "To: "), fmt(hp->h_to, fo), gotcha++;
+               fmt("To: ", hp->h_to, fo), gotcha++;
        if (hp->h_subject != NOSTR && w & GSUBJECT)
                fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
        if (hp->h_cc != NOSTR && w & GCC)
        if (hp->h_subject != NOSTR && w & GSUBJECT)
                fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
        if (hp->h_cc != NOSTR && w & GCC)
-               fprintf(fo, "Cc: "), fmt(hp->h_cc, fo), gotcha++;
+               fmt("Cc: ", hp->h_cc, fo), gotcha++;
        if (hp->h_bcc != NOSTR && w & GBCC)
        if (hp->h_bcc != NOSTR && w & GBCC)
-               fprintf(fo, "Bcc: "), fmt(hp->h_bcc, fo), gotcha++;
+               fmt("Bcc: ", hp->h_bcc, fo), gotcha++;
        if (gotcha && w & GNL)
                putc('\n', fo);
        return(0);
        if (gotcha && w & GNL)
                putc('\n', fo);
        return(0);
@@ -504,26 +515,44 @@ puthead(hp, fo, w)
  * Format the given text to not exceed 72 characters.
  */
 
  * Format the given text to not exceed 72 characters.
  */
 
-fmt(str, fo)
-       register char *str;
+fmt(str, txt, fo)
+       register char *str, *txt;
        register FILE *fo;
 {
        register int col;
        register FILE *fo;
 {
        register int col;
-       register char *cp;
-
-       cp = str;
-       col = 0;
-       while (*cp) {
-               if (*cp == ' ' && col > 65) {
-                       fprintf(fo, "\n    ");
+       register char *bg, *bl, *pt, ch;
+
+       col = strlen(str);
+       if (col)
+               fprintf(fo, "%s", str);
+       pt = bg = txt;
+       bl = 0;
+       while (*bg) {
+               pt++;
+               if (++col >72) {
+                       if (!bl) {
+                               bl = bg;
+                               while (*bl && !isspace(*bl))
+                                       bl++;
+                       }
+                       if (!*bl)
+                               goto finish;
+                       ch = *bl;
+                       *bl = '\0';
+                       fprintf(fo, "%s\n    ", bg);
                        col = 4;
                        col = 4;
-                       cp++;
-                       continue;
+                       *bl = ch;
+                       pt = bg = ++bl;
+                       bl = 0;
+               }
+               if (!*pt) {
+finish:
+                       fprintf(fo, "%s\n", bg);
+                       return;
                }
                }
-               putc(*cp++, fo);
-               col++;
+               if (isspace(*pt))
+                       bl = pt;
        }
        }
-       putc('\n', fo);
 }
 
 /*
 }
 
 /*