Например, я печатаю ls
. Если я поставлю execvp
выше fork()
, это будет работать. Если я положу execvp
внутрь ребенка, execvp
потерпит неудачу. Это потому что arr не может перейти в дочерний или ....
int bash(char *user_input){
// -------------------split the user_input store into arr -------------//
int counter = 0;
int size = 1;
char *arr[10];
char* token = strtok(user_input," ");
arr[0] = token;
token[strcspn(token,"\n")] = 0;
while(token!=NULL){
counter++;
token = strtok(NULL," ");
if(token == NULL){
break;
}else{
size++;
token[strcspn(token,"\n")] = 0;
arr[counter] = token;
}
}
arr[size] = NULL;
// -------------------split the user_input store into arr -------------//
// -------fork() below-----//
int pid = fork();
int status = 1;
if(pid >= 0){
if (pid == 0) {
// child is running
if(execvp(arr[0], arr) < 0){ // ------ CANNOT runs here
perror("execvp failed");
}
}else{
//parent is running wait the child
wait(&status);
}
}
return 0;
}
Вот сообщение об ошибке:
execvp failed: No such file or directory
myshell >