Added NMH_UNUSED macro for suppressing warnings about unused parameters
[mmh] / uip / mhparse.c
index 7c10000..1b49efd 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.
 #include <h/mhparse.h>
 #include <h/utils.h>
 
-#ifdef HAVE_SYS_WAIT_H
-# include <sys/wait.h>
-#endif
-
 
 extern int debugsw;
 
@@ -204,12 +198,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);
@@ -1046,6 +1042,8 @@ invalid:
 static int
 InitGeneric (CT ct)
 {
+    NMH_UNUSED (ct);
+
     return OK;         /* not much to do here */
 }
 
@@ -1764,7 +1762,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);
@@ -1781,8 +1779,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");
@@ -1972,7 +1983,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);
@@ -1989,8 +2000,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");
@@ -2177,7 +2201,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);
@@ -2194,8 +2218,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");
@@ -2450,10 +2487,8 @@ openFTP (CT ct, char **file)
     if ((ftp = context_find (nmhaccessftp)) && !*ftp)
        ftp = NULL;
 
-#ifndef BUILTIN_FTP
     if (!ftp)
        return NOTOK;
-#endif
 
     switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
        case NOTOK:
@@ -2545,16 +2580,13 @@ 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");
        return NOTOK;
     }
 
-#ifdef BUILTIN_FTP
-    if (ftp)
-#endif
     {
        int child_id, i, vecp;
        char *vec[9];
@@ -2590,9 +2622,6 @@ openFTP (CT ct, char **file)
 
            default:
                if (pidXwait (child_id, NULL)) {
-#ifdef BUILTIN_FTP
-losing_ftp:
-#endif
                    username = password = NULL;
                    ce->ce_unlink = 1;
                    return NOTOK;
@@ -2600,14 +2629,6 @@ losing_ftp:
                break;
        }
     }
-#ifdef BUILTIN_FTP
-    else
-       if (ftp_get (e->eb_site, user, pass, e->eb_dir, e->eb_name,
-                    ce->ce_file,
-                    e->eb_mode && !mh_strcasecmp (e->eb_mode, "ascii"), 0)
-               == NOTOK)
-           goto losing_ftp;
-#endif
 
     if (cachefile[0]) {
        if (caching)
@@ -2747,7 +2768,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);