From 28d4480f62795b46bfbe73293494dfdcf1fc6aed Mon Sep 17 00:00:00 2001 From: David Given Date: Sun, 17 Jul 2022 20:47:53 +0200 Subject: [PATCH] It turns out that you can't use freopen() to set binary mode of stdin/stdout on Windows; so add a new system function called sys_setbinarymode which does it instead. Then find lots more binary mode flags which need setting. --- modules/src/system/build.lua | 1 + modules/src/system/setbinarymode.c | 14 ++++++++++++++ modules/src/system/system.h | 4 ++++ util/ego/build.lua | 1 + util/ego/ca/ca.c | 8 +++++--- util/ego/cf/cf.c | 10 +++++----- util/ego/ic/ic.c | 10 +++++----- util/ego/ic/ic_lib.c | 2 +- util/ego/il/il.c | 28 ++++++++++++++-------------- util/ego/ra/makeitems.c | 4 ++-- util/ego/share/get.c | 4 ++-- util/ego/share/go.c | 14 +++++++------- util/ego/share/makecldef.c | 4 ++-- util/ego/share/show.c | 2 +- util/opt/main.c | 6 ++++-- 15 files changed, 68 insertions(+), 44 deletions(-) create mode 100644 modules/src/system/setbinarymode.c diff --git a/modules/src/system/build.lua b/modules/src/system/build.lua index b2cbd71f6..4c2b8e52d 100644 --- a/modules/src/system/build.lua +++ b/modules/src/system/build.lua @@ -10,6 +10,7 @@ clibrary { "./write.c", "./syssystem.c", "./strndup.c", + "./setbinarymode.c", }, hdrs = { "./system.h" }, } diff --git a/modules/src/system/setbinarymode.c b/modules/src/system/setbinarymode.c new file mode 100644 index 000000000..8a6408e6a --- /dev/null +++ b/modules/src/system/setbinarymode.c @@ -0,0 +1,14 @@ +#include "system.h" +#include + +#if defined WIN32 +#include +#endif + +void sys_setbinarymode(FILE* fp) +{ + #if defined WIN32 + setmode(fileno(fp), O_BINARY); + #endif +} + diff --git a/modules/src/system/system.h b/modules/src/system/system.h index 2489c1f03..582dd44e9 100644 --- a/modules/src/system/system.h +++ b/modules/src/system/system.h @@ -89,4 +89,8 @@ char* sys_tmpnam(char *buffer); extern char* strndup(const char* s, size_t n); #endif +/* Sets stdin, stdout or stderr to binary mode --- required on Windows, noop on + * sane systems. */ +extern void sys_setbinarymode(FILE* fp); + #endif /* __SYSTEM_INCLUDED__ */ diff --git a/util/ego/build.lua b/util/ego/build.lua index 51c57d7fc..8fbf43bca 100644 --- a/util/ego/build.lua +++ b/util/ego/build.lua @@ -40,6 +40,7 @@ cprogram { "./ca/ca_put.h", "util/ego/share+lib", "modules/src/em_data+lib", + "modules/src/system+lib", "h+emheaders", }, vars = { diff --git a/util/ego/ca/ca.c b/util/ego/ca/ca.c index 1bf73d24a..5f50d8ddd 100644 --- a/util/ego/ca/ca.c +++ b/util/ego/ca/ca.c @@ -13,6 +13,7 @@ #include #include #include +#include "system.h" #include "../share/types.h" #include "ca.h" #include "../share/debug.h" @@ -257,15 +258,16 @@ char* argv[]; fproc = getptable(pname_in); /* proc table */ fdblock = getdtable(dname_in); /* data block table */ dlength = makedmap(fdblock); /* allocate dmap table */ - df = openfile(dname_out, "r"); + df = openfile(dname_out, "rb"); getdnames(df); fclose(df); - pf = openfile(pname_out, "r"); + pf = openfile(pname_out, "rb"); getpnames(pf); fclose(pf); uniq_names(); - f = openfile(lname_in, "r"); + f = openfile(lname_in, "rb"); f2 = stdout; + sys_setbinarymode(f2); cputmagic(f2); /* write magic number */ while ((lnp = get_ca_lines(f, &curproc)) != (line_p)0) { diff --git a/util/ego/cf/cf.c b/util/ego/cf/cf.c index 6ee94af46..1f10e24ed 100644 --- a/util/ego/cf/cf.c +++ b/util/ego/cf/cf.c @@ -557,15 +557,15 @@ char* argv[]; fdblock = getdtable(dname_in); /* data block table */ lpi_set = Cempty_set(plength); cai_set = Cempty_set(plength); - if ((f = fopen(lname_in, "r")) == NULL) + if ((f = fopen(lname_in, "rb")) == NULL) { error("cannot open %s", lname_in); } - if ((f2 = fopen(lname_out, "w")) == NULL) + if ((f2 = fopen(lname_out, "wb")) == NULL) { error("cannot open %s", lname_out); } - if ((gf2 = fopen(bname_out, "w")) == NULL) + if ((gf2 = fopen(bname_out, "wb")) == NULL) { error("cannot open %s", bname_out); } @@ -603,12 +603,12 @@ char* argv[]; /* Compute transitive closure of used/changed * variables information for every procedure. */ - if ((f = fopen(dname_out, "w")) == NULL) + if ((f = fopen(dname_out, "wb")) == NULL) { error("cannot open %s", dname_out); } putdtable(fdblock, f); - if ((f = fopen(pname_out, "w")) == NULL) + if ((f = fopen(pname_out, "wb")) == NULL) { error("cannot open %s", pname_out); } diff --git a/util/ego/ic/ic.c b/util/ego/ic/ic.c index a0203aefb..af0907bac 100644 --- a/util/ego/ic/ic.c +++ b/util/ego/ic/ic.c @@ -83,9 +83,9 @@ char* argv[]; const char* dname_out = argv[6]; const char* lname_out = argv[7]; - FILE* lfile = openfile(lname_out, "w"); - FILE* pdump = openfile(pdump_out, "w"); - FILE* ddump = openfile(ddump_out, "w"); + FILE* lfile = openfile(lname_out, "wb"); + FILE* pdump = openfile(pdump_out, "wb"); + FILE* ddump = openfile(ddump_out, "wb"); FILE* dfile; FILE* pfile; @@ -117,9 +117,9 @@ char* argv[]; cleanprocs(prochash, NPROCHASH, 0); cleandblocks(symhash, NSYMHASH, 0); /* Now write the datablock table and the proctable */ - dfile = openfile(dname_out, "w"); + dfile = openfile(dname_out, "wb"); putdtable(fdblock, dfile); - pfile = openfile(pname_out, "w"); + pfile = openfile(pname_out, "wb"); putptable(fproc, pfile, FALSE); exit(0); } diff --git a/util/ego/ic/ic_lib.c b/util/ego/ic/ic_lib.c index 3933493e8..b2bf800e1 100644 --- a/util/ego/ic/ic_lib.c +++ b/util/ego/ic/ic_lib.c @@ -298,7 +298,7 @@ char* argv[]; return NULL; } filename = argv[argcnt]; - if ((curfile = fopen(filename, "r")) == NULL) + if ((curfile = fopen(filename, "rb")) == NULL) { error("cannot open %s", filename); } diff --git a/util/ego/il/il.c b/util/ego/il/il.c index 6fd30dca9..1f3455538 100644 --- a/util/ego/il/il.c +++ b/util/ego/il/il.c @@ -71,10 +71,10 @@ STATIC void pass1(const char *lnam, const char *bnam, const char *cnam) short kind; line_p l; - f = openfile(lnam, "r"); - gf = openfile(bnam, "r"); - cf = openfile(cnam, "w"); - ccf = openfile(ccname, "w"); + f = openfile(lnam, "rb"); + gf = openfile(bnam, "rb"); + cf = openfile(cnam, "wb"); + ccf = openfile(ccname, "wb"); mesregs = Lempty_set(); apriori(fproc); /* use information from the procedure table to @@ -132,9 +132,9 @@ STATIC void pass2(const char *cnam, long space) FILE* cf, *cf2, *ccf; call_p c, a; - cf = openfile(cnam, "r"); - cf2 = openfile(cname2, "w"); - ccf = openfile(ccname, "r"); + cf = openfile(cnam, "rb"); + cf2 = openfile(cname2, "wb"); + ccf = openfile(ccname, "rb"); while ((c = getcall(cf)) != (call_p)0) { /* process all calls */ @@ -161,7 +161,7 @@ STATIC void pass2(const char *cnam, long space) fclose(ccf); if (!kp_temps) unlink(ccname); - cf2 = openfile(cname2, "r"); + cf2 = openfile(cname2, "rb"); add_actuals(fproc, cf2); cleancals(fproc); /* remove calls that were not selected */ /* add actual parameters to each selected call */ @@ -186,12 +186,12 @@ void pass3(const char *lnam, const char *lnam2) line_p l, startscan, cal; short lastcid; /* last call-id seen */ - lfile = openfile(lnam, "r"); - lfilerand = openfile(lnam, "r"); - lfile2 = openfile(lnam2, "w"); + lfile = openfile(lnam, "rb"); + lfilerand = openfile(lnam, "rb"); + lfile2 = openfile(lnam2, "wb"); if (verbose) { - sfile = openfile(sname, "w"); + sfile = openfile(sname, "wb"); } mesregs = Lempty_set(); while ((l = get_text(lfile, &curproc)) != (line_p)0) @@ -356,10 +356,10 @@ char* argv[]; space = total_size * space / 100; pass2(cname, space); /* select calls to be expanded */ pass3(files->lname_in, files->lname_out); /* do substitutions */ - f = openfile(files->dname_out, "w"); + f = openfile(files->dname_out, "wb"); il_cleanptab(fproc); /* remove extended data structures */ putdtable(fdblock, f); - f = openfile(files->pname_out, "w"); + f = openfile(files->pname_out, "wb"); putptable(fproc, f, FALSE); report("inline substitutions", Ssubst); #ifdef VERBOSE diff --git a/util/ego/ra/makeitems.c b/util/ego/ra/makeitems.c index f8bc64989..8be973b99 100644 --- a/util/ego/ra/makeitems.c +++ b/util/ego/ra/makeitems.c @@ -69,10 +69,10 @@ int main(int argc, char *argv[]) if (argc != 3) { error("usage: makeitems mnemfile itemfile"); } - if ((f1 = fopen(argv[1],"r")) == NULL) { + if ((f1 = fopen(argv[1],"rb")) == NULL) { error("cannot open mnemonic file"); } - if ((f2 = fopen(argv[2],"r")) == NULL) { + if ((f2 = fopen(argv[2],"rb")) == NULL) { error("cannot open item file"); } convert(f1,f2); diff --git a/util/ego/share/get.c b/util/ego/share/get.c index 84a805828..16a88286e 100644 --- a/util/ego/share/get.c +++ b/util/ego/share/get.c @@ -167,7 +167,7 @@ proc_p getptable(const char *pname) proc_p head, p, *pp; short all; - if ((curinp = fopen(pname,"r")) == NULL) { + if ((curinp = fopen(pname,"rb")) == NULL) { error("cannot open %s",pname); } @@ -234,7 +234,7 @@ dblock_p getdtable(const char *dname) int n; head = (dblock_p) 0; - if ((curinp = fopen(dname,"r")) == NULL) { + if ((curinp = fopen(dname,"rb")) == NULL) { error("cannot open %s", dname); } olength = getshort(); diff --git a/util/ego/share/go.c b/util/ego/share/go.c index 0ccd3c6e9..477d54dea 100644 --- a/util/ego/share/go.c +++ b/util/ego/share/go.c @@ -33,7 +33,7 @@ STATIC void mach_init(char* machfile, void (*phase_machinit)(void *)) FILE* f; - f = openfile(machfile, "r"); + f = openfile(machfile, "rb"); fscanf(f, "%d", &ws); fscanf(f, "%d", &ps); if (ws != ps && ps != 2 * ws) @@ -104,10 +104,10 @@ void go(int argc, char * const *argv, (*initialize)(NULL); if (optimize == no_action) return; - f = openfile(files->lname_in, "r"); - gf = openfile(files->bname_in, "r"); - f2 = openfile(files->lname_out, "w"); - gf2 = openfile(files->bname_out, "w"); + f = openfile(files->lname_in, "rb"); + gf = openfile(files->bname_in, "rb"); + f2 = openfile(files->lname_out, "wb"); + gf2 = openfile(files->bname_out, "wb"); mesregs = Lempty_set(); while (getunit(gf, f, &kind, &g, &l, &curproc, TRUE)) { @@ -135,10 +135,10 @@ void go(int argc, char * const *argv, fclose(f2); fclose(gf); fclose(gf2); - f = openfile(files->dname_out, "w"); + f = openfile(files->dname_out, "wb"); putdtable(fdblock, f); /* fclose(f); done by putdtable */ - f = openfile(files->pname_out, "w"); + f = openfile(files->pname_out, "wb"); putptable(fproc, f, TRUE); /* fclose(f); done by putptable */ core_usage(); diff --git a/util/ego/share/makecldef.c b/util/ego/share/makecldef.c index ac831ed3b..5358809c4 100644 --- a/util/ego/share/makecldef.c +++ b/util/ego/share/makecldef.c @@ -75,10 +75,10 @@ int main(int argc, char *argv[]) if (argc != 3) { error("usage: makeclassdef mnemfile classfile"); } - if ((f1 = fopen(argv[1],"r")) == NULL) { + if ((f1 = fopen(argv[1],"rb")) == NULL) { error("cannot open mnemonic file"); } - if ((f2 = fopen(argv[2],"r")) == NULL) { + if ((f2 = fopen(argv[2],"rb")) == NULL) { error("cannot open class file"); } convert(f1,f2); diff --git a/util/ego/share/show.c b/util/ego/share/show.c index 7741f0cbf..f709a62b7 100644 --- a/util/ego/share/show.c +++ b/util/ego/share/show.c @@ -93,7 +93,7 @@ main(argc, argv) if (argc != 3 || argv[1][0] != '-') { error("usage: %s -[ldpbc] filename",argv[0]); } - if ((f = fopen(argv[2], "r")) == NULL) { + if ((f = fopen(argv[2], "rb")) == NULL) { error("cannot open %s", argv[2]); } switch(argv[1][1]) { diff --git a/util/opt/main.c b/util/opt/main.c index 7b75fb5c0..10b9da0a8 100644 --- a/util/opt/main.c +++ b/util/opt/main.c @@ -59,8 +59,8 @@ void fileinit(void) } else { + sys_setbinarymode(stdout); outfile = stdout; - freopen(NULL, "wb", stdout); outshort(sp_magic); } } @@ -85,10 +85,12 @@ int main(int argc, char* argv[]) exit(EXIT_FAILURE); } if (argc) + { if (freopen(*argv, "rb", stdin) == NULL) error("Cannot open %s", *argv); + } else - freopen(NULL, "rb", stdin); + sys_setbinarymode(stdin); fileinit(); #ifdef USEMALLOC