From acd2fe1351a77f45403209332904ef9a84927ab0 Mon Sep 17 00:00:00 2001 From: David Given Date: Sun, 17 Jul 2022 20:11:21 +0200 Subject: [PATCH] More binary flags; and, remember to truncate existing files when trying to create new ones. --- modules/src/system/open.c | 2 +- util/ack/run.c | 4 ++-- util/opt/main.c | 6 +++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/src/system/open.c b/modules/src/system/open.c index cb6e7537e..85fa71e7a 100644 --- a/modules/src/system/open.c +++ b/modules/src/system/open.c @@ -45,7 +45,7 @@ sys_open(path, flag, filep) } /* Fall through */ case OP_WRITE: - if ((fd = open(path, O_CREAT|O_WRONLY|O_BINARY, 0666)) < 0) + if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666)) < 0) return 0; break; default: diff --git a/util/ack/run.c b/util/ack/run.c index e8265aee9..2ed761bbc 100644 --- a/util/ack/run.c +++ b/util/ack/run.c @@ -119,7 +119,7 @@ static int run_exec(trf* phase) oldstdout = dup(1); close(1); - if (open(out.p_path, O_CREAT|O_WRONLY|O_BINARY, 0666) != 1) + if (open(out.p_path, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666) != 1) { close(1); dup(2); @@ -139,7 +139,7 @@ static int run_exec(trf* phase) if (oldstdout != -1) { - int i = close(1); + close(1); dup2(oldstdout, 1); close(oldstdout); } diff --git a/util/opt/main.c b/util/opt/main.c index 2856ffd33..7b75fb5c0 100644 --- a/util/opt/main.c +++ b/util/opt/main.c @@ -60,6 +60,7 @@ void fileinit(void) else { outfile = stdout; + freopen(NULL, "wb", stdout); outshort(sp_magic); } } @@ -84,8 +85,11 @@ int main(int argc, char* argv[]) exit(EXIT_FAILURE); } if (argc) - if (freopen(*argv, "r", stdin) == NULL) + if (freopen(*argv, "rb", stdin) == NULL) error("Cannot open %s", *argv); + else + freopen(NULL, "rb", stdin); + fileinit(); #ifdef USEMALLOC coreinit();