Escape arguments to filter commands

This will allow both the path to git-crypt and the path to the key file
to contain arbitrary characters, notably spaces.
This commit is contained in:
Andrew Ayer
2013-04-04 18:52:06 -07:00
parent b10fbcd299
commit 2b936c74f1
3 changed files with 22 additions and 17 deletions

View File

@@ -112,3 +112,17 @@ void open_tempfile (std::fstream& file, std::ios_base::openmode mode)
delete[] path;
}
std::string escape_shell_arg (const std::string& str)
{
std::string new_str;
new_str.push_back('"');
for (std::string::const_iterator it(str.begin()); it != str.end(); ++it) {
if (*it == '"' || *it == '\\' || *it == '$' || *it == '`') {
new_str.push_back('\\');
}
new_str.push_back(*it);
}
new_str.push_back('"');
return new_str;
}