ref: refs/heads/master
commit 857a759788926e5b1d600e195a680d69cdb837ef
Author: Michael Andres
Date: Thu Sep 24 16:48:54 2009 +0200
ExternalProgram: Allow redirecting a commands standard input.
---
zypp/ExternalProgram.cc | 18 ++++++++++++++++++
zypp/ExternalProgram.h | 10 +++++++++-
2 files changed, 27 insertions(+), 1 deletions(-)
diff --git a/zypp/ExternalProgram.cc b/zypp/ExternalProgram.cc
index 522126a..78ca127 100644
--- a/zypp/ExternalProgram.cc
+++ b/zypp/ExternalProgram.cc
@@ -194,6 +194,15 @@ namespace zypp {
int to_external[2], from_external[2]; // fds for pair of pipes
int master_tty, slave_tty; // fds for pair of ttys
+ const char * redirectStdin = 0;
+ if ( argv[0] && *argv[0] == '<' )
+ {
+ redirectStdin = argv[0]+1;
+ if ( *redirectStdin == '\0' )
+ redirectStdin = "/dev/null";
+ ++argv;
+ }
+
// do not remove the single quotes around every argument, copy&paste of
// command to shell will not work otherwise!
{
@@ -205,6 +214,8 @@ namespace zypp {
cmdstr << argv[i];
cmdstr << '\'';
}
+ if ( redirectStdin )
+ cmdstr << " < '" << redirectStdin << "'";
_command = cmdstr.str();
}
DBG << "Executing " << _command << endl;
@@ -265,6 +276,13 @@ namespace zypp {
::close(to_external [1]); // Belongs to father process
}
+ if ( redirectStdin )
+ {
+ ::close( 0 );
+ int inp_fd = open( redirectStdin, O_RDONLY );
+ dup2( inp_fd, 0 );
+ }
+
// Handle stderr
if (stderr_disp == Discard_Stderr)
{
diff --git a/zypp/ExternalProgram.h b/zypp/ExternalProgram.h
index f33a6d6..ff15162 100644
--- a/zypp/ExternalProgram.h
+++ b/zypp/ExternalProgram.h
@@ -94,7 +94,15 @@ namespace zypp {
* Start an external program by giving the arguments as an arry of char *pointers.
* If environment is provided, varaiables will be added to the childs environment,
* overwriting existing ones.
- * \throws ExternalProgramException if fork fails.
+ *
+ * Stdin redirection: If the \b 1st argument starts with a \b '<', the remaining
+ * part is treated as file opened for reading on standard input (or \c /dev/null
+ * if empty).
+ * \code
+ * // cat file /tmp/x
+ * const char* argv[] = { "