Skip to content

Commit 0c8524a

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 ff6d6c0 commit 0c8524a

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

generate-cat-file.c

+11-14
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

@@ -1290,6 +1286,17 @@ int parse_hwids_arg(char *hwids, struct list_node **hwid)
12901286

12911287
int main(int argc, char **argv)
12921288
{
1289+
#ifdef IS_WINDOWS
1290+
#ifdef _O_BINARY
1291+
if (_setmode(_fileno(stdout), _O_BINARY) == -1)
1292+
#endif
1293+
{
1294+
fatal("cannot set binary mode for stdout\noperation canceled due to translation(known \"corruption\" in text mode)\nhttps://stackoverflow.com/a/5537079");
1295+
}
1296+
1297+
#endif /* IS_WINDOWS */
1298+
1299+
12931300
struct pkcs7_toplevel s = { 0 };
12941301
struct known_oids oids = { 0 };
12951302

@@ -1405,16 +1412,6 @@ int main(int argc, char **argv)
14051412
//free(hardware_ids);
14061413
//hardware_ids = NULL;
14071414

1408-
#ifdef IS_WINDOWS
1409-
#ifdef HAVE_SETMODE
1410-
if (_setmode(_fileno(stdout), _O_BINARY) == -1)
1411-
fatal("cannot set binary mode for stdout\noutput canceled due to translation(known \"corruption\" in text mode)\nhttps://stackoverflow.com/a/5537079");
1412-
#else
1413-
freopen("CON", "wb", stdout);
1414-
//stdout = fdopen(STDOUT_FILENO, "wb");
1415-
#endif
1416-
#endif
1417-
14181415
/* and write to stdout or so ... */
14191416
fwrite(buffer, buflen, 1, stdout);
14201417
free(buffer); buffer = NULL;

strip-pe-image.c

+9-12
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)