OSDN Git Service

* src/lha.h: correct usage of mkstemp().
authorarai <arai@6a8cc165-1e22-0410-a132-eb4e3f353aba>
Sun, 19 May 2002 19:59:56 +0000 (19:59 +0000)
committerarai <arai@6a8cc165-1e22-0410-a132-eb4e3f353aba>
Sun, 19 May 2002 19:59:56 +0000 (19:59 +0000)
* src/lhadd.c (build_temporary_file): ditto.

* src/lharc.c (fatal_error): ditto.

git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/lha/lha/trunk@73 6a8cc165-1e22-0410-a132-eb4e3f353aba

src/lha.h
src/lhadd.c
src/lharc.c

index 904a2af..6bb3f0d 100644 (file)
--- a/src/lha.h
+++ b/src/lha.h
@@ -191,7 +191,7 @@ EXTERN long         reading_size;
 EXTERN unsigned int n_max;
 
 /* lhadd.c */
-EXTERN FILE            *temporary_fp;
+EXTERN int temporary_fd = -1;
 
 /* ------------------------------------------------------------------------ */
 /*     Functions                                                                                                                               */
index b73d20e..3f0981c 100644 (file)
@@ -268,22 +268,24 @@ delete(oafp, nafp)
 static FILE    *
 build_temporary_file()
 {
-       int             old_umask;
-       FILE           *afp;
+    FILE *afp;
 
-       build_temporary_name();
-       signal(SIGINT, interrupt);
+    signal(SIGINT, interrupt);
 #ifdef SIGHUP
-       signal(SIGHUP, interrupt);
+    signal(SIGHUP, interrupt);
 #endif
 
-       old_umask = umask(077);
-       afp = xfopen(temporary_name, WRITE_BINARY);
-       remove_temporary_at_error = TRUE;
-       temporary_fp = afp;
-       umask(old_umask);
+    remove_temporary_at_error = TRUE;
+    temporary_fd = build_temporary_name();
+    if (fd == -1)
+        fatal_error(temporary_name);
 
-       return afp;
+    afp = fdopen(temporary_name, WRITE_BINARY);
+    if (afp == NULL)
+        fatal_error(temporary_name);
+
+
+    return afp;
 }
 
 /* ------------------------------------------------------------------------ */
index 3c04c64..4747ee6 100644 (file)
@@ -505,12 +505,15 @@ void
 fatal_error(msg)
        char           *msg;
 {
-       message_1("Fatal error:", "", msg);
+    message_1("Fatal error:", "", msg);
 
-       if (remove_temporary_at_error)
-               unlink(temporary_name);
+    if (remove_temporary_at_error) {
+        if (temporary_fd != -1)
+            close(temporary_fd);
+        unlink(temporary_name);
+    }
 
-       exit(1);
+    exit(1);
 }
 
 /* ------------------------------------------------------------------------ */
@@ -535,8 +538,8 @@ interrupt(signo)
        errno = 0;
        message("Interrupted\n", "");
 
-       if (temporary_fp)
-               fclose(temporary_fp);
+       if (temporary_fd != -1)
+               close(temporary_fd);
        unlink(temporary_name);
        if (recover_archive_when_interrupt)
                rename(backup_archive_name, archive_name);
@@ -907,7 +910,7 @@ free_files(filec, filev)
 /*                                                                                                                                                     */
 /* ------------------------------------------------------------------------ */
 /* Build temporary file name and store to TEMPORARY_NAME */
-void
+int
 build_temporary_name()
 {
 #ifdef TMP_FILENAME_TEMPLATE
@@ -918,11 +921,6 @@ build_temporary_name()
        else {
                sprintf(temporary_name, "%s/lhXXXXXX", extract_directory);
        }
-#ifdef HAVE_MKSTEMP
-       mkstemp(temporary_name);
-#else
-       mktemp(temporary_name);
-#endif
 #else
        char           *p, *s;
 
@@ -931,11 +929,12 @@ build_temporary_name()
                if (*p == '/')
                        s = p;
        strcpy((s ? s + 1 : temporary_name), "lhXXXXXX");
+#endif
 #ifdef HAVE_MKSTEMP
-       mkstemp(temporary_name);
+       return mkstemp(temporary_name);
 #else
        mktemp(temporary_name);
-#endif
+    return open(temporary_name, O_CREAT|O_EXCL|O_RDWR|O_BINARY, 0600);
 #endif
 }