Skip to content

Commit 17e3714

Browse files
author
Daniel Kroening
authored
Merge pull request #3336 from diffblue/fix-run3
fix a run() variant on non-Windows OSs [blocks: #2310]
2 parents 70d9432 + d6c0016 commit 17e3714

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/util/run.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,25 @@ int run(
493493
#else
494494
std::string command;
495495

496+
bool first = true;
497+
498+
// note we use 'what' instead of 'argv[0]' as the name of the executable
496499
for(const auto &arg : argv)
497-
command += " " + shell_quote(arg);
500+
{
501+
if(first) // this is argv[0]
502+
{
503+
command += shell_quote(what);
504+
first = false;
505+
}
506+
else
507+
command += " " + shell_quote(arg);
508+
}
509+
510+
if(!std_input.empty())
511+
command += " < " + shell_quote(std_input);
512+
513+
if(!std_error.empty())
514+
command += " 2> " + shell_quote(std_error);
498515

499516
FILE *stream=popen(command.c_str(), "r");
500517

src/util/run.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,26 @@ Date: August 2012
1818

1919
int run(const std::string &what, const std::vector<std::string> &argv);
2020

21+
/// This runs the executable given by the file name \p what.
22+
/// Control returns when execution has finished.
23+
/// Stdin, stdout and stderr may be redirected from/to a given file.
24+
/// Give the empty string to retain the default handle.
25+
/// Any shell-meta characters in the executable, \p argv and the I/O
26+
/// redirect files are escaped as needed.
2127
int run(
2228
const std::string &what,
2329
const std::vector<std::string> &argv,
2430
const std::string &std_input,
2531
const std::string &std_output,
2632
const std::string &std_error);
2733

28-
/// A variant that streams the stdout of the child into an ostream
34+
/// This runs the executable given by the file name \p what.
35+
/// Control returns when execution has finished.
36+
/// Stdin and stderr may be redirected from/to a given file.
37+
/// Give the empty string to retain the default handle.
38+
/// Any output to stdout is stored in the \p std_output stream buffer.
39+
/// Any shell-meta characters in the executable, \p argv and the I/O
40+
/// redirect files are escaped as needed.
2941
int run(
3042
const std::string &what,
3143
const std::vector<std::string> &argv,

0 commit comments

Comments
 (0)