]> diplodocus.org Git - nmh/blobdiff - uip/forw.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / forw.c
index 45fb740f771c1276e2a6d151cbaca6794ee2b4f5..ce8a4fccd8a583f53a27687e449ffe139431d4c1 100644 (file)
@@ -1,6 +1,4 @@
-
-/*
- * forw.c -- forward a message, or group of messages.
+/* forw.c -- forward a message, or group of messages.
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
@@ -11,6 +9,7 @@
 #include <fcntl.h>
 #include <h/tws.h>
 #include <h/utils.h>
+#include "sbr/m_maildir.h"
 
 
 #define        IFORMAT "digest-issue-%s"
@@ -41,7 +40,7 @@
     X("nodashstuffing", 0, NBITSTUFFSW) \
     X("version", 0, VERSIONSW) \
     X("help", 0, HELPSW) \
-    X("file file", 4, FILESW)                 /* interface from msh */ \
+    X("file file", 4, FILESW) \
     X("build", 5, BILDSW)                     /* interface from mhe */ \
     X("from address", 0, FROMSW) \
     X("to address", 0, TOSW) \
@@ -112,20 +111,13 @@ main (int argc, char **argv)
     char *dmsg = NULL, *digest = NULL, *ed = NULL;
     char *file = NULL, *filter = NULL, *folder = NULL, *fwdmsg = NULL;
     char *from = NULL, *to = NULL, *cc = NULL, *subject = NULL, *fcc = NULL;
-    char *form = NULL, buf[BUFSIZ], value[10];
+    char *form = NULL, buf[BUFSIZ];
     char **argp, **arguments;
     struct stat st;
     struct msgs_array msgs = { 0, 0, NULL };
-
     int buildsw = 0;
 
-#ifdef LOCALE
-    setlocale(LC_ALL, "");
-#endif
-    invo_name = r1bindex (argv[0], '/');
-
-    /* read user profile/context */
-    context_read();
+    if (nmh_init(argv[0], 1)) { return 1; }
 
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
@@ -170,7 +162,8 @@ main (int argc, char **argv)
                    nwhat = 0;
                    continue;
                case BILDSW:
-                   buildsw++;  /* fall... */
+                   buildsw++;
+                   /* FALLTHRU */
                case NWHATSW: 
                    nwhat++;
                    continue;
@@ -218,7 +211,7 @@ main (int argc, char **argv)
                case DGSTSW: 
                    if (!(cp = *argp++) || *cp == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
-                   digest = getcpy(cp);
+                   digest = mh_xstrdup(cp);
                    mime = 0;
                    continue;
                case ISSUESW:
@@ -254,10 +247,10 @@ main (int argc, char **argv)
                    continue;
 
                case BITSTUFFSW: 
-                   dashstuff = 1;      /* trinary logic */
+                   dashstuff = 1;      /* ternary logic */
                    continue;
                case NBITSTUFFSW: 
-                   dashstuff = -1;     /* trinary logic */
+                   dashstuff = -1;     /* ternary logic */
                    continue;
 
                case FROMSW:
@@ -283,7 +276,7 @@ main (int argc, char **argv)
                case SUBJECTSW:
                    if (!(cp = *argp++) || *cp == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
-                   subject = getcpy(cp);
+                   subject = mh_xstrdup(cp);
                    continue;
 
                case WIDTHSW:
@@ -297,14 +290,13 @@ main (int argc, char **argv)
        if (*cp == '+' || *cp == '@') {
            if (folder)
                adios (NULL, "only one folder at a time!");
-           else
-               folder = pluspath (cp);
+            folder = pluspath (cp);
        } else {
            app_msgarg(&msgs, cp);
        }
     }
 
-    cwd = getcpy (pwd ());
+    cwd = mh_xstrdup(pwd ());
 
     if (!context_find ("path"))
        free (path ("./", TFOLDER));
@@ -320,7 +312,8 @@ try_it_again:
     if (!buildsw && stat (drft, &st) != NOTOK) {
        printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
        for (i = LISTDSW; i != YESW;) {
-           if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
+           if (!(argp = read_switch_multiword ("\nDisposition? ",
+                                               isdf ? aqrnl : aqrl)))
                done (1);
            switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
                case NOSW: 
@@ -338,7 +331,7 @@ try_it_again:
                        i = YESW;
                    break;
                default: 
-                   advise (NULL, "say what?");
+                   inform("say what?");
                    break;
            }
        }
@@ -384,7 +377,7 @@ try_it_again:
 
        for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
            if (is_selected (mp, msgnum)) {
-               fwdmsg = strdup(m_name(msgnum));
+               fwdmsg = mh_xstrdup(m_name(msgnum));
                break;
            }
 
@@ -460,11 +453,9 @@ try_it_again:
 
        if (digest) {
            snprintf (buf, sizeof(buf), IFORMAT, digest);
-           snprintf (value, sizeof(value), "%d", issue);
-           context_replace (buf, getcpy (value));
+           context_replace (buf, mh_xstrdup(m_str(issue)));
            snprintf (buf, sizeof(buf), VFORMAT, digest);
-           snprintf (value, sizeof(value), "%d", volume);
-           context_replace (buf, getcpy (value));
+           context_replace (buf, mh_xstrdup(m_str(volume)));
        }
 
        context_replace (pfolder, folder);      /* update current folder   */
@@ -493,7 +484,7 @@ mhl_draft (int out, char *digest, int volume, int issue,
             char *file, char *filter, int dashstuff)
 {
     pid_t child_id;
-    int i, msgnum, pd[2];
+    int msgnum, pd[2];
     char buf1[BUFSIZ];
     char buf2[BUFSIZ];
     char *program;
@@ -504,9 +495,7 @@ mhl_draft (int out, char *digest, int volume, int issue,
 
     argsplit_msgarg(&vec, mhlproc, &program);
 
-    for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
-       sleep (5);
-
+    child_id = fork();
     switch (child_id) {
        case NOTOK: 
            adios ("fork", "unable to");
@@ -516,7 +505,6 @@ mhl_draft (int out, char *digest, int volume, int issue,
            dup2 (pd[1], 1);
            close (pd[1]);
 
-           i = 1;
            app_msgarg(&vec, "-forwall");
            app_msgarg(&vec, "-form");
            app_msgarg(&vec, filter);
@@ -544,7 +532,7 @@ mhl_draft (int out, char *digest, int volume, int issue,
 
            for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
                if (is_selected (mp, msgnum))
-                   app_msgarg(&vec, getcpy (m_name (msgnum)));
+                   app_msgarg(&vec, mh_xstrdup(m_name (msgnum)));
            }
 
            app_msgarg(&vec, NULL);
@@ -575,7 +563,7 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst
 {
     int fd,i, msgcnt, msgnum;
     int len, buflen;
-    register char *bp, *msgnam;
+    char *bp, *msgnam;
     char buffer[BUFSIZ];
 
     msgcnt = 1;
@@ -596,7 +584,7 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst
 
                if (msgnum == mp->lowsel) {
                    snprintf (bp, buflen, " Forwarded Message%s",
-                       mp->numsel > 1 ? "s" : "");
+                       PLURALS(mp->numsel));
                } else {
                    snprintf (bp, buflen, " Message %d", msgcnt);
                }
@@ -606,7 +594,9 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst
 
                strncpy (bp, "\n\n", buflen);
            }
-           write (out, buffer, strlen (buffer));
+           if (write (out, buffer, strlen (buffer)) < 0) {
+               advise (drft, "write");
+           }
 
            if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
                admonish (msgnam, "unable to read message");
@@ -631,9 +621,11 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst
        strncpy (buffer, delim4, sizeof(buffer));
     } else {
        snprintf (buffer, sizeof(buffer), "\n------- End of Forwarded Message%s\n",
-               mp->numsel > 1 ? "s" : "");
+               PLURALS(mp->numsel));
+    }
+    if (write (out, buffer, strlen (buffer)) < 0) {
+       advise (drft, "write");
     }
-    write (out, buffer, strlen (buffer));
 
     if (digest) {
        snprintf (buffer, sizeof(buffer), "End of %s Digest [Volume %d Issue %d]\n",
@@ -643,7 +635,9 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst
            *bp++ = '*';
        *bp++ = '\n';
        *bp = 0;
-       write (out, buffer, strlen (buffer));
+       if (write (out, buffer, strlen (buffer)) < 0) {
+           advise (drft, "write");
+       }
     }
 }
 
@@ -659,12 +653,18 @@ copy_mime_draft (int out)
     char buffer[BUFSIZ];
 
     snprintf (buffer, sizeof(buffer), "#forw [forwarded message%s] +%s",
-       mp->numsel == 1 ? "" : "s", mp->foldpath);
-    write (out, buffer, strlen (buffer));
+       PLURALS(mp->numsel), mp->foldpath);
+    if (write (out, buffer, strlen (buffer)) < 0) {
+       advise (drft, "write");
+    }
     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
        if (is_selected (mp, msgnum)) {
            snprintf (buffer, sizeof(buffer), " %s", m_name (msgnum));
-           write (out, buffer, strlen (buffer));
+           if (write (out, buffer, strlen (buffer)) < 0) {
+               advise (drft, "write");
+           }
        }
-    write (out, "\n", 1);
+    if (write (out, "\n", 1) < 0) {
+       advise (drft, "write newline");
+    }
 }