2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <sys/locking.h>
25 #include <sys/utime.h>
27 void nixio_open__mingw(lua_State
*L
) {
32 if (WSAStartup(MAKEWORD(2, 2), &wsa
)) {
33 luaL_error(L
, "Unable to initialize Winsock");
38 NIXIO_WSA_CONSTANT(WSAEACCES
);
39 NIXIO_WSA_CONSTANT(WSAEINTR
);
40 NIXIO_WSA_CONSTANT(WSAEINVAL
);
41 NIXIO_WSA_CONSTANT(WSAEBADF
);
42 NIXIO_WSA_CONSTANT(WSAEFAULT
);
43 NIXIO_WSA_CONSTANT(WSAEMFILE
);
44 NIXIO_WSA_CONSTANT(WSAENAMETOOLONG
);
45 NIXIO_WSA_CONSTANT(WSAELOOP
);
46 NIXIO_WSA_CONSTANT(WSAEAFNOSUPPORT
);
47 NIXIO_WSA_CONSTANT(WSAENOBUFS
);
48 NIXIO_WSA_CONSTANT(WSAEPROTONOSUPPORT
);
49 NIXIO_WSA_CONSTANT(WSAENOPROTOOPT
);
50 NIXIO_WSA_CONSTANT(WSAEADDRINUSE
);
51 NIXIO_WSA_CONSTANT(WSAENETDOWN
);
52 NIXIO_WSA_CONSTANT(WSAENETUNREACH
);
53 NIXIO_WSA_CONSTANT(WSAECONNABORTED
);
54 NIXIO_WSA_CONSTANT(WSAECONNRESET
);
56 lua_setfield(L
, -2, "const_sock");
59 const char* nixio__mgw_inet_ntop
60 (int af
, const void *src
, char *dst
, socklen_t size
) {
61 struct sockaddr_storage saddr
;
62 memset(&saddr
, 0, sizeof(saddr
));
64 DWORD hostlen
= size
, sl
;
66 struct sockaddr_in
*saddr4
= (struct sockaddr_in
*)&saddr
;
67 memcpy(&saddr4
->sin_addr
, src
, sizeof(saddr4
->sin_addr
));
68 saddr4
->sin_family
= AF_INET
;
70 sl
= sizeof(struct sockaddr_in
);
71 } else if (af
== AF_INET6
) {
72 struct sockaddr_in6
*saddr6
= (struct sockaddr_in6
*)&saddr
;
73 memcpy(&saddr6
->sin6_addr
, src
, sizeof(saddr6
->sin6_addr
));
74 saddr6
->sin6_family
= AF_INET6
;
75 saddr6
->sin6_port
= 0;
76 sl
= sizeof(struct sockaddr_in6
);
80 if (WSAAddressToString((struct sockaddr
*)&saddr
, sl
, NULL
, dst
, &hostlen
)) {
86 int nixio__mgw_inet_pton (int af
, const char *src
, void *dst
) {
87 struct sockaddr_storage sa
;
90 if (!WSAStringToAddress((char*)src
, af
, NULL
, (struct sockaddr
*)&sa
, &sl
)) {
92 struct in_addr ina
= ((struct sockaddr_in
*)&sa
)->sin_addr
;
93 memcpy(dst
, &ina
, sizeof(ina
));
95 } else if (af
== AF_INET6
) {
96 struct in_addr6 ina6
= ((struct sockaddr_in6
*)&sa
)->sin6_addr
;
97 memcpy(dst
, &ina6
, sizeof(ina6
));
100 WSASetLastError(WSAEAFNOSUPPORT
);
108 int nixio__mgw_nanosleep(const struct timespec
*req
, struct timespec
*rem
) {
113 Sleep(req
->tv_sec
* 1000 + req
->tv_nsec
* 1000000);
117 int nixio__mgw_poll(struct pollfd
*fds
, int nfds
, int timeout
) {
124 int high
= 0, rf
= 0, wf
= 0, ef
= 0;
125 fd_set rfds
, wfds
, efds
;
130 tv
.tv_sec
= timeout
/ 1000;
131 tv
.tv_usec
= (timeout
% 1000) * 1000;
133 for (int i
= 0; i
< nfds
; i
++) {
134 if (fds
->events
& POLLIN
) {
135 FD_SET(fds
->fd
, &rfds
);
138 if (fds
->events
& POLLOUT
) {
139 FD_SET(fds
->fd
, &wfds
);
142 if (fds
->events
& POLLERR
) {
143 FD_SET(fds
->fd
, &efds
);
146 if (fds
->fd
> high
) {
151 int stat
= select(high
+ 1, (rf
) ? &rfds
: NULL
,
152 (wf
) ? &wfds
: NULL
, (ef
) ? &efds
: NULL
, &tv
);
154 errno
= WSAGetLastError();
160 for (int i
= 0; i
< nfds
; i
++) {
162 if ((fds
->events
& POLLIN
) && FD_ISSET(fds
->fd
, &rfds
)) {
163 fds
->revents
|= POLLIN
;
165 if ((fds
->events
& POLLOUT
) && FD_ISSET(fds
->fd
, &wfds
)) {
166 fds
->revents
|= POLLOUT
;
168 if ((fds
->events
& POLLERR
) && FD_ISSET(fds
->fd
, &efds
)) {
169 fds
->revents
|= POLLERR
;
179 int nixio__mgw_lockf(int fd
, int cmd
, off_t len
) {
183 stat
= _locking(fd
, _LK_LOCK
, len
);
184 } while (stat
== -1 && errno
== EDEADLOCK
);
185 } else if (cmd
== F_TLOCK
) {
186 stat
= _locking(fd
, _LK_NBLCK
, len
);
187 } else if (cmd
== F_ULOCK
) {
188 stat
= _locking(fd
, _LK_UNLCK
, len
);
196 char* nixio__mgw_realpath(const char *path
, char *resolved
) {
197 if (GetFullPathName(path
, PATH_MAX
, resolved
, NULL
)) {
200 errno
= GetLastError();
205 int nixio__mgw_link(const char *oldpath
, const char *newpath
) {
206 if (!CreateHardLink(newpath
, oldpath
, NULL
)) {
207 errno
= GetLastError();
214 int nixio__mgw_utimes(const char *filename
, const struct timeval times
[2]) {
215 struct _utimbuf timebuffer
;
216 timebuffer
.actime
= times
[0].tv_sec
;
217 timebuffer
.modtime
= times
[1].tv_sec
;
219 return _utime(filename
, &timebuffer
);