金子です。

>一応、NT4.0 では動くことを確認しました。
>ただし、Win98 ではプロセス呼び出しのあたりはいろいろおかしくて
>(今回の修正とは関係なく)テストしてると簡単にアプリケーション
>エラーになります。
>こんなのとか。
>
>f = open("|./cat.exe", "w")
>f.puts("hello")
>f.close
>
>もう少し調べてみる必要がありそうですね。

すみません。
上記スクリプトは実はNTでも動いていませんでした。
というわけで、このスクリプトもなんとか動作するように
修正してみました。

修正箇所が増えたので全体のパッチをつけます。

===============================================================
diff -u -p io.c.orig io.c 
--- io.c.orig	Wed Nov 24 16:13:51 1999
+++ io.c	Fri Dec 24 00:42:17 1999
@@ -1448,7 +1448,8 @@ pipe_open(pname, mode)
 	pipe_add_fptr(fptr);
 	if (modef & FMODE_READABLE) fptr->f  = f;
 	if (modef & FMODE_WRITABLE) {
-	    fptr->f2 = f;
+	    if (fptr->f) fptr->f2 = f;
+	    else fptr->f = f;
 	    rb_io_synchronized(fptr);
 	}
 	return (VALUE)port;
===============================================================
diff -u -p win32/win32.c.orig win32/win32.c 
--- win32/win32.c.orig	Tue Nov 09 12:57:27 1999
+++ win32/win32.c	Fri Dec 24 00:43:39 1999
@@ -495,7 +495,7 @@ mypopen (char *cmd, char *mode) 
 		int p[2];
 
 		BOOL fRet;
-		HANDLE hInFile, hOutFile, hStdin, hStdout;
+		HANDLE hInFile, hOutFile;
 		LPCSTR lpApplicationName = NULL;
 		LPTSTR lpCommandLine;
 		LPTSTR lpCmd2 = NULL;
@@ -509,20 +509,6 @@ mypopen (char *cmd, char *mode) 
 		sa.lpSecurityDescriptor = NULL;
 		sa.bInheritHandle       = TRUE;
 
-		if (!reading) {
-        	FILE *fp;
-
-			fp = (_popen)(cmd, mode);
-
-			MyPopenRecord[slot].inuse = TRUE;
-			MyPopenRecord[slot].pipe = fp;
-			MyPopenRecord[slot].pid = -1;
-
-			if (!fp)
-                        rb_fatal("cannot open pipe \"%s\" (%s)", cmd, strerror(errno));
-				return fp;
-		}
-
 		fRet = CreatePipe(&hInFile, &hOutFile, &sa, 2048L);
 		if (!fRet)
                         rb_fatal("cannot open pipe \"%s\" (%s)", cmd, strerror(errno));
@@ -533,35 +519,14 @@ mypopen (char *cmd, char *mode) 
 		aStartupInfo.dwFlags    = STARTF_USESTDHANDLES;
 
 		if (reading) {
-			aStartupInfo.hStdInput  = GetStdHandle(STD_OUTPUT_HANDLE);//hStdin;
-			aStartupInfo.hStdError  = INVALID_HANDLE_VALUE;
-			//for save
-			DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
-			  GetCurrentProcess(), &hStdout,
-			  0, FALSE, DUPLICATE_SAME_ACCESS
-			);
-			//for redirect
-			DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
-			  GetCurrentProcess(), &hStdin,
-			  0, TRUE, DUPLICATE_SAME_ACCESS
-			);
+			aStartupInfo.hStdInput  = GetStdHandle(STD_INPUT_HANDLE);
 			aStartupInfo.hStdOutput = hOutFile;
 		}
 		else {
-			aStartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); //hStdout;
-			aStartupInfo.hStdError  = INVALID_HANDLE_VALUE;
-			// for save
-			DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
-			  GetCurrentProcess(), &hStdin,
-			  0, FALSE, DUPLICATE_SAME_ACCESS
-			);
-			//for redirect
-			DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
-			  GetCurrentProcess(), &hStdout,
-			  0, TRUE, DUPLICATE_SAME_ACCESS
-			);
-			aStartupInfo.hStdInput = hInFile;
+			aStartupInfo.hStdInput  = hInFile;
+			aStartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
 		}
+		aStartupInfo.hStdError  = GetStdHandle(STD_ERROR_HANDLE);
 
 		dwCreationFlags = (NORMAL_PRIORITY_CLASS);
 
@@ -587,24 +552,12 @@ mypopen (char *cmd, char *mode) 
 		CloseHandle(aProcessInformation.hThread);
 
 		if (reading) {
-			HANDLE hDummy;
-
 			fd = _open_osfhandle((long)hInFile,  (_O_RDONLY | pipemode));
 			CloseHandle(hOutFile);
-			DuplicateHandle(GetCurrentProcess(), hStdout,
-			  GetCurrentProcess(), &hDummy,
-			  0, TRUE, (DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)
-			);
 		}
 		else {
-			HANDLE hDummy;
-
 		    fd = _open_osfhandle((long)hOutFile, (_O_WRONLY | pipemode));
 			CloseHandle(hInFile);
-			DuplicateHandle(GetCurrentProcess(), hStdin,
-			  GetCurrentProcess(), &hDummy,
-			  0, TRUE, (DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)
-			);
 		}
 
===============================================================

-- 
金子 尚史 (KANEKO Naoshi)
e-mail: wbs01621 / mail.wbs.ne.jp