简体   繁体   中英

execvp ls: cannot access ' ': No such file or directory

When I try to execute a command like ls without arguments with execvp, I get the error: ls: cannot access '': No such file or directory.

I dont know why, cause it seems like he is searching for a parameter ''.

The code works like this:

int main(int argc, char *argv[])
{
    char command[250] = "ls";              
    argvs[0] = command;
    argvs[1] = NULL;
    if (execvp( argvs[0], argvs) == -1 )
       perror("exec failed");

    return 0;
 }

Do I have to give as minimum one argument in execvp ? and set this to NULL or some other nullterminations?

If argvs is declared as below

char *argvs[2]; /* array of pointer */

then the below code should work on your machine

int main(int argc, char *argv[]) {
        char command[250] = "ls";
        char *argvs[2];
        argvs[0] = command;
        argvs[1] = NULL;
        if (execvp( argvs[0], argvs) == -1 )
                perror("exec failed");

        return 0;
}

Do I have to give as minimum one argument in execvp?

From the manual page of execvp

int execvp(const char *file, char *const argv[]); /*its having fixed no of argument */

that means you have to provide exactly two argument to execvp() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM