From a7cc28f7adaad8eae86ec6bcc36aa745794862e0 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Sun, 26 May 2024 02:50:59 -0400 Subject: unprotbas: don't open output file until ready to write (avoids zero-length output files, on error). --- unprotbas.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'unprotbas.c') diff --git a/unprotbas.c b/unprotbas.c index 4ed831f..bf8fc76 100644 --- a/unprotbas.c +++ b/unprotbas.c @@ -67,6 +67,7 @@ int protect_code = 0; /* file handles */ FILE *input_file = NULL; FILE *output_file = NULL; +char *output_filename = NULL; void die(const char *msg) { fprintf(stderr, "%s: %s\n", self, msg); @@ -649,7 +650,7 @@ void open_input(const char *name) { } void open_output(const char *name) { - if(!name) { + if(!name || (strcmp(name, "-") == 0)) { if(isatty(fileno(stdout))) { die("refusing to write binary data to the terminal"); } @@ -713,8 +714,8 @@ void parse_args(int argc, char **argv) { case 0: if(!input_file) open_input(NULL); - else if(!output_file) - open_output(NULL); + else if(!output_filename) + output_filename = *argv; else invalid_args(*argv); break; @@ -724,14 +725,14 @@ void parse_args(int argc, char **argv) { if(!input_file) open_input(*argv); else if(!checkonly && !output_file) - open_output(*argv); + output_filename = *argv; else invalid_args(*argv); } } if(!input_file) die("no input file given (use - for stdin)"); - if(!checkonly && !output_file) die("no output file given (use - for stdout)"); + if(!checkonly && !output_filename) die("no output file given (use - for stdout)"); if(keepvars && forcevars) die("-f and -n are mutually exclusive"); if(readmap && writemap) die("-r and -w are mutually exclusive"); if(readmap && keepvars) die("-r and -n are mutually exclusive (maybe you want -w?)"); @@ -742,6 +743,7 @@ void parse_args(int argc, char **argv) { } int main(int argc, char **argv) { + int outbytes; parse_args(argc, argv); filelen = readfile(); @@ -798,9 +800,10 @@ int main(int argc, char **argv) { } } - int got = fwrite(data, 1, filelen, output_file); + open_output(output_filename); + outbytes = fwrite(data, 1, filelen, output_file); fclose(output_file); - if(verbose) fprintf(stderr, "wrote %d bytes\n", got); + if(verbose) fprintf(stderr, "wrote %d bytes\n", outbytes); if(writemap) write_var_map(); -- cgit v1.2.3