其他答案包括这个fork和exec解决方案,但我声称这是唯一正确的方法.
转义shell参数容易出错并浪费时间,就像在存在更安全和更有效的参数绑定API时尝试转义SQL参数一样愚蠢.
这是一个示例函数:
void play(const char *path)
{
/* Fork, then exec */
pid = fork();
if( pid < 0 ) {
/* This is an error! */
return;
}
if( pid == 0 ) {
/* This is the child */
freopen( "/dev/null", "r", stdin );
freopen( "/dev/null", "w", stdout );
freopen( "/dev/null", "w", stderr );
execlp( "mplayer", "mplayer", path, (char *)0 );
/* This is also an error! */
return;
}
}
Run Code Online (Sandbox Code Playgroud)
没有一种解决方案可以在任何地方使用,因为不同的shell对特殊字符是什么以及如何解释它们有不同的想法.对于bash,在用文件名替换每个单引号后,你可能会用单引号包围整个文件名'"'"'(第一个单引号停止序列,"'"将字面单引号附加到字符串,最后的单引号开始再次引用的序列).更好的解决方案是找到一种在不使用系统的情况下调用程序的方法,例如使用带有一个exec函数的fork,这样就没有shell插值.