386BSD 0.1 development
[unix-history] / usr / src / usr.bin / mail / send.c
index c59e193..06588b5 100644 (file)
@@ -2,21 +2,37 @@
  * Copyright (c) 1980 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1980 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.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)send.c     5.18 (Berkeley) %G%";
+static char sccsid[] = "@(#)send.c     5.23 (Berkeley) 2/9/91";
 #endif /* not lint */
 
 #include "rcv.h"
 #endif /* not lint */
 
 #include "rcv.h"
@@ -47,7 +63,18 @@ send(mp, obuf, doign, prefix)
        register char *cp, *cp2;
        register int c;
        int length;
        register char *cp, *cp2;
        register int c;
        int length;
+       int prefixlen;
 
 
+       /*
+        * Compute the prefix string, without trailing whitespace
+        */
+       if (prefix != NOSTR) {
+               cp2 = 0;
+               for (cp = prefix; *cp; cp++)
+                       if (*cp != ' ' && *cp != '\t')
+                               cp2 = cp;
+               prefixlen = cp2 == 0 ? 0 : cp2 - prefix + 1;
+       }
        ibuf = setinput(mp);
        count = mp->m_size;
        ishead = 1;
        ibuf = setinput(mp);
        count = mp->m_size;
        ishead = 1;
@@ -139,8 +166,16 @@ send(mp, obuf, doign, prefix)
                        }
                }
                if (!ignoring) {
                        }
                }
                if (!ignoring) {
-                       if (prefix != NOSTR && length > 1)
-                               fputs(prefix, obuf);
+                       /*
+                        * Strip trailing whitespace from prefix
+                        * if line is blank.
+                        */
+                       if (prefix != NOSTR)
+                               if (length > 1)
+                                       fputs(prefix, obuf);
+                               else
+                                       (void) fwrite(prefix, sizeof *prefix,
+                                                       prefixlen, obuf);
                        (void) fwrite(line, sizeof *line, length, obuf);
                        if (ferror(obuf))
                                return -1;
                        (void) fwrite(line, sizeof *line, length, obuf);
                        if (ferror(obuf))
                                return -1;
@@ -158,8 +193,15 @@ send(mp, obuf, doign, prefix)
                                break;
                        }
                        count -= c = strlen(line);
                                break;
                        }
                        count -= c = strlen(line);
+                       /*
+                        * Strip trailing whitespace from prefix
+                        * if line is blank.
+                        */
                        if (c > 1)
                                fputs(prefix, obuf);
                        if (c > 1)
                                fputs(prefix, obuf);
+                       else
+                               (void) fwrite(prefix, sizeof *prefix,
+                                               prefixlen, obuf);
                        (void) fwrite(line, sizeof *line, c, obuf);
                        if (ferror(obuf))
                                return -1;
                        (void) fwrite(line, sizeof *line, c, obuf);
                        if (ferror(obuf))
                                return -1;
@@ -323,10 +365,10 @@ mail1(hp, printheaders)
                if (access(_PATH_MAIL_LOG, 0) == 0) {
                        FILE *postage;
 
                if (access(_PATH_MAIL_LOG, 0) == 0) {
                        FILE *postage;
 
-                       if ((postage = fopen(_PATH_MAIL_LOG, "a")) != NULL) {
+                       if ((postage = Fopen(_PATH_MAIL_LOG, "a")) != NULL) {
                                fprintf(postage, "%s %d %ld\n", myname,
                                    count(to), fsize(mtf));
                                fprintf(postage, "%s %d %ld\n", myname,
                                    count(to), fsize(mtf));
-                               (void) fclose(postage);
+                               (void) Fclose(postage);
                        }
                }
                prepare_child(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT)|
                        }
                }
                prepare_child(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT)|
@@ -345,7 +387,7 @@ mail1(hp, printheaders)
        else
                free_child(pid);
 out:
        else
                free_child(pid);
 out:
-       (void) fclose(mtf);
+       (void) Fclose(mtf);
 }
 
 /*
 }
 
 /*
@@ -386,16 +428,16 @@ infix(hp, fi)
        register FILE *nfo, *nfi;
        register int c;
 
        register FILE *nfo, *nfi;
        register int c;
 
-       if ((nfo = fopen(tempMail, "w")) == NULL) {
+       if ((nfo = Fopen(tempMail, "w")) == NULL) {
                perror(tempMail);
                return(fi);
        }
                perror(tempMail);
                return(fi);
        }
-       if ((nfi = fopen(tempMail, "r")) == NULL) {
+       if ((nfi = Fopen(tempMail, "r")) == NULL) {
                perror(tempMail);
                perror(tempMail);
-               (void) fclose(nfo);
+               (void) Fclose(nfo);
                return(fi);
        }
                return(fi);
        }
-       (void) remove(tempMail);
+       (void) rm(tempMail);
        (void) puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA);
        c = getc(fi);
        while (c != EOF) {
        (void) puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA);
        c = getc(fi);
        while (c != EOF) {
@@ -410,13 +452,13 @@ infix(hp, fi)
        (void) fflush(nfo);
        if (ferror(nfo)) {
                perror(tempMail);
        (void) fflush(nfo);
        if (ferror(nfo)) {
                perror(tempMail);
-               (void) fclose(nfo);
-               (void) fclose(nfi);
+               (void) Fclose(nfo);
+               (void) Fclose(nfi);
                rewind(fi);
                return(fi);
        }
                rewind(fi);
                return(fi);
        }
-       (void) fclose(nfo);
-       (void) fclose(fi);
+       (void) Fclose(nfo);
+       (void) Fclose(fi);
        rewind(nfi);
        return(nfi);
 }
        rewind(nfi);
        return(nfi);
 }
@@ -493,7 +535,7 @@ savemail(name, fi)
        time_t now, time();
        char *ctime();
 
        time_t now, time();
        char *ctime();
 
-       if ((fo = fopen(name, "a")) == NULL) {
+       if ((fo = Fopen(name, "a")) == NULL) {
                perror(name);
                return (-1);
        }
                perror(name);
                return (-1);
        }
@@ -505,7 +547,7 @@ savemail(name, fi)
        (void) fflush(fo);
        if (ferror(fo))
                perror(name);
        (void) fflush(fo);
        if (ferror(fo))
                perror(name);
-       (void) fclose(fo);
+       (void) Fclose(fo);
        rewind(fi);
        return (0);
 }
        rewind(fi);
        return (0);
 }