win32/win32_compat.c: Reformat this file
This file has been reformatted with clang-format --style=file and the Linux kernel .clang-format style file. Signed-off-by: Bart Van Assche <bvanassche@acm.org>
This commit is contained in:
@@ -46,156 +46,142 @@ static int dummy ATTRIBUTE((unused));
|
|||||||
/* Windows needs this header file for the implementation of inet_aton() */
|
/* Windows needs this header file for the implementation of inet_aton() */
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
int win32_inet_pton(int af, const char * src, void * dst)
|
int win32_inet_pton(int af, const char *src, void *dst)
|
||||||
{
|
{
|
||||||
struct sockaddr_in sa;
|
struct sockaddr_in sa;
|
||||||
int len = sizeof(SOCKADDR);
|
int len = sizeof(SOCKADDR);
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
int strLen = strlen(src) + 1;
|
int strLen = strlen(src) + 1;
|
||||||
#ifdef UNICODE
|
#ifdef UNICODE
|
||||||
wchar_t *srcNonConst = (wchar_t *)malloc(strLen*sizeof(wchar_t));
|
wchar_t *srcNonConst = (wchar_t *)malloc(strLen * sizeof(wchar_t));
|
||||||
memset(srcNonConst, 0, strLen);
|
memset(srcNonConst, 0, strLen);
|
||||||
MultiByteToWideChar(CP_ACP, 0, src, -1, srcNonConst, strLen);
|
MultiByteToWideChar(CP_ACP, 0, src, -1, srcNonConst, strLen);
|
||||||
#else
|
#else
|
||||||
char *srcNonConst = (char *)malloc(strLen);
|
char *srcNonConst = (char *)malloc(strLen);
|
||||||
memset(srcNonConst, 0, strLen);
|
memset(srcNonConst, 0, strLen);
|
||||||
strncpy(srcNonConst, src, strLen);
|
strncpy(srcNonConst, src, strLen);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if( WSAStringToAddress(srcNonConst,af,NULL,(LPSOCKADDR)&sa,&len) == 0 )
|
if (WSAStringToAddress(srcNonConst, af, NULL, (LPSOCKADDR)&sa, &len) ==
|
||||||
{
|
0) {
|
||||||
ret = 1;
|
ret = 1;
|
||||||
}
|
} else {
|
||||||
else
|
if (WSAGetLastError() == WSAEINVAL) {
|
||||||
{
|
ret = -1;
|
||||||
if( WSAGetLastError() == WSAEINVAL )
|
}
|
||||||
{
|
}
|
||||||
ret = -1;
|
free(srcNonConst);
|
||||||
}
|
memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));
|
||||||
}
|
return ret;
|
||||||
free(srcNonConst);
|
|
||||||
memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int win32_poll(struct pollfd *fds, unsigned int nfds, int timo)
|
int win32_poll(struct pollfd *fds, unsigned int nfds, int timo)
|
||||||
{
|
{
|
||||||
struct timeval timeout, *toptr;
|
struct timeval timeout, *toptr;
|
||||||
fd_set ifds, ofds, efds, *ip, *op;
|
fd_set ifds, ofds, efds, *ip, *op;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
// Set up the file-descriptor sets in ifds, ofds and efds.
|
// Set up the file-descriptor sets in ifds, ofds and efds.
|
||||||
FD_ZERO(&ifds);
|
FD_ZERO(&ifds);
|
||||||
FD_ZERO(&ofds);
|
FD_ZERO(&ofds);
|
||||||
FD_ZERO(&efds);
|
FD_ZERO(&efds);
|
||||||
for (i = 0, op = ip = 0; i < nfds; ++i)
|
for (i = 0, op = ip = 0; i < nfds; ++i) {
|
||||||
{
|
fds[i].revents = 0;
|
||||||
fds[i].revents = 0;
|
if (fds[i].events & (POLLIN | POLLPRI)) {
|
||||||
if(fds[i].events & (POLLIN|POLLPRI))
|
ip = &ifds;
|
||||||
{
|
FD_SET(fds[i].fd, ip);
|
||||||
ip = &ifds;
|
}
|
||||||
FD_SET(fds[i].fd, ip);
|
if (fds[i].events & POLLOUT) {
|
||||||
}
|
op = &ofds;
|
||||||
if(fds[i].events & POLLOUT)
|
FD_SET(fds[i].fd, op);
|
||||||
{
|
}
|
||||||
op = &ofds;
|
FD_SET(fds[i].fd, &efds);
|
||||||
FD_SET(fds[i].fd, op);
|
}
|
||||||
}
|
|
||||||
FD_SET(fds[i].fd, &efds);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up the timeval structure for the timeout parameter
|
// Set up the timeval structure for the timeout parameter
|
||||||
if(timo < 0)
|
if (timo < 0) {
|
||||||
{
|
toptr = 0;
|
||||||
toptr = 0;
|
} else {
|
||||||
}
|
toptr = &timeout;
|
||||||
else
|
timeout.tv_sec = timo / 1000;
|
||||||
{
|
timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
|
||||||
toptr = &timeout;
|
}
|
||||||
timeout.tv_sec = timo / 1000;
|
|
||||||
timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef DEBUG_POLL
|
#ifdef DEBUG_POLL
|
||||||
printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
|
printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
|
||||||
(long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op);
|
(long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op);
|
||||||
#endif
|
#endif
|
||||||
rc = select(0, ip, op, &efds, toptr);
|
rc = select(0, ip, op, &efds, toptr);
|
||||||
#ifdef DEBUG_POLL
|
#ifdef DEBUG_POLL
|
||||||
printf("Exiting select rc=%d\n", rc);
|
printf("Exiting select rc=%d\n", rc);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(rc <= 0)
|
if (rc <= 0)
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
if(rc > 0)
|
if (rc > 0) {
|
||||||
{
|
for (i = 0; i < nfds; ++i) {
|
||||||
for (i = 0; i < nfds; ++i)
|
int fd = fds[i].fd;
|
||||||
{
|
if (fds[i].events & (POLLIN | POLLPRI) &&
|
||||||
int fd = fds[i].fd;
|
FD_ISSET(fd, &ifds))
|
||||||
if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
|
fds[i].revents |= POLLIN;
|
||||||
fds[i].revents |= POLLIN;
|
if (fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
|
||||||
if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
|
fds[i].revents |= POLLOUT;
|
||||||
fds[i].revents |= POLLOUT;
|
if (FD_ISSET(fd,
|
||||||
if(FD_ISSET(fd, &efds)) // Some error was detected ... should be some way to know.
|
&efds)) // Some error was detected ... should be some way to know.
|
||||||
fds[i].revents |= POLLHUP;
|
fds[i].revents |= POLLHUP;
|
||||||
#ifdef DEBUG_POLL
|
#ifdef DEBUG_POLL
|
||||||
printf("%d %d %d revent = %x\n",
|
printf("%d %d %d revent = %x\n", FD_ISSET(fd, &ifds),
|
||||||
FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds),
|
FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds),
|
||||||
fds[i].revents
|
fds[i].revents);
|
||||||
);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
|
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
|
||||||
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
|
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
|
||||||
#else
|
#else
|
||||||
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
|
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct timezone
|
struct timezone {
|
||||||
{
|
int tz_minuteswest; /* minutes W of Greenwich */
|
||||||
int tz_minuteswest; /* minutes W of Greenwich */
|
int tz_dsttime; /* type of dst correction */
|
||||||
int tz_dsttime; /* type of dst correction */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int win32_gettimeofday(struct timeval *tv, struct timezone *tz)
|
int win32_gettimeofday(struct timeval *tv, struct timezone *tz)
|
||||||
{
|
{
|
||||||
FILETIME ft;
|
FILETIME ft;
|
||||||
unsigned __int64 tmpres = 0;
|
unsigned __int64 tmpres = 0;
|
||||||
static int tzflag;
|
static int tzflag;
|
||||||
|
|
||||||
if (NULL != tv)
|
if (NULL != tv) {
|
||||||
{
|
GetSystemTimeAsFileTime(&ft);
|
||||||
GetSystemTimeAsFileTime(&ft);
|
|
||||||
|
|
||||||
tmpres |= ft.dwHighDateTime;
|
tmpres |= ft.dwHighDateTime;
|
||||||
tmpres <<= 32;
|
tmpres <<= 32;
|
||||||
tmpres |= ft.dwLowDateTime;
|
tmpres |= ft.dwLowDateTime;
|
||||||
|
|
||||||
/*converting file time to unix epoch*/
|
/*converting file time to unix epoch*/
|
||||||
tmpres -= DELTA_EPOCH_IN_MICROSECS;
|
tmpres -= DELTA_EPOCH_IN_MICROSECS;
|
||||||
tmpres /= 10; /*convert into microseconds*/
|
tmpres /= 10; /*convert into microseconds*/
|
||||||
tv->tv_sec = (long)(tmpres / 1000000UL);
|
tv->tv_sec = (long)(tmpres / 1000000UL);
|
||||||
tv->tv_usec = (long)(tmpres % 1000000UL);
|
tv->tv_usec = (long)(tmpres % 1000000UL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (NULL != tz)
|
if (NULL != tz) {
|
||||||
{
|
if (!tzflag) {
|
||||||
if (!tzflag)
|
_tzset();
|
||||||
{
|
tzflag++;
|
||||||
_tzset();
|
}
|
||||||
tzflag++;
|
tz->tz_minuteswest = _timezone / 60;
|
||||||
}
|
tz->tz_dsttime = _daylight;
|
||||||
tz->tz_minuteswest = _timezone / 60;
|
}
|
||||||
tz->tz_dsttime = _daylight;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t win32_readv(int fd, const struct iovec *iov, int iovcnt)
|
ssize_t win32_readv(int fd, const struct iovec *iov, int iovcnt)
|
||||||
|
|||||||
Reference in New Issue
Block a user