Alexandre Julliard : bin2res: Clean output files when aborting on an error or signal.

Alexandre Julliard julliard at wine.codeweavers.com
Sat May 27 06:34:48 CDT 2006


Module: wine
Branch: refs/heads/master
Commit: 3f1bc3a0a2ff3541e13ef5864656c1a56bac7a74
URL:    http://source.winehq.org/git/?p=wine.git;a=commit;h=3f1bc3a0a2ff3541e13ef5864656c1a56bac7a74

Author: Alexandre Julliard <julliard at winehq.org>
Date:   Sat May 27 13:23:59 2006 +0200

bin2res: Clean output files when aborting on an error or signal.

---

 tools/bin2res.c |   34 ++++++++++++++++++++++++++++------
 1 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/tools/bin2res.c b/tools/bin2res.c
index 090bbf4..3ccabb0 100644
--- a/tools/bin2res.c
+++ b/tools/bin2res.c
@@ -23,6 +23,7 @@
 #include "config.h"
 #include "wine/port.h"
 
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
@@ -33,6 +34,8 @@ #ifdef HAVE_SYS_PARAM_H
 # include <sys/param.h>
 #endif
 
+static const char *clean_file;
+
 static const char* help =
         "Usage: bin2res [OPTIONS] <rsrc.rc>\n"
 	"  -a archive binaries into the <rsrc.rc> file\n"
@@ -70,6 +73,16 @@ static void usage(void)
     exit(1);
 }
 
+static void cleanup_files(void)
+{
+    if (clean_file) unlink( clean_file );
+}
+
+static void exit_on_signal( int sig )
+{
+    exit(1);  /* this will call the atexit functions */
+}
+
 static int insert_hexdump (FILE* outfile, FILE* infile)
 {
     int i, c;
@@ -148,6 +161,7 @@ static int process_resources(const char*
 	    strcpy(tmp_file_name, "/tmp/bin2res-XXXXXX.temp");
 	    if ((fd = mkstemps(tmp_file_name, 5)) == -1) return 0;
 	}
+	clean_file = tmp_file_name;
 	if (!(ftmp = fdopen(fd, "w"))) return 0;
     }
 
@@ -167,17 +181,21 @@ static int process_resources(const char*
 	    if (inserting) fputc(c, ftmp);
 	if (c == EOF) break;
 
-	if (!(fres = fopen(res_file_name, inserting ? "rb" : "wb"))) break;
 	if (inserting)
 	{
+	    if (!(fres = fopen(res_file_name, "rb"))) break;
 	    if (!insert_hexdump(ftmp, fres)) break;
 	    while ( (c = fgetc(fin)) != EOF && c != '}') /**/;
+	    fclose(fres);
 	}
 	else
 	{
+	    clean_file = res_file_name;
+	    if (!(fres = fopen(res_file_name, "wb"))) break;
 	    if (!extract_hexdump(fres, fin)) break;
+	    fclose(fres);
+	    clean_file = NULL;
 	}
-	fclose(fres);
     }
 
     fclose(fin);
@@ -191,13 +209,10 @@ static int process_resources(const char*
             {
                 /* try unlinking first, Windows rename is brain-damaged */
                 if (unlink(input_file_name) < 0 || rename(tmp_file_name, input_file_name) < 0)
-                {
-                    unlink(tmp_file_name);
                     return 0;
-                }
             }
+            clean_file = NULL;
         }
-	else unlink(tmp_file_name);
     }
 
     return c == EOF;
@@ -210,6 +225,13 @@ int main(int argc, char **argv)
     const char* input_file_name = 0;
     const char* specific_file_name = 0;
 
+    atexit( cleanup_files );
+    signal( SIGTERM, exit_on_signal );
+    signal( SIGINT, exit_on_signal );
+#ifdef SIGHUP
+    signal( SIGHUP, exit_on_signal );
+#endif
+
     while((optc = getopt(argc, argv, "axi:o:fhv")) != EOF)
     {
 	switch(optc)




More information about the wine-cvs mailing list