From 082fa31119cdea91fd9c1589241519765f79b9e4 Mon Sep 17 00:00:00 2001
From: Mathieu Westphal <mathieu.westphal@kitware.com>
Date: Thu, 27 Feb 2025 13:30:37 +0100
Subject: [PATCH] Windows: Fix includes in lib in order to support compilation
 on Windows

---
 src/file.c              | 16 +++++++++++-----
 src/input_file.c        |  8 ++++++++
 src/iterator_fallback.c |  6 ++++++
 src/stream_cdf_i.c      |  8 +++++++-
 4 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/src/file.c b/src/file.c
index 2086da8a5..bd602ef2b 100644
--- a/src/file.c
+++ b/src/file.c
@@ -10,19 +10,25 @@
 #include "config.h"
 #endif
 
-#include <unistd.h>
-
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <io.h>
 #include <limits.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdbool.h>
 #include <string.h>
-#include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/types.h>
+
+// On Windows, define ssize_t manually
+#ifdef _WIN32
+#define ssize_t __int64
+#else
+#include <unistd.h>
+#endif
 
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>  // gettimeofday()
@@ -392,7 +398,7 @@ fileFlush(int fileID)
     {
       FILE *fp = fileptr->fp;
       retval = fflush(fp);
-      if (retval == 0) retval = fsync(fileno(fp));
+      if (retval == 0) retval = fflush(fp);
       if (retval != 0) retval = errno;
     }
 
diff --git a/src/input_file.c b/src/input_file.c
index f34d24c48..4c920efa2 100644
--- a/src/input_file.c
+++ b/src/input_file.c
@@ -18,8 +18,16 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <io.h>
 #include <string.h>
+
+// On Windows, define ssize_t and pread manually
+#ifdef _WIN32
+#define ssize_t __int64
+#define pread read
+#else
 #include <unistd.h>
+#endif
 
 #if HAVE_PTHREAD
 #include <pthread.h>
diff --git a/src/iterator_fallback.c b/src/iterator_fallback.c
index a2252bd8a..fd7bc6db7 100644
--- a/src/iterator_fallback.c
+++ b/src/iterator_fallback.c
@@ -19,7 +19,13 @@
 #include <assert.h>
 #include <limits.h>
 #include <stdlib.h>
+
+// On Windows, define ssize_t manually
+#ifdef _WIN32
+#define ssize_t __int64
+#else
 #include <unistd.h>
+#endif
 
 struct CdiFallbackIterator
 {
diff --git a/src/stream_cdf_i.c b/src/stream_cdf_i.c
index 708565eca..3096c518c 100644
--- a/src/stream_cdf_i.c
+++ b/src/stream_cdf_i.c
@@ -6,7 +6,6 @@
 
 #include <ctype.h>
 #include <limits.h>
-#include <unistd.h>
 
 #include "dmemory.h"
 #include "cdi_int.h"
@@ -19,6 +18,13 @@
 #include "cdf_lazy_grid.h"
 #include "cdf_filter.h"
 
+// On Windows, define strcasecmp manually
+#ifdef _WIN32
+#define strcasecmp _stricmp
+#else
+#include <unistd.h>
+#endif
+
 enum VarStatus
 {
   UndefVar = -1,
-- 
GitLab