Skip to content

Commit

Permalink
Merge pull request #399 from notwa/binary-mode
Browse files Browse the repository at this point in the history
Open binary files in binary mode
  • Loading branch information
vnmakarov authored Jun 4, 2024
2 parents b02f955 + d559aa9 commit 4cd05d9
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion adt-tests/mir-reduce-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <inputfile>\n", argv[0]);
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion c2mir/c2mir-driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
}
Expand Down
4 changes: 2 additions & 2 deletions mir-bin-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion mir-tests/test-read.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions mir-utils/b2ctab.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

#include "mir.h"

#ifdef _WIN32
/* <stdio.h> provides _fileno */
#include <fcntl.h> /* provides _O_BINARY */
#include <io.h> /* 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;
static FILE *output_mir_code_file;

Expand All @@ -14,6 +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 ();

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;
Expand Down
11 changes: 11 additions & 0 deletions mir-utils/b2m.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
/* Transform mir binary form from stdin into mir text to stdout. */

#include "mir.h"

#ifdef _WIN32
/* <stdio.h> provides _fileno */
#include <fcntl.h> /* provides _O_BINARY */
#include <io.h> /* 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 ();

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;
Expand Down
10 changes: 10 additions & 0 deletions mir-utils/m2b.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@

#include "mir.h"

#ifdef _WIN32
/* <stdio.h> provides _fileno */
#include <fcntl.h> /* provides _O_BINARY */
#include <io.h> /* provides _setmode */
#define set_filemode_binary(F) _setmode (_fileno (F), _O_BINARY)
#else
#define set_filemode_binary(F) 0
#endif

DEF_VARR (char);

int main (int argc, char *argv[]) {
MIR_context_t ctx = MIR_init ();
VARR (char) * str;
int c;

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;
Expand Down
2 changes: 1 addition & 1 deletion mir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4cd05d9

Please sign in to comment.