Add Windows ctime_r implementation and add empty ioctl.h header
authorBruce Cran <bruce.cran@gmail.com>
Wed, 6 May 2015 22:30:46 +0000 (16:30 -0600)
committerJens Axboe <axboe@fb.com>
Thu, 7 May 2015 16:57:00 +0000 (10:57 -0600)
stat.c now uses ctime_r(), so add an implementation for Windows.
It's expected that ioctl.h exists on each platform, even if it's
not used: add an empty file on Windows.

Signed-off-by: Jens Axboe <axboe@fb.com>
os/windows/posix.c
os/windows/posix/include/sys/ioctl.h [new file with mode: 0644]

index d238c64a16219607ced8eb7dd4807552361046e6..41fc480df9f14f92a75ff07a5fda30fa098deb06 100755 (executable)
@@ -229,6 +229,30 @@ char *dlerror(void)
        return dl_error;
 }
 
        return dl_error;
 }
 
+/* Copied from http://blogs.msdn.com/b/joshpoley/archive/2007/12/19/date-time-formats-and-conversions.aspx */
+void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime)
+{
+    LARGE_INTEGER jan1970FT;
+    LARGE_INTEGER utcFT;
+    jan1970FT.QuadPart = 116444736000000000LL; // january 1st 1970
+    utcFT.QuadPart = ((unsigned __int64)dosTime) * 10000000 + jan1970FT.QuadPart;
+
+    FileTimeToSystemTime((FILETIME*)&utcFT, systemTime);
+}
+
+char* ctime_r(const time_t *t, char *buf)
+{
+    SYSTEMTIME systime;
+    const char * const dayOfWeek[] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
+    const char * const monthOfYear[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+
+    Time_tToSystemTime(*t, &systime);
+    /* We don't know how long `buf` is, but assume it's rounded up from the minimum of 25 to 32 */
+    StringCchPrintfA(buf, 32, "%s %s %d %02d:%02d:%02d %04d", dayOfWeek[systime.wDayOfWeek - 1], monthOfYear[systime.wMonth - 1],
+                                                                                systime.wDay, systime.wHour, systime.wMinute, systime.wSecond, systime.wYear);
+    return buf;
+}
+
 int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
 {
        FILETIME fileTime;
 int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
 {
        FILETIME fileTime;
diff --git a/os/windows/posix/include/sys/ioctl.h b/os/windows/posix/include/sys/ioctl.h
new file mode 100644 (file)
index 0000000..a42247d
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef IOCTL_H
+#define IOCTL_H
+
+/* This file is empty since it only needs to exist on Windows
+   but isn't otherwise used */
+
+#endif /* IOCTL_H */
\ No newline at end of file