]> diplodocus.org Git - nmh/blobdiff - uip/whom.c
inc/test-eom-align: Create test mboxes in less steps.
[nmh] / uip / whom.c
index 4a3f81dca97f23da7cbc8ec0fc44b84fb79793f1..76f434e4d500e4f6960e1fc7c2870f4ad4065f8e 100644 (file)
@@ -1,6 +1,4 @@
-
-/*
- * whom.c -- report to whom a message would be sent
+/* whom.c -- report to whom a message would be sent
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
@@ -8,8 +6,11 @@
  */
 
 #include <h/mh.h>
+#include "h/done.h"
 #include <h/utils.h>
 #include <h/signals.h>
+#include "sbr/m_maildir.h"
+#include "sbr/m_mktemp.h"
 
 #ifndef CYRUS_SASL
 # define SASLminc(a) (a)
@@ -58,8 +59,9 @@ int
 main (int argc, char **argv)
 {
     pid_t child_id = OK;
-    int i, status, isdf = 0;
-    int distsw = 0, vecp = 0;
+    int status, isdf = 0;
+    int vecp = 0;
+    bool distsw;
     char *cp, *dfolder = NULL, *dmsg = NULL;
     char *msg = NULL, **ap, **argp, backup[BUFSIZ];
     char buf[BUFSIZ], **arguments, *vec[MAXARGS];
@@ -74,6 +76,12 @@ main (int argc, char **argv)
     vec[vecp++] = "-library";
     vec[vecp++] = getcpy (m_maildir (""));
 
+    if ((cp = context_find ("credentials"))) {
+       /* post doesn't read context so need to pass credentials. */
+       vec[vecp++] = "-credentials";
+       vec[vecp++] = cp;
+    }
+
     /* Don't need to feed fileproc or mhlproc to post because
        it doesn't use them when used for whom. */
 
@@ -143,8 +151,7 @@ main (int argc, char **argv)
        }
        if (msg)
            adios (NULL, "only one draft at a time!");
-       else
-           vec[vecp++] = msg = cp;
+        vec[vecp++] = msg = cp;
     }
 
     /* allow Aliasfile: profile entry */
@@ -161,48 +168,40 @@ main (int argc, char **argv)
            cp  = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
        msg = vec[vecp++] = cp;
     }
-    if ((cp = getenv ("mhdist"))
-           && *cp
-           && (distsw = atoi (cp))
-           && (cp = getenv ("mhaltmsg"))
-           && *cp) {
+
+    distsw = (cp = getenv("mhdist")) && *cp && atoi(cp) &&
+        (cp = getenv("mhaltmsg")) && *cp;
+    if (distsw) {
        if (distout (msg, cp, backup) == NOTOK)
            done (1);
        vec[vecp++] = "-dist";
-       distsw++;
     }
     vec[vecp] = NULL;
 
     closefds (3);
 
     if (distsw) {
-       for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
-           sleep (5);
-    }
-
-    switch (distsw ? child_id : OK) {
-       case NOTOK:
-           advise (NULL, "unable to fork, so checking directly...");
-           /* FALLTHRU */
-       case OK:
-           execvp (postproc, vec);
-           fprintf (stderr, "unable to exec ");
-           perror (postproc);
-           _exit (-1);
-
-       default:
-           SIGNAL (SIGHUP, SIG_IGN);
-           SIGNAL (SIGINT, SIG_IGN);
-           SIGNAL (SIGQUIT, SIG_IGN);
-           SIGNAL (SIGTERM, SIG_IGN);
-
-           status = pidwait(child_id, OK);
-
-           (void) m_unlink (msg);
-           if (rename (backup, msg) == NOTOK)
-               adios (msg, "unable to rename %s to", backup);
-           done (status);
+        if ((child_id = fork()) == -1)
+            adios("fork", "failed:");
+
+        if (child_id) {
+            SIGNAL (SIGHUP, SIG_IGN);
+            SIGNAL (SIGINT, SIG_IGN);
+            SIGNAL (SIGQUIT, SIG_IGN);
+            SIGNAL (SIGTERM, SIG_IGN);
+
+            status = pidwait(child_id, OK);
+
+            (void) m_unlink (msg);
+            if (rename (backup, msg) == NOTOK)
+                adios (msg, "unable to rename %s to", backup);
+            done (status);
+        }
     }
 
-    return 0;  /* dead code to satisfy the compiler */
+    /* Either the child, or no fork occurred. */
+    execvp (postproc, vec);
+    fprintf (stderr, "unable to exec ");
+    perror (postproc);
+    _exit(1);
 }