Skip to content

Commit 4ab6ac1

Browse files
XakerTwojohannesthoma
authored andcommitted
update compatibility fix - remove freopen() and fdopen()
freopen() - with "CON" redirection is broken and output always prints to console window; NULL is not allowed in windows fdopen() - not works plus stdout is not assignable so _setmode() is the only option
1 parent 62649d8 commit 4ab6ac1

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

generate-cat-file.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
#include <io.h>
1616

1717
#define IS_WINDOWS
18-
19-
#ifdef _O_BINARY
20-
#define HAVE_SETMODE
21-
#endif
2218
#endif
2319

2420

@@ -1263,6 +1259,17 @@ int parse_hwids_arg(char *hwids, struct list_node **hwid)
12631259

12641260
int main(int argc, char **argv)
12651261
{
1262+
#ifdef IS_WINDOWS
1263+
#ifdef _O_BINARY
1264+
if (_setmode(_fileno(stdout), _O_BINARY) == -1)
1265+
#endif
1266+
{
1267+
fatal("cannot set binary mode for stdout\noperation canceled due to translation(known \"corruption\" in text mode)\nhttps://stackoverflow.com/a/5537079");
1268+
}
1269+
1270+
#endif /* IS_WINDOWS */
1271+
1272+
12661273
struct pkcs7_toplevel s = { 0 };
12671274
struct known_oids oids = { 0 };
12681275

@@ -1377,16 +1384,6 @@ int main(int argc, char **argv)
13771384
a_guid = NULL;
13781385
hwids = NULL;
13791386

1380-
#ifdef IS_WINDOWS
1381-
#ifdef HAVE_SETMODE
1382-
if (_setmode(_fileno(stdout), _O_BINARY) == -1)
1383-
fatal("cannot set binary mode for stdout\noutput canceled due to translation(known \"corruption\" in text mode)\nhttps://stackoverflow.com/a/5537079");
1384-
#else
1385-
freopen("CON", "wb", stdout);
1386-
//stdout = fdopen(STDOUT_FILENO, "wb");
1387-
#endif
1388-
#endif
1389-
13901387
/* and write to stdout or so ... */
13911388
fwrite(buffer, buflen, 1, stdout);
13921389
free(buffer); buffer = NULL;

strip-pe-image.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
#include <io.h>
1010

1111
#define IS_WINDOWS
12-
13-
#ifdef _O_BINARY
14-
#define HAVE_SETMODE
15-
#endif
1612
#endif
1713

1814
char *read_file(const char *fname, long *size_return)
@@ -22,20 +18,21 @@ char *read_file(const char *fname, long *size_return)
2218
FILE *f;
2319

2420
#ifdef IS_WINDOWS
25-
#ifdef HAVE_SETMODE
21+
#ifdef _O_BINARY
2622
if (_setmode(_fileno(stdout), _O_BINARY) == -1)
23+
#endif
2724
{
28-
fprintf(stderr, "cannot set binary mode for stdout\noutput canceled due to translation(known \"corruption\" in text mode)\nhttps://stackoverflow.com/a/5537079");
25+
fprintf(stderr, "cannot set binary mode for stdout\noperation canceled due to translation(known \"corruption\" in text mode)\nhttps://stackoverflow.com/a/5537079");
2926
exit(1);
3027
}
31-
#else
32-
freopen("CON", "wb", stdout);
33-
//stdout = fdopen(STDOUT_FILENO, "wb");
34-
#endif
3528
f = fopen(fname, "rb");
36-
#else
29+
30+
#else /* not IS_WINDOWS */
31+
3732
f = fopen(fname, "r");
38-
#endif
33+
34+
#endif /* IS_WINDOWS */
35+
3936

4037
if (f == NULL) {
4138
perror("opening image file");

0 commit comments

Comments
 (0)