OSDN Git Service

use the tmpfile() function to create temporary file.
authorKoji Arai <jca02266@gmail.com>
Mon, 23 Jun 2008 15:18:14 +0000 (00:18 +0900)
committerKoji Arai <jca02266@gmail.com>
Mon, 23 Jun 2008 15:18:14 +0000 (00:18 +0900)
ar.c
filelib.c
prototypes.h

diff --git a/ar.c b/ar.c
index 70a2111..021d50f 100644 (file)
--- a/ar.c
+++ b/ar.c
@@ -86,8 +86,6 @@ which_method(char *id)
     return NULL;
 }
 
-static char *temp_name = NULL;
-
 static void
 print_usage()
 {
@@ -197,13 +195,6 @@ search(int argc, char *argv[], struct lzh_header *h)
     return 0;
 }
 
-static void
-exitfunc(void)
-{
-    if (temp_name)
-        remove(temp_name);
-}
-
 #include "getopt_long.h"
 
 void
@@ -309,11 +300,9 @@ open_tempfile()
 {
     FILE *outfile;
 
-    temp_name = tmpnam(NULL);
-    outfile = fopen(temp_name, "wb");
+    outfile = tmpfile();
     if (outfile == NULL)
         error("Can't open temporary file");
-    atexit(exitfunc);
 
     return outfile;
 }
@@ -368,6 +357,8 @@ main(int argc, char *argv[])
 
     if (strcmp(archive_file, "-") == 0)
         opts.archive_to_stdio = 1;
+    if (opts.archive_to_stdio)
+        opts.quiet = 2;
 
     argv++;
     argc--;
@@ -379,9 +370,6 @@ main(int argc, char *argv[])
     case 'a':
     case 'u':
     case 'c':
-        if (opts.archive_to_stdio)
-            opts.quiet = 2;
-
         outfile = open_tempfile();
         wp->fp = outfile;
         wp->buf = 0;
@@ -404,15 +392,14 @@ main(int argc, char *argv[])
         fputc(0, outfile);      /* end of archive */
         if (ferror(outfile))
             error("Can't write");
-        fclose(outfile);
+        rewind(outfile);
         if (opts.archive_to_stdio) {
-            if (move_file_to_stream(temp_name, stdout) == -1)
-                error("fail to move_file_to_stream(): %s -> %s",temp_name,"stdout");
+            if (copy_stream(outfile, stdout) == -1)
+                error("fail to copy_stream_to_file(): temp -> %s","stdout");
         }
         else {
-            unlink(archive_file);
-            if (xrename(temp_name, archive_file) == -1)
-                error("fail to rename(): %s -> %s",temp_name,archive_file);
+            if (copy_stream_to_file(outfile, archive_file) == -1)
+                error("fail to copy_stream_to_file(): temp -> %s",archive_file);
         }
         exit(0);
         break;
@@ -543,20 +530,21 @@ main(int argc, char *argv[])
             error("Can't write");
         if (!opts.archive_to_stdio)
             unlink(archive_file);
-        fclose(outfile);
+
         fclose(arcfile);
+        rewind(outfile);
         if (cmd == 'd') {
             if (arc_count > count) {
-                if (xrename(temp_name, archive_file) == -1)
-                    error("fail to rename(): %s -> %s",temp_name,archive_file);
+                if (copy_stream_to_file(outfile, archive_file) == -1)
+                    error("fail to copy_stream_to_file(): temp -> %s",archive_file);
             }
             else {
                 message("The archive file \"%s\" was removed because it would be empty.", archive_file);
             }
         }
         else {
-            if (xrename(temp_name, archive_file) == -1)
-                error("fail to rename(): %s -> %s",temp_name,archive_file);
+            if (copy_stream_to_file(outfile, archive_file) == -1)
+                error("fail to copy_stream_to_file(): temp -> %s",archive_file);
         }
         exit(0);
     }
index 61f39df..8707a02 100644 (file)
--- a/filelib.c
+++ b/filelib.c
@@ -87,6 +87,25 @@ err:
 }
 
 int
+copy_stream_to_file(FILE *rfp, char *file)
+{
+    FILE *wfp = NULL;
+
+    if ((wfp = fopen(file, "a")) == NULL)
+        goto err;
+
+    copy_stream(rfp, wfp);
+
+    if (wfp) fclose(wfp);
+
+    return 0;
+err:
+    if (wfp) fclose(wfp);
+
+    return -1;
+}
+
+int
 xrename(char *from, char *to)
 {
     FILE *rfp = NULL, *wfp = NULL;
index b2caeb7..50dec10 100644 (file)
@@ -56,6 +56,7 @@ int file_exists P_((char *file));
 int file_mtime P_((char *file, time_t *t));
 int copy_stream P_((FILE *rfp, FILE *wfp));
 int move_file_to_stream P_((char *file, FILE *wfp));
+int copy_stream_to_file P_((FILE *rfp, char *file));
 int xrename P_((char *from, char *to));
 /* header.c */
 int read_header P_((FILE *fp, struct lzh_header *h));