From e39c7d3a83b0363c620c82a942675188b2ec252c Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 28 May 2024 03:00:11 -0700 Subject: [PATCH 1/3] Open binary files in binary mode. --- adt-tests/mir-reduce-test.c | 2 +- c2mir/c2mir-driver.c | 2 +- mir-bin-run.c | 4 ++-- mir-tests/test-read.h | 2 +- mir.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/adt-tests/mir-reduce-test.c b/adt-tests/mir-reduce-test.c index e5e05912cb..0fe1c7c83f 100644 --- a/adt-tests/mir-reduce-test.c +++ b/adt-tests/mir-reduce-test.c @@ -46,7 +46,7 @@ int main (int argc, const char *argv[]) { size_t i, n; double start = real_usec_time (); - if (argc != 2 || (input_file = fopen (argv[1], "r")) == NULL) { + if (argc != 2 || (input_file = fopen (argv[1], "rb")) == NULL) { fprintf (stderr, "usage: %s \n", argv[0]); return 1; } diff --git a/c2mir/c2mir-driver.c b/c2mir/c2mir-driver.c index f83c9cb7b6..da7434cfa8 100644 --- a/c2mir/c2mir-driver.c +++ b/c2mir/c2mir-driver.c @@ -223,7 +223,7 @@ static void *open_lib (const char *dir, const char *name) { if ((res = dlopen (VARR_ADDR (char, temp_string), RTLD_LAZY)) == NULL) { #ifndef _WIN32 FILE *f; - if ((f = fopen (VARR_ADDR (char, temp_string), "r")) != NULL) { + if ((f = fopen (VARR_ADDR (char, temp_string), "rb")) != NULL) { fclose (f); fprintf (stderr, "loading %s:%s\n", VARR_ADDR (char, temp_string), dlerror ()); } diff --git a/mir-bin-run.c b/mir-bin-run.c index 533a8c2199..1cb900be03 100644 --- a/mir-bin-run.c +++ b/mir-bin-run.c @@ -149,7 +149,7 @@ static void *open_lib (const char *dir, const char *name) { VARR_PUSH (char, temp_string, 0); if ((res = dlopen (VARR_ADDR (char, temp_string), RTLD_LAZY)) == NULL) { #ifndef _WIN32 - if ((f = fopen (VARR_ADDR (char, temp_string), "r")) != NULL) { + if ((f = fopen (VARR_ADDR (char, temp_string), "rb")) != NULL) { fclose (f); fprintf (stderr, "loading %s:%s\n", VARR_ADDR (char, temp_string), dlerror ()); } @@ -303,7 +303,7 @@ int main (int argc, char **argv, char **envp) { MIR_item_t main_func = NULL; MIR_context_t mctx = MIR_init (); - FILE *mir_file = fopen (argv[1], "r"); + FILE *mir_file = fopen (argv[1], "rb"); if (!mir_file) { fprintf (stderr, "failed to open file '%s'\n", argv[1]); return 1; diff --git a/mir-tests/test-read.h b/mir-tests/test-read.h index cda0a638ce..bcf12a3c9c 100644 --- a/mir-tests/test-read.h +++ b/mir-tests/test-read.h @@ -3,7 +3,7 @@ static char *read_file (const char *name) { size_t flen, rlen; char *str; - if ((f = fopen (name, "r")) == NULL) { + if ((f = fopen (name, "rb")) == NULL) { perror (name); exit (1); } diff --git a/mir.c b/mir.c index 287a19d45a..0f7bb7ba49 100644 --- a/mir.c +++ b/mir.c @@ -6906,7 +6906,7 @@ void _MIR_dump_code (const char *name, uint8_t *code, size_t code_len) { #endif #else sprintf (bfname, "_mir_%lu.bin", (unsigned long) getpid ()); - if ((bf = fopen (bfname, "w")) == NULL) return; + if ((bf = fopen (bfname, "wb")) == NULL) return; fprintf (f, "void code (void) {}\n"); for (i = 0; i < code_len; i++) fputc (code[i], bf); fclose (f); From 40fb334222858aeadac3b70f7e36d8b67a8da0a3 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 28 May 2024 14:09:46 -0700 Subject: [PATCH 2/3] Set binary mode on stdin/stdout as necessary. --- mir-utils/b2ctab.c | 10 ++++++++++ mir-utils/b2m.c | 11 +++++++++++ mir-utils/m2b.c | 10 ++++++++++ 3 files changed, 31 insertions(+) diff --git a/mir-utils/b2ctab.c b/mir-utils/b2ctab.c index 2936c110bc..86a28c5407 100644 --- a/mir-utils/b2ctab.c +++ b/mir-utils/b2ctab.c @@ -2,6 +2,12 @@ #include "mir.h" +#ifdef _WIN32 +/* provides _fileno */ +#include /* provides _O_BINARY */ +#include /* provides _setmode */ +#endif + static size_t output_mir_code_byte_num; static FILE *output_mir_code_file; @@ -14,6 +20,10 @@ static int output_mir_code_byte (MIR_context_t ctx MIR_UNUSED, uint8_t byte) { int main (int argc, char *argv[]) { MIR_context_t ctx = MIR_init (); +#ifdef _WIN32 + if (_setmode (_fileno (stdin), _O_BINARY) == -1) return 1; +#endif + if (argc != 1) { fprintf (stderr, "Usage: %s < mir-binary-file > C-file\n", argv[1]); return 1; diff --git a/mir-utils/b2m.c b/mir-utils/b2m.c index 76fd085edc..6be258b1c0 100644 --- a/mir-utils/b2m.c +++ b/mir-utils/b2m.c @@ -1,9 +1,20 @@ /* Transform mir binary form from stdin into mir text to stdout. */ #include "mir.h" + +#ifdef _WIN32 +/* provides _fileno */ +#include /* provides _O_BINARY */ +#include /* provides _setmode */ +#endif + int main (int argc, char *argv[]) { MIR_context_t ctx = MIR_init (); +#ifdef _WIN32 + if (_setmode (_fileno (stdin), _O_BINARY) == -1) return 1; +#endif + if (argc != 1) { fprintf (stderr, "Usage: %s < mir-binary-file > mir-text-file\n", argv[1]); return 1; diff --git a/mir-utils/m2b.c b/mir-utils/m2b.c index 9f982c9cf8..5f9b7931c0 100644 --- a/mir-utils/m2b.c +++ b/mir-utils/m2b.c @@ -3,6 +3,12 @@ #include "mir.h" +#ifdef _WIN32 +/* provides _fileno */ +#include /* provides _O_BINARY */ +#include /* provides _setmode */ +#endif + DEF_VARR (char); int main (int argc, char *argv[]) { @@ -10,6 +16,10 @@ int main (int argc, char *argv[]) { VARR (char) * str; int c; +#ifdef _WIN32 + if (_setmode (_fileno (stdout), _O_BINARY) == -1) return 1; +#endif + if (argc != 1) { fprintf (stderr, "Usage: %s < mir-text-file > mir-binary-file\n", argv[1]); return 1; From d559aa93135ff531391b1b9b50e57a42a9cc4103 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Wed, 29 May 2024 15:50:16 -0700 Subject: [PATCH 3/3] Use a macro to reduce the number of ifdefs. --- mir-utils/b2ctab.c | 8 ++++---- mir-utils/b2m.c | 8 ++++---- mir-utils/m2b.c | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/mir-utils/b2ctab.c b/mir-utils/b2ctab.c index 86a28c5407..3a63c8bb18 100644 --- a/mir-utils/b2ctab.c +++ b/mir-utils/b2ctab.c @@ -6,6 +6,9 @@ /* provides _fileno */ #include /* provides _O_BINARY */ #include /* provides _setmode */ +#define set_filemode_binary(F) _setmode (_fileno (F), _O_BINARY) +#else +#define set_filemode_binary(F) 0 #endif static size_t output_mir_code_byte_num; @@ -20,10 +23,7 @@ static int output_mir_code_byte (MIR_context_t ctx MIR_UNUSED, uint8_t byte) { int main (int argc, char *argv[]) { MIR_context_t ctx = MIR_init (); -#ifdef _WIN32 - if (_setmode (_fileno (stdin), _O_BINARY) == -1) return 1; -#endif - + if (set_filemode_binary (stdin) == -1) return 1; if (argc != 1) { fprintf (stderr, "Usage: %s < mir-binary-file > C-file\n", argv[1]); return 1; diff --git a/mir-utils/b2m.c b/mir-utils/b2m.c index 6be258b1c0..3509900536 100644 --- a/mir-utils/b2m.c +++ b/mir-utils/b2m.c @@ -6,15 +6,15 @@ /* provides _fileno */ #include /* provides _O_BINARY */ #include /* provides _setmode */ +#define set_filemode_binary(F) _setmode (_fileno (F), _O_BINARY) +#else +#define set_filemode_binary(F) 0 #endif int main (int argc, char *argv[]) { MIR_context_t ctx = MIR_init (); -#ifdef _WIN32 - if (_setmode (_fileno (stdin), _O_BINARY) == -1) return 1; -#endif - + if (set_filemode_binary (stdin) == -1) return 1; if (argc != 1) { fprintf (stderr, "Usage: %s < mir-binary-file > mir-text-file\n", argv[1]); return 1; diff --git a/mir-utils/m2b.c b/mir-utils/m2b.c index 5f9b7931c0..643d2ccd85 100644 --- a/mir-utils/m2b.c +++ b/mir-utils/m2b.c @@ -7,6 +7,9 @@ /* provides _fileno */ #include /* provides _O_BINARY */ #include /* provides _setmode */ +#define set_filemode_binary(F) _setmode (_fileno (F), _O_BINARY) +#else +#define set_filemode_binary(F) 0 #endif DEF_VARR (char); @@ -16,10 +19,7 @@ int main (int argc, char *argv[]) { VARR (char) * str; int c; -#ifdef _WIN32 - if (_setmode (_fileno (stdout), _O_BINARY) == -1) return 1; -#endif - + if (set_filemode_binary (stdout) == -1) return 1; if (argc != 1) { fprintf (stderr, "Usage: %s < mir-text-file > mir-binary-file\n", argv[1]); return 1;