Commit 3f1bc3a0 authored by Alexandre Julliard's avatar Alexandre Julliard

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

parent 25dc6d91
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "config.h" #include "config.h"
#include "wine/port.h" #include "wine/port.h"
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
...@@ -33,6 +34,8 @@ ...@@ -33,6 +34,8 @@
# include <sys/param.h> # include <sys/param.h>
#endif #endif
static const char *clean_file;
static const char* help = static const char* help =
"Usage: bin2res [OPTIONS] <rsrc.rc>\n" "Usage: bin2res [OPTIONS] <rsrc.rc>\n"
" -a archive binaries into the <rsrc.rc> file\n" " -a archive binaries into the <rsrc.rc> file\n"
...@@ -70,6 +73,16 @@ static void usage(void) ...@@ -70,6 +73,16 @@ static void usage(void)
exit(1); 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) static int insert_hexdump (FILE* outfile, FILE* infile)
{ {
int i, c; int i, c;
...@@ -148,6 +161,7 @@ static int process_resources(const char* input_file_name, const char* specific_f ...@@ -148,6 +161,7 @@ static int process_resources(const char* input_file_name, const char* specific_f
strcpy(tmp_file_name, "/tmp/bin2res-XXXXXX.temp"); strcpy(tmp_file_name, "/tmp/bin2res-XXXXXX.temp");
if ((fd = mkstemps(tmp_file_name, 5)) == -1) return 0; if ((fd = mkstemps(tmp_file_name, 5)) == -1) return 0;
} }
clean_file = tmp_file_name;
if (!(ftmp = fdopen(fd, "w"))) return 0; if (!(ftmp = fdopen(fd, "w"))) return 0;
} }
...@@ -167,17 +181,21 @@ static int process_resources(const char* input_file_name, const char* specific_f ...@@ -167,17 +181,21 @@ static int process_resources(const char* input_file_name, const char* specific_f
if (inserting) fputc(c, ftmp); if (inserting) fputc(c, ftmp);
if (c == EOF) break; if (c == EOF) break;
if (!(fres = fopen(res_file_name, inserting ? "rb" : "wb"))) break;
if (inserting) if (inserting)
{ {
if (!(fres = fopen(res_file_name, "rb"))) break;
if (!insert_hexdump(ftmp, fres)) break; if (!insert_hexdump(ftmp, fres)) break;
while ( (c = fgetc(fin)) != EOF && c != '}') /**/; while ( (c = fgetc(fin)) != EOF && c != '}') /**/;
fclose(fres);
} }
else else
{ {
clean_file = res_file_name;
if (!(fres = fopen(res_file_name, "wb"))) break;
if (!extract_hexdump(fres, fin)) break; if (!extract_hexdump(fres, fin)) break;
}
fclose(fres); fclose(fres);
clean_file = NULL;
}
} }
fclose(fin); fclose(fin);
...@@ -191,14 +209,11 @@ static int process_resources(const char* input_file_name, const char* specific_f ...@@ -191,14 +209,11 @@ static int process_resources(const char* input_file_name, const char* specific_f
{ {
/* try unlinking first, Windows rename is brain-damaged */ /* try unlinking first, Windows rename is brain-damaged */
if (unlink(input_file_name) < 0 || rename(tmp_file_name, input_file_name) < 0) if (unlink(input_file_name) < 0 || rename(tmp_file_name, input_file_name) < 0)
{
unlink(tmp_file_name);
return 0; return 0;
} }
clean_file = NULL;
} }
} }
else unlink(tmp_file_name);
}
return c == EOF; return c == EOF;
} }
...@@ -210,6 +225,13 @@ int main(int argc, char **argv) ...@@ -210,6 +225,13 @@ int main(int argc, char **argv)
const char* input_file_name = 0; const char* input_file_name = 0;
const char* specific_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) while((optc = getopt(argc, argv, "axi:o:fhv")) != EOF)
{ {
switch(optc) switch(optc)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment