diff --git a/otherlibs/win32unix/accept.c b/otherlibs/win32unix/accept.c index f751c3e39..0e44f5800 100644 --- a/otherlibs/win32unix/accept.c +++ b/otherlibs/win32unix/accept.c @@ -29,7 +29,7 @@ CAMLprim value unix_accept(sock) int oldvalue, oldvaluelen, newvalue, retcode; union sock_addr_union addr; socklen_param_type addr_len; - int errcode = 0; + DWORD errcode = 0; oldvaluelen = sizeof(oldvalue); retcode = getsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, @@ -43,9 +43,8 @@ CAMLprim value unix_accept(sock) addr_len = sizeof(sock_addr); enter_blocking_section(); snew = accept(sconn, &addr.s_gen, &addr_len); + if (snew == INVALID_SOCKET) errcode = WSAGetLastError (); leave_blocking_section(); - if( snew == INVALID_SOCKET ) - errcode = WSAGetLastError (); if (retcode == 0) { /* Restore initial mode */ setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, diff --git a/otherlibs/win32unix/connect.c b/otherlibs/win32unix/connect.c index 74e62252d..da7d09c71 100644 --- a/otherlibs/win32unix/connect.c +++ b/otherlibs/win32unix/connect.c @@ -22,16 +22,17 @@ CAMLprim value unix_connect(socket, address) value socket, address; { SOCKET s = Socket_val(socket); - int retcode; union sock_addr_union addr; socklen_param_type addr_len; + DWORD errcode = 0; get_sockaddr(address, &addr, &addr_len); enter_blocking_section(); - retcode = connect(s, &addr.s_gen, addr_len); + if (connect(s, &addr.s_gen, addr_len) == -1) + errcode = WSAGetLastError(); leave_blocking_section(); - if (retcode == -1) { - win32_maperr(WSAGetLastError()); + if (errcode) { + win32_maperr(errcode); uerror("connect", Nothing); } return Val_unit; diff --git a/otherlibs/win32unix/read.c b/otherlibs/win32unix/read.c index 704cec2c7..c87d55ed0 100644 --- a/otherlibs/win32unix/read.c +++ b/otherlibs/win32unix/read.c @@ -19,35 +19,34 @@ #include #include "unixsupport.h" -CAMLprim value unix_read(value fd, value buf, value ofs, value len) +CAMLprim value unix_read(value fd, value buf, value ofs, value vlen) { + intnat len; DWORD numbytes, numread; char iobuf[UNIX_BUFFER_SIZE]; + DOWRD errcode = 0; Begin_root (buf); - numbytes = Long_val(len); - if (numbytes > UNIX_BUFFER_SIZE) numbytes = UNIX_BUFFER_SIZE; + len = Long_val(vlen); + numbytes = len > UNIX_BUFFER_SIZE ? UNIX_BUFFER_SIZE : len; if (Descr_kind_val(fd) == KIND_SOCKET) { int ret; SOCKET s = Socket_val(fd); enter_blocking_section(); ret = recv(s, iobuf, numbytes, 0); + if (ret == SOCKET_ERROR) errcode = WSAGetLastError(); leave_blocking_section(); - if (ret == SOCKET_ERROR) { - win32_maperr(WSAGetLastError()); - uerror("read", Nothing); - } numread = ret; } else { - BOOL ret; HANDLE h = Handle_val(fd); enter_blocking_section(); - ret = ReadFile(h, iobuf, numbytes, &numread, NULL); + if (! ReadFile(h, iobuf, numbytes, &numread, NULL)) + errcode = GetLastError(); leave_blocking_section(); - if (! ret) { - win32_maperr(GetLastError()); - uerror("read", Nothing); - } + } + if (errcode) { + win32_maperr(errcode); + uerror("read", Nothing); } memmove (&Byte(buf, Long_val(ofs)), iobuf, numread); End_roots(); diff --git a/otherlibs/win32unix/select.c b/otherlibs/win32unix/select.c index 4fa8e788b..a7b68217e 100644 --- a/otherlibs/win32unix/select.c +++ b/otherlibs/win32unix/select.c @@ -54,6 +54,7 @@ CAMLprim value unix_select(value readfds, value writefds, value exceptfds, value int retcode; value res; value read_list = Val_unit, write_list = Val_unit, except_list = Val_unit; + DWORD errcode = 0; Begin_roots3 (readfds, writefds, exceptfds) Begin_roots3 (read_list, write_list, except_list) @@ -79,10 +80,11 @@ CAMLprim value unix_select(value readfds, value writefds, value exceptfds, value tvp = &tv; } enter_blocking_section(); - retcode = select(FD_SETSIZE, &read, &write, &except, tvp); + if (select(FD_SETSIZE, &read, &write, &except, tvp) == -1) + errcode = WSAGetLastError(); leave_blocking_section(); - if (retcode == -1) { - win32_maperr(WSAGetLastError()); + if (errcode) { + win32_maperr(errcode); uerror("select", Nothing); } read_list = fdset_to_fdlist(readfds, &read); diff --git a/otherlibs/win32unix/sendrecv.c b/otherlibs/win32unix/sendrecv.c index 7359feb58..d820f5d0b 100644 --- a/otherlibs/win32unix/sendrecv.c +++ b/otherlibs/win32unix/sendrecv.c @@ -31,15 +31,17 @@ CAMLprim value unix_recv(value sock, value buff, value ofs, value len, value fla int ret; intnat numbytes; char iobuf[UNIX_BUFFER_SIZE]; + DWORD errcode = 0; Begin_root (buff); numbytes = Long_val(len); if (numbytes > UNIX_BUFFER_SIZE) numbytes = UNIX_BUFFER_SIZE; enter_blocking_section(); ret = recv(s, iobuf, (int) numbytes, flg); + if (ret == -1) errcode = WSAGetLastError(); leave_blocking_section(); if (ret == -1) { - win32_maperr(WSAGetLastError()); + win32_maperr(errcode); uerror("recv", Nothing); } memmove (&Byte(buff, Long_val(ofs)), iobuf, ret); @@ -58,6 +60,7 @@ CAMLprim value unix_recvfrom(value sock, value buff, value ofs, value len, value value adr = Val_unit; union sock_addr_union addr; socklen_param_type addr_len; + DWORD errcode = 0; Begin_roots2 (buff, adr); numbytes = Long_val(len); @@ -65,9 +68,10 @@ CAMLprim value unix_recvfrom(value sock, value buff, value ofs, value len, value addr_len = sizeof(sock_addr); enter_blocking_section(); ret = recvfrom(s, iobuf, (int) numbytes, flg, &addr.s_gen, &addr_len); + if (ret == -1) errcode = WSAGetLastError(); leave_blocking_section(); if (ret == -1) { - win32_maperr(WSAGetLastError()); + win32_maperr(errcode); uerror("recvfrom", Nothing); } memmove (&Byte(buff, Long_val(ofs)), iobuf, ret); @@ -86,15 +90,17 @@ CAMLprim value unix_send(value sock, value buff, value ofs, value len, value fla int ret; intnat numbytes; char iobuf[UNIX_BUFFER_SIZE]; + DWORD errcode = 0; numbytes = Long_val(len); if (numbytes > UNIX_BUFFER_SIZE) numbytes = UNIX_BUFFER_SIZE; memmove (iobuf, &Byte(buff, Long_val(ofs)), numbytes); enter_blocking_section(); ret = send(s, iobuf, (int) numbytes, flg); + if (ret == -1) errcode = WSAGetLastError(); leave_blocking_section(); if (ret == -1) { - win32_maperr(WSAGetLastError()); + win32_maperr(errcode); uerror("send", Nothing); } return Val_int(ret); @@ -109,6 +115,7 @@ value unix_sendto_native(value sock, value buff, value ofs, value len, value fla char iobuf[UNIX_BUFFER_SIZE]; union sock_addr_union addr; socklen_param_type addr_len; + DWORD errcode = 0; get_sockaddr(dest, &addr, &addr_len); numbytes = Long_val(len); @@ -116,9 +123,10 @@ value unix_sendto_native(value sock, value buff, value ofs, value len, value fla memmove (iobuf, &Byte(buff, Long_val(ofs)), numbytes); enter_blocking_section(); ret = sendto(s, iobuf, (int) numbytes, flg, &addr.s_gen, addr_len); + if (ret == -1) errcode = WSAGetLastError(); leave_blocking_section(); if (ret == -1) { - win32_maperr(WSAGetLastError()); + win32_maperr(errcode); uerror("sendto", Nothing); } return Val_int(ret); diff --git a/otherlibs/win32unix/winwait.c b/otherlibs/win32unix/winwait.c index bac345b40..873c84a5d 100644 --- a/otherlibs/win32unix/winwait.c +++ b/otherlibs/win32unix/winwait.c @@ -43,14 +43,16 @@ CAMLprim value win_waitpid(value vflags, value vpid_req) int flags; DWORD status, retcode; HANDLE pid_req = (HANDLE) Long_val(vpid_req); + DWORD errcode = 0; flags = convert_flag_list(vflags, wait_flag_table); if ((flags & CAML_WNOHANG) == 0) { enter_blocking_section(); retcode = WaitForSingleObject(pid_req, INFINITE); + if (retcode == WAIT_FAILED) errcode = GetLastError(); leave_blocking_section(); - if (retcode == WAIT_FAILED) { - win32_maperr(GetLastError()); + if (errcode) { + win32_maperr(errcode); uerror("waitpid", Nothing); } } diff --git a/otherlibs/win32unix/write.c b/otherlibs/win32unix/write.c index 8c255e11a..284a4755b 100644 --- a/otherlibs/win32unix/write.c +++ b/otherlibs/win32unix/write.c @@ -25,6 +25,7 @@ CAMLprim value unix_write(value fd, value buf, value vofs, value vlen) intnat ofs, len, written; DWORD numbytes, numwritten; char iobuf[UNIX_BUFFER_SIZE]; + DWORD errcode = 0; Begin_root (buf); ofs = Long_val(vofs); @@ -38,22 +39,19 @@ CAMLprim value unix_write(value fd, value buf, value vofs, value vlen) SOCKET s = Socket_val(fd); enter_blocking_section(); ret = send(s, iobuf, numbytes, 0); + if (ret == SOCKET_ERROR) errcode = WSAGetLastError(); leave_blocking_section(); - if (ret == SOCKET_ERROR) { - win32_maperr(WSAGetLastError()); - uerror("write", Nothing); - } numwritten = ret; } else { - BOOL ret; HANDLE h = Handle_val(fd); enter_blocking_section(); - ret = WriteFile(h, iobuf, numbytes, &numwritten, NULL); + if (! WriteFile(h, iobuf, numbytes, &numwritten, NULL)) + errcode = GetLastError(); leave_blocking_section(); - if (! ret) { - win32_maperr(GetLastError()); - uerror("write", Nothing); - } + } + if (errcode) { + win32_maperr(errcode); + uerror("write", Nothing); } written += numwritten; ofs += numwritten; @@ -81,22 +79,19 @@ CAMLprim value unix_single_write(value fd, value buf, value vofs, value vlen) SOCKET s = Socket_val(fd); enter_blocking_section(); ret = send(s, iobuf, numbytes, 0); + if (ret == SOCKET_ERROR) errcode = WSAGetLastError(); leave_blocking_section(); - if (ret == SOCKET_ERROR) { - win32_maperr(WSAGetLastError()); - uerror("single_write", Nothing); - } numwritten = ret; } else { - BOOL ret; HANDLE h = Handle_val(fd); enter_blocking_section(); - ret = WriteFile(h, iobuf, numbytes, &numwritten, NULL); + if (! WriteFile(h, iobuf, numbytes, &numwritten, NULL)) + errcode = GetLastError(); leave_blocking_section(); - if (! ret) { - win32_maperr(GetLastError()); - uerror("single_write", Nothing); - } + } + if (errcode) { + win32_maperr(errcode); + uerror("single_write", Nothing); } written = numwritten; }