aboutsummaryrefslogtreecommitdiff
path: root/listamsb.c
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2025-03-12 04:45:56 -0400
committerB. Watson <urchlay@slackware.uk>2025-03-12 04:45:56 -0400
commit2703b4f3d32941d3aff315d4698bbc279a1ff5a5 (patch)
tree44f8e2b13cc81bd4e1fd4d5ff27c751e41d38b5a /listamsb.c
parent14bfd5572a83bc202842ab70c3c02094053d93d6 (diff)
downloadbw-atari8-tools-2703b4f3d32941d3aff315d4698bbc279a1ff5a5.tar.gz
listamsb: finish() cleanup, fix errmsg for -D with stdout, rm output file if empty due to error.
Diffstat (limited to 'listamsb.c')
-rw-r--r--listamsb.c31
1 files changed, 23 insertions, 8 deletions
diff --git a/listamsb.c b/listamsb.c
index 9bf404c..ec1b027 100644
--- a/listamsb.c
+++ b/listamsb.c
@@ -5,6 +5,7 @@
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
+#include <sys/stat.h>
#include "amsbtok.h"
@@ -151,6 +152,12 @@ int is_comment_start(unsigned char tok) {
return (tok == TOK_REM || tok == TOK_SQUOTE || tok == TOK_BANG);
}
+off_t get_file_len(const char *name) {
+ struct stat st;
+ if(stat(name, &st) != 0) return 0;
+ return st.st_size;
+}
+
/* post-processing: print "summary", exit. called by either die()
or main() (on normal exit). */
void finish(int rv) {
@@ -159,9 +166,8 @@ void finish(int rv) {
verbose(1, "read %d bytes", bytes_read);
verbose(1, "listed %d line%s", linecount, plural(linecount));
- if(!linecount) warn("no lines of code in program");
-
if(header_read) {
+ if(!linecount) warn("no lines of code in program");
progsize = bytes_read - 3;
if(proglen == progsize) {
verbose(1, "file size matches proglen");
@@ -175,12 +181,12 @@ void finish(int rv) {
fputs("AMSB will stop LOADing before the end of this file\n", stderr);
}
}
+ if(fgetc(infile) != EOF) warn("trailing garbage at end of file");
} else {
- warn("file is %d byte%s, too short to be an AMSB program", bytes_read, plural(bytes_read));
+ if(feof(infile))
+ warn("file is %d byte%s, too short to be an AMSB program", bytes_read, plural(bytes_read));
}
- if(fgetc(infile) != EOF) warn("trailing garbage at end of file");
-
if(warnings) {
fprintf(stderr, "%s: file has %d warning%s\n",
self, warnings, plural(warnings));
@@ -193,6 +199,11 @@ void finish(int rv) {
if(got != 0) {
os_err("a8cat child process failed, do you have a8cat on your PATH?");
}
+ } else {
+ fclose(outfile);
+ /* if we were writing to actual file and no data was written, rm it */
+ if(outname && get_file_len(outname) == 0)
+ unlink(outname);
}
verbose(2, "spaces outside strings/comments: %d (%.1f%%)",
@@ -255,7 +266,10 @@ void read_header(void) {
locked = read_byte();
if(locked > 1) die("not an AMSB file: first byte is $%02x, not $00 or $01", locked);
- if(locked) verbose(1, "program is locked, decrypting");
+ if(locked)
+ verbose(1, "program is locked, decrypting");
+ else
+ verbose(1, "program is not locked");
proglen = read_word();
@@ -910,8 +924,9 @@ void parse_args(int argc, char **argv) {
if(!freopen(outname, "wb", stdout))
os_err("%s: %s", outname, strerror(errno));
verbose(1, "redirected stdout to %s", outname);
- } else if(crunch) {
- os_err("can't use stdout with -C, must give an output filename");
+ } else if(crunch || decrunch) {
+ os_err("can't use stdout with -%c, must give an output filename",
+ crunch ? 'C' : 'D');
}
}