]> diplodocus.org Git - nmh/blobdiff - sbr/m_mktemp.c
Fixed inc(1) and %(me) to not obey Local-Mailbox profile component.
[nmh] / sbr / m_mktemp.c
index 17cf74580265b4fb92210bce3d495c8ae12b4702..228de438b20d837858ceafdd3d140e8439005914 100644 (file)
@@ -1,14 +1,17 @@
-/*
- * m_mktemp.c -- Construct a temporary file.
+/* m_mktemp.c -- Construct a temporary file.
  *
  * This code is Copyright (c) 2010, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
  *
  * This code is Copyright (c) 2010, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
-#include <h/mh.h>
-#include <h/utils.h>
-#include <h/signals.h>
+#include "h/mh.h"
+#include "concat.h"
+#include "r1bindex.h"
+#include "h/utils.h"
+#include "h/signals.h"
+#include "m_maildir.h"
+#include "m_mktemp.h"
 
 static void register_for_removal(const char *);
 
 
 static void register_for_removal(const char *);
 
@@ -50,7 +53,6 @@ m_mktemp (
 {
     static char tmpfil[BUFSIZ];
     int fd = -1;
 {
     static char tmpfil[BUFSIZ];
     int fd = -1;
-    int keep_open = 0;
     mode_t oldmode = umask(077);
 
     if (pfx_in == NULL) {
     mode_t oldmode = umask(077);
 
     if (pfx_in == NULL) {
@@ -68,9 +70,10 @@ m_mktemp (
 
     register_for_removal(tmpfil);
 
 
     register_for_removal(tmpfil);
 
+    bool keep_open = false;
     if (fd_ret != NULL) {
         *fd_ret = fd;
     if (fd_ret != NULL) {
         *fd_ret = fd;
-        keep_open = 1;
+        keep_open = true;
     }
     if (fp_ret != NULL) {
         FILE *fp = fdopen(fd, "w+");
     }
     if (fp_ret != NULL) {
         FILE *fp = fdopen(fd, "w+");
@@ -83,7 +86,7 @@ m_mktemp (
             return NULL;
         }
         *fp_ret = fp;
             return NULL;
         }
         *fp_ret = fp;
-        keep_open = 1;
+        keep_open = true;
     }
     if (!keep_open) {
         close(fd);
     }
     if (!keep_open) {
         close(fd);
@@ -150,15 +153,13 @@ m_mktemps(
 {
     char *tmpfil;
     int fd = -1;
 {
     char *tmpfil;
     int fd = -1;
-    int keep_open = 0;
     mode_t oldmode = umask(077);
 
     if (suffix == NULL) {
         if ((tmpfil = m_mktemp2(NULL, pfx_in, fd_ret, fp_ret))) {
     mode_t oldmode = umask(077);
 
     if (suffix == NULL) {
         if ((tmpfil = m_mktemp2(NULL, pfx_in, fd_ret, fp_ret))) {
-            return add(tmpfil, NULL);
-        } else {
-            return NULL;
+            return mh_xstrdup(tmpfil);
         }
         }
+        return NULL;
     }
 
 #if HAVE_MKSTEMPS
     }
 
 #if HAVE_MKSTEMPS
@@ -170,7 +171,7 @@ m_mktemps(
 
     fd = mkstemps(tmpfil, (int) strlen(suffix));
 #else  /* ! HAVE_MKSTEMPS */
 
     fd = mkstemps(tmpfil, (int) strlen(suffix));
 #else  /* ! HAVE_MKSTEMPS */
-    /* Solaris 10, e.g. */
+    /* NetBSD, Solaris 10 */
 
     if (pfx_in == NULL) {
         tmpfil = concat(get_temp_dir(), "/nmhXXXXXX", NULL);
 
     if (pfx_in == NULL) {
         tmpfil = concat(get_temp_dir(), "/nmhXXXXXX", NULL);
@@ -181,31 +182,15 @@ m_mktemps(
     fd = mkstemp(tmpfil);
     {
         char *oldfilename = tmpfil;
     fd = mkstemp(tmpfil);
     {
         char *oldfilename = tmpfil;
-
         tmpfil = concat(oldfilename, suffix, NULL);
 
         tmpfil = concat(oldfilename, suffix, NULL);
 
-        /* link(2) requires that the new path not exist.  And if we
-           have to resort to rename(2), at least try to remove a file
-           that would be in the way. */
-        if (unlink(tmpfil) != 0  &&  errno != ENOENT) {
-            advise("unlink", "Failed to unlink \"%s\"", tmpfil);
-            (void) unlink(oldfilename);
-            free(oldfilename);
-            free(tmpfil);
-            return NULL;
-        }
-
-        /* link() doesn't always work, such as on Windows FAT
-           filesystems.  If it fails, try rename(). */
-        if (link(oldfilename, tmpfil) != 0  &&
-            rename(oldfilename, tmpfil) != 0) {
+        if (rename(oldfilename, tmpfil) != 0) {
             (void) unlink(oldfilename);
             free(oldfilename);
             free(tmpfil);
             return NULL;
         }
 
             (void) unlink(oldfilename);
             free(oldfilename);
             free(tmpfil);
             return NULL;
         }
 
-        (void) unlink(oldfilename);
         free(oldfilename);
     }
 #endif /* ! HAVE_MKSTEMPS */
         free(oldfilename);
     }
 #endif /* ! HAVE_MKSTEMPS */
@@ -218,9 +203,10 @@ m_mktemps(
 
     register_for_removal(tmpfil);
 
 
     register_for_removal(tmpfil);
 
+    bool keep_open = false;
     if (fd_ret != NULL) {
         *fd_ret = fd;
     if (fd_ret != NULL) {
         *fd_ret = fd;
-        keep_open = 1;
+        keep_open = true;
     }
     if (fp_ret != NULL) {
         FILE *fp = fdopen(fd, "w+");
     }
     if (fp_ret != NULL) {
         FILE *fp = fdopen(fd, "w+");
@@ -234,7 +220,7 @@ m_mktemps(
             return NULL;
         }
         *fp_ret = fp;
             return NULL;
         }
         *fp_ret = fp;
-        keep_open = 1;
+        keep_open = true;
     }
     if (!keep_open) {
         close(fd);
     }
     if (!keep_open) {
         close(fd);
@@ -246,7 +232,7 @@ m_mktemps(
 
 
 char *
 
 
 char *
-get_temp_dir()
+get_temp_dir(void)
 {
     /* Ignore envvars if we are setuid */
     if ((getuid()==geteuid()) && (getgid()==getegid())) {
 {
     /* Ignore envvars if we are setuid */
     if ((getuid()==geteuid()) && (getgid()==getegid())) {
@@ -272,9 +258,10 @@ static svector_t exit_filelist = NULL;
  * Register a file for removal at program termination.
  */
 static void
  * Register a file for removal at program termination.
  */
 static void
-register_for_removal(const char *pathname) {
+register_for_removal(const char *pathname)
+{
     if (exit_filelist == NULL) exit_filelist = svector_create(20);
     if (exit_filelist == NULL) exit_filelist = svector_create(20);
-    (void) svector_push_back(exit_filelist, add(pathname, NULL));
+    (void) svector_push_back(exit_filelist, mh_xstrdup(pathname));
 }
 
 /*
 }
 
 /*
@@ -296,7 +283,8 @@ register_for_removal(const char *pathname) {
  * quickly created with the same name.
  */
 void
  * quickly created with the same name.
  */
 void
-unregister_for_removal(int remove_files) {
+unregister_for_removal(int remove_files)
+{
     if (exit_filelist) {
         size_t i;
 
     if (exit_filelist) {
         size_t i;
 
@@ -319,7 +307,8 @@ unregister_for_removal(int remove_files) {
  * any case, unlink it.
  */
 int
  * any case, unlink it.
  */
 int
-m_unlink(const char *pathname) {
+m_unlink(const char *pathname)
+{
     if (exit_filelist) {
         char **slot = svector_find(exit_filelist, pathname);
 
     if (exit_filelist) {
         char **slot = svector_find(exit_filelist, pathname);
 
@@ -337,7 +326,8 @@ m_unlink(const char *pathname) {
  * Remove all registered temporary files.
  */
 void
  * Remove all registered temporary files.
  */
 void
-remove_registered_files_atexit() {
+remove_registered_files_atexit(void)
+{
     unregister_for_removal(1);
 }
 
     unregister_for_removal(1);
 }
 
@@ -349,7 +339,8 @@ remove_registered_files_atexit() {
  * in case the use was expecting a core dump.
  */
 void
  * in case the use was expecting a core dump.
  */
 void
-remove_registered_files(int sig) {
+remove_registered_files(int sig)
+{
     struct sigaction act;
 
     /*
     struct sigaction act;
 
     /*
@@ -371,13 +362,13 @@ remove_registered_files(int sig) {
 
         exit(1);
     } else {
 
         exit(1);
     } else {
+        remove_registered_files_atexit();
+
         act.sa_handler = SIG_DFL;
         (void) sigemptyset(&act.sa_mask);
         act.sa_flags = 0;
         (void) sigaction(sig, &act, 0);
 
         act.sa_handler = SIG_DFL;
         (void) sigemptyset(&act.sa_mask);
         act.sa_flags = 0;
         (void) sigaction(sig, &act, 0);
 
-        remove_registered_files_atexit();
-
         (void) raise(sig);
     }
 }
         (void) raise(sig);
     }
 }