Index: tclIO.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclIO.c,v
retrieving revision 1.168
diff -u -r1.168 tclIO.c
--- tclIO.c	18 Nov 2009 22:41:41 -0000	1.168
+++ tclIO.c	1 Dec 2009 23:44:15 -0000
@@ -120,6 +120,7 @@
 static Tcl_Obj *	FixLevelCode(Tcl_Obj *msg);
 static void		SpliceChannel(Tcl_Channel chan);
 static void		CutChannel(Tcl_Channel chan);
+static int WillRead(Channel *chanPtr);
 
 /*
  * Simplifying helper macros. All may use their argument(s) multiple times.
@@ -276,6 +277,10 @@
     int dstSize,
     int *errnoPtr)
 {
+    if (WillRead(chanPtr) < 0) {
+        return -1;
+    }
+
     return chanPtr->typePtr->inputProc(chanPtr->instanceData, dst, dstSize,
 	    errnoPtr);
 }
@@ -3831,6 +3836,33 @@
     }
 }
 
+static void WillWrite(Channel *chanPtr)
+{
+    int inputBuffered;
+
+    if ((chanPtr->typePtr->seekProc != NULL)
+        && ((inputBuffered = Tcl_InputBuffered((Tcl_Channel) chanPtr)) > 0)) {
+        int ignore;
+        DiscardInputQueued(chanPtr->state, 0);
+        ChanSeek(chanPtr, - inputBuffered, SEEK_CUR, &ignore);
+    }
+}
+
+static int WillRead(Channel *chanPtr)
+{
+    if ((chanPtr->typePtr->seekProc != NULL)
+        && (Tcl_OutputBuffered((Tcl_Channel) chanPtr) > 0)) {
+        if ((chanPtr->state->curOutPtr != NULL)
+            && IsBufferReady(chanPtr->state->curOutPtr)) {
+            SetFlag(chanPtr->state, BUFFER_READY);
+        }
+        if (FlushChannel(NULL, chanPtr, 0) != 0) {
+            return -1;
+        }
+    }
+    return 0;
+}
+
 /*
  *----------------------------------------------------------------------
  *
@@ -3864,6 +3896,10 @@
     char *dst;
     int dstMax, sawLF, savedLF, total, dstLen, toWrite, translate;
 
+    if (srcLen) {
+        WillWrite(chanPtr);
+    }
+
     total = 0;
     sawLF = 0;
     savedLF = 0;
@@ -3965,6 +4001,10 @@
     Tcl_Encoding encoding;
     char safe[BUFFER_PADDING];
 
+    if (srcLen) {
+        WillWrite(chanPtr);
+    }
+
     total = 0;
     sawLF = 0;
     savedLF = 0;
@@ -7035,8 +7075,10 @@
      * pre-read input data.
      */
 
-    if (Tcl_Seek(chan, (Tcl_WideInt) 0, SEEK_CUR) == Tcl_LongAsWide(-1)) {
-	return TCL_ERROR;
+    WillWrite(chanPtr);
+
+    if (WillRead(chanPtr) < 0) {
+        return TCL_ERROR;
     }
 
     /*
