5544c1b
From 27ca56812d2140c774f9d2b67a2919ef47c69758 Mon Sep 17 00:00:00 2001
5544c1b
From: Stefan Weil <sw@weilnetz.de>
5544c1b
Date: Sat, 22 Sep 2012 22:26:19 +0200
5544c1b
Subject: [PATCH] w32: Add implementation of gmtime_r, localtime_r
5544c1b
5544c1b
Those functions are missing in MinGW.
5544c1b
5544c1b
Some versions of MinGW-w64 include defines for gmtime_r and localtime_r.
5544c1b
Older versions of these macros are buggy (they return a pointer to a
5544c1b
static variable), therefore we don't want them. Newer versions are
5544c1b
similar to the code used here, but without the memset.
5544c1b
5544c1b
The implementation which is used here is not strictly reentrant,
5544c1b
but sufficiently good for QEMU on w32 or w64.
5544c1b
5544c1b
Signed-off-by: Stefan Weil <sw@weilnetz.de>
5544c1b
[blauwirbel@gmail.com: added comment about locking]
5544c1b
Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
5544c1b
(cherry picked from commit d3e8f95753114a827f9cd8e819b1d5cc8333f76b)
5544c1b
5544c1b
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
5544c1b
---
5544c1b
 oslib-win32.c   | 24 ++++++++++++++++++++++++
5544c1b
 qemu-os-win32.h |  6 ++++++
5544c1b
 2 files changed, 30 insertions(+)
5544c1b
5544c1b
diff --git a/oslib-win32.c b/oslib-win32.c
5544c1b
index ffbc6d0..51b33e8 100644
5544c1b
--- a/oslib-win32.c
5544c1b
+++ b/oslib-win32.c
5544c1b
@@ -74,6 +74,30 @@ void qemu_vfree(void *ptr)
5544c1b
     VirtualFree(ptr, 0, MEM_RELEASE);
5544c1b
 }
5544c1b
 
5544c1b
+/* FIXME: add proper locking */
5544c1b
+struct tm *gmtime_r(const time_t *timep, struct tm *result)
5544c1b
+{
5544c1b
+    struct tm *p = gmtime(timep);
5544c1b
+    memset(result, 0, sizeof(*result));
5544c1b
+    if (p) {
5544c1b
+        *result = *p;
5544c1b
+        p = result;
5544c1b
+    }
5544c1b
+    return p;
5544c1b
+}
5544c1b
+
5544c1b
+/* FIXME: add proper locking */
5544c1b
+struct tm *localtime_r(const time_t *timep, struct tm *result)
5544c1b
+{
5544c1b
+    struct tm *p = localtime(timep);
5544c1b
+    memset(result, 0, sizeof(*result));
5544c1b
+    if (p) {
5544c1b
+        *result = *p;
5544c1b
+        p = result;
5544c1b
+    }
5544c1b
+    return p;
5544c1b
+}
5544c1b
+
5544c1b
 void socket_set_block(int fd)
5544c1b
 {
5544c1b
     unsigned long opt = 0;
5544c1b
diff --git a/qemu-os-win32.h b/qemu-os-win32.h
5544c1b
index b3e451b..8ba466d 100644
5544c1b
--- a/qemu-os-win32.h
5544c1b
+++ b/qemu-os-win32.h
5544c1b
@@ -68,6 +68,12 @@
5544c1b
 /* Declaration of ffs() is missing in MinGW's strings.h. */
5544c1b
 int ffs(int i);
5544c1b
 
5544c1b
+/* Missing POSIX functions. Don't use MinGW-w64 macros. */
5544c1b
+#undef gmtime_r
5544c1b
+struct tm *gmtime_r(const time_t *timep, struct tm *result);
5544c1b
+#undef localtime_r
5544c1b
+struct tm *localtime_r(const time_t *timep, struct tm *result);
5544c1b
+
5544c1b
 static inline void os_setup_signal_handling(void) {}
5544c1b
 static inline void os_daemonize(void) {}
5544c1b
 static inline void os_setup_post(void) {}