From 31ab445fead6824a54f8162d19d5681dd85ebbf0 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 10 Oct 2002 21:07:16 +0000 Subject: * win32/win32.c (rb_w32_fclose, rb_w32_close): use closesocket() for socket. [ruby-win32:382] * win32/win32.c (StartSockets): set NtSocketsInitialized. * win32/win32.h: prototypes; rb_w32_fclose, rb_w32_close git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2956 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +++++++ win32/win32.c | 87 ++++++++++++++++++++++++++++++++++++++++++----------------- win32/win32.h | 13 +++++---- 3 files changed, 80 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index b53d157496..95a5e2923d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Fri Oct 11 06:05:30 2002 Nobuyoshi Nakada + + * win32/win32.c (rb_w32_fclose, rb_w32_close): use closesocket() + for socket. [ruby-win32:382] + + * win32/win32.c (StartSockets): set NtSocketsInitialized. + + * win32/win32.h: prototypes; rb_w32_fclose, rb_w32_close + Fri Oct 11 00:24:57 2002 Nobuyoshi Nakada * gc.c (ruby_xmalloc, ruby_xrealloc): restrict total allocation diff --git a/win32/win32.c b/win32/win32.c index 89b41af33d..0e155ef9e6 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -33,7 +33,11 @@ #define index(x, y) strchr((x), (y)) #endif #define isdirsep(x) ((x) == '/' || (x) == '\\') + #undef stat +#undef fclose +#undef close +#undef setsockopt #ifndef bool #define bool int @@ -1673,7 +1677,7 @@ rb_w32_select (int nfds, fd_set *rd, fd_set *wr, fd_set *ex, #endif /* USE_INTERRUPT_WINSOCK */ int file_nfds; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } r = 0; @@ -1727,7 +1731,7 @@ StartSockets () WORD version; WSADATA retdata; int ret; - int iSockOpt; + int iSockOpt; // // initalize the winsock interface and insure that it's @@ -1765,6 +1769,7 @@ StartSockets () interrupted_event = CreateSignal(); if (!interrupted_event) rb_fatal("Unable to create interrupt event!\n"); + NtSocketsInitialized = 1; } #undef accept @@ -1774,7 +1779,7 @@ rb_w32_accept(int s, struct sockaddr *addr, int *addrlen) { SOCKET r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1797,7 +1802,7 @@ rb_w32_bind(int s, struct sockaddr *addr, int addrlen) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1814,7 +1819,7 @@ int rb_w32_connect(int s, struct sockaddr *addr, int addrlen) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1832,7 +1837,7 @@ int rb_w32_getpeername(int s, struct sockaddr *addr, int *addrlen) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1849,7 +1854,7 @@ int rb_w32_getsockname(int s, struct sockaddr *addr, int *addrlen) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1864,7 +1869,7 @@ int rb_w32_getsockopt(int s, int level, int optname, char *optval, int *optlen) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1881,7 +1886,7 @@ int rb_w32_ioctlsocket(int s, long cmd, u_long *argp) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1898,7 +1903,7 @@ int rb_w32_listen(int s, int backlog) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1915,7 +1920,7 @@ int rb_w32_recv(int s, char *buf, int len, int flags) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1933,7 +1938,7 @@ rb_w32_recvfrom(int s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1950,7 +1955,7 @@ int rb_w32_send(int s, char *buf, int len, int flags) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1968,7 +1973,7 @@ rb_w32_sendto(int s, char *buf, int len, int flags, struct sockaddr *to, int tolen) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -1985,7 +1990,7 @@ int rb_w32_setsockopt(int s, int level, int optname, char *optval, int optlen) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -2002,7 +2007,7 @@ int rb_w32_shutdown(int s, int how) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -2021,7 +2026,7 @@ rb_w32_socket(int af, int type, int protocol) SOCKET s; int fd; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -2043,7 +2048,7 @@ struct hostent * rb_w32_gethostbyaddr (char *addr, int len, int type) { struct hostent *r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -2060,7 +2065,7 @@ struct hostent * rb_w32_gethostbyname (char *name) { struct hostent *r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -2077,7 +2082,7 @@ int rb_w32_gethostname (char *name, int len) { int r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -2094,7 +2099,7 @@ struct protoent * rb_w32_getprotobyname (char *name) { struct protoent *r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -2111,7 +2116,7 @@ struct protoent * rb_w32_getprotobynumber (int num) { struct protoent *r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -2128,7 +2133,7 @@ struct servent * rb_w32_getservbyname (char *name, char *proto) { struct servent *r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -2145,7 +2150,7 @@ struct servent * rb_w32_getservbyport (int port, char *proto) { struct servent *r; - if (!NtSocketsInitialized++) { + if (!NtSocketsInitialized) { StartSockets(); } RUBY_CRITICAL({ @@ -2947,3 +2952,37 @@ pid_t rb_w32_getpid(void) return pid; } + +int +rb_w32_fclose(FILE *fp) +{ + int fd = fileno(fp); + SOCKET sock = TO_SOCKET(fd); + + if (fflush(fp)) return -1; + if (!is_socket(sock)) { + return fclose(fp); + } + _set_osfhnd(fd, (SOCKET)INVALID_HANDLE_VALUE); + fclose(fp); + if (closesocket(sock) == SOCKET_ERROR) { + errno = WSAGetLastError(); + return -1; + } + return 0; +} + +int +rb_w32_close(int fd) +{ + SOCKET sock = TO_SOCKET(fd); + + if (!is_socket(sock)) { + return _close(fd); + } + if (closesocket(sock) == SOCKET_ERROR) { + errno = WSAGetLastError(); + return -1; + } + return 0; +} diff --git a/win32/win32.h b/win32/win32.h index 31a3f0734f..67c054c3fd 100644 --- a/win32/win32.h +++ b/win32/win32.h @@ -1,5 +1,5 @@ -#ifndef EXT_NT_H -#define EXT_NT_H +#ifndef RUBY_WIN32_H +#define RUBY_WIN32_H /* * Copyright (c) 1993, Intergraph Corporation @@ -87,8 +87,8 @@ extern "C++" { #define fputc(_c, _stream) putc(_c, _stream) #define getchar() rb_w32_getc(stdin) #define putchar(_c) rb_w32_putc(_c, stdout) -#define fgetchar(_stream) getchar() -#define fputchar(_c, _stream) putchar(_c) +#define fgetchar() getchar() +#define fputchar(_c) putchar(_c) #ifndef __BORLANDC__ @@ -128,7 +128,8 @@ extern "C++" { #define _open _sopen #endif -#define close _close +#define close rb_w32_close +#define fclose rb_w32_fclose #define creat _creat #define eof _eof #define filelength _filelength @@ -476,6 +477,8 @@ void rb_w32_enter_critical(void); void rb_w32_leave_critical(void); int rb_w32_putc(int, FILE*); int rb_w32_getc(FILE*); +int rb_w32_close(int); +int rb_w32_fclose(FILE*); #define Sleep(msec) (void)rb_w32_sleep(msec) /* -- cgit v1.2.3