From 9b1a64193bda3a0ea8d83f1acf50eeea73473f7c Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 19 Dec 2009 03:33:59 +0000 Subject: Don't try to swap user's input fd with stdin when it is already stdin. Same for output git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1060 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- lib/libsystem.c | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'lib/libsystem.c') diff --git a/lib/libsystem.c b/lib/libsystem.c index 31c23bf6..2b00191f 100644 --- a/lib/libsystem.c +++ b/lib/libsystem.c @@ -54,26 +54,31 @@ execProgram(const char * const progName, /* Make stdinFd Standard Input. Make stdoutFd Standard Output. */ - stdinSaveFd = dup(STDIN); - stdoutSaveFd = dup(STDOUT); - - close(STDIN); - close(STDOUT); - - dup2(stdinFd, STDIN); - dup2(stdoutFd, STDOUT); + if (stdinFd != STDIN) { + stdinSaveFd = dup(STDIN); + close(STDIN); + dup2(stdinFd, STDIN); + } + if (stdoutFd != STDOUT) { + stdoutSaveFd = dup(STDOUT); + close(STDOUT); + dup2(stdoutFd, STDOUT); + } rc = execvp(progName, (char **)argArray); execErrno = errno; - close(STDIN); - close(STDOUT); - dup2(stdinSaveFd, STDIN); - dup2(stdoutSaveFd, STDOUT); - close(stdinSaveFd); - close(stdoutSaveFd); - + if (stdinFd != STDIN) { + close(STDIN); + dup2(stdinSaveFd, STDIN); + close(stdinSaveFd); + } + if (stdoutFd != STDOUT) { + close(STDOUT); + dup2(stdoutSaveFd, STDOUT); + close(stdoutSaveFd); + } if (rc < 0) pm_error("Unable to exec '%s' " "(i.e. the program did not run at all). " -- cgit 1.4.1