aboutsummaryrefslogtreecommitdiff
path: root/src/io.c
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2025-11-27 19:14:50 -0500
committerB. Watson <urchlay@slackware.uk>2025-11-27 19:14:50 -0500
commit0a1b08e58e1ef9a5d60295deb4bf158562718909 (patch)
tree4775128dfa048e1bbb07aca75f935d518b4c2dda /src/io.c
parent8aec307634ac63cf88e77df0e48fde1de0649b03 (diff)
downloadunalf-0a1b08e58e1ef9a5d60295deb4bf158562718909.tar.gz
Add -f (fix EOF junk) option to unalf.
Diffstat (limited to 'src/io.c')
-rw-r--r--src/io.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/io.c b/src/io.c
index 4e20505..53bc0dd 100644
--- a/src/io.c
+++ b/src/io.c
@@ -14,8 +14,16 @@ static void die_not_alf(void) {
exit(1);
}
-static void eof_junk(void) {
- fprintf(stderr, "%s: junk at EOF (ignoring)\n", self);
+/* truncating the file by name could be a race condition, but I don't
+ think it's going to be a real-world problem for anyone. */
+static void eof_junk(long pos) {
+ fprintf(stderr, "%s: junk at EOF (%s)\n", self, opts.fixjunk ? "removing" : "ignoring");
+ if(opts.fixjunk) {
+ if(truncate(in_filename, pos) < 0) {
+ fprintf(stderr, "%s: could not remove junk: ", self);
+ perror(in_filename);
+ }
+ }
}
static void check_hdr_size(const char *name, unsigned long size) {
@@ -49,6 +57,9 @@ static void sanity_check_header(void) {
int read_alf_header(void) {
u8 h1, h2;
int bytes;
+ long read_pos;
+
+ read_pos = ftell(in_file);
bytes = fread(mem + alf_header, 1, 29, in_file);
@@ -59,7 +70,7 @@ int read_alf_header(void) {
die_not_alf();
} else if(bytes < 29) {
if(headers_read) {
- eof_junk();
+ eof_junk(read_pos);
return 0;
} else {
die_not_alf();
@@ -79,7 +90,7 @@ int read_alf_header(void) {
}
if(headers_read)
- eof_junk();
+ eof_junk(read_pos);
else
die_not_alf();