diff options
author | B. Watson <urchlay@slackware.uk> | 2024-05-26 02:50:59 -0400 |
---|---|---|
committer | B. Watson <urchlay@slackware.uk> | 2024-05-26 02:50:59 -0400 |
commit | a7cc28f7adaad8eae86ec6bcc36aa745794862e0 (patch) | |
tree | f3b98b30a66821cf51fea84818e0b66a4fa991e1 | |
parent | e4288a04fc0d14190743e6e2700315436d468817 (diff) | |
download | bw-atari8-tools-a7cc28f7adaad8eae86ec6bcc36aa745794862e0.tar.gz |
unprotbas: don't open output file until ready to write (avoids zero-length output files, on error).
-rw-r--r-- | unprotbas.c | 17 |
1 files changed, 10 insertions, 7 deletions
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(); |