开发者

linuux - execvp : On executing ls command - get error " ls: cannot access /etc : No such file or directory"

The below function takes in a array of char pointers Eg : arr[0]: ls arr[1]: -l arr[2]: -a arr[3]: /etc arr[4]:NULL /* Since execvp expects a NULL at the end */

// function call is runCmd(arr); the function definition is below :

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);

                /*开发者_StackOverflow社区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);
        }
}

After executing I get the message -

ls: cannot access /etc
: No such file or directory


Looks like you are reading in the command and forgetting to strip the trailing newline, causing your ls to try and list the directory "/etc\n".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜