Remove RCS keywords, since they no longer work after git migration.
[mmh] / uip / mhparse.c
index f5ff85c..d40d78c 100644 (file)
@@ -2,8 +2,6 @@
 /*
  * mhparse.c -- routines to parse the contents of MIME messages
  *
- * $Id$
- *
  * 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.
@@ -204,12 +202,14 @@ parse_mime (char *file)
      * Check if file is actually standard input
      */
     if ((is_stdin = !(strcmp (file, "-")))) {
-       file = add (m_tmpfil (invo_name), NULL);
-       if ((fp = fopen (file, "w+")) == NULL) {
-           advise (file, "unable to fopen for writing and reading");
-           return NULL;
-       }
+        char *tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
+        if (tfile == NULL) {
+            advise("mhparse", "unable to create temporary file");
+            return NULL;
+        }
+       file = add (tfile, NULL);
        chmod (file, 0600);
+
        while (fgets (buffer, sizeof(buffer), stdin))
            fputs (buffer, fp);
        fflush (fp);
@@ -1058,7 +1058,7 @@ static int
 InitText (CT ct)
 {
     char buffer[BUFSIZ];
-    char *chset;
+    char *chset = NULL;
     char **ap, **ep, *cp;
     struct k2v *kv;
     struct text *t;
@@ -1087,9 +1087,12 @@ InitText (CT ct)
     /* check if content specified a character set */
     if (*ap) {
        /* match character set or set to CHARSET_UNKNOWN */
-       for (kv = Charset; kv->kv_key; kv++)
-           if (!mh_strcasecmp (*ep, kv->kv_key))
+       for (kv = Charset; kv->kv_key; kv++) {
+           if (!mh_strcasecmp (*ep, kv->kv_key)) {
+               chset = *ep;
                break;
+           }
+       }
        t->tx_charset = kv->kv_value;
     } else {
        t->tx_charset = CHARSET_UNSPECIFIED;
@@ -1761,7 +1764,7 @@ openBase64 (CT ct, char **file)
     }
 
     if (*file == NULL) {
-       ce->ce_file = add (m_scratch ("", tmp), NULL);
+       ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL);
        ce->ce_unlink = 1;
     } else {
        ce->ce_file = add (*file, NULL);
@@ -1778,8 +1781,21 @@ openBase64 (CT ct, char **file)
                   ci->ci_type);
         cp = context_find (buffer);
     }
-    if (cp != NULL && *cp != '\0')
-        ce->ce_file = add (cp, ce->ce_file);
+    if (cp != NULL && *cp != '\0') {
+        if (ce->ce_unlink) {
+            // Temporary file already exists, so we rename to
+            // version with extension.
+            char *file_org = strdup(ce->ce_file);
+            ce->ce_file = add (cp, ce->ce_file);
+            if (rename(file_org, ce->ce_file)) {
+                adios (ce->ce_file, "unable to rename %s to ", file_org);
+            }
+            free(file_org);
+
+        } else {
+            ce->ce_file = add (cp, ce->ce_file);
+        }
+    }
 
     if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
        content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
@@ -1969,7 +1985,7 @@ openQuoted (CT ct, char **file)
     }
 
     if (*file == NULL) {
-       ce->ce_file = add (m_scratch ("", tmp), NULL);
+       ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL);
        ce->ce_unlink = 1;
     } else {
        ce->ce_file = add (*file, NULL);
@@ -1986,8 +2002,21 @@ openQuoted (CT ct, char **file)
                   ci->ci_type);
         cp = context_find (buffer);
     }
-    if (cp != NULL && *cp != '\0')
-        ce->ce_file = add (cp, ce->ce_file);
+    if (cp != NULL && *cp != '\0') {
+        if (ce->ce_unlink) {
+            // Temporary file already exists, so we rename to
+            // version with extension.
+            char *file_org = strdup(ce->ce_file);
+            ce->ce_file = add (cp, ce->ce_file);
+            if (rename(file_org, ce->ce_file)) {
+                adios (ce->ce_file, "unable to rename %s to ", file_org);
+            }
+            free(file_org);
+
+        } else {
+            ce->ce_file = add (cp, ce->ce_file);
+        }
+    }
 
     if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
        content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
@@ -2174,7 +2203,7 @@ open7Bit (CT ct, char **file)
     }
 
     if (*file == NULL) {
-       ce->ce_file = add (m_scratch ("", tmp), NULL);
+       ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL);
        ce->ce_unlink = 1;
     } else {
        ce->ce_file = add (*file, NULL);
@@ -2191,8 +2220,21 @@ open7Bit (CT ct, char **file)
                   ci->ci_type);
         cp = context_find (buffer);
     }
-    if (cp != NULL && *cp != '\0')
-        ce->ce_file = add (cp, ce->ce_file);
+    if (cp != NULL && *cp != '\0') {
+        if (ce->ce_unlink) {
+            // Temporary file already exists, so we rename to
+            // version with extension.
+            char *file_org = strdup(ce->ce_file);
+            ce->ce_file = add (cp, ce->ce_file);
+            if (rename(file_org, ce->ce_file)) {
+                adios (ce->ce_file, "unable to rename %s to ", file_org);
+            }
+            free(file_org);
+
+        } else {
+            ce->ce_file = add (cp, ce->ce_file);
+        }
+    }
 
     if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
        content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
@@ -2542,7 +2584,7 @@ openFTP (CT ct, char **file)
     else if (caching)
        ce->ce_file = add (cachefile, NULL);
     else
-       ce->ce_file = add (m_scratch ("", tmp), NULL);
+       ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL);
 
     if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
        content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
@@ -2744,7 +2786,7 @@ openMail (CT ct, char **file)
     }
 
     if (*file == NULL) {
-       ce->ce_file = add (m_scratch ("", tmp), NULL);
+       ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL);
        ce->ce_unlink = 1;
     } else {
        ce->ce_file = add (*file, NULL);