【发布时间】:2015-09-29 21:17:19
【问题描述】:
我想通过 exec() 运行“ls”命令,而我的代码是 exec("/bin/ls", NULL),但我得到一个文本显示“A NULL argv[0] was passed through an exec system call 。”如果我添加“all”作为参数,它就可以了。 exec("/bin/ls","all",NULL)
但是,当我使用 exec("/bin/ps", NULL) 时,它可以正常工作。那么你能帮我弄清楚我的程序有什么问题吗?
顺便说一句:我使用 execl()
#include <iostream>
#include <unistd.h> //required for fork()
#include <sys/types.h> //required for wait()
#include <sys/wait.h> //required for wait()
using namespace std;
int main(){
string cmd="";
string cmdpath="/bin/";
cout<<endl<<getcwd(NULL,0)<<" >> ";
cin>>cmd;
cout<<endl;
string cmdCmdpath = cmdpath+cmd;
const char* charcmd = cmdCmdpath.c_str();
int x = fork();
if(x!=0){
cout<<"The command "<<cmd<<" is running"<<endl;
wait(NULL);
cout<<"Im parent!"<<endl;
}else if (x==0){
cout<<"Im child!"<<endl;
execl(charcmd,NULL);
cout<<"Child done"<<endl;
}
}
【问题讨论】:
-
你的程序在哪里?