Solved why does the execve always execute failed

Code:
int main() {
    int ret1 = execve("/bin/ls"," "," " );

    if(ret1<1){
        err(ret1,"execve failed");
    }
return 0;

}

When I run this code, I got execve failed: Bad address
It's my fault, it need pass the argv array pointer and envp array pointer.
 
Last edited by a moderator:
When I change this code to this, I can run but the result is wrong. It output some environment variable.
Code:
    char *argv[1] = {'\0'};
    int ret1 = execv("/bin/ls",argv );
 
Last edited by a moderator:
how to get evironment variables ? is there a function to get it? or run setenv with execv?

execve(2)
The argument envp is also a pointer to a null-terminated array of character pointers to null-terminated strings.
A pointer to this array is normally stored in the global variable environ.

environ(7)
A process can query, update, and delete these strings using the getenv(3), setenv(3), and unsetenv(3) functions, respectively.

SEE ALSO
execve(2), getenv(3), setenv(3), unsetenv(3) and environ(7)

EXAMPLES

L145: src/contrib/nvi/common/util.c


L070: src/usr.sbin/pw/pw_log.c


L079: src/stand/efi/loader/bootinfo.c


L175: src/bin/ls/ls.c


L143: src/crypto/openssh/readpass.c

 
Back
Top