下面的函数接受一个char指针数组Eg: arr [0]:ls arr [1]: - 1 arr [2]: - a arr [3]:/等 arr [4]:NULL / *因为execvp在结尾处需要NULL * /
// function call is runCmd(arr);
功能定义如下:
void runCmd(char *arr[]){
pid_t child_pid,tpid;
int child_status;
child_pid = fork();
if(child_pid == 0){
/* The child process executes the exec*/
execvp(arr[0],arr);
/*if it returns it must have failed */
fflush(stdout);
printf("Unknown Command \n");
exit(0);
}
else {
/* let the parent wait for the child */
do{
tpid = wait(&child_status);
}while(tpid != child_pid);
}
}
执行后我收到消息 -
ls: cannot access /etc
: No such file or directory
答案 0 :(得分:7)
看起来您正在阅读命令并忘记删除尾随换行符,导致ls
尝试列出目录"/etc\n"
。