]> diplodocus.org Git - nmh/blobdiff - uip/forwsbr.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / forwsbr.c
index 87006620010435ac41b76d6fe34b2303bb94d031..1ae630d5889c9974877c6dd078ae38ddd50e681b 100644 (file)
@@ -1,6 +1,4 @@
-
-/*
- * forwsbr.c -- subroutine to build a draft from a component file
+/* forwsbr.c -- subroutine to build a draft from a component file
  *
  * This code is Copyright (c) 2012, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
 #include <h/fmt_scan.h>
 #include <h/tws.h>
 #include <h/utils.h>
+#include "sbr/m_mktemp.h"
 
 /*
  * Take from replsbr.c - a buffer big enough to read in data header lines
  * in reasonable chunks but not enough to slurp in the whole message
  */
 
-static char msgbuf[256];
-#define COMPFREE(c) if (c->c_text) free(c->c_text)
+static char msgbuf[NMH_BUFSIZ];
+#define COMPFREE(c) free(c->c_text)
 
 /*
  * A list of components we treat as addresses
@@ -48,12 +47,14 @@ build_form (char *form, char *digest, int *dat, char *from, char *to,
     int        in;
     int fmtsize, state;
     int i;
-    register char *nfs;
-    char *line, tmpfil[BUFSIZ], name[NAMESZ], **ap;
+    char *nfs;
+    char tmpfil[BUFSIZ], name[NAMESZ], **ap;
+    charstring_t line;
     FILE *tmp;
-    register struct comp *cptr;
+    struct comp *cptr;
     struct format *fmt;
     char *cp = NULL;
+    m_getfld_state_t gstate;
 
     /*
      * Open the message we'll be scanning for components
@@ -86,9 +87,10 @@ build_form (char *form, char *digest, int *dat, char *from, char *to,
      * these routines?
      */
 
-    for (state = FLD;;) {
+    gstate = m_getfld_state_init(tmp);
+    for (;;) {
        int msg_count = sizeof msgbuf;
-       state = m_getfld(state, name, msgbuf, &msg_count, tmp);
+       state = m_getfld2(&gstate, name, msgbuf, &msg_count);
        switch (state) {
            case FLD:
            case FLDPLUS:
@@ -102,13 +104,14 @@ build_form (char *form, char *digest, int *dat, char *from, char *to,
                if (i != -1) {
                    while (state == FLDPLUS) {
                        msg_count = sizeof msgbuf;
-                       state = m_getfld(state, name, msgbuf, &msg_count, tmp);
+                       state = m_getfld2(&gstate, name, msgbuf, &msg_count);
                        fmt_appendcomp(i, name, msgbuf);
                    }
                }
-               while (state == FLDPLUS)
+               while (state == FLDPLUS) {
                    msg_count = sizeof msgbuf;
-                   state = m_getfld(state, name, msgbuf, &msg_count, tmp);
+                   state = m_getfld2(&gstate, name, msgbuf, &msg_count);
+               }
                break;
 
            case LENERR:
@@ -118,7 +121,7 @@ build_form (char *form, char *digest, int *dat, char *from, char *to,
                goto finished;
 
            default:
-               adios(NULL, "m_getfld() returned %d", state);
+               adios(NULL, "m_getfld2() returned %d", state);
        }
     }
 
@@ -133,6 +136,7 @@ build_form (char *form, char *digest, int *dat, char *from, char *to,
      */
 
 finished:
+    m_getfld_state_destroy (&gstate);
 
     cptr = fmt_findcomp ("digest");
     if (cptr) {
@@ -171,20 +175,22 @@ finished:
     }
 
     cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
-    if (cp == NULL) adios("forw", "unable to create temporary file");
+    if (cp == NULL) {
+       adios(NULL, "unable to create temporary file in %s", get_temp_dir());
+    }
     strncpy (tmpfil, cp, sizeof(tmpfil));
-    unlink (tmpfil);
+    (void) m_unlink (tmpfil);
     if ((in = dup (fileno (tmp))) == NOTOK)
        adios ("dup", "unable to");
 
-    line = mh_xmalloc ((unsigned) fmtsize);
-    fmt_scan (fmt, line, fmtsize - 1, fmtsize, dat);
-    fputs (line, tmp);
-    free (line);
+    line = charstring_create (fmtsize);
+    fmt_scan (fmt, line, fmtsize, dat, NULL);
+    fputs (charstring_buffer (line), tmp);
+    charstring_free (line);
     if (fclose (tmp))
        adios (tmpfil, "error writing");
 
-    lseek (in, (off_t) 0, SEEK_SET);
+    lseek(in, 0, SEEK_SET);
 
     /*
      * Free any component buffers that we allocated