Harald Geyer: back out fork/vfork workaround and handle the issue directly
[mmh] / uip / replsbr.c
index 57eb6cd..0b0ba6e 100644 (file)
@@ -3,12 +3,17 @@
  * replsbr.c -- routines to help repl along...
  *
  * $Id$
+ *
+ * This code is Copyright (c) 2002, by the authors of nmh.  See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
  */
 
 #include <h/mh.h>
 #include <h/addrsbr.h>
 #include <h/fmt_scan.h>
 #include <sys/file.h>          /* L_SET */
+#include <errno.h>
 
 extern short ccto;             /* from repl.c */
 extern short cccc;
@@ -72,14 +77,16 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
     register char **nxtbuf;
     register char **ap;
     register struct comp **savecomp;
-    int        char_read = 0, format_len;
+    int        char_read = 0, format_len, mask;
     char name[NAMESZ], *scanl, *cp;
     FILE *out;
 
-    umask(~m_gmprot());
+    mask = umask(~m_gmprot());
     if ((out = fopen (drft, "w")) == NULL)
        adios (drft, "unable to create");
 
+    umask(mask);
+
     /* get new format string */
     cp = new_fs (form, NULL, NULL);
     format_len = strlen (cp);
@@ -160,13 +167,14 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
                                tmpbuf = *nxtbuf++;
                            } else {
                                i = strlen (cp = cptr->c_text) - 1;
-                               if (cp[i] == '\n')
+                               if (cp[i] == '\n') {
                                    if (cptr->c_type & CT_ADDR) {
                                        cp[i] = '\0';
                                        cp = add (",\n\t", cp);
                                    } else {
                                        cp = add ("\t", cp);
                                    }
+                               }
                                cptr->c_text = add (tmpbuf, cp);
                            }
                            while (state == FLDPLUS) {
@@ -240,19 +248,24 @@ finished:
      * or add mhn directives
      */
     if (filter) {
+       fflush(out);
+       if (ferror (out))
+           adios (drft, "error writing");
+       
        replfilter (inb, out, filter);
     } else if (mime && mp) {
            fprintf (out, "#forw [original message] +%s %s\n",
                     mp->foldpath, m_name (mp->lowsel));
     }
 
+    fflush(out);
     if (ferror (out))
        adios (drft, "error writing");
     fclose (out);
 
     /* return dynamically allocated buffers */
     free (scanl);
-    for (nxtbuf = compbuffers, i = ncomps; cptr = *savecomp++; nxtbuf++, i--)
+    for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
        free (cptr->c_text);    /* if not nxtbuf, nxtbuf already freed */
     while ( i-- > 0)
         free (*nxtbuf++);      /* free unused nxtbufs */
@@ -334,7 +347,7 @@ formataddr (char *orig, char *str)
     }
 
     /* concatenate all the new addresses onto 'buf' */
-    for (isgroup = 0; cp = getname (str); ) {
+    for (isgroup = 0; (cp = getname (str)); ) {
        if ((mp = getm (cp, dfhost, dftype, AD_NAME, error)) == NULL) {
            snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
            badaddrs = add (baddr, badaddrs);
@@ -405,6 +418,8 @@ insert (struct mailname *np)
 
 /*
  * Call the mhlproc
+ *
+ * This function expects that argument out has been fflushed by the caller.
  */
 
 static void
@@ -412,6 +427,7 @@ replfilter (FILE *in, FILE *out, char *filter)
 {
     int        pid;
     char *mhl;
+    char *errstr;
 
     if (filter == NULL)
        return;
@@ -423,7 +439,6 @@ replfilter (FILE *in, FILE *out, char *filter)
 
     rewind (in);
     lseek (fileno(in), (off_t) 0, SEEK_SET);
-    fflush (out);
 
     switch (pid = vfork ()) {
        case NOTOK: 
@@ -435,8 +450,12 @@ replfilter (FILE *in, FILE *out, char *filter)
            closefds (3);
 
            execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL);
-           fprintf (stderr, "unable to exec ");
-           perror (mhlproc);
+           errstr = strerror(errno);
+           write(2, "unable to exec ", 15);
+           write(2, mhlproc, strlen(mhlproc));
+           write(2, ": ", 2);
+           write(2, errstr, strlen(errstr));
+           write(2, "\n", 1);
            _exit (-1);
 
        default: