]> diplodocus.org Git - nmh/blobdiff - uip/mhoutsbr.c
lkopen_dot() would block forever if another process held a lock
[nmh] / uip / mhoutsbr.c
index d3a0a85b656ae8da038bf66cd4723fe06ee0c6cd..9f024e08592c87708dc8af0bdf9a20050096c9f2 100644 (file)
@@ -12,8 +12,6 @@
 #include <fcntl.h>
 #include <h/signals.h>
 #include <h/md5.h>
-#include <errno.h>
-#include <signal.h>
 #include <h/mts.h>
 #include <h/tws.h>
 #include <h/mime.h>
@@ -83,14 +81,10 @@ output_content (CT ct, FILE *out)
 {
     int result = 0;
     CI ci = &ct->c_ctinfo;
-    char *boundary = ci->ci_values[0], **ap, **vp;
+    char *boundary = "", *cp;
 
-    for (ap = ci->ci_attrs, vp = ci->ci_values; *ap; ++ap, ++vp) {
-        if (! mh_strcasecmp ("boundary", *ap)) {
-            boundary = *vp;
-            break;
-        }
-    }
+    if ((cp = get_param(ci->ci_first_pm, "boundary", '-', 0)))
+       boundary = cp;
 
     /*
      * Output all header fields for this content
@@ -102,8 +96,11 @@ output_content (CT ct, FILE *out)
      * "message/external", then we are done with the
      * headers (since it has no body).
      */
-    if (ct->c_ctexbody)
+    if (ct->c_ctexbody) {
+       if (boundary && *boundary != '\0')
+           free(boundary);
        return OK;
+    }
 
     /*
      * Now output the content bodies.
@@ -118,14 +115,26 @@ output_content (CT ct, FILE *out)
            putc ('\n', out);
 
        m = (struct multipart *) ct->c_ctparams;
+
+        if (m->mp_content_before) {
+           fprintf (out, "%s", m->mp_content_before);
+        }
+
        for (part = m->mp_parts; part; part = part->mp_next) {
            CT p = part->mp_part;
 
            fprintf (out, "\n--%s\n", boundary);
-           if (output_content (p, out) == NOTOK)
+           if (output_content (p, out) == NOTOK) {
+               if (boundary && *boundary != '\0')
+                   free(boundary);
                return NOTOK;
+           }
        }
        fprintf (out, "\n--%s--\n", boundary);
+
+        if (m->mp_content_after) {
+           fprintf (out, "%s", m->mp_content_after);
+        }
     }
     break;
 
@@ -152,7 +161,14 @@ output_content (CT ct, FILE *out)
     default:
        switch (ct->c_encoding) {
        case CE_7BIT:
-           putc ('\n', out);
+           /* Special case:  if this is a non-MIME message with no
+              body, don't emit the newline that would appear between
+              the headers and body.  In that case, the call to
+              write8Bit() shouldn't be needed, but is harmless. */
+           if (ct->c_ctinfo.ci_first_pm != NULL  ||
+               ct->c_begin != ct->c_end) {
+               putc ('\n', out);
+           }
            result = write8Bit (ct, out);
            break;
 
@@ -184,6 +200,9 @@ output_content (CT ct, FILE *out)
        break;
     }
 
+    if (boundary && *boundary != '\0')
+       free(boundary);
+
     return result;
 }
 
@@ -212,7 +231,7 @@ output_headers (CT ct, FILE *out)
 static int
 writeExternalBody (CT ct, FILE *out)
 {
-    char **ap, **ep, *cp;
+    char *cp;
     struct exbody *e = (struct exbody *) ct->c_ctparams;
                
     putc ('\n', out);
@@ -232,17 +251,22 @@ writeExternalBody (CT ct, FILE *out)
                continue;
 
            case 'N':
-               for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
-                   if (!mh_strcasecmp (*ap, "name")) {
-                       fprintf (out, "%s", *ep);
-                       break;
-                   }
+               cp = get_param(ci2->ci_first_pm, "name", '_', 0);
+               if (cp) {
+                   fputs (cp, out);
+                   free (cp);
+               }
                continue;
 
            case 'T':
                fprintf (out, "%s/%s", ci2->ci_type, ci2->ci_subtype);
-               for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
-                   fprintf (out, "; %s=\"%s\"", *ap, *ep);
+               cp = output_params(strlen(ci2->ci_type) +
+                                  strlen(ci2->ci_subtype) + 1,
+                                  ci2->ci_first_pm, NULL, 0);
+               if (cp) {
+                   fputs (cp, out);
+                   free (cp);
+               }
                continue;
 
            case 'n':
@@ -282,17 +306,18 @@ static int
 write8Bit (CT ct, FILE *out)
 {
     int fd;
+    size_t inbytes;
     char c, *file, buffer[BUFSIZ];
-    CE ce = ct->c_cefile;
+    CE ce = &ct->c_cefile;
 
     file = NULL;
     if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
        return NOTOK;
 
     c = '\n';
-    while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
-       c = buffer[strlen (buffer) - 1];
-       fputs (buffer, out);
+    while ((inbytes = fread (buffer, 1, sizeof buffer, ce->ce_fp)) > 0) {
+        c = buffer[inbytes - 1];
+        fwrite (buffer, 1, inbytes, out);
     }
     if (c != '\n')
        putc ('\n', out);
@@ -311,15 +336,15 @@ writeQuoted (CT ct, FILE *out)
 {
     int fd;
     char *cp, *file;
-    char c, buffer[BUFSIZ];
-    CE ce = ct->c_cefile;
+    char c = '\0', buffer[BUFSIZ];
+    CE ce = &ct->c_cefile;
+    int n = 0;
 
     file = NULL;
     if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
        return NOTOK;
 
     while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
-       int n;
 
        cp = buffer + strlen (buffer) - 1;
        if ((c = *cp) == '\n')
@@ -327,10 +352,9 @@ writeQuoted (CT ct, FILE *out)
 
        if (strncmp (cp = buffer, "From ", sizeof("From ") - 1) == 0) {
            fprintf (out, "=%02X", *cp++ & 0xff);
-           n = 3;
-       } else {
-           n = 0;
+           n += 3;
        }
+
        for (; *cp; cp++) {
            if (n > CPERLIN - 3) {
                fputs ("=\n", out);
@@ -364,11 +388,13 @@ three_print:
                fputs ("=\n", out);
 
            putc ('\n', out);
-       } else {
-           fputs ("=\n", out);
+           n = 0;
        }
     }
 
+    if (c != '\n')
+       putc ('\n', out);
+
     (*ct->c_ceclosefnx) (ct);
     return OK;
 }
@@ -383,13 +409,13 @@ writeBase64ct (CT ct, FILE *out)
 {
     int        fd, result;
     char *file;
-    CE ce = ct->c_cefile;
+    CE ce = &ct->c_cefile;
 
     file = NULL;
     if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
        return NOTOK;
 
-    result = writeBase64aux (ce->ce_fp, out);
+    result = writeBase64aux (ce->ce_fp, out, (ct->c_type == CT_TEXT));
     (*ct->c_ceclosefnx) (ct);
     return result;
 }