--- ./src/ascmagic.c~0	2021-05-10 01:34:02.000000000 +0300
+++ ./src/ascmagic.c	2021-10-24 11:55:40.512500000 +0300
@@ -273,7 +273,7 @@ file_ascmagic_with_encoding(struct magic
 				goto done;
 
 		if (has_long_lines)
-			if (file_printf(ms, ", with very long lines (%zu)",
+			if (file_printf(ms, ", with very long lines (%" SIZE_T_FORMAT "u)",
 			    has_long_lines) == -1)
 				goto done;
 
--- ./src/cdf_time.c~0	2019-03-12 22:43:05.000000000 +0200
+++ ./src/cdf_time.c	2021-10-24 11:58:22.387500000 +0300
@@ -171,8 +171,13 @@ cdf_ctime(const time_t *sec, char *buf)
 	char *ptr = ctime_r(sec, buf);
 	if (ptr != NULL)
 		return buf;
+#ifdef WIN32
+	(void)snprintf(buf, 26, "*Bad* 0x%16.16I64x\n",
+	    CAST(long long, *sec));
+#else
 	(void)snprintf(buf, 26, "*Bad* %#16.16" INT64_T_FORMAT "x\n",
 	    CAST(long long, *sec));
+#endif
 	return buf;
 }
 
--- ./src/compress.c~0	2020-12-08 23:26:00.000000000 +0200
+++ ./src/compress.c	2021-10-24 16:30:31.700000000 +0300
@@ -449,7 +454,21 @@ file_pipe2file(struct magic_set *ms, int
 	ssize_t r;
 	int tfd;
 
+#ifdef WIN32
+	char *t;
+	buf[0] = '\0';
+	if ((t = getenv("TEMP")) != NULL)
+		(void)strlcpy(buf, t, sizeof buf);
+	else if ((t = getenv("TMP")) != NULL)
+		(void)strlcpy(buf, t, sizeof buf);
+	else if ((t = getenv("TMPDIR")) != NULL)
+		(void)strlcpy(buf, t, sizeof buf);
+	if (buf[0] != '\0')
+		(void)strcat(buf, "/");
+	(void)strcat(buf, "file.XXXXXX");
+#else
 	(void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
+#endif
--- ./src/file.h~0	2021-07-03 16:49:16.000000000 +0300
+++ ./src/file.h	2021-10-24 16:12:24.918750000 +0300
@@ -70,10 +70,14 @@
 #  define INT64_T_FORMAT "I64"
 #  define INTMAX_T_FORMAT "I64"
 # endif
+# define INT64_T_FORMAT_X "0x%I64"
+# define INTMAX_T_FORMAT_X "0x%I64"
 #else
 # define SIZE_T_FORMAT "z"
 # define INT64_T_FORMAT "ll"
+# define INT64_T_FORMAT_X "%#ll"
 # define INTMAX_T_FORMAT "j"
+# define INTMAX_T_FORMAT_X "%#j"
 #endif
 
 #include <stdio.h>	/* Include that here, to make sure __P gets defined */
--- ./src/funcs.c~0	2021-07-03 16:49:16.000000000 +0300
+++ ./src/funcs.c	2021-10-24 15:27:32.575000000 +0300
@@ -146,7 +149,7 @@ file_vprintf(struct magic_set *ms, const
 		size_t blen = ms->o.blen;
 		free(buf);
 		file_clearbuf(ms);
-		file_error(ms, 0, "Output buffer space exceeded %d+%zu", len,
+		file_error(ms, 0, "Output buffer space exceeded %d+%" SIZE_T_FORMAT "u", len,
 		    blen);
 		return -1;
 	}
@@ -790,11 +793,25 @@ protected int
 file_parse_guid(const char *s, uint64_t *guid)
 {
 	struct guid *g = CAST(struct guid *, CAST(void *, guid));
+#ifndef WIN32
 	return sscanf(s,
 	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
 	    &g->data1, &g->data2, &g->data3, &g->data4[0], &g->data4[1],
 	    &g->data4[2], &g->data4[3], &g->data4[4], &g->data4[5],
 	    &g->data4[6], &g->data4[7]) == 11 ? 0 : -1;
+#else
+	/* MS-Windows runtime doesn't support %hhx, except under
+	   non-default __USE_MINGW_ANSI_STDIO.  */
+	uint16_t data16[8];
+	int rv = sscanf(s, "%8x-%4hx-%4hx-%2hx%2hx-%2hx%2hx%2hx%2hx%2hx%2hx",
+	    &g->data1, &g->data2, &g->data3, &data16[0], &data16[1],
+	    &data16[2], &data16[3], &data16[4], &data16[5],
+	    &data16[6], &data16[7]) == 11 ? 0 : -1;
+	int i;
+	for (i = 0; i < 8; i++)
+		g->data4[i] = data16[i];
+	return rv;
+#endif
 }
 
 protected int
@@ -803,11 +820,19 @@ file_print_guid(char *str, size_t len, c
 	const struct guid *g = CAST(const struct guid *,
 	    CAST(const void *, guid));
 
+#ifndef WIN32
 	return snprintf(str, len, "%.8X-%.4hX-%.4hX-%.2hhX%.2hhX-"
 	    "%.2hhX%.2hhX%.2hhX%.2hhX%.2hhX%.2hhX",
 	    g->data1, g->data2, g->data3, g->data4[0], g->data4[1],
 	    g->data4[2], g->data4[3], g->data4[4], g->data4[5],
 	    g->data4[6], g->data4[7]);
+#else
+	return snprintf(str, len, "%.8X-%.4hX-%.4hX-%.2hX%.2hX-"
+	    "%.2hX%.2hX%.2hX%.2hX%.2hX%.2hX",
+	    g->data1, g->data2, g->data3, g->data4[0], g->data4[1],
+	    g->data4[2], g->data4[3], g->data4[4], g->data4[5],
+	    g->data4[6], g->data4[7]);
+#endif
 }
 
 protected int
@@ -818,15 +843,21 @@ file_pipe_closexec(int *fds)
 #else
 	if (pipe(fds) == -1)
 		return -1;
+#ifdef F_SETFD
 	(void)fcntl(fds[0], F_SETFD, FD_CLOEXEC);
 	(void)fcntl(fds[1], F_SETFD, FD_CLOEXEC);
+#endif
 	return 0;
 #endif
 }
 
 protected int
-file_clear_closexec(int fd) {
+file_clear_closexec(int fd __attribute__ ((__unused__))) {
+#ifdef F_SETFD
 	return fcntl(fd, F_SETFD, 0);
+#else
+	return 0;
+#endif
 }
 
 protected char *
--- ./src/magic.c~0	2021-09-23 22:41:43.000000000 +0300
+++ ./src/magic.c	2021-10-23 18:09:18.090625000 +0300
@@ -221,6 +221,10 @@ out:
 		default_magic = NULL;
 	}
 
+	/* Before anything else, try to get a magic file from user HOME */
+	if ((home = getenv("HOME")) != NULL)
+		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
+
 	/* First, try to get a magic file from user-application data */
 	if ((home = getenv("LOCALAPPDATA")) != NULL)
 		_w32_append_path(&hmagicpath, "%s%s", home, hmagic);
@@ -273,9 +277,22 @@ unreadable_info(struct magic_set *ms, mo
 		if (access(file, W_OK) == 0)
 			if (file_printf(ms, "writable, ") == -1)
 				return -1;
+#ifndef WIN32
 		if (access(file, X_OK) == 0)
 			if (file_printf(ms, "executable, ") == -1)
 				return -1;
+#else
+		/* X_OK doesn't work well on MS-Windows */
+		{
+			char *p = strrchr(file, '.');
+			if (p && (stricmp(p, ".exe")
+				  || stricmp(p, ".dll")
+				  || stricmp(p, ".bat")
+				  || stricmp(p, ".cmd")))
+				if (file_printf(ms, "writable, ") == -1)
+					return -1;
+		}
+#endif
 	}
 	if (S_ISREG(md))
 		if (file_printf(ms, "regular file, ") == -1)
@@ -462,7 +479,7 @@ file_or_fd(struct magic_set *ms, const c
 			rv = 0;
 			goto done;
 		}
-#if O_CLOEXEC == 0
+#if O_CLOEXEC == 0 && !defined(__MINGW32__)
 		(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
 #endif
 	}
--- ./src/readelf.c~0	2021-07-03 16:49:16.000000000 +0300
+++ ./src/readelf.c	2021-10-24 11:32:50.450000000 +0300
@@ -378,7 +378,7 @@ dophn_core(struct magic_set *ms, int cla
 		if (pread(fd, xph_addr, xph_sizeof, off) <
 		    CAST(ssize_t, xph_sizeof)) {
 			if (file_printf(ms, 
-			    ", can't read elf program headers at %jd",
+			    ", can't read elf program headers at %" INTMAX_T_FORMAT "d",
 			    (intmax_t)off) == -1)
 				return -1;
 			return 0;
@@ -400,7 +400,7 @@ dophn_core(struct magic_set *ms, int cla
 		len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
 		offs = xph_offset;
 		if ((bufsize = pread(fd, nbuf, len, offs)) == -1) {
-			if (file_printf(ms, " can't read note section at %jd",
+			if (file_printf(ms, " can't read note section at %" INTMAX_T_FORMAT "d",
 			    (intmax_t)offs) == -1)
 				return -1;
 			return 0;
@@ -948,7 +948,7 @@ get_offset_from_virtaddr(struct magic_se
 		if (pread(fd, xph_addr, xph_sizeof, off) <
 		    CAST(ssize_t, xph_sizeof)) {
 			if (file_printf(ms,
-			    ", can't read elf program header at %jd",
+			    ", can't read elf program header at %" INTMAX_T_FORMAT "d",
 			    (intmax_t)off) == -1)
 				return -1;
 			return 0;
@@ -982,7 +982,7 @@ get_string_on_virtaddr(struct magic_set 
 	    fsize, virtaddr);
 	if (offset < 0 ||
 	    (buflen = pread(fd, buf, CAST(size_t, buflen), offset)) <= 0) {
-		(void)file_printf(ms, ", can't read elf string at %jd",
+		(void)file_printf(ms, ", can't read elf string at %" INTMAX_T_FORMAT "d",
 		    (intmax_t)offset);
 		return 0;
 	}
@@ -1370,7 +1370,7 @@ doshn(struct magic_set *ms, int clazz, i
 	/* Read offset of name section to be able to read section names later */
 	offs = CAST(off_t, (off + size * strtab));
 	if (pread(fd, xsh_addr, xsh_sizeof, offs) < CAST(ssize_t, xsh_sizeof)) {
-		if (file_printf(ms, ", missing section headers at %jd",
+		if (file_printf(ms, ", missing section headers at %" INTMAX_T_FORMAT "d",
 		    (intmax_t)offs) == -1)
 			return -1;
 		return 0;
@@ -1378,7 +1378,7 @@ doshn(struct magic_set *ms, int clazz, i
 	name_off = xsh_offset;
 
 	if (fsize != SIZE_UNKNOWN && fsize < name_off) {
-		if (file_printf(ms, ", too large section header offset %jd",
+		if (file_printf(ms, ", too large section header offset %" INTMAX_T_FORMAT "d",
 		    (intmax_t)name_off) == -1)
 			return -1;
 		return 0;
@@ -1390,7 +1390,7 @@ doshn(struct magic_set *ms, int clazz, i
 		if ((namesize = pread(fd, name, sizeof(name) - 1, offs))
 		    == -1) {
 			if (file_printf(ms, 
-			    ", can't read name of elf section at %jd",
+			    ", can't read name of elf section at %" INTMAX_T_FORMAT "d",
 			    (intmax_t)offs) == -1)
 				return -1;
 			return 0;
@@ -1403,7 +1403,7 @@ doshn(struct magic_set *ms, int clazz, i
 
 		if (pread(fd, xsh_addr, xsh_sizeof, off) <
 		    CAST(ssize_t, xsh_sizeof)) {
-			if (file_printf(ms, ", can't read elf section at %jd",
+			if (file_printf(ms, ", can't read elf section at %" INTMAX_T_FORMAT "d",
 			    (intmax_t)off) == -1)
 				return -1;
 			return 0;
@@ -1433,9 +1433,9 @@ doshn(struct magic_set *ms, int clazz, i
 			if (CAST(uintmax_t, (xsh_size + xsh_offset)) >
 			    CAST(uintmax_t, fsize)) {
 				if (file_printf(ms,
-				    ", note offset/size %#" INTMAX_T_FORMAT
-				    "x+%#" INTMAX_T_FORMAT "x exceeds"
-				    " file size %#" INTMAX_T_FORMAT "x",
+				    ", note offset/size " INTMAX_T_FORMAT_X
+				    "x+" INTMAX_T_FORMAT_X "x exceeds"
+				    " file size " INTMAX_T_FORMAT_X "x",
 				    CAST(uintmax_t, xsh_offset),
 				    CAST(uintmax_t, xsh_size),
 				    CAST(uintmax_t, fsize)) == -1)
@@ -1452,7 +1452,7 @@ doshn(struct magic_set *ms, int clazz, i
 			    CAST(ssize_t, xsh_size)) {
 				free(nbuf);
 				if (file_printf(ms,
-				    ", can't read elf note at %jd",
+				    ", can't read elf note at %" INTMAX_T_FORMAT "d",
 				    (intmax_t)offs) == -1)
 					return -1;
 				return 0;
@@ -1546,8 +1546,8 @@ doshn(struct magic_set *ms, int clazz, i
 				default:
 					if (file_printf(ms,
 					    ", with unknown capability "
-					    "%#" INT64_T_FORMAT "x = %#"
-					    INT64_T_FORMAT "x",
+					    INT64_T_FORMAT_X "x = "
+					    INT64_T_FORMAT_X "x",
 					    CAST(unsigned long long, xcap_tag),
 					    CAST(unsigned long long, xcap_val))
 					    == -1)
@@ -1601,13 +1601,13 @@ doshn(struct magic_set *ms, int clazz, i
 			}
 			if (cap_hw1)
 				if (file_printf(ms,
-				    " unknown hardware capability %#"
-				    INT64_T_FORMAT "x",
+				    " unknown hardware capability "
+				    INT64_T_FORMAT_X "x",
 				    CAST(unsigned long long, cap_hw1)) == -1)
 					return -1;
 		} else {
 			if (file_printf(ms,
-			    " hardware capability %#" INT64_T_FORMAT "x",
+			    " hardware capability " INT64_T_FORMAT_X "x",
 			    CAST(unsigned long long, cap_hw1)) == -1)
 				return -1;
 		}
@@ -1623,8 +1623,8 @@ doshn(struct magic_set *ms, int clazz, i
 		cap_sf1 &= ~SF1_SUNW_MASK;
 		if (cap_sf1)
 			if (file_printf(ms,
-			    ", with unknown software capability %#"
-			    INT64_T_FORMAT "x",
+			    ", with unknown software capability "
+			    INT64_T_FORMAT_X "x",
 			    CAST(unsigned long long, cap_sf1)) == -1)
 				return -1;
 	}
@@ -1669,7 +1669,7 @@ dophn_exec(struct magic_set *ms, int cla
 		if (pread(fd, xph_addr, xph_sizeof, off) <
 		    CAST(ssize_t, xph_sizeof)) {
 			if (file_printf(ms,
-			    ", can't read elf program headers at %jd",
+			    ", can't read elf program headers at %" INTMAX_T_FORMAT "d",
 			    (intmax_t)off) == -1)
 				return -1;
 			return 0;
@@ -1690,7 +1690,11 @@ dophn_exec(struct magic_set *ms, int cla
 			if (((align = xph_align) & 0x80000000UL) != 0 ||
 			    align < 4) {
 				if (file_printf(ms,
+#ifdef WIN32
+				    ", invalid note alignment 0x%lx",
+#else
 				    ", invalid note alignment %#lx",
+#endif
 				    CAST(unsigned long, align)) == -1)
 					return -1;
 				align = 4;
@@ -1715,7 +1719,7 @@ dophn_exec(struct magic_set *ms, int cla
 			bufsize = pread(fd, nbuf, len, offs);
 			if (bufsize == -1) {
 				if (file_printf(ms,
-				    ", can't read section at %jd",
+				    ", can't read section at %" INTMAX_T_FORMAT "d",
 				    (intmax_t)offs) == -1)
 					return -1;
 				return 0;
