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));
@przemoc
Copy link
Author

przemoc commented Feb 7, 2016

@patroza
Copy link

patroza commented Oct 26, 2017

@przemoc thanks a lot! However it seems the program is not compiled with utf-8 arguments support, would this be possible?
e.g Описание.pdf returns: pigz.exe: skipping: ????????.pdf does not exist

@przemoc
Copy link
Author

przemoc commented Feb 17, 2018

commit 454c707ca233ee6ed64e51428be4ba6ea4296539
Author: Przemyslaw Pawelczyk <[email protected]>
Date:   2018-02-17 13:26:30 +0100

    Create new patch for building pigz 2.4 on MSYS2+MinGW-w64.

@przemoc
Copy link
Author

przemoc commented Feb 17, 2018

@patroza Yeah, it is possible, but would require a bit more intrusive changes in the source code that I am willing to do right now.

@neurolabusc
Copy link

@przemoc and @patroza - I have created a fork that I think handles non-Latin filenames like Описание.pdf. I do not typically use Windows, and only use Latin characters in my own work, so I would be grateful for any advice.

@neurolabusc
Copy link

By the way, I made my changes to for MSVC, and I do not have any experience with MINGW32. It is possible that a few changes of the ifdef clauses one could use wmain with MINGW32. If you implement this, I would like to know about it. My ultimate goal is a pull request to the main pigs repository that would provide robust support for Windows, and supporting both MINGW32 and MSVC would be fantastic.

@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