Cannot find env passed to execve function

I would like to check if the environment variable that passed in the execve () function is actually passed , so I made this code (Main.c):

int main(){

    char PATH[4];
    strcpy(PATH, "bin");
    char * newargv[] = {"./get","", (char*)0};
    char * newenviron[] = {PATH};
    execve("./get", newargv, newenviron);
    perror("execve");
    return 0;
}

(get.c):

int main()
{
    const char* s = getenv("PATH");
    printf("PATH :%s\n",s);

}

But, when I execute the binary file released by Main.c, I get this output:

PATH: (null)

whereas I want to see

PATH: bin

Do you have any explanation?

+4
source share
2 answers
  • The PATH string buffer is not large enough for the string you are trying to insert into it.

  • The environment string should be "PATH=bin", and not just "bin".

  • As another answer indicates, you need to end the list of environment strings with a null pointer, i.e. char *newenviron[] = {PATH, 0};.

, , . , : http://nibot-lab.livejournal.com/115837.html

+4

VARIABLE_NAME=value of the variable.

PATH ( C, ) PATH=bin.

, ( , , ), , .

execve(2) manpage ( ):

envp . environ. ,       (. (7)).

environ(7) manpage:

, , execve (2), . `` name = value ''.

+4

Source: https://habr.com/ru/post/1670794/


All Articles