Skip to content

Instantly share code, notes, and snippets.

@przemoc
Last active March 27, 2024 09:35
Show Gist options
  • Save przemoc/bd6c342ff7f31a0c4ec2 to your computer and use it in GitHub Desktop.
Save przemoc/bd6c342ff7f31a0c4ec2 to your computer and use it in GitHub Desktop.
Make pigz.c compilable on MSYS2+MinGW-w64
Make pigz.c (2.4) compilable on MSYS2+MinGW-w64.
diff --git a/pigz.c b/pigz.c
index bbbfd2cd19a1..85a61eeca674 100644
--- a/pigz.c
+++ b/pigz.c
@@ -324,6 +324,11 @@
// Use large file functions if available.
#define _FILE_OFFSET_BITS 64
+#ifdef __MINGW32__
+/* Support 'j' length modifier (intmax_t) in printf(). */
+#define __USE_MINGW_ANSI_STDIO 1
+#endif
+
// Included headers and what is expected from each.
#include <stdio.h> // fflush(), fprintf(), fputs(), getchar(), putc(),
// puts(), printf(), vasprintf(), stderr, EOF, NULL,
@@ -385,6 +390,15 @@
# define utimes(p,t) 0
# define lstat(p,s) stat(p,s)
# define _exit(s) exit(s)
+
+#define WINAPI __stdcall
+#define INVALID_HANDLE_VALUE ((HANDLE)-1)
+
+typedef void *HANDLE;
+typedef int BOOL;
+
+BOOL WINAPI FlushFileBuffers(HANDLE hFile);
+
#endif
#include "zlib.h" // deflateInit2(), deflateReset(), deflate(),
@@ -3640,10 +3654,22 @@ local void touch(char *path, time_t t) {
local void out_push(void) {
if (g.outd == -1)
return;
+#ifdef __MINGW32__
+ int ret = -1;
+ HANDLE h = (HANDLE)_get_osfhandle(g.outd);
+ if (h == INVALID_HANDLE_VALUE)
+ errno = EBADF;
+ else {
+ ret = -!FlushFileBuffers(h);
+ if (ret == -1)
+ errno = EINVAL;
+ }
+#else
#ifdef F_FULLSYNC
int ret = fcntl(g.outd, F_FULLSYNC);
#else
int ret = fsync(g.outd);
+#endif
#endif
if (ret == -1)
throw(errno, "sync error on %s (%s)", g.outf, strerror(errno));
@mateli
Copy link

mateli commented Mar 27, 2024

Is it impossible to get this to work with the cygwin api's in mingw? Rather than introducing direct Windows API calls?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment