From bd0c824cbd2ddcd1696ab853ba64a33290bd62da Mon Sep 17 00:00:00 2001
From: das <das>
Date: Fri, 10 Apr 2009 20:46:01 +0000
Subject: 	* unix/tclUnixChan.c:		TclUnixWaitForFile(): use FD_*
 macros 	* macosx/tclMacOSXNotify.c:	to manipulate select masks
 (Cassoff). 					[Bug 1960647]

---
 ChangeLog                |  4 +++
 macosx/tclMacOSXNotify.c | 65 +++++++++++++++++++++++-------------------------
 unix/tclUnixChan.c       | 63 ++++++++++++++++++++++------------------------
 3 files changed, 65 insertions(+), 67 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 77cf1d9..b733402 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2008-04-10  Daniel Steffen  <das@users.sourceforge.net>
 
+	* unix/tclUnixChan.c:		TclUnixWaitForFile(): use FD_* macros
+	* macosx/tclMacOSXNotify.c:	to manipulate select masks (Cassoff).
+					[Bug 1960647]
+
 	* unix/tclLoadDyld.c:		use RTLD_GLOBAL instead of RTLD_LOCAL.
 					[Bug 1961211]
 
diff --git a/macosx/tclMacOSXNotify.c b/macosx/tclMacOSXNotify.c
index e3e619c..b627321 100644
--- a/macosx/tclMacOSXNotify.c
+++ b/macosx/tclMacOSXNotify.c
@@ -12,7 +12,7 @@
  * See the file "license.terms" for information on usage and redistribution of
  * this file, and for a DISCLAIMER OF ALL WARRANTIES.
  *
- * RCS: @(#) $Id: tclMacOSXNotify.c,v 1.23 2009/04/10 18:02:37 das Exp $
+ * RCS: @(#) $Id: tclMacOSXNotify.c,v 1.24 2009/04/10 20:46:01 das Exp $
  */
 
 #include "tclInt.h"
@@ -1558,16 +1558,25 @@ TclUnixWaitForFile(
 {
     Tcl_Time abortTime = {0, 0}, now; /* silence gcc 4 warning */
     struct timeval blockTime, *timeoutPtr;
-    int index, numFound, result = 0;
-    fd_mask bit;
-    fd_mask readyMasks[3*MASK_SIZE];
-    fd_mask *maskp[3];		/* This array reflects the readable/writable
-				 * conditions that were found to exist by the
-				 * last call to select. */
+    int numFound, result = 0;
+    fd_set readableMask;
+    fd_set writableMask;
+    fd_set exceptionalMask;
 
 #define SET_BITS(var, bits)	((var) |= (bits))
 #define CLEAR_BITS(var, bits)	((var) &= ~(bits))
 
+#ifndef _DARWIN_C_SOURCE
+    /*
+     * Sanity check fd.
+     */
+
+    if (fd >= FD_SETSIZE) {
+	Tcl_Panic("TclUnixWaitForFile can't handle file id %d", fd);
+	/* must never get here, or select masks overrun will occur below */
+    }
+#endif
+
     /*
      * If there is a non-zero finite timeout, compute the time when we give
      * up.
@@ -1591,16 +1600,12 @@ TclUnixWaitForFile(
     }
 
     /*
-     * Initialize the ready masks and compute the mask offsets.
+     * Initialize the select masks.
      */
 
-    if (fd >= FD_SETSIZE) {
-	Tcl_Panic("TclWaitForFile can't handle file id %d", fd);
-	/* must never get here, or readyMasks overrun will occur below */
-    }
-    memset(readyMasks, 0, 3*MASK_SIZE*sizeof(fd_mask));
-    index = fd / (NBBY*sizeof(fd_mask));
-    bit = ((fd_mask)1) << (fd % (NBBY*sizeof(fd_mask)));
+    FD_ZERO(&readableMask);
+    FD_ZERO(&writableMask);
+    FD_ZERO(&exceptionalMask);
 
     /*
      * Loop in a mini-event loop of our own, waiting for either the file to
@@ -1622,41 +1627,33 @@ TclUnixWaitForFile(
 	}
 
 	/*
-	 * Set the appropriate bit in the ready masks for the fd.
+	 * Setup the select masks for the fd.
 	 */
 
-	if (mask & TCL_READABLE) {
-	    readyMasks[index] |= bit;
+	if (mask & TCL_READABLE)  {
+	    FD_SET(fd, &readableMask);
 	}
-	if (mask & TCL_WRITABLE) {
-	    (readyMasks+MASK_SIZE)[index] |= bit;
+	if (mask & TCL_WRITABLE)  {
+	    FD_SET(fd, &writableMask);
 	}
 	if (mask & TCL_EXCEPTION) {
-	    (readyMasks+2*(MASK_SIZE))[index] |= bit;
+	    FD_SET(fd, &exceptionalMask);
 	}
 
 	/*
 	 * Wait for the event or a timeout.
 	 */
 
-	/*
-	 * This is needed to satisfy GCC 3.3's strict aliasing rules.
-	 */
-
-	maskp[0] = &readyMasks[0];
-	maskp[1] = &readyMasks[MASK_SIZE];
-	maskp[2] = &readyMasks[2*MASK_SIZE];
-	numFound = select(fd+1, (SELECT_MASK *) maskp[0],
-		(SELECT_MASK *) maskp[1],
-		(SELECT_MASK *) maskp[2], timeoutPtr);
+	numFound = select(fd + 1, &readableMask, &writableMask,
+		&exceptionalMask, timeoutPtr);
 	if (numFound == 1) {
-	    if (readyMasks[index] & bit) {
+	    if (FD_ISSET(fd, &readableMask))   {
 		SET_BITS(result, TCL_READABLE);
 	    }
-	    if ((readyMasks+MASK_SIZE)[index] & bit) {
+	    if (FD_ISSET(fd, &writableMask))  {
 		SET_BITS(result, TCL_WRITABLE);
 	    }
-	    if ((readyMasks+2*(MASK_SIZE))[index] & bit) {
+	    if (FD_ISSET(fd, &exceptionalMask)) { 
 		SET_BITS(result, TCL_EXCEPTION);
 	    }
 	    result &= mask;
diff --git a/unix/tclUnixChan.c b/unix/tclUnixChan.c
index 18faa99..47cba16 100644
--- a/unix/tclUnixChan.c
+++ b/unix/tclUnixChan.c
@@ -10,7 +10,7 @@
  * See the file "license.terms" for information on usage and redistribution of
  * this file, and for a DISCLAIMER OF ALL WARRANTIES.
  *
- * RCS: @(#) $Id: tclUnixChan.c,v 1.98 2009/04/10 18:02:37 das Exp $
+ * RCS: @(#) $Id: tclUnixChan.c,v 1.99 2009/04/10 20:46:01 das Exp $
  */
 
 #include "tclInt.h"	/* Internal definitions for Tcl. */
@@ -3052,11 +3052,20 @@ TclUnixWaitForFile(
     Tcl_Time abortTime = {0, 0}, now; /* silence gcc 4 warning */
     struct timeval blockTime, *timeoutPtr;
     int index, numFound, result = 0;
-    fd_mask bit;
-    fd_mask readyMasks[3*MASK_SIZE];
-    fd_mask *maskp[3];		/* This array reflects the readable/writable
-				 * conditions that were found to exist by the
-				 * last call to select. */
+    fd_set readableMask;
+    fd_set writableMask;
+    fd_set exceptionalMask;
+
+#ifndef _DARWIN_C_SOURCE
+    /*
+     * Sanity check fd.
+     */
+
+    if (fd >= FD_SETSIZE) {
+	Tcl_Panic("TclUnixWaitForFile can't handle file id %d", fd);
+	/* must never get here, or select masks overrun will occur below */
+    }
+#endif
 
     /*
      * If there is a non-zero finite timeout, compute the time when we give
@@ -3081,16 +3090,12 @@ TclUnixWaitForFile(
     }
 
     /*
-     * Initialize the ready masks and compute the mask offsets.
+     * Initialize the select masks.
      */
 
-    if (fd >= FD_SETSIZE) {
-	Tcl_Panic("TclWaitForFile can't handle file id %d", fd);
-	/* must never get here, or readyMasks overrun will occur below */
-    }
-    memset(readyMasks, 0, 3*MASK_SIZE*sizeof(fd_mask));
-    index = fd / (NBBY*sizeof(fd_mask));
-    bit = ((fd_mask)1) << (fd % (NBBY*sizeof(fd_mask)));
+    FD_ZERO(&readableMask);
+    FD_ZERO(&writableMask);
+    FD_ZERO(&exceptionalMask);
 
     /*
      * Loop in a mini-event loop of our own, waiting for either the file to
@@ -3112,41 +3117,33 @@ TclUnixWaitForFile(
 	}
 
 	/*
-	 * Set the appropriate bit in the ready masks for the fd.
+	 * Setup the select masks for the fd.
 	 */
 
-	if (mask & TCL_READABLE) {
-	    readyMasks[index] |= bit;
+	if (mask & TCL_READABLE)  {
+	    FD_SET(fd, &readableMask);
 	}
-	if (mask & TCL_WRITABLE) {
-	    (readyMasks+MASK_SIZE)[index] |= bit;
+	if (mask & TCL_WRITABLE)  {
+	    FD_SET(fd, &writableMask);
 	}
 	if (mask & TCL_EXCEPTION) {
-	    (readyMasks+2*(MASK_SIZE))[index] |= bit;
+	    FD_SET(fd, &exceptionalMask);
 	}
 
 	/*
 	 * Wait for the event or a timeout.
 	 */
 
-	/*
-	 * This is needed to satisfy GCC 3.3's strict aliasing rules.
-	 */
-
-	maskp[0] = &readyMasks[0];
-	maskp[1] = &readyMasks[MASK_SIZE];
-	maskp[2] = &readyMasks[2*MASK_SIZE];
-	numFound = select(fd+1, (SELECT_MASK *) maskp[0],
-		(SELECT_MASK *) maskp[1],
-		(SELECT_MASK *) maskp[2], timeoutPtr);
+	numFound = select(fd + 1, &readableMask, &writableMask,
+		&exceptionalMask, timeoutPtr);
 	if (numFound == 1) {
-	    if (readyMasks[index] & bit) {
+	    if (FD_ISSET(fd, &readableMask))   {
 		SET_BITS(result, TCL_READABLE);
 	    }
-	    if ((readyMasks+MASK_SIZE)[index] & bit) {
+	    if (FD_ISSET(fd, &writableMask))  {
 		SET_BITS(result, TCL_WRITABLE);
 	    }
-	    if ((readyMasks+2*(MASK_SIZE))[index] & bit) {
+	    if (FD_ISSET(fd, &exceptionalMask)) { 
 		SET_BITS(result, TCL_EXCEPTION);
 	    }
 	    result &= mask;
-- 
cgit v0.12