Update Changes.
[p5sagit/p5-mst-13.2.git] / win32 / win32sck.c
index cc96688..0ce53c3 100644 (file)
 #      define TO_SOCKET(x)     (x)
 #endif /* USE_SOCKETS_AS_HANDLES */
 
-#ifdef USE_THREADS
+#if defined(USE_THREADS) || defined(USE_ITHREADS)
 #define StartSockets() \
     STMT_START {                                       \
        if (!wsock_started)                             \
            start_sockets();                            \
-       set_socktype();                         \
+       set_socktype();                                 \
     } STMT_END
 #else
 #define StartSockets() \
@@ -104,8 +104,8 @@ void
 set_socktype(void)
 {
 #ifdef USE_SOCKETS_AS_HANDLES
-#ifdef USE_THREADS
-    dTHX;
+#if defined(USE_THREADS) || defined(USE_ITHREADS)
+    dTHXo;
     if (!w32_init_socktype) {
 #endif
        int iSockOpt = SO_SYNCHRONOUS_NONALERT;
@@ -114,7 +114,7 @@ set_socktype(void)
         */
        setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
                    (char *)&iSockOpt, sizeof(iSockOpt));
-#ifdef USE_THREADS
+#if defined(USE_THREADS) || defined(USE_ITHREADS)
        w32_init_socktype = 1;
     }
 #endif
@@ -143,7 +143,7 @@ my_fdopen(int fd, char *mode)
     /*
      * If we get here, then fd is actually a socket.
      */
-    Newz(1310, fp, 1, FILE);
+    Newz(1310, fp, 1, FILE);   /* XXX leak, good thing this code isn't used */
     if(fp == NULL) {
        errno = ENOMEM;
        return NULL;
@@ -422,18 +422,28 @@ win32_socket(int af, int type, int protocol)
 int
 my_fclose (FILE *pf)
 {
-    int osf, retval;
+    int osf;
     if (!wsock_started)                /* No WinSock? */
        return(fclose(pf));     /* Then not a socket. */
     osf = TO_SOCKET(fileno(pf));/* Get it now before it's gone! */
-    retval = fclose(pf);       /* Must fclose() before closesocket() */
-    if (osf != -1
-       && closesocket(osf) == SOCKET_ERROR
-       && WSAGetLastError() != WSAENOTSOCK)
-    {
-       return EOF;
+    if (osf != -1) {
+       int err;
+       win32_fflush(pf);
+       err = closesocket(osf);
+       if (err == 0) {
+           (void)fclose(pf);   /* handle already closed, ignore error */
+           return 0;
+       }
+       else if (err == SOCKET_ERROR) {
+           err = WSAGetLastError();
+           if (err != WSAENOTSOCK) {
+               (void)fclose(pf);
+               errno = err;
+               return EOF;
+           }
+       }
     }
-    return retval;
+    return fclose(pf);
 }
 
 struct hostent *