当我执行以下代码并将参数作为“ls - l”时,我得到中止陷阱:6并且程序退出。但是当我只是给ls作为输入时,程序工作正常。为什么会发生这种情况。
此处:act_arg包含字符串“ls”“ - l”
void function_run()
{
char bar[100] = "/bin/";
char f_arg[100];
int baz;
int qux = 1;
strncat(bar,act_arg[0],sizeof(act_arg[0]));
while(act_arg[qux] != NULL)
{
strncat(f_arg,act_arg[qux] ,sizeof(act_arg[qux]));
}
//execute
if(fork() == 0)
{
baz = execlp(bar,act_arg[0],act_arg[1],NULL);
if(baz == -1)
{
eng_run(); //to run execlp from normal lang run
//wait(NULL);
}
else
{
//wait(NULL);
exit(0);
}
}
fflush(stdout);
}
答案 0 :(得分:1)
这是错误的:
if(fork() == 0)
{
baz = execlp(bar,act_arg[0],act_arg[1],NULL);
if(baz == -1)
{
你需要向execlp发送所有参数,包括“ls”所以:
if(fork() == 0)
{
baz = execlp(bar,act_arg[0],act_arg[0],NULL);
if(baz == -1)
{
顺便说一下:
if(fork() == 0)
{
baz = execve(act_arg[0], &act_arg[1]); //&act_arg[0] I don't remember
if(baz == -1)
{