Implement nice() for Windows
[fio.git] / os / windows / posix.c
index d238c64a16219607ced8eb7dd4807552361046e6..bbd93e979522e098d9a438e0ccfdc10da0b89ec0 100755 (executable)
@@ -229,6 +229,32 @@ 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)
+{
+    FILETIME utcFT;
+    LONGLONG jan1970;
+
+    jan1970 = Int32x32To64(dosTime, 10000000) + 116444736000000000;
+    utcFT.dwLowDateTime = (DWORD)jan1970;
+    utcFT.dwHighDateTime = jan1970 >> 32;
+
+    FileTimeToSystemTime((FILETIME*)&utcFT, systemTime);
+}
+
+char* ctime_r(const time_t *t, char *buf)
+{
+    SYSTEMTIME systime;
+    const char * const dayOfWeek[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
+    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, 31, "%s %s %d %02d:%02d:%02d %04d\n", dayOfWeek[systime.wDayOfWeek % 7], monthOfYear[(systime.wMonth - 1) % 12],
+                                                                                systime.wDay, systime.wHour, systime.wMinute, systime.wSecond, systime.wYear);
+    return buf;
+}
+
 int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
 {
        FILETIME fileTime;
@@ -621,10 +647,19 @@ int setgid(gid_t gid)
 
 int nice(int incr)
 {
-       if (incr != 0) {
-               errno = EINVAL;
-               return -1;
-       }
+       DWORD prioclass = NORMAL_PRIORITY_CLASS;
+       
+       if (incr < -15)
+               prioclass = HIGH_PRIORITY_CLASS;
+       else if (incr < 0)
+               prioclass = ABOVE_NORMAL_PRIORITY_CLASS;
+       else if (incr > 15)
+               prioclass = IDLE_PRIORITY_CLASS;
+       else if (incr > 0)
+               prioclass = BELOW_NORMAL_PRIORITY_CLASS;
+       
+       if (!SetPriorityClass(GetCurrentProcess(), prioclass))
+               log_err("fio: SetPriorityClass failed\n");
 
        return 0;
 }
@@ -864,7 +899,7 @@ struct dirent *readdir(DIR *dirp)
 
        if (dirp->find_handle == INVALID_HANDLE_VALUE) {
                char search_pattern[MAX_PATH];
-               StringCchPrintfA(search_pattern, MAX_PATH, "%s\\*", dirp->dirname);
+               StringCchPrintfA(search_pattern, MAX_PATH-1, "%s\\*", dirp->dirname);
                dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
                if (dirp->find_handle == INVALID_HANDLE_VALUE)
                        return NULL;