Skip to content

Commit 4a3fa10

Browse files
XakerTwojohannesthoma
authored andcommitted
compatibility fix, allow compilation and proper redirect on windows
marcos for sys/errno.h should be re-checked
1 parent 38a6c99 commit 4a3fa10

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

generate-cat-file.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@
22
#include <stdlib.h>
33
#include <stdbool.h>
44
#include <string.h>
5-
#include <sys/errno.h>
65
#include <ctype.h>
76
#include <unistd.h>
87
#include <time.h>
98

9+
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
10+
#include <sys/errno.h>
11+
#endif
12+
13+
#if defined(_WINDOWS) || defined(_WIN32) || defined(WIN32)
14+
#include <fcntl.h>
15+
#include <io.h>
16+
17+
#define IS_WINDOWS
18+
19+
#ifdef _O_BINARY
20+
#define HAVE_SETMODE
21+
#endif
22+
#endif
23+
24+
1025
/* DER encoding */
1126

1227
#define INTEGER_TAG 0x02
@@ -1362,6 +1377,16 @@ int main(int argc, char **argv)
13621377
a_guid = NULL;
13631378
hwids = NULL;
13641379

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+
13651390
/* and write to stdout or so ... */
13661391
fwrite(buffer, buflen, 1, stdout);
13671392
free(buffer); buffer = NULL;

0 commit comments

Comments
 (0)