This source file includes following definitions.
- w32initWinSock
- sigaction
- kill
- fsync
- wait3
- replace_random
- replace_setsockopt
- replace_ftruncate
- replace_rename
- win32_proxy_threadproc
- pthread_create
- pthread_detach
- pthread_self
- win32_pthread_join
- pthread_cond_init
- pthread_cond_destroy
- pthread_cond_wait
- pthread_cond_signal
- fork
- getrusage
- gettimeofday
- wstrtod
- strerror_r
- wsa_strerror
#ifdef _WIN32
#include <process.h>
#include <stdlib.h>
#include <errno.h>
#ifndef FD_SETSIZE
# define FD_SETSIZE 16000
#endif
#include <winsock2.h>
#include <windows.h>
#include <signal.h>
#include <time.h>
#include <locale.h>
#include <math.h>
#include "win32fixes.h"
#define NOTUSED(V) ((void) V)
#define THREAD_STACK_SIZE (1024*1024*4)
int w32initWinSock(void) {
WSADATA t_wsa;
WORD wVers;
int iError;
wVers = MAKEWORD(2, 2);
iError = WSAStartup(wVers, &t_wsa);
if(iError != NO_ERROR || LOBYTE(t_wsa.wVersion) != 2 || HIBYTE(t_wsa.wVersion) != 2 ) {
return 0;
};
return 1;
}
int sigaction(int sig, struct sigaction *in, struct sigaction *out) {
NOTUSED(out);
if (in->sa_flags & SA_SIGINFO)
signal(sig, in->sa_sigaction);
else
signal(sig, in->sa_handler);
return 0;
}
int kill(pid_t pid, int sig) {
if (sig == SIGKILL) {
HANDLE h = OpenProcess(PROCESS_TERMINATE, 0, pid);
if (!TerminateProcess(h, 127)) {
errno = EINVAL;
CloseHandle(h);
return -1;
};
CloseHandle(h);
return 0;
} else {
errno = EINVAL;
return -1;
};
}
int fsync (int fd) {
HANDLE h = (HANDLE) _get_osfhandle(fd);
DWORD err;
if (h == INVALID_HANDLE_VALUE) {
errno = EBADF;
return -1;
}
if (!FlushFileBuffers(h)) {
err = GetLastError();
switch (err) {
case ERROR_INVALID_HANDLE:
errno = EINVAL;
break;
default:
errno = EIO;
}
return -1;
}
return 0;
}
pid_t wait3(int *stat_loc, int options, void *rusage) {
NOTUSED(stat_loc);
NOTUSED(options);
NOTUSED(rusage);
return (pid_t) waitpid((intptr_t) -1, 0, WAIT_FLAGS);
}
int replace_random() {
unsigned int x=0;
if (RtlGenRandom == NULL) {
HMODULE lib = LoadLibraryA("advapi32.dll");
RtlGenRandom = (RtlGenRandomFunc)GetProcAddress(lib, "SystemFunction036");
if (RtlGenRandom == NULL) return 1;
}
RtlGenRandom(&x, sizeof(UINT_MAX));
return (int)(x >> 1);
}
int replace_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen) {
return (setsockopt)((SOCKET)socket, level, optname, (const char *)optval, optlen);
}
int replace_ftruncate(int fd, off64_t length) {
HANDLE h = (HANDLE) _get_osfhandle (fd);
LARGE_INTEGER l, o;
if (h == INVALID_HANDLE_VALUE) {
errno = EBADF;
return -1;
}
l.QuadPart = length;
if (!SetFilePointerEx(h, l, &o, FILE_BEGIN)) return -1;
if (!SetEndOfFile(h)) return -1;
return 0;
}
int replace_rename(const char *src, const char *dst) {
int retries = 50;
while (1) {
if (MoveFileEx(src, dst, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) {
return 0;
} else {
errno = GetLastError();
if (errno != 5) break;
retries--;
if (retries == 0) {
retries = 50;
Sleep(10);
}
}
}
return -1;
}
#if 0
typedef struct thread_params
{
void *(*func)(void *);
void * arg;
} thread_params;
static unsigned __stdcall win32_proxy_threadproc(void *arg) {
thread_params *p = (thread_params *) arg;
p->func(p->arg);
free(p);
_endthreadex(0);
return 0;
}
int pthread_create(pthread_t *thread, const void *unused,
void *(*start_routine)(void*), void *arg) {
HANDLE h;
thread_params *params = (thread_params *)malloc(sizeof(thread_params));
NOTUSED(unused);
params->func = start_routine;
params->arg = arg;
#ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
# define STACK_SIZE_PARAM_IS_A_RESERVATION 4096
#endif
h =(HANDLE) _beginthreadex(NULL,
THREAD_STACK_SIZE,
win32_proxy_threadproc,
params,
STACK_SIZE_PARAM_IS_A_RESERVATION,
thread
);
if (!h)
return errno;
CloseHandle(h);
return 0;
}
int pthread_detach (pthread_t thread) {
NOTUSED(thread);
return 0;
}
pthread_t pthread_self(void) {
return GetCurrentThreadId();
}
int win32_pthread_join(pthread_t *thread, void **value_ptr) {
int result;
HANDLE h = OpenThread(SYNCHRONIZE, FALSE, *thread);
NOTUSED(value_ptr);
switch (WaitForSingleObject(h, INFINITE)) {
case WAIT_OBJECT_0:
result = 0;
case WAIT_ABANDONED:
result = EINVAL;
default:
result = GetLastError();
}
CloseHandle(h);
return result;
}
int pthread_cond_init(pthread_cond_t *cond, const void *unused) {
NOTUSED(unused);
cond->waiters = 0;
cond->was_broadcast = 0;
InitializeCriticalSection(&cond->waiters_lock);
cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
if (!cond->sema) {
errno = GetLastError();
return -1;
}
cond->continue_broadcast = CreateEvent(NULL,
FALSE,
FALSE,
NULL);
if (!cond->continue_broadcast) {
errno = GetLastError();
return -1;
}
return 0;
}
int pthread_cond_destroy(pthread_cond_t *cond) {
CloseHandle(cond->sema);
CloseHandle(cond->continue_broadcast);
DeleteCriticalSection(&cond->waiters_lock);
return 0;
}
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
int last_waiter;
EnterCriticalSection(&cond->waiters_lock);
cond->waiters++;
LeaveCriticalSection(&cond->waiters_lock);
LeaveCriticalSection(mutex);
WaitForSingleObject(cond->sema, INFINITE);
EnterCriticalSection(&cond->waiters_lock);
cond->waiters--;
last_waiter = cond->was_broadcast && cond->waiters == 0;
LeaveCriticalSection(&cond->waiters_lock);
if (last_waiter) {
SetEvent(cond->continue_broadcast);
}
EnterCriticalSection(mutex);
return 0;
}
int pthread_cond_signal(pthread_cond_t *cond) {
int have_waiters;
EnterCriticalSection(&cond->waiters_lock);
have_waiters = cond->waiters > 0;
LeaveCriticalSection(&cond->waiters_lock);
if (have_waiters)
return ReleaseSemaphore(cond->sema, 1, NULL) ?
0 : GetLastError();
else
return 0;
}
#endif
int fork(void) {
return -1;
}
int getrusage(int who, struct rusage * r) {
FILETIME starttime, exittime, kerneltime, usertime;
ULARGE_INTEGER li;
if (r == NULL) {
errno = EFAULT;
return -1;
}
memset(r, 0, sizeof(struct rusage));
if (who == RUSAGE_SELF) {
if (!GetProcessTimes(GetCurrentProcess(),
&starttime,
&exittime,
&kerneltime,
&usertime))
{
errno = EFAULT;
return -1;
}
}
if (who == RUSAGE_CHILDREN) {
starttime.dwLowDateTime = 0;
starttime.dwHighDateTime = 0;
exittime.dwLowDateTime = 0;
exittime.dwHighDateTime = 0;
kerneltime.dwLowDateTime = 0;
kerneltime.dwHighDateTime = 0;
usertime.dwLowDateTime = 0;
usertime.dwHighDateTime = 0;
}
memcpy(&li, &kerneltime, sizeof(FILETIME));
li.QuadPart /= 10L;
r->ru_stime.tv_sec = (long)(li.QuadPart / 1000000L);
r->ru_stime.tv_usec = (long)(li.QuadPart % 1000000L);
memcpy(&li, &usertime, sizeof(FILETIME));
li.QuadPart /= 10L;
r->ru_utime.tv_sec = (long)(li.QuadPart / 1000000L);
r->ru_utime.tv_usec = (long)(li.QuadPart % 1000000L);
return 0;
}
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
#if 0
struct timezone
{
int tz_minuteswest;
int tz_dsttime;
};
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
unsigned __int64 tmpres = 0;
static int tzflag;
if (NULL != tv)
{
GetSystemTimeAsFileTime(&ft);
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
tmpres -= DELTA_EPOCH_IN_MICROSECS;
tmpres /= 10;
tv->tv_sec = (long)(tmpres / 1000000UL);
tv->tv_usec = (long)(tmpres % 1000000UL);
}
if (NULL != tz)
{
if (!tzflag)
{
_tzset();
tzflag++;
}
tz->tz_minuteswest = _timezone / 60;
tz->tz_dsttime = _daylight;
}
return 0;
}
static _locale_t clocale = NULL;
double wstrtod(const char *nptr, char **eptr) {
double d;
char *leptr;
#if 0
if (clocale == NULL)
clocale = _create_locale(LC_ALL, "C");
#endif
d = _strtod_l(nptr, &leptr, clocale);
if (d == 0 && nptr == leptr) {
int neg = 0;
while (isspace(*nptr))
nptr++;
if (*nptr == '+')
nptr++;
else if (*nptr == '-') {
nptr++;
neg = 1;
}
if (strnicmp("INF", nptr, 3) == 0) {
if (eptr != NULL) {
if (strnicmp("INFINITE", nptr, 8) == 0)
*eptr = (char*)(nptr + 8);
else
*eptr = (char*)(nptr + 3);
}
if (neg == 1)
return -HUGE_VAL;
else
return HUGE_VAL;
} else if (strnicmp("NAN", nptr, 3) == 0) {
if (eptr != NULL)
*eptr = (char*)(nptr + 3);
d = HUGE_VAL;
return d * 0;
}
}
if (eptr != NULL)
*eptr = leptr;
return d;
}
int strerror_r(int err, char* buf, size_t buflen) {
int size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
0,
buf,
(DWORD)buflen,
NULL);
if (size == 0) {
char* strerr = strerror(err);
if (strlen(strerr) >= buflen) {
errno = ERANGE;
return -1;
}
strcpy(buf, strerr);
}
if (size > 2 && buf[size - 2] == '\r') {
buf[size - 2] = '\0';
}
return 0;
}
char wsa_strerror_buf[128];
char *wsa_strerror(int err) {
int size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
0,
wsa_strerror_buf,
128,
NULL);
if (size == 0) return strerror(err);
if (size > 2 && wsa_strerror_buf[size - 2] == '\r') {
wsa_strerror_buf[size - 2] = '\0';
}
return wsa_strerror_buf;
}
#endif
#endif