* win32/win32.c (pipe_exec): remove unnecessary SetStdHandle().

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3394 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
usa 2003-01-22 14:21:07 +00:00
parent c80a51e89f
commit 0243dab598
2 changed files with 41 additions and 76 deletions

View File

@ -1,3 +1,7 @@
Wed Jan 22 23:19:57 2003 NAKAMURA Usaku <usa@ruby-lang.org>
* win32/win32.c (pipe_exec): remove unnecessary SetStdHandle().
Tue Jan 21 20:29:31 2003 Michal Rokos <michal@rokos.homeip.net> Tue Jan 21 20:29:31 2003 Michal Rokos <michal@rokos.homeip.net>
* mkmf.rb: make possible to add files to clean and distclean targets * mkmf.rb: make possible to add files to clean and distclean targets

View File

@ -457,13 +457,12 @@ pipe_exec(char *cmd, int mode, FILE **fpr, FILE **fpw)
struct ChildRecord* child; struct ChildRecord* child;
HANDLE hReadIn, hReadOut; HANDLE hReadIn, hReadOut;
HANDLE hWriteIn, hWriteOut; HANDLE hWriteIn, hWriteOut;
HANDLE hSavedStdIn, hSavedStdOut;
HANDLE hDupInFile, hDupOutFile; HANDLE hDupInFile, hDupOutFile;
HANDLE hCurProc; HANDLE hCurProc;
SECURITY_ATTRIBUTES sa; SECURITY_ATTRIBUTES sa;
BOOL fRet; BOOL fRet;
BOOL reading, writing; BOOL reading, writing;
int fdin, fdout; int fd;
int pipemode; int pipemode;
char modes[3]; char modes[3];
int ret; int ret;
@ -471,15 +470,24 @@ pipe_exec(char *cmd, int mode, FILE **fpr, FILE **fpw)
/* Figure out what we're doing... */ /* Figure out what we're doing... */
writing = (mode & (O_WRONLY | O_RDWR)) ? TRUE : FALSE; writing = (mode & (O_WRONLY | O_RDWR)) ? TRUE : FALSE;
reading = ((mode & O_RDWR) || !writing) ? TRUE : FALSE; reading = ((mode & O_RDWR) || !writing) ? TRUE : FALSE;
pipemode = (mode & O_BINARY) ? O_BINARY : O_TEXT; if (mode & O_BINARY) {
pipemode = O_BINARY;
modes[1] = 'b';
modes[2] = '\0';
}
else {
pipemode = O_TEXT;
modes[1] = '\0';
}
sa.nLength = sizeof (SECURITY_ATTRIBUTES); sa.nLength = sizeof (SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL; sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE; sa.bInheritHandle = TRUE;
ret = -1;
hWriteIn = hReadOut = NULL;
/* create pipe, save parent's STDIN/STDOUT and redirect them for child */
RUBY_CRITICAL(do { RUBY_CRITICAL(do {
ret = -1; /* create pipe */
hCurProc = GetCurrentProcess(); hCurProc = GetCurrentProcess();
if (reading) { if (reading) {
fRet = CreatePipe(&hReadIn, &hReadOut, &sa, 2048L); fRet = CreatePipe(&hReadIn, &hReadOut, &sa, 2048L);
@ -487,9 +495,7 @@ pipe_exec(char *cmd, int mode, FILE **fpr, FILE **fpw)
errno = GetLastError(); errno = GetLastError();
break; break;
} }
hSavedStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if (!DuplicateHandle(hCurProc, hReadIn, hCurProc, &hDupInFile, 0,
if (!SetStdHandle(STD_OUTPUT_HANDLE, hReadOut) ||
!DuplicateHandle(hCurProc, hReadIn, hCurProc, &hDupInFile, 0,
FALSE, DUPLICATE_SAME_ACCESS)) { FALSE, DUPLICATE_SAME_ACCESS)) {
errno = GetLastError(); errno = GetLastError();
CloseHandle(hReadIn); CloseHandle(hReadIn);
@ -503,80 +509,47 @@ pipe_exec(char *cmd, int mode, FILE **fpr, FILE **fpw)
fRet = CreatePipe(&hWriteIn, &hWriteOut, &sa, 2048L); fRet = CreatePipe(&hWriteIn, &hWriteOut, &sa, 2048L);
if (!fRet) { if (!fRet) {
errno = GetLastError(); errno = GetLastError();
write_pipe_failed:
if (reading) { if (reading) {
CloseHandle(hDupInFile); CloseHandle(hDupInFile);
CloseHandle(hReadOut); CloseHandle(hReadOut);
} }
break; break;
} }
hSavedStdIn = GetStdHandle(STD_INPUT_HANDLE); if (!DuplicateHandle(hCurProc, hWriteOut, hCurProc, &hDupOutFile, 0,
if (!SetStdHandle(STD_INPUT_HANDLE, hWriteIn) ||
!DuplicateHandle(hCurProc, hWriteOut, hCurProc, &hDupOutFile, 0,
FALSE, DUPLICATE_SAME_ACCESS)) { FALSE, DUPLICATE_SAME_ACCESS)) {
errno = GetLastError(); errno = GetLastError();
CloseHandle(hWriteIn); CloseHandle(hWriteIn);
CloseHandle(hWriteOut); CloseHandle(hWriteOut);
CloseHandle(hCurProc); CloseHandle(hCurProc);
if (reading) { goto write_pipe_failed;
CloseHandle(hDupInFile);
CloseHandle(hReadOut);
}
break;
} }
CloseHandle(hWriteOut); CloseHandle(hWriteOut);
} }
CloseHandle(hCurProc); CloseHandle(hCurProc);
/* create child process */ /* create child process */
child = CreateChild(cmd, NULL, &sa, NULL, NULL, NULL); child = CreateChild(cmd, NULL, &sa, hWriteIn, hReadOut, NULL);
if (!child) { if (!child) {
if (reading) { if (reading) {
SetStdHandle(STD_OUTPUT_HANDLE, hSavedStdOut);
CloseHandle(hReadOut); CloseHandle(hReadOut);
CloseHandle(hDupInFile); CloseHandle(hDupInFile);
} }
if (writing) { if (writing) {
SetStdHandle(STD_INPUT_HANDLE, hSavedStdIn);
CloseHandle(hWriteIn); CloseHandle(hWriteIn);
CloseHandle(hDupOutFile); CloseHandle(hDupOutFile);
} }
break; break;
} }
/* restore STDIN/STDOUT */ /* associate handle to fp */
if (reading) { if (reading) {
if (!SetStdHandle(STD_OUTPUT_HANDLE, hSavedStdOut)) { fd = rb_w32_open_osfhandle((long)hDupInFile,
errno = GetLastError(); (_O_RDONLY | pipemode));
CloseChildHandle(child);
CloseHandle(hReadOut);
CloseHandle(hDupInFile);
if (writing) {
CloseHandle(hWriteIn);
CloseHandle(hDupOutFile);
}
break;
}
}
if (writing) {
if (!SetStdHandle(STD_INPUT_HANDLE, hSavedStdIn)) {
errno = GetLastError();
CloseChildHandle(child);
CloseHandle(hWriteIn);
CloseHandle(hDupOutFile);
if (reading) {
CloseHandle(hReadOut);
CloseHandle(hDupInFile);
}
break;
}
}
if (reading) {
fdin = rb_w32_open_osfhandle((long)hDupInFile,
(_O_RDONLY | pipemode));
CloseHandle(hReadOut); CloseHandle(hReadOut);
if (fdin == -1) { if (fd == -1) {
CloseHandle(hDupInFile); CloseHandle(hDupInFile);
read_open_failed:
if (writing) { if (writing) {
CloseHandle(hWriteIn); CloseHandle(hWriteIn);
CloseHandle(hDupOutFile); CloseHandle(hDupOutFile);
@ -584,42 +557,30 @@ pipe_exec(char *cmd, int mode, FILE **fpr, FILE **fpw)
CloseChildHandle(child); CloseChildHandle(child);
break; break;
} }
modes[0] = 'r';
if ((*fpr = (FILE *)fdopen(fd, modes)) == NULL) {
_close(fd);
goto read_open_failed;
}
} }
if (writing) { if (writing) {
fdout = rb_w32_open_osfhandle((long)hDupOutFile, fd = rb_w32_open_osfhandle((long)hDupOutFile,
(_O_WRONLY | pipemode)); (_O_WRONLY | pipemode));
CloseHandle(hWriteIn); CloseHandle(hWriteIn);
if (fdout == -1) { if (fd == -1) {
CloseHandle(hDupOutFile); CloseHandle(hDupOutFile);
if (reading) { write_open_failed:
_close(fdin);
}
CloseChildHandle(child);
break;
}
}
if (reading) {
sprintf(modes, "r%s", pipemode == O_BINARY ? "b" : "");
if ((*fpr = (FILE *)fdopen(fdin, modes)) == NULL) {
_close(fdin);
if (writing) {
_close(fdout);
}
CloseChildHandle(child);
break;
}
}
if (writing) {
sprintf(modes, "w%s", pipemode == O_BINARY ? "b" : "");
if ((*fpw = (FILE *)fdopen(fdout, modes)) == NULL) {
_close(fdout);
if (reading) { if (reading) {
fclose(*fpr); fclose(*fpr);
} }
CloseChildHandle(child); CloseChildHandle(child);
break; break;
} }
modes[0] = 'w';
if ((*fpw = (FILE *)fdopen(fd, modes)) == NULL) {
_close(fd);
goto write_open_failed;
}
} }
ret = child->pid; ret = child->pid;
} while (0)); } while (0));