]> diplodocus.org Git - nmh/blobdiff - uip/whom.c
vector.c: Move interface to own file.
[nmh] / uip / whom.c
index b1e8b1ed026e60506923bb643873ac98e4abdfca..d7bd2cacd32e1ba28f009aa041866d8c1b8efd6f 100644 (file)
@@ -1,14 +1,20 @@
-
-/*
- * 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
  * complete copyright information.
  */
 
-#include <h/mh.h>
-#include <h/signals.h>
+#include "h/mh.h"
+#include "sbr/path.h"
+#include "sbr/print_version.h"
+#include "sbr/print_help.h"
+#include "sbr/error.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)
@@ -57,13 +63,14 @@ 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];
 
-    if (nmh_init(argv[0], 1)) { return 1; }
+    if (nmh_init(argv[0], true, false)) { return 1; }
 
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
@@ -71,7 +78,13 @@ main (int argc, char **argv)
     vec[vecp++] = invo_name;
     vec[vecp++] = "-whom";
     vec[vecp++] = "-library";
-    vec[vecp++] = getcpy (m_maildir (""));
+    vec[vecp++] = mh_xstrdup(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. */
@@ -83,7 +96,7 @@ main (int argc, char **argv)
                    ambigsw (cp, switches);
                    done (1);
                case UNKWNSW: 
-                   adios (NULL, "-%s unknown", cp);
+                   die("-%s unknown", cp);
 
                case HELPSW: 
                    snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
@@ -109,17 +122,17 @@ main (int argc, char **argv)
 
                case DFOLDSW: 
                    if (dfolder)
-                       adios (NULL, "only one draft folder at a time!");
+                       die("only one draft folder at a time!");
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
                            *cp != '@' ? TFOLDER : TSUBCWF);
                    continue;
                case DMSGSW: 
                    if (dmsg)
-                       adios (NULL, "only one draft message at a time!");
+                       die("only one draft message at a time!");
                    if (!(dmsg = *argp++) || *dmsg == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    continue;
                case NDFLDSW: 
                    dfolder = NULL;
@@ -135,72 +148,64 @@ main (int argc, char **argv)
                case MTSSW:
                    vec[vecp++] = --cp;
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    vec[vecp++] = cp;
                    continue;
            }
        }
        if (msg)
-           adios (NULL, "only one draft at a time!");
-       else
-           vec[vecp++] = msg = cp;
+           die("only one draft at a time!");
+        vec[vecp++] = msg = cp;
     }
 
     /* allow Aliasfile: profile entry */
     if ((cp = context_find ("Aliasfile"))) {
        char *dp = NULL;
 
-       for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
+       for (ap = brkstring(dp = mh_xstrdup(cp), " ", "\n"); ap && *ap; ap++) {
            vec[vecp++] = "-alias";
            vec[vecp++] = *ap;
        }
     }
 
     if (msg == NULL) {
-           cp  = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
+           cp  = mh_xstrdup(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...");
-       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);
 }