Fio 2.6
[fio.git] / os / windows / posix.c
index 6a7841d5513d743b3451ec14dd94daf11f27b679..41fc480df9f14f92a75ff07a5fda30fa098deb06 100755 (executable)
 #include <unistd.h>
 #include <dirent.h>
 #include <pthread.h>
+#include <time.h>
 #include <semaphore.h>
 #include <sys/shm.h>
 #include <sys/mman.h>
 #include <sys/uio.h>
 #include <sys/resource.h>
 #include <sys/poll.h>
+#include <sys/wait.h>
+#include <setjmp.h>
 
 #include "../os-windows.h"
 #include "../../lib/hweight.h"
@@ -226,6 +229,30 @@ char *dlerror(void)
        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;
@@ -421,6 +448,7 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
        {
                static LARGE_INTEGER freq = {{0,0}};
                LARGE_INTEGER counts;
+               uint64_t t;
 
                QueryPerformanceCounter(&counts);
                if (freq.QuadPart == 0)
@@ -429,7 +457,7 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
                tp->tv_sec = counts.QuadPart / freq.QuadPart;
                /* Get the difference between the number of ns stored
                 * in 'tv_sec' and that stored in 'counts' */
-               uint64_t t = tp->tv_sec * freq.QuadPart;
+               t = tp->tv_sec * freq.QuadPart;
                t = counts.QuadPart - t;
                /* 't' now contains the number of cycles since the last second.
                 * We want the number of nanoseconds, so multiply out by 1,000,000,000
@@ -661,11 +689,6 @@ int getrusage(int who, struct rusage *r_usage)
        return 0;
 }
 
-int posix_fadvise(int fd, off_t offset, off_t len, int advice)
-{
-       return 0;
-}
-
 int posix_madvise(void *addr, size_t len, int advice)
 {
        log_err("%s is not implemented\n", __func__);
@@ -710,9 +733,22 @@ ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
 
 ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
 {
-       log_err("%s is not implemented\n", __func__);
-       errno = ENOSYS;
-       return -1;
+       int i;
+       DWORD bytes_written = 0;
+       for (i = 0; i < iovcnt; i++)
+       {
+               int len = send((SOCKET)fildes, iov[i].iov_base, iov[i].iov_len, 0);
+               if (len == SOCKET_ERROR)
+               {
+                       DWORD err = GetLastError();
+                       errno = win_to_posix_error(err);
+                       bytes_written = -1;
+                       break;
+               }
+               bytes_written += len;
+       }
+
+       return bytes_written;
 }
 
 long long strtoll(const char *restrict str, char **restrict endptr,
@@ -754,7 +790,6 @@ int poll(struct pollfd fds[], nfds_t nfds, int timeout)
 
                FD_SET(fds[i].fd, &exceptfds);
        }
-
        rc = select(nfds, &readfds, &writefds, &exceptfds, to);
 
        if (rc != SOCKET_ERROR) {
@@ -774,7 +809,6 @@ int poll(struct pollfd fds[], nfds_t nfds, int timeout)
                                fds[i].revents |= POLLHUP;
                }
        }
-
        return rc;
 }
 
@@ -876,6 +910,14 @@ uid_t geteuid(void)
        return -1;
 }
 
+in_addr_t inet_network(const char *cp)
+{
+       in_addr_t hbo;
+       in_addr_t nbo = inet_addr(cp);
+       hbo = ((nbo & 0xFF) << 24) + ((nbo & 0xFF00) << 8) + ((nbo & 0xFF0000) >> 8) + ((nbo & 0xFF000000) >> 24);
+       return hbo;
+}
+
 const char* inet_ntop(int af, const void *restrict src,
                char *restrict dst, socklen_t size)
 {