Bugzilla bug #62877: checked in patches contributed by Javier Pedemonte
authorwtc%netscape.com
Fri, 02 Mar 2001 02:40:52 +0000
changeset 1762 18b433a4b00356d5a73688b316dcd5a41559ba2b
parent 1758 119766608da60a3e0301c96b1d8723a5ee759db3
child 1764 d865c5d903c27de924e916a86fa8f04e2691b41a
push idunknown
push userunknown
push dateunknown
bugs62877
Bugzilla bug #62877: checked in patches contributed by Javier Pedemonte <pedemont@us.ibm.com> and Dmitry Kubov <dmitry@north.cs.msu.su>. Modified files: prinet.h, _os2.h, os2poll.c, and os2sock.c.
pr/include/md/_os2.h
pr/include/prinet.h
pr/src/md/os2/os2poll.c
pr/src/md/os2/os2sock.c
--- a/pr/include/md/_os2.h
+++ b/pr/include/md/_os2.h
@@ -54,16 +54,21 @@
  *    o Use errno rather than sock_errno()
  *    o Use close rather than soclose
  *    o Ignore sock_init calls.
  */
 #define strcmpi stricmp 
 #define sock_errno() errno
 #define soclose close
 #define sock_init()
+
+#include <sys/builtin.h>
+#include <sys/smutex.h>
+static _smutex _md_shm_lock = 0;
+
 #endif
 
 /*
  * Internal configuration macros
  */
 
 #define PR_LINKER_ARCH      "os2"
 #define _PR_SI_SYSNAME        "OS2"
@@ -264,17 +269,24 @@ extern PRInt32 _MD_CloseSocket(PRInt32 o
 #define _MD_CLOSE_SOCKET              _MD_CloseSocket
 #define _MD_SENDTO                    (_PR_MD_SENDTO)
 #define _MD_RECVFROM                  (_PR_MD_RECVFROM)
 #define _MD_SOCKETPAIR(s, type, proto, sv) -1
 #define _MD_GETSOCKNAME               (_PR_MD_GETSOCKNAME)
 #define _MD_GETPEERNAME               (_PR_MD_GETPEERNAME)
 #define _MD_GETSOCKOPT                (_PR_MD_GETSOCKOPT)
 #define _MD_SETSOCKOPT                (_PR_MD_SETSOCKOPT)
+
+#ifdef XP_OS2_EMX
+extern PRInt32 _MD_SELECT(int nfds, fd_set *readfds, fd_set *writefds,
+                                    fd_set *exceptfds, struct timeval *timeout);
+#else
 #define _MD_SELECT                    select
+#endif
+
 #define _MD_FSYNC                     _PR_MD_FSYNC
 #define _MD_SET_FD_INHERITABLE        (_PR_MD_SET_FD_INHERITABLE)
 
 #define _MD_INIT_ATOMIC               _PR_MD_INIT_ATOMIC
 #define _MD_ATOMIC_INCREMENT(x)       _PR_MD_ATOMIC_INCREMENT(x)
 #define _MD_ATOMIC_ADD(x,y)			  _PR_MD_ATOMIC_ADD(x,y)
 #define _MD_ATOMIC_DECREMENT(x)       _PR_MD_ATOMIC_DECREMENT(x)
 #define _MD_ATOMIC_SET(x,y)           _PR_MD_ATOMIC_SET(x, y)
--- a/pr/include/prinet.h
+++ b/pr/include/prinet.h
@@ -86,17 +86,17 @@ struct sockaddr_dl;
 #include <rpc/types.h> /* the only place that defines INADDR_LOOPBACK */
 #endif
 
 /*
  * OS/2 hack.  For some reason INADDR_LOOPBACK is not defined in the
  * socket headers.
  */
 #if defined(OS2) && !defined(INADDR_LOOPBACK)
-#define INADDR_LOOPBACK gethostid()
+#define INADDR_LOOPBACK 0x7f000001
 #endif
 
 /*
  * Prototypes of ntohl() etc. are declared in <machine/endian.h>
  * on these platforms.
  */
 #if defined(BSDI) || defined(OSF1)
 #include <machine/endian.h>
--- a/pr/src/md/os2/os2poll.c
+++ b/pr/src/md/os2/os2poll.c
@@ -72,16 +72,19 @@ PRInt32 _PR_MD_PR_POLL(
     ** Is it an empty set? If so, just sleep for the timeout and return
     */
     if (0 == npds)
     {
         PR_Sleep(timeout);
         return 0;
     }
 
+    remaining = timeout;
+    start = PR_IntervalNow();
+
   	FD_ZERO(&rd);
   	FD_ZERO(&wt);
     FD_ZERO(&ex);
 
 	ready = 0;
     for (pd = pds, epd = pd + npds; pd < epd; pd++)
     {
         PRInt16 in_flags_read = 0, in_flags_write = 0;
@@ -172,19 +175,16 @@ PRInt32 _PR_MD_PR_POLL(
                     pd->out_flags = PR_POLL_NVAL;  /* bogii */
                 }
             }
         }
     }
 
     if (0 != ready) return ready;  /* no need to block */
 
-    remaining = timeout;
-    start = PR_IntervalNow();
-
 retry:
     if (timeout != PR_INTERVAL_NO_TIMEOUT)
     {
         PRInt32 ticksPerSecond = PR_TicksPerSecond();
         tv.tv_sec = remaining / ticksPerSecond;
         tv.tv_usec = remaining - (ticksPerSecond * tv.tv_sec);
         tv.tv_usec = (PR_USEC_PER_SEC * tv.tv_usec) / ticksPerSecond;
         tvp = &tv;
@@ -271,8 +271,156 @@ retry:
         {
    		    PR_ASSERT(err != EINTR);  /* should have been handled above */
    		    _PR_MD_MAP_SELECT_ERROR(err);
         }
    }
    return ready;
  }
 
+#ifdef XP_OS2_EMX
+HMTX thread_select_mutex = 0;	/* because EMX's select is not thread safe - duh! */
+
+typedef struct _thread_select_st {
+	int		nfds;
+	int		isrdfds;
+	struct _fd_set *readfds;
+	int		iswrfds;
+	struct _fd_set *writefds;
+	int		isexfds;
+	struct _fd_set *exceptfds;
+	int		istimeout;
+	struct timeval	timeout;
+	volatile HEV	event;
+	int		result;
+	int		select_errno;
+	volatile int	done;
+} *pthread_select_t;
+	
+void _thread_select(void * arg)
+{
+	pthread_select_t	self = arg;
+	int			result, chkstdin;
+	struct _fd_set		readfds;
+	struct _fd_set		writefds;
+	struct _fd_set		exceptfds;
+	HEV			event = self->event;
+
+	chkstdin = (self->isrdfds && FD_ISSET(0,self->readfds))?1:0;
+
+	do {
+		struct timeval	timeout = {0L,0L};
+
+
+		if (self->isrdfds) readfds = *self->readfds;
+		if (self->iswrfds) writefds = *self->writefds;
+		if (self->isexfds) exceptfds = *self->exceptfds;
+		
+		if (chkstdin) FD_CLR(0,&readfds);
+
+		if (!thread_select_mutex) 
+			DosCreateMutexSem(NULL,&thread_select_mutex,0,1);
+		else
+			DosRequestMutexSem(thread_select_mutex,SEM_INDEFINITE_WAIT);
+		result = select(
+			self->nfds, 
+			self->isrdfds?&readfds:NULL,
+			self->iswrfds?&writefds:NULL,
+			self->isexfds?&exceptfds:NULL,
+			&timeout);
+		DosReleaseMutexSem(thread_select_mutex);
+
+		if (chkstdin) {
+			int charcount = 0, res;
+			res = ioctl(0,FIONREAD,&charcount);
+			if (res==0 && charcount>0) FD_SET(0,&readfds);
+		}
+				
+		if (result>0) {
+			self->done++;
+			if (self->isrdfds) *self->readfds = readfds;
+			if (self->iswrfds) *self->writefds = writefds;
+			if (self->isexfds) *self->exceptfds = exceptfds;
+		} else
+		if (result) self->done++;
+		else DosSleep(1);
+
+	} while (self->event!=0 && self->done==0);
+
+	if (self->event) {
+		self->select_errno = (result < 0)?errno:0;
+		self->result = result;
+		self->done = 3;
+		DosPostEventSem(event);
+	} else {
+		self->done = 3;
+		free(self);
+	}
+
+}
+
+PRInt32
+_MD_SELECT(int nfds, fd_set *readfds, fd_set *writefds,
+                  fd_set *exceptfds, struct timeval *timeout)
+{
+	pthread_select_t sel;
+	HEV		ev = 0;
+	HTIMER		timer = 0;
+	int		result = 0;
+	APIRET		rc;
+	unsigned long	msecs = SEM_INDEFINITE_WAIT;
+
+	if (timeout) {
+		if (timeout->tv_sec != 0 || timeout->tv_usec != 0) 
+			msecs = (timeout->tv_sec * 1000L) + (timeout->tv_usec / 1000L);
+		else
+			msecs = SEM_IMMEDIATE_RETURN;
+	};
+
+	if (!(sel = (pthread_select_t) malloc(sizeof(struct _thread_select_st)))) {
+		result = -1;
+		errno = ENOMEM;
+	} else {
+		sel->nfds = nfds;
+		sel->isrdfds = readfds?1:0;
+		if (sel->isrdfds) sel->readfds = readfds;
+		sel->iswrfds = writefds?1:0;
+		if (sel->iswrfds) sel->writefds = writefds;
+		sel->isexfds = exceptfds?1:0;
+		if (sel->isexfds) sel->exceptfds = exceptfds;
+		sel->istimeout = timeout?1:0;
+		if (sel->istimeout) sel->timeout = *timeout;
+	
+		rc = DosCreateEventSem(NULL,&ev,0,FALSE);
+
+		sel->event = ev;
+		if (msecs == SEM_IMMEDIATE_RETURN)
+			sel->done = 1;
+		else
+			sel->done = 0;
+
+		if (_beginthread(_thread_select,NULL,65536,(void *)sel) == -1) {
+			result = -1; sel->event = 0;
+			DosCloseEventSem(ev);
+		} else {
+			rc = DosWaitEventSem(ev,msecs);
+			if ((!sel->done) && (msecs != SEM_IMMEDIATE_RETURN)) {	/* Interrupted by other thread or timeout */
+				sel->event = 0;
+				result = 0;
+				errno = ETIMEDOUT;
+				
+			} else {
+				while (sel->done && sel->done != 3) {
+					DosSleep(1);
+				}
+				sel->event = 0;
+				result = sel->result;
+				if (sel->select_errno) errno = sel->select_errno;
+				free(sel);
+			}
+			rc = DosCloseEventSem(ev);
+		}
+	}
+
+	return (result);
+}
+
+#endif
--- a/pr/src/md/os2/os2sock.c
+++ b/pr/src/md/os2/os2sock.c
@@ -67,17 +67,17 @@ PRInt32
     PRInt32   err;
 
     sock = socket(af, type, flags);
 
     if (sock == -1 ) 
     {
         int rv = sock_errno();
         soclose(sock);
-		_PR_MD_MAP_SOCKET_ERROR(rv);
+        _PR_MD_MAP_SOCKET_ERROR(rv);
         return (PRInt32) -1;
     }
 
     /*
     ** Make the socket Non-Blocking
     */
     rv = ioctl( sock, FIONBIO, (char *) &one, sizeof(one));
     if ( rv != 0 )
@@ -94,29 +94,29 @@ PRInt32
 **
 */
 PRInt32
 _MD_CloseSocket(PRInt32 osfd)
 {
     PRInt32 rv = -1;
 
     rv = soclose((int) osfd );
-	if (rv < 0)
-		_PR_MD_MAP_SOCKET_ERROR(sock_errno());
+    if (rv < 0)
+        _PR_MD_MAP_SOCKET_ERROR(sock_errno());
 
     return rv;
 }
 
 PRInt32
 _MD_SocketAvailable(PRFileDesc *fd)
 {
     PRInt32 result;
 
     if (ioctl(fd->secret->md.osfd, FIONREAD, (char *) &result, sizeof(result)) < 0) {
-		PR_SetError(PR_BAD_DESCRIPTOR_ERROR, sock_errno());
+        PR_SetError(PR_BAD_DESCRIPTOR_ERROR, sock_errno());
         return -1;
     }
     return result;
 }
 
 PRInt32
 _MD_Accept(PRFileDesc *fd, PRNetAddr *raddr, PRUint32 *rlen,
               PRIntervalTime timeout )
@@ -136,94 +136,97 @@ PRInt32
     if (timeout == PR_INTERVAL_NO_TIMEOUT) 
     {
         while ((rv = accept(osfd, (struct sockaddr *) raddr, (int *) rlen)) == -1) 
         {
             if (((err = sock_errno()) == EWOULDBLOCK) 
                 && (!fd->secret->nonblocking))
             {
 #ifdef BSD_SELECT
-                if ((rv = select(osfd + 1, &rd, NULL, NULL,NULL)) == -1) {
+                if ((rv = _MD_SELECT(osfd + 1, &rd, NULL, NULL,NULL)) == -1) {
 #else
-                if ((rv = select(socks, 1, 0, 0, -1)) == -1) {
+                if ((rv = _MD_SELECT(socks, 1, 0, 0, -1)) == -1) {
 #endif
-					_PR_MD_MAP_SELECT_ERROR(sock_errno());
+                    _PR_MD_MAP_SELECT_ERROR(sock_errno());
                     break;
-            } 
+                } 
             } 
             else {
-				_PR_MD_MAP_ACCEPT_ERROR(err);
+                _PR_MD_MAP_ACCEPT_ERROR(err);
                 break;
-        }
+            }
         }
         return(rv);
     } 
     else if (timeout == PR_INTERVAL_NO_WAIT) 
     {
         if ((rv = accept(osfd, (struct sockaddr *) raddr, (int *) rlen)) == -1)
         {
             if (((err = sock_errno()) == EWOULDBLOCK) 
                 && (!fd->secret->nonblocking))
             {
-				PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+                PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
             }
             else
             {
                 _PR_MD_MAP_ACCEPT_ERROR(err);
             }
         }
-            return(rv);
+        return(rv);
     } 
     else 
     {
 retry:
         if ((rv = accept(osfd, (struct sockaddr *) raddr, (int *) rlen)) == -1) 
         {
             if (((err = sock_errno()) == EWOULDBLOCK) 
                 && (!fd->secret->nonblocking))
             {
 #ifdef BSD_SELECT
                 tv.tv_sec = PR_IntervalToSeconds(timeout);
-                tv.tv_usec = PR_IntervalToMicroseconds(
-                    timeout - PR_SecondsToInterval(tv.tv_sec));
+                tv.tv_usec = PR_IntervalToMicroseconds(timeout - PR_SecondsToInterval(tv.tv_sec));
                 tvp = &tv;
-                rv = select(osfd + 1, &rd, NULL, NULL, tvp);
+                rv = _MD_SELECT(osfd + 1, &rd, NULL, NULL, tvp);
 #else
                 long lTimeout = PR_IntervalToMilliseconds(timeout); 
-                rv = select(socks, 1, 0, 0, lTimeout);
+                rv = _MD_SELECT(socks, 1, 0, 0, lTimeout);
 #endif
                 if (rv > 0) {
                     goto retry;
                 } 
                 else if (rv == 0) 
                 {
-					PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+                    PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
                     rv = -1;
                 } else {
-					_PR_MD_MAP_SELECT_ERROR(sock_errno());
+                    _PR_MD_MAP_SELECT_ERROR(sock_errno());
                 }
             } else {
-				_PR_MD_MAP_ACCEPT_ERROR(err);
+                _PR_MD_MAP_ACCEPT_ERROR(err);
             }
         }
     }
     return(rv);
 } /* end _MD_Accept() */
 
 
 
 PRInt32
 _PR_MD_CONNECT(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen, 
                PRIntervalTime timeout)
 {
     PRInt32 osfd = fd->secret->md.osfd;
     PRInt32 rv;
     int err, len;
 #ifdef BSD_SELECT
+#ifdef XP_OS2//_VACPP
+    fd_set wd;
+#else
     fd_set wd, ex;
+#endif  #vacpp
     struct timeval tv, *tvp;
 #else
     int socks[1]; 
     long lTimeout = -1;
 #endif
 
     if ((rv = connect(osfd, (struct sockaddr *) addr, addrlen)) == -1) 
     {
@@ -231,40 +234,70 @@ PRInt32
         if ((!fd->secret->nonblocking) && ((err == EINPROGRESS) || (err == EWOULDBLOCK)))
         {
 #ifdef BSD_SELECT
            if (timeout == PR_INTERVAL_NO_TIMEOUT)
                tvp = NULL;
             else 
             {
                 tv.tv_sec = PR_IntervalToSeconds(timeout);
-                tv.tv_usec = PR_IntervalToMicroseconds(
-                timeout - PR_SecondsToInterval(tv.tv_sec));
+                tv.tv_usec = PR_IntervalToMicroseconds(timeout - PR_SecondsToInterval(tv.tv_sec));
                 tvp = &tv;
             }
 
             FD_ZERO(&wd);
             FD_SET(osfd, &wd);
+#ifdef XP_OS2//_VACPP
+            rv = _MD_SELECT(osfd + 1, NULL, &wd, NULL, tvp);
+#else
             FD_ZERO(&ex);
             FD_SET(osfd, &ex);
-            rv = select(osfd + 1, NULL, &wd, &ex, tvp);
-#else
+            rv = _MD_SELECT(osfd + 1, NULL, &wd, &ex, tvp);
+#endif  #vacpp
+#else #!bsd_select
             if (timeout == PR_INTERVAL_NO_TIMEOUT)
                 lTimeout = -1;
             else 
             {
                 lTimeout = PR_IntervalToMilliseconds(timeout);  
             }
 	    
             socks[0] = osfd; 
-            rv = select(socks, 0, 1, 1, lTimeout);
+#ifdef XP_OS2//_VACPP
+            rv = _MD_SELECT(socks, 0, 1, 0, lTimeout);
+#else
+            rv = _MD_SELECT(socks, 0, 1, 1, lTimeout);
+#endif  #vacpp
 #endif
             if (rv > 0) 
             {
 #ifdef BSD_SELECT
+#ifdef XP_OS2//_VACPP
+                if (FD_ISSET(osfd, &wd))
+                {
+                    //DosSleep(0);
+                    len = sizeof(err);
+                    if (getsockopt(osfd, SOL_SOCKET, SO_ERROR,
+                           (char *) &err, &len) < 0)
+                    {  
+                        _PR_MD_MAP_GETSOCKOPT_ERROR(sock_errno());
+                        return -1;
+                    }
+
+                    if (err != 0)
+                    {
+                        _PR_MD_MAP_CONNECT_ERROR(err);
+                        return -1;
+                    }
+                    else
+                        return 0;   /* it's connected */
+                }
+                else
+                    return -1;
+#else
                if (FD_ISSET(osfd, &ex))
                {
                    DosSleep(0);
                    len = sizeof(err);
                    if (getsockopt(osfd, SOL_SOCKET, SO_ERROR,
                            (char *) &err, &len) < 0)
                    {  
                        _PR_MD_MAP_GETSOCKOPT_ERROR(sock_errno());
@@ -276,34 +309,47 @@ PRInt32
                        PR_SetError(PR_UNKNOWN_ERROR, 0);
                    return -1;
                }
                if (FD_ISSET(osfd, &wd))
                {
                    /* it's connected */
                    return 0;
                }
-#else
-               if (getsockopt(osfd, SOL_SOCKET, SO_ERROR,
-                       (char *) &err, &len) < 0)
-               {  
-                   _PR_MD_MAP_GETSOCKOPT_ERROR(sock_errno());
-                   return -1;
-               }
-               else
-                  return 0; /* It's connected ! */
+#endif  #vacpp
+#else #!bsd_select
+                if (socks[0] == osfd)
+                {
+                  len = sizeof(err);
+                  if (getsockopt(osfd, SOL_SOCKET, SO_ERROR,
+                         (char *) &err, &len) < 0)
+                  {  
+                    _PR_MD_MAP_GETSOCKOPT_ERROR(sock_errno());
+                    return -1;
+                  }
+
+                  if (err != 0)
+                  {
+                    _PR_MD_MAP_CONNECT_ERROR(err);
+                    return -1;
+                  }
+                  else
+                    return 0;   /* it's connected */
+                }
+                else
+                  return -1;
 #endif
-            } 
+            }
             else if (rv == 0) 
             {
-				PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+                PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
                 return(-1);
             } else if (rv < 0) 
             {
-				_PR_MD_MAP_SELECT_ERROR(sock_errno());
+                _PR_MD_MAP_SELECT_ERROR(sock_errno());
                 return(-1);
             }
         } 
         _PR_MD_MAP_CONNECT_ERROR(err);
     }
     return rv;
 }
 
@@ -359,43 +405,43 @@ PRInt32
            } 
            else 
            {
                tv.tv_sec = PR_IntervalToSeconds(timeout);
                tv.tv_usec = PR_IntervalToMicroseconds(
                timeout - PR_SecondsToInterval(tv.tv_sec));
                tvp = &tv;
            }
-           if ((rv = select(osfd + 1, &rd, NULL, NULL, tvp)) == -1) 
+           if ((rv = _MD_SELECT(osfd + 1, &rd, NULL, NULL, tvp)) == -1) 
 #else
             socks[0] = osfd; 
             if (timeout == PR_INTERVAL_NO_TIMEOUT) 
             {
                 lTimeout = -1; 
             } 
             else 
             {
                 lTimeout = PR_IntervalToMilliseconds(timeout); 
             }
-            if ((rv = select(socks, 1, 0, 0, lTimeout)) == -1) 
+            if ((rv = _MD_SELECT(socks, 1, 0, 0, lTimeout)) == -1) 
 #endif
             {
-				_PR_MD_MAP_SELECT_ERROR(sock_errno());
+                _PR_MD_MAP_SELECT_ERROR(sock_errno());
                 return -1;
             } 
             else if (rv == 0) 
             {
-				PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+                PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
                 rv = -1;
                 break;
             }
         } 
         else 
         {
-			_PR_MD_MAP_RECV_ERROR(err);
+            _PR_MD_MAP_RECV_ERROR(err);
             break;
         }
     } /* end while() */
     return(rv);
 }
 
 PRInt32
 _PR_MD_SEND(PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags,
@@ -428,42 +474,42 @@ PRInt32
                 {
                     tv.tv_sec = PR_IntervalToSeconds(timeout);
                     tv.tv_usec = PR_IntervalToMicroseconds(
                         timeout - PR_SecondsToInterval(tv.tv_sec));
                     tvp = &tv;
                 }
                 FD_ZERO(&wd);
                 FD_SET(osfd, &wd);
-                if ((rv = select( osfd + 1, NULL, &wd, NULL,tvp)) == -1) {
+                if ((rv = _MD_SELECT( osfd + 1, NULL, &wd, NULL,tvp)) == -1) {
 #else
                 if ( timeout == PR_INTERVAL_NO_TIMEOUT ) 
                 {
                     lTimeout = -1; 
                 } 
                 else 
                 {
                     lTimeout = PR_IntervalToMilliseconds(timeout); 
                 }
                 socks[0] = osfd; 
-                if ((rv = select( socks, 0, 1, 0, lTimeout)) == -1) {
+                if ((rv = _MD_SELECT( socks, 0, 1, 0, lTimeout)) == -1) {
 #endif
-					_PR_MD_MAP_SELECT_ERROR(sock_errno());
+                    _PR_MD_MAP_SELECT_ERROR(sock_errno());
                     break;
-				}
+                }
                 if (rv == 0) 
                 {
-					PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
-                        return -1;
+                    PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+                    return -1;
                 }
             } 
             else {
-				_PR_MD_MAP_SEND_ERROR(err);
+                _PR_MD_MAP_SEND_ERROR(err);
                 return -1;
-        }
+            }
         }
         bytesSent += rv;
         if (fd->secret->nonblocking)
         {
             break;
         }
         if ((rv >= 0) && (bytesSent < amount )) 
         {
@@ -476,36 +522,36 @@ PRInt32
            {
                tv.tv_sec = PR_IntervalToSeconds(timeout);
                tv.tv_usec = PR_IntervalToMicroseconds(
                    timeout - PR_SecondsToInterval(tv.tv_sec));
                tvp = &tv;
            }
            FD_ZERO(&wd);
            FD_SET(osfd, &wd);
-           if ((rv = select(osfd + 1, NULL, &wd, NULL,tvp)) == -1) {
+           if ((rv = _MD_SELECT(osfd + 1, NULL, &wd, NULL,tvp)) == -1) {
 #else
             if ( timeout == PR_INTERVAL_NO_TIMEOUT ) 
             {
                 lTimeout = -1; 
             } 
             else 
             {
                 lTimeout = PR_IntervalToMilliseconds(timeout); 
             }
             socks[0] = osfd; 
-            if ((rv = select(socks, 0, 1, 0,lTimeout)) == -1) {
+            if ((rv = _MD_SELECT(socks, 0, 1, 0,lTimeout)) == -1) {
 #endif
-				_PR_MD_MAP_SELECT_ERROR(sock_errno());
+                _PR_MD_MAP_SELECT_ERROR(sock_errno());
                 break;
-			}
+            }
             if (rv == 0) 
             {
-				PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
-                    return -1;
+                PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+                return -1;
             }
         }
     }
     return bytesSent;
 }
 
 PRInt32
 _PR_MD_SENDTO(PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags,
@@ -539,42 +585,42 @@ PRInt32
                {
                    tv.tv_sec = PR_IntervalToSeconds(timeout);
                    tv.tv_usec = PR_IntervalToMicroseconds(
                        timeout - PR_SecondsToInterval(tv.tv_sec));
                    tvp = &tv;
                }
                FD_ZERO(&wd);
                FD_SET(osfd, &wd);
-               if ((rv = select(osfd + 1, NULL, &wd, NULL, tvp)) == -1) {
+               if ((rv = _MD_SELECT(osfd + 1, NULL, &wd, NULL, tvp)) == -1) {
 #else
                 if ( timeout == PR_INTERVAL_NO_TIMEOUT ) 
                 {
                     lTimeout = -1; 
                 } 
                 else 
                 {
                     lTimeout = PR_IntervalToMilliseconds(timeout);
                 }
                 socks[0] = osfd; 
-                if ((rv = select(socks, 0, 1, 0, lTimeout)) == -1) {
+                if ((rv = _MD_SELECT(socks, 0, 1, 0, lTimeout)) == -1) {
 #endif
-					_PR_MD_MAP_SELECT_ERROR(sock_errno());
+                    _PR_MD_MAP_SELECT_ERROR(sock_errno());
                     break;
-				}
+                }
                 if (rv == 0) 
                 {
-					PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
-                        return -1;
+                    PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+                    return -1;
                 }
             } 
             else {
-				_PR_MD_MAP_SENDTO_ERROR(err);
+                _PR_MD_MAP_SENDTO_ERROR(err);
                 return -1;
-        }
+            }
         }
         bytesSent += rv;
         if (fd->secret->nonblocking)
         {
             break;
         }
         if ((rv >= 0) && (bytesSent < amount )) 
         {
@@ -587,36 +633,36 @@ PRInt32
            {
                tv.tv_sec = PR_IntervalToSeconds(timeout);
                tv.tv_usec = PR_IntervalToMicroseconds(
                    timeout - PR_SecondsToInterval(tv.tv_sec));
                tvp = &tv;
            }
            FD_ZERO(&wd);
            FD_SET(osfd, &wd);
-           if ((rv = select( osfd + 1, NULL, &wd, NULL, tvp)) == -1) {
+           if ((rv = _MD_SELECT( osfd + 1, NULL, &wd, NULL, tvp)) == -1) {
 #else
             if ( timeout == PR_INTERVAL_NO_TIMEOUT ) 
             {
                 lTimeout = -1; 
             } 
             else 
             {
                 lTimeout = PR_IntervalToMilliseconds(timeout);  
             }
             socks[0] = osfd; 
-            if ((rv = select( socks, 0, 1, 0, lTimeout)) == -1) {
+            if ((rv = _MD_SELECT( socks, 0, 1, 0, lTimeout)) == -1) {
 #endif
-				_PR_MD_MAP_SELECT_ERROR(sock_errno());
+                _PR_MD_MAP_SELECT_ERROR(sock_errno());
                 break;
-			}
+            }
             if (rv == 0) 
             {
-				PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
-                    return -1;
+                PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+                return -1;
             }
         }
     }
     return bytesSent;
 }
 
 PRInt32
 _PR_MD_RECVFROM(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags,
@@ -635,58 +681,58 @@ PRInt32
 
     while ((rv = recvfrom( osfd, (char *) buf, amount, 0, (struct sockaddr *) addr,
             (int *) addrlen)) == -1) 
     {
         if (((err = sock_errno()) == EWOULDBLOCK) 
             && (!fd->secret->nonblocking))
         {
 #ifdef BSD_SELECT
-           if (timeout == PR_INTERVAL_NO_TIMEOUT) 
-           {
-               tvp = NULL;
-           } 
-           else 
-           {
-               tv.tv_sec = PR_IntervalToSeconds(timeout);
-               tv.tv_usec = PR_IntervalToMicroseconds(
-               timeout - PR_SecondsToInterval(tv.tv_sec));
-               tvp = &tv;
-           }
-           FD_ZERO(&rd);
-           FD_SET(osfd, &rd);
-           if ((rv = select(osfd + 1, &rd, NULL, NULL, tvp)) == -1) 
+            if (timeout == PR_INTERVAL_NO_TIMEOUT) 
+            {
+                tvp = NULL;
+            } 
+            else 
+            {
+                tv.tv_sec = PR_IntervalToSeconds(timeout);
+                tv.tv_usec = PR_IntervalToMicroseconds(
+                timeout - PR_SecondsToInterval(tv.tv_sec));
+                tvp = &tv;
+            }
+            FD_ZERO(&rd);
+            FD_SET(osfd, &rd);
+            if ((rv = _MD_SELECT(osfd + 1, &rd, NULL, NULL, tvp)) == -1) 
 #else
             if (timeout == PR_INTERVAL_NO_TIMEOUT) 
             {
                 lTimeout = -1;
             } 
             else 
             {
                 lTimeout = PR_IntervalToMilliseconds(timeout);  
             }
             socks[0] = osfd; 
-            if ((rv = select(socks, 1, 0, 0, lTimeout)) == -1) 
+            if ((rv = _MD_SELECT(socks, 1, 0, 0, lTimeout)) == -1) 
 #endif
             {
-				_PR_MD_MAP_SELECT_ERROR(sock_errno());
+                _PR_MD_MAP_SELECT_ERROR(sock_errno());
                 return -1;
             } else if (rv == 0) 
             {
-				PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+                PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
                 rv = -1;
                 break;
             }
 
             /* recvfrom blows this value away if it fails first time */
             *addrlen = addrlen_temp;
         } 
         else 
         {
-			_PR_MD_MAP_RECVFROM_ERROR(err);
+            _PR_MD_MAP_RECVFROM_ERROR(err);
             break;
         }
     }
     return(rv);
 }
 
 PRInt32
 _PR_MD_WRITEV(PRFileDesc *fd, const PRIOVec *iov, PRInt32 iov_size, PRIntervalTime timeout)
@@ -721,77 +767,77 @@ PRInt32
         }
     }
     return sent;
 }
 
 PRInt32
 _PR_MD_SHUTDOWN(PRFileDesc *fd, PRIntn how)
 {
-PRInt32 rv;
+    PRInt32 rv;
 
     rv = shutdown(fd->secret->md.osfd, how);
-	if (rv < 0)
-		_PR_MD_MAP_SHUTDOWN_ERROR(sock_errno());
-	return rv;
+    if (rv < 0)
+        _PR_MD_MAP_SHUTDOWN_ERROR(sock_errno());
+    return rv;
 }
 
 PRStatus
 _PR_MD_GETSOCKNAME(PRFileDesc *fd, PRNetAddr *addr, PRUint32 *len)
 {
     PRInt32 rv;
 
     rv = getsockname((int)fd->secret->md.osfd, (struct sockaddr *)addr, (int *) len);
     if (rv==0)
-		return PR_SUCCESS;
-	else {
-		_PR_MD_MAP_GETSOCKNAME_ERROR(sock_errno());
-		return PR_FAILURE;
-	}
+        return PR_SUCCESS;
+    else {
+        _PR_MD_MAP_GETSOCKNAME_ERROR(sock_errno());
+        return PR_FAILURE;
+    }
 }
 
 PRStatus
 _PR_MD_GETPEERNAME(PRFileDesc *fd, PRNetAddr *addr, PRUint32 *len)
 {
     PRInt32 rv;
 
     rv = getpeername((int)fd->secret->md.osfd, (struct sockaddr *)addr, (int *) len);
     if (rv==0)
-		return PR_SUCCESS;
-	else {
-		_PR_MD_MAP_GETPEERNAME_ERROR(sock_errno());
-		return PR_FAILURE;
-	}
+        return PR_SUCCESS;
+    else {
+        _PR_MD_MAP_GETPEERNAME_ERROR(sock_errno());
+        return PR_FAILURE;
+    }
 }
 
 PRStatus
 _PR_MD_GETSOCKOPT(PRFileDesc *fd, PRInt32 level, PRInt32 optname, char* optval, PRInt32* optlen)
 {
     PRInt32 rv;
 
     rv = getsockopt((int)fd->secret->md.osfd, level, optname, optval, optlen);
     if (rv==0)
-		return PR_SUCCESS;
-	else {
-		_PR_MD_MAP_GETSOCKOPT_ERROR(sock_errno());
-		return PR_FAILURE;
-	}
+        return PR_SUCCESS;
+    else {
+        _PR_MD_MAP_GETSOCKOPT_ERROR(sock_errno());
+        return PR_FAILURE;
+    }
 }
 
 PRStatus
 _PR_MD_SETSOCKOPT(PRFileDesc *fd, PRInt32 level, PRInt32 optname, const char* optval, PRInt32 optlen)
 {
     PRInt32 rv;
 
     rv = setsockopt((int)fd->secret->md.osfd, level, optname, (char *) optval, optlen);
     if (rv==0)
-		return PR_SUCCESS;
-	else {
-		_PR_MD_MAP_SETSOCKOPT_ERROR(sock_errno());
-		return PR_FAILURE;
-	}
+        return PR_SUCCESS;
+    else {
+        _PR_MD_MAP_SETSOCKOPT_ERROR(sock_errno());
+        return PR_FAILURE;
+    }
 }
 
 void
 _MD_MakeNonblock(PRFileDesc *f)
 {
     return; /* do nothing! */ 
 }