This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix creation of libperl519s.a on Win32 with MinGW
[perl5.git] / win32 / win32sck.c
index 7798168..3773f40 100644 (file)
@@ -1,7 +1,7 @@
 /* win32sck.c
  *
  * (c) 1995 Microsoft Corporation. All rights reserved. 
- *             Developed by hip communications inc., http://info.hip.com/info/
+ *             Developed by hip communications inc.
  * Portions (c) 1993 Intergraph Corporation. All rights reserved.
  *
  *    You may distribute under the terms of either the GNU General Public
 #include <io.h>
 
 /* thanks to Beverly Brown     (beverly@datacube.com) */
-#ifdef USE_SOCKETS_AS_HANDLES
-#      define OPEN_SOCKET(x)   win32_open_osfhandle(x,O_RDWR|O_BINARY)
-#      define TO_SOCKET(x)     _get_osfhandle(x)
-#else
-#      define OPEN_SOCKET(x)   (x)
-#      define TO_SOCKET(x)     (x)
-#endif /* USE_SOCKETS_AS_HANDLES */
+#define OPEN_SOCKET(x) win32_open_osfhandle(x,O_RDWR|O_BINARY)
+#define TO_SOCKET(x)   _get_osfhandle(x)
 
 #define StartSockets() \
     STMT_START {                                       \
@@ -68,7 +63,6 @@ EndSockets(void)
 void
 start_sockets(void) 
 {
-    dTHX;
     unsigned short version;
     WSADATA retdata;
     int ret;
@@ -87,69 +81,42 @@ start_sockets(void)
     wsock_started = 1;
 }
 
-#ifndef USE_SOCKETS_AS_HANDLES
-#undef fdopen
-FILE *
-my_fdopen(int fd, char *mode)
-{
-    FILE *fp;
-    char sockbuf[256];
-    int optlen = sizeof(sockbuf);
-    int retval;
-
-    if (!wsock_started)
-       return(fdopen(fd, mode));
-
-    retval = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
-    if(retval == SOCKET_ERROR && WSAGetLastError() == WSAENOTSOCK) {
-       return(fdopen(fd, mode));
-    }
-
-    /*
-     * If we get here, then fd is actually a socket.
-     */
-    Newxz(fp, 1, FILE);        /* XXX leak, good thing this code isn't used */
-    if(fp == NULL) {
-       errno = ENOMEM;
-       return NULL;
-    }
-
-    fp->_file = fd;
-    if(*mode == 'r')
-       fp->_flag = _IOREAD;
-    else
-       fp->_flag = _IOWRT;
-   
-    return fp;
-}
-#endif /* USE_SOCKETS_AS_HANDLES */
-
-
+/* in no sockets Win32 builds, these use the inline functions defined in
+ * perl.h
+ */
 u_long
 win32_htonl(u_long hostlong)
 {
+#ifndef WIN32_NO_SOCKETS
     StartSockets();
+#endif
     return htonl(hostlong);
 }
 
 u_short
 win32_htons(u_short hostshort)
 {
+#ifndef WIN32_NO_SOCKETS
     StartSockets();
+#endif
     return htons(hostshort);
 }
 
 u_long
 win32_ntohl(u_long netlong)
 {
+#ifndef WIN32_NO_SOCKETS
     StartSockets();
+#endif
     return ntohl(netlong);
 }
 
 u_short
 win32_ntohs(u_short netshort)
 {
+#ifndef WIN32_NO_SOCKETS
     StartSockets();
+#endif
     return ntohs(netshort);
 }
 
@@ -258,21 +225,10 @@ int
 win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const struct timeval* timeout)
 {
     int r;
-#ifdef USE_SOCKETS_AS_HANDLES
     int i, fd, save_errno = errno;
     FD_SET nrd, nwr, nex;
+    bool just_sleep = TRUE;
 
-    /* winsock seems incapable of dealing with all three null fd_sets,
-     * so do the (millisecond) sleep as a special case
-     */
-    if (!(rd || wr || ex)) {
-       if (timeout)
-           Sleep(timeout->tv_sec  * 1000 +
-                 timeout->tv_usec / 1000);     /* do the best we can */
-       else
-           Sleep(UINT_MAX);
-       return 0;
-    }
     StartSockets();
 
     FD_ZERO(&nrd);
@@ -282,17 +238,32 @@ win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const
        if (rd && PERL_FD_ISSET(i,rd)) {
            fd = TO_SOCKET(i);
            FD_SET((unsigned)fd, &nrd);
+            just_sleep = FALSE;
        }
        if (wr && PERL_FD_ISSET(i,wr)) {
            fd = TO_SOCKET(i);
            FD_SET((unsigned)fd, &nwr);
+            just_sleep = FALSE;
        }
        if (ex && PERL_FD_ISSET(i,ex)) {
            fd = TO_SOCKET(i);
            FD_SET((unsigned)fd, &nex);
+            just_sleep = FALSE;
        }
     }
 
+    /* winsock seems incapable of dealing with all three fd_sets being empty,
+     * so do the (millisecond) sleep as a special case
+     */
+    if (just_sleep) {
+       if (timeout)
+           Sleep(timeout->tv_sec  * 1000 +
+                 timeout->tv_usec / 1000);     /* do the best we can */
+       else
+           Sleep(UINT_MAX);
+       return 0;
+    }
+
     errno = save_errno;
     SOCKET_TEST_ERROR(r = select(nfds, &nrd, &nwr, &nex, timeout));
     save_errno = errno;
@@ -315,9 +286,6 @@ win32_select(int nfds, Perl_fd_set* rd, Perl_fd_set* wr, Perl_fd_set* ex, const
        }
     }
     errno = save_errno;
-#else
-    SOCKET_TEST_ERROR(r = select(nfds, rd, wr, ex, timeout));
-#endif
     return r;
 }
 
@@ -367,9 +335,6 @@ win32_closesocket(SOCKET s)
     return r;
 }
 
-#ifdef USE_SOCKETS_AS_HANDLES
-#define WIN32_OPEN_SOCKET(af, type, protocol) open_ifs_socket(af, type, protocol)
-
 void
 convert_proto_info_w2a(WSAPROTOCOL_INFOW *in, WSAPROTOCOL_INFOA *out)
 {
@@ -428,25 +393,17 @@ open_ifs_socket(int af, int type, int protocol)
     return out;
 }
 
-#else
-#define WIN32_OPEN_SOCKET(af, type, protocol) socket(af, type, protocol)
-#endif
-
 SOCKET
 win32_socket(int af, int type, int protocol)
 {
     SOCKET s;
 
-#ifndef USE_SOCKETS_AS_HANDLES
-    SOCKET_TEST(s = socket(af, type, protocol), INVALID_SOCKET);
-#else
     StartSockets();
 
-    if((s = WIN32_OPEN_SOCKET(af, type, protocol)) == INVALID_SOCKET)
+    if((s = open_ifs_socket(af, type, protocol)) == INVALID_SOCKET)
        errno = WSAGetLastError();
     else
        s = OPEN_SOCKET(s);
-#endif /* USE_SOCKETS_AS_HANDLES */
 
     return s;
 }
@@ -468,9 +425,6 @@ int my_close(int fd)
        int err;
        err = closesocket(osf);
        if (err == 0) {
-#if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
-            _set_osfhnd(fd, INVALID_HANDLE_VALUE);
-#endif
            (void)close(fd);    /* handle already closed, ignore error */
            return 0;
        }
@@ -499,9 +453,6 @@ my_fclose (FILE *pf)
        win32_fflush(pf);
        err = closesocket(osf);
        if (err == 0) {
-#if defined(USE_FIXED_OSFHANDLE) || defined(PERL_MSVCRT_READFIX)
-            _set_osfhnd(win32_fileno(pf), INVALID_HANDLE_VALUE);
-#endif
            (void)fclose(pf);   /* handle already closed, ignore error */
            return 0;
        }
@@ -517,62 +468,6 @@ my_fclose (FILE *pf)
     return fclose(pf);
 }
 
-#undef fstat
-int
-my_fstat(int fd, Stat_t *sbufptr)
-{
-    /* This fixes a bug in fstat() on Windows 9x.  fstat() uses the
-     * GetFileType() win32 syscall, which will fail on Windows 9x.
-     * So if we recognize a socket on Windows 9x, we return the
-     * same results as on Windows NT/2000.
-     * XXX this should be extended further to set S_IFSOCK on
-     * sbufptr->st_mode.
-     */
-    int osf;
-    if (!wsock_started || IsWinNT()) {
-#if defined(WIN64) || defined(USE_LARGE_FILES)
-#if defined(__BORLANDC__) /* buk */
-       return win32_fstat(fd, sbufptr );
-#else
-       return _fstati64(fd, sbufptr);
-#endif
-#else
-       return fstat(fd, sbufptr);
-#endif
-    }
-
-    osf = TO_SOCKET(fd);
-    if (osf != -1) {
-       char sockbuf[256];
-       int optlen = sizeof(sockbuf);
-       int retval;
-
-       retval = getsockopt((SOCKET)osf, SOL_SOCKET, SO_TYPE, sockbuf, &optlen);
-       if (retval != SOCKET_ERROR || WSAGetLastError() != WSAENOTSOCK) {
-#if defined(__BORLANDC__)&&(__BORLANDC__<=0x520)
-           sbufptr->st_mode = S_IFIFO;
-#else
-           sbufptr->st_mode = _S_IFIFO;
-#endif
-           sbufptr->st_rdev = sbufptr->st_dev = (dev_t)fd;
-           sbufptr->st_nlink = 1;
-           sbufptr->st_uid = sbufptr->st_gid = sbufptr->st_ino = 0;
-           sbufptr->st_atime = sbufptr->st_mtime = sbufptr->st_ctime = 0;
-           sbufptr->st_size = (Off_t)0;
-           return 0;
-       }
-    }
-#if defined(WIN64) || defined(USE_LARGE_FILES)
-#if defined(__BORLANDC__) /* buk */
-    return win32_fstat(fd, sbufptr );
-#else
-    return _fstati64(fd, sbufptr);
-#endif
-#else
-    return fstat(fd, sbufptr);
-#endif
-}
-
 struct hostent *
 win32_gethostbyaddr(const char *addr, int len, int type)
 {
@@ -621,11 +516,12 @@ win32_getprotobynumber(int num)
 struct servent *
 win32_getservbyname(const char *name, const char *proto)
 {
-    dTHX;    
+    dTHXa(NULL);    
     struct servent *r;
 
     SOCKET_TEST(r = getservbyname(name, proto), NULL);
     if (r) {
+        aTHXa(PERL_GET_THX);
        r = win32_savecopyservent(&w32_servent, r, proto);
     }
     return r;
@@ -634,11 +530,12 @@ win32_getservbyname(const char *name, const char *proto)
 struct servent *
 win32_getservbyport(int port, const char *proto)
 {
-    dTHX; 
+    dTHXa(NULL)
     struct servent *r;
 
     SOCKET_TEST(r = getservbyport(port, proto), NULL);
     if (r) {
+        aTHXa(PERL_GET_THX);
        r = win32_savecopyservent(&w32_servent, r, proto);
     }
     return r;
@@ -647,7 +544,6 @@ win32_getservbyport(int port, const char *proto)
 int
 win32_ioctl(int i, unsigned int u, char *data)
 {
-    dTHX;
     u_long u_long_arg; 
     int retval;
     
@@ -692,101 +588,88 @@ win32_inet_addr(const char FAR *cp)
 void
 win32_endhostent() 
 {
-    dTHX;
-    Perl_croak_nocontext("endhostent not implemented!\n");
+    win32_croak_not_implemented("endhostent");
 }
 
 void
 win32_endnetent()
 {
-    dTHX;
-    Perl_croak_nocontext("endnetent not implemented!\n");
+    win32_croak_not_implemented("endnetent");
 }
 
 void
 win32_endprotoent()
 {
-    dTHX;
-    Perl_croak_nocontext("endprotoent not implemented!\n");
+    win32_croak_not_implemented("endprotoent");
 }
 
 void
 win32_endservent()
 {
-    dTHX;
-    Perl_croak_nocontext("endservent not implemented!\n");
+    win32_croak_not_implemented("endservent");
 }
 
 
 struct netent *
 win32_getnetent(void) 
 {
-    dTHX;
-    Perl_croak_nocontext("getnetent not implemented!\n");
+    win32_croak_not_implemented("getnetent");
     return (struct netent *) NULL;
 }
 
 struct netent *
 win32_getnetbyname(char *name) 
 {
-    dTHX;
-    Perl_croak_nocontext("getnetbyname not implemented!\n");
+    win32_croak_not_implemented("getnetbyname");
     return (struct netent *)NULL;
 }
 
 struct netent *
 win32_getnetbyaddr(long net, int type) 
 {
-    dTHX;
-    Perl_croak_nocontext("getnetbyaddr not implemented!\n");
+    win32_croak_not_implemented("getnetbyaddr");
     return (struct netent *)NULL;
 }
 
 struct protoent *
 win32_getprotoent(void) 
 {
-    dTHX;
-    Perl_croak_nocontext("getprotoent not implemented!\n");
+    win32_croak_not_implemented("getprotoent");
     return (struct protoent *) NULL;
 }
 
 struct servent *
 win32_getservent(void) 
 {
-    dTHX;
-    Perl_croak_nocontext("getservent not implemented!\n");
+    win32_croak_not_implemented("getservent");
     return (struct servent *) NULL;
 }
 
 void
 win32_sethostent(int stayopen)
 {
-    dTHX;
-    Perl_croak_nocontext("sethostent not implemented!\n");
+    win32_croak_not_implemented("sethostent");
 }
 
 
 void
 win32_setnetent(int stayopen)
 {
-    dTHX;
-    Perl_croak_nocontext("setnetent not implemented!\n");
+    win32_croak_not_implemented("setnetent");
 }
 
 
 void
 win32_setprotoent(int stayopen)
 {
-    dTHX;
-    Perl_croak_nocontext("setprotoent not implemented!\n");
+    win32_croak_not_implemented("setprotoent");
 }
 
 
 void
 win32_setservent(int stayopen)
 {
-    dTHX;
-    Perl_croak_nocontext("setservent not implemented!\n");
+    win32_croak_not_implemented("setservent");
 }
 
 static struct servent*
@@ -795,11 +678,9 @@ win32_savecopyservent(struct servent*d, struct servent*s, const char *proto)
     d->s_name = s->s_name;
     d->s_aliases = s->s_aliases;
     d->s_port = s->s_port;
-#ifndef __BORLANDC__   /* Buggy on Win95 and WinNT-with-Borland-WSOCK */
-    if (!IsWin95() && s->s_proto && strlen(s->s_proto))
+    if (s->s_proto && strlen(s->s_proto))
        d->s_proto = s->s_proto;
     else
-#endif
     if (proto && strlen(proto))
        d->s_proto = (char *)proto;
     else