types: remove the [u_]quad type and replace with [u]int64
[deb_libnfs.git] / win32 / win32_compat.c
CommitLineData
7f799388
M
1/*
2Copyright (c) 2006 by Dan Kennedy.
3Copyright (c) 2006 by Juliusz Chroboczek.
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21THE SOFTWARE.
22*/
23
24#ifndef WIN32
25
26static int dummy ATTRIBUTE((unused));
27
28#else
29#include "win32_compat.h"
30#include <errno.h>
31#include <stdio.h>
20a96d42 32
7f799388
M
33#undef poll
34#undef socket
35#undef connect
36#undef accept
37#undef shutdown
38#undef getpeername
39#undef sleep
40#undef inet_aton
41#undef gettimeofday
42#undef stat
7f799388
M
43#define assert(a)
44
45/* Windows needs this header file for the implementation of inet_aton() */
46#include <ctype.h>
47
48int win32_inet_pton(int af, const char * src, void * dst)
49{
20a96d42
M
50 struct sockaddr_in sa;
51 int len = sizeof(SOCKADDR);
52 int ret = -1;
53 int strLen = strlen(src) + 1;
54#ifdef UNICODE
55 wchar_t *srcNonConst = (wchar_t *)malloc(strLen*sizeof(wchar_t));
56 memset(srcNonConst, 0, strLen);
57 MultiByteToWideChar(CP_ACP, 0, src, -1, srcNonConst, strLen);
58#else
59 char *srcNonConst = (char *)malloc(strLen);
60 memset(srcNonConst, 0, strLen);
61 strncpy(srcNonConst, src, strLen);
62#endif
7f799388 63
20a96d42 64 if( WSAStringToAddress(srcNonConst,af,NULL,(LPSOCKADDR)&sa,&len) == 0 )
7f799388 65 {
20a96d42 66 ret = 1;
7f799388 67 }
20a96d42 68 else
7f799388 69 {
20a96d42
M
70 if( WSAGetLastError() == WSAEINVAL )
71 {
72 ret = -1;
73 }
7f799388 74 }
20a96d42
M
75 free(srcNonConst);
76 memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));
7f799388
M
77 return ret;
78}
79
20a96d42 80int win32_poll(struct pollfd *fds, unsigned int nfds, int timo)
7f799388 81{
20a96d42
M
82 struct timeval timeout, *toptr;
83 fd_set ifds, ofds, efds, *ip, *op;
84 unsigned int i;
85 int rc;
86
87 // Set up the file-descriptor sets in ifds, ofds and efds.
88 FD_ZERO(&ifds);
89 FD_ZERO(&ofds);
90 FD_ZERO(&efds);
91 for (i = 0, op = ip = 0; i < nfds; ++i)
92 {
93 fds[i].revents = 0;
94 if(fds[i].events & (POLLIN|POLLPRI))
95 {
96 ip = &ifds;
97 FD_SET(fds[i].fd, ip);
98 }
99 if(fds[i].events & POLLOUT)
100 {
101 op = &ofds;
102 FD_SET(fds[i].fd, op);
7f799388 103 }
20a96d42
M
104 FD_SET(fds[i].fd, &efds);
105 }
106
107 // Set up the timeval structure for the timeout parameter
108 if(timo < 0)
109 {
110 toptr = 0;
111 }
112 else
113 {
114 toptr = &timeout;
115 timeout.tv_sec = timo / 1000;
116 timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000;
117 }
7f799388
M
118
119#ifdef DEBUG_POLL
20a96d42
M
120 printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n",
121 (long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op);
7f799388 122#endif
20a96d42 123 rc = select(0, ip, op, &efds, toptr);
7f799388 124#ifdef DEBUG_POLL
20a96d42 125 printf("Exiting select rc=%d\n", rc);
7f799388
M
126#endif
127
20a96d42
M
128 if(rc <= 0)
129 return rc;
7f799388 130
20a96d42
M
131 if(rc > 0)
132 {
133 for (i = 0; i < nfds; ++i)
134 {
135 int fd = fds[i].fd;
136 if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds))
137 fds[i].revents |= POLLIN;
138 if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds))
139 fds[i].revents |= POLLOUT;
140 if(FD_ISSET(fd, &efds)) // Some error was detected ... should be some way to know.
141 fds[i].revents |= POLLHUP;
7f799388 142#ifdef DEBUG_POLL
20a96d42
M
143 printf("%d %d %d revent = %x\n",
144 FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds),
145 fds[i].revents
146 );
7f799388 147#endif
7f799388 148 }
20a96d42
M
149 }
150 return rc;
7f799388 151}
20a96d42
M
152
153#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
154 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
155#else
156 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
157#endif
158
159struct timezone
160{
161 int tz_minuteswest; /* minutes W of Greenwich */
162 int tz_dsttime; /* type of dst correction */
163};
164
165int win32_gettimeofday(struct timeval *tv, struct timezone *tz)
166{
167 FILETIME ft;
168 unsigned __int64 tmpres = 0;
169 static int tzflag;
170
171 if (NULL != tv)
172 {
173 GetSystemTimeAsFileTime(&ft);
174
175 tmpres |= ft.dwHighDateTime;
176 tmpres <<= 32;
177 tmpres |= ft.dwLowDateTime;
178
179 /*converting file time to unix epoch*/
180 tmpres -= DELTA_EPOCH_IN_MICROSECS;
181 tmpres /= 10; /*convert into microseconds*/
182 tv->tv_sec = (long)(tmpres / 1000000UL);
183 tv->tv_usec = (long)(tmpres % 1000000UL);
184 }
185
186 if (NULL != tz)
187 {
188 if (!tzflag)
189 {
190 _tzset();
191 tzflag++;
192 }
193 tz->tz_minuteswest = _timezone / 60;
194 tz->tz_dsttime = _daylight;
195 }
196
197 return 0;
198}
199
7f799388 200#endif