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.
This commit is contained in:
David Given 2022-07-17 20:47:53 +02:00
parent acd2fe1351
commit 28d4480f62
15 changed files with 68 additions and 44 deletions

View file

@ -10,6 +10,7 @@ clibrary {
"./write.c", "./write.c",
"./syssystem.c", "./syssystem.c",
"./strndup.c", "./strndup.c",
"./setbinarymode.c",
}, },
hdrs = { "./system.h" }, hdrs = { "./system.h" },
} }

View file

@ -0,0 +1,14 @@
#include "system.h"
#include <fcntl.h>
#if defined WIN32
#include <io.h>
#endif
void sys_setbinarymode(FILE* fp)
{
#if defined WIN32
setmode(fileno(fp), O_BINARY);
#endif
}

View file

@ -89,4 +89,8 @@ char* sys_tmpnam(char *buffer);
extern char* strndup(const char* s, size_t n); extern char* strndup(const char* s, size_t n);
#endif #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__ */ #endif /* __SYSTEM_INCLUDED__ */

View file

@ -40,6 +40,7 @@ cprogram {
"./ca/ca_put.h", "./ca/ca_put.h",
"util/ego/share+lib", "util/ego/share+lib",
"modules/src/em_data+lib", "modules/src/em_data+lib",
"modules/src/system+lib",
"h+emheaders", "h+emheaders",
}, },
vars = { vars = {

View file

@ -13,6 +13,7 @@
#include <string.h> #include <string.h>
#include <em_pseu.h> #include <em_pseu.h>
#include <em_mes.h> #include <em_mes.h>
#include "system.h"
#include "../share/types.h" #include "../share/types.h"
#include "ca.h" #include "ca.h"
#include "../share/debug.h" #include "../share/debug.h"
@ -257,15 +258,16 @@ char* argv[];
fproc = getptable(pname_in); /* proc table */ fproc = getptable(pname_in); /* proc table */
fdblock = getdtable(dname_in); /* data block table */ fdblock = getdtable(dname_in); /* data block table */
dlength = makedmap(fdblock); /* allocate dmap table */ dlength = makedmap(fdblock); /* allocate dmap table */
df = openfile(dname_out, "r"); df = openfile(dname_out, "rb");
getdnames(df); getdnames(df);
fclose(df); fclose(df);
pf = openfile(pname_out, "r"); pf = openfile(pname_out, "rb");
getpnames(pf); getpnames(pf);
fclose(pf); fclose(pf);
uniq_names(); uniq_names();
f = openfile(lname_in, "r"); f = openfile(lname_in, "rb");
f2 = stdout; f2 = stdout;
sys_setbinarymode(f2);
cputmagic(f2); /* write magic number */ cputmagic(f2); /* write magic number */
while ((lnp = get_ca_lines(f, &curproc)) != (line_p)0) while ((lnp = get_ca_lines(f, &curproc)) != (line_p)0)
{ {

View file

@ -557,15 +557,15 @@ char* argv[];
fdblock = getdtable(dname_in); /* data block table */ fdblock = getdtable(dname_in); /* data block table */
lpi_set = Cempty_set(plength); lpi_set = Cempty_set(plength);
cai_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); 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); 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); error("cannot open %s", bname_out);
} }
@ -603,12 +603,12 @@ char* argv[];
/* Compute transitive closure of used/changed /* Compute transitive closure of used/changed
* variables information for every procedure. * 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); error("cannot open %s", dname_out);
} }
putdtable(fdblock, f); putdtable(fdblock, f);
if ((f = fopen(pname_out, "w")) == NULL) if ((f = fopen(pname_out, "wb")) == NULL)
{ {
error("cannot open %s", pname_out); error("cannot open %s", pname_out);
} }

View file

@ -83,9 +83,9 @@ char* argv[];
const char* dname_out = argv[6]; const char* dname_out = argv[6];
const char* lname_out = argv[7]; const char* lname_out = argv[7];
FILE* lfile = openfile(lname_out, "w"); FILE* lfile = openfile(lname_out, "wb");
FILE* pdump = openfile(pdump_out, "w"); FILE* pdump = openfile(pdump_out, "wb");
FILE* ddump = openfile(ddump_out, "w"); FILE* ddump = openfile(ddump_out, "wb");
FILE* dfile; FILE* dfile;
FILE* pfile; FILE* pfile;
@ -117,9 +117,9 @@ char* argv[];
cleanprocs(prochash, NPROCHASH, 0); cleanprocs(prochash, NPROCHASH, 0);
cleandblocks(symhash, NSYMHASH, 0); cleandblocks(symhash, NSYMHASH, 0);
/* Now write the datablock table and the proctable */ /* Now write the datablock table and the proctable */
dfile = openfile(dname_out, "w"); dfile = openfile(dname_out, "wb");
putdtable(fdblock, dfile); putdtable(fdblock, dfile);
pfile = openfile(pname_out, "w"); pfile = openfile(pname_out, "wb");
putptable(fproc, pfile, FALSE); putptable(fproc, pfile, FALSE);
exit(0); exit(0);
} }

View file

@ -298,7 +298,7 @@ char* argv[];
return NULL; return NULL;
} }
filename = argv[argcnt]; filename = argv[argcnt];
if ((curfile = fopen(filename, "r")) == NULL) if ((curfile = fopen(filename, "rb")) == NULL)
{ {
error("cannot open %s", filename); error("cannot open %s", filename);
} }

View file

@ -71,10 +71,10 @@ STATIC void pass1(const char *lnam, const char *bnam, const char *cnam)
short kind; short kind;
line_p l; line_p l;
f = openfile(lnam, "r"); f = openfile(lnam, "rb");
gf = openfile(bnam, "r"); gf = openfile(bnam, "rb");
cf = openfile(cnam, "w"); cf = openfile(cnam, "wb");
ccf = openfile(ccname, "w"); ccf = openfile(ccname, "wb");
mesregs = Lempty_set(); mesregs = Lempty_set();
apriori(fproc); apriori(fproc);
/* use information from the procedure table to /* use information from the procedure table to
@ -132,9 +132,9 @@ STATIC void pass2(const char *cnam, long space)
FILE* cf, *cf2, *ccf; FILE* cf, *cf2, *ccf;
call_p c, a; call_p c, a;
cf = openfile(cnam, "r"); cf = openfile(cnam, "rb");
cf2 = openfile(cname2, "w"); cf2 = openfile(cname2, "wb");
ccf = openfile(ccname, "r"); ccf = openfile(ccname, "rb");
while ((c = getcall(cf)) != (call_p)0) while ((c = getcall(cf)) != (call_p)0)
{ {
/* process all calls */ /* process all calls */
@ -161,7 +161,7 @@ STATIC void pass2(const char *cnam, long space)
fclose(ccf); fclose(ccf);
if (!kp_temps) if (!kp_temps)
unlink(ccname); unlink(ccname);
cf2 = openfile(cname2, "r"); cf2 = openfile(cname2, "rb");
add_actuals(fproc, cf2); add_actuals(fproc, cf2);
cleancals(fproc); /* remove calls that were not selected */ cleancals(fproc); /* remove calls that were not selected */
/* add actual parameters to each selected call */ /* add actual parameters to each selected call */
@ -186,12 +186,12 @@ void pass3(const char *lnam, const char *lnam2)
line_p l, startscan, cal; line_p l, startscan, cal;
short lastcid; /* last call-id seen */ short lastcid; /* last call-id seen */
lfile = openfile(lnam, "r"); lfile = openfile(lnam, "rb");
lfilerand = openfile(lnam, "r"); lfilerand = openfile(lnam, "rb");
lfile2 = openfile(lnam2, "w"); lfile2 = openfile(lnam2, "wb");
if (verbose) if (verbose)
{ {
sfile = openfile(sname, "w"); sfile = openfile(sname, "wb");
} }
mesregs = Lempty_set(); mesregs = Lempty_set();
while ((l = get_text(lfile, &curproc)) != (line_p)0) while ((l = get_text(lfile, &curproc)) != (line_p)0)
@ -356,10 +356,10 @@ char* argv[];
space = total_size * space / 100; space = total_size * space / 100;
pass2(cname, space); /* select calls to be expanded */ pass2(cname, space); /* select calls to be expanded */
pass3(files->lname_in, files->lname_out); /* do substitutions */ 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 */ il_cleanptab(fproc); /* remove extended data structures */
putdtable(fdblock, f); putdtable(fdblock, f);
f = openfile(files->pname_out, "w"); f = openfile(files->pname_out, "wb");
putptable(fproc, f, FALSE); putptable(fproc, f, FALSE);
report("inline substitutions", Ssubst); report("inline substitutions", Ssubst);
#ifdef VERBOSE #ifdef VERBOSE

View file

@ -69,10 +69,10 @@ int main(int argc, char *argv[])
if (argc != 3) { if (argc != 3) {
error("usage: makeitems mnemfile itemfile"); error("usage: makeitems mnemfile itemfile");
} }
if ((f1 = fopen(argv[1],"r")) == NULL) { if ((f1 = fopen(argv[1],"rb")) == NULL) {
error("cannot open mnemonic file"); error("cannot open mnemonic file");
} }
if ((f2 = fopen(argv[2],"r")) == NULL) { if ((f2 = fopen(argv[2],"rb")) == NULL) {
error("cannot open item file"); error("cannot open item file");
} }
convert(f1,f2); convert(f1,f2);

View file

@ -167,7 +167,7 @@ proc_p getptable(const char *pname)
proc_p head, p, *pp; proc_p head, p, *pp;
short all; short all;
if ((curinp = fopen(pname,"r")) == NULL) { if ((curinp = fopen(pname,"rb")) == NULL) {
error("cannot open %s",pname); error("cannot open %s",pname);
} }
@ -234,7 +234,7 @@ dblock_p getdtable(const char *dname)
int n; int n;
head = (dblock_p) 0; head = (dblock_p) 0;
if ((curinp = fopen(dname,"r")) == NULL) { if ((curinp = fopen(dname,"rb")) == NULL) {
error("cannot open %s", dname); error("cannot open %s", dname);
} }
olength = getshort(); olength = getshort();

View file

@ -33,7 +33,7 @@ STATIC void mach_init(char* machfile, void (*phase_machinit)(void *))
FILE* f; FILE* f;
f = openfile(machfile, "r"); f = openfile(machfile, "rb");
fscanf(f, "%d", &ws); fscanf(f, "%d", &ws);
fscanf(f, "%d", &ps); fscanf(f, "%d", &ps);
if (ws != ps && ps != 2 * ws) if (ws != ps && ps != 2 * ws)
@ -104,10 +104,10 @@ void go(int argc, char * const *argv,
(*initialize)(NULL); (*initialize)(NULL);
if (optimize == no_action) if (optimize == no_action)
return; return;
f = openfile(files->lname_in, "r"); f = openfile(files->lname_in, "rb");
gf = openfile(files->bname_in, "r"); gf = openfile(files->bname_in, "rb");
f2 = openfile(files->lname_out, "w"); f2 = openfile(files->lname_out, "wb");
gf2 = openfile(files->bname_out, "w"); gf2 = openfile(files->bname_out, "wb");
mesregs = Lempty_set(); mesregs = Lempty_set();
while (getunit(gf, f, &kind, &g, &l, &curproc, TRUE)) while (getunit(gf, f, &kind, &g, &l, &curproc, TRUE))
{ {
@ -135,10 +135,10 @@ void go(int argc, char * const *argv,
fclose(f2); fclose(f2);
fclose(gf); fclose(gf);
fclose(gf2); fclose(gf2);
f = openfile(files->dname_out, "w"); f = openfile(files->dname_out, "wb");
putdtable(fdblock, f); putdtable(fdblock, f);
/* fclose(f); done by putdtable */ /* fclose(f); done by putdtable */
f = openfile(files->pname_out, "w"); f = openfile(files->pname_out, "wb");
putptable(fproc, f, TRUE); putptable(fproc, f, TRUE);
/* fclose(f); done by putptable */ /* fclose(f); done by putptable */
core_usage(); core_usage();

View file

@ -75,10 +75,10 @@ int main(int argc, char *argv[])
if (argc != 3) { if (argc != 3) {
error("usage: makeclassdef mnemfile classfile"); error("usage: makeclassdef mnemfile classfile");
} }
if ((f1 = fopen(argv[1],"r")) == NULL) { if ((f1 = fopen(argv[1],"rb")) == NULL) {
error("cannot open mnemonic file"); error("cannot open mnemonic file");
} }
if ((f2 = fopen(argv[2],"r")) == NULL) { if ((f2 = fopen(argv[2],"rb")) == NULL) {
error("cannot open class file"); error("cannot open class file");
} }
convert(f1,f2); convert(f1,f2);

View file

@ -93,7 +93,7 @@ main(argc, argv)
if (argc != 3 || argv[1][0] != '-') { if (argc != 3 || argv[1][0] != '-') {
error("usage: %s -[ldpbc] filename",argv[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]); error("cannot open %s", argv[2]);
} }
switch(argv[1][1]) { switch(argv[1][1]) {

View file

@ -59,8 +59,8 @@ void fileinit(void)
} }
else else
{ {
sys_setbinarymode(stdout);
outfile = stdout; outfile = stdout;
freopen(NULL, "wb", stdout);
outshort(sp_magic); outshort(sp_magic);
} }
} }
@ -85,10 +85,12 @@ int main(int argc, char* argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (argc) if (argc)
{
if (freopen(*argv, "rb", stdin) == NULL) if (freopen(*argv, "rb", stdin) == NULL)
error("Cannot open %s", *argv); error("Cannot open %s", *argv);
}
else else
freopen(NULL, "rb", stdin); sys_setbinarymode(stdin);
fileinit(); fileinit();
#ifdef USEMALLOC #ifdef USEMALLOC