understanding requirements for execve and setting environment vars

前端 未结 3 514
遇见更好的自我
遇见更好的自我 2021-02-05 15:58

We are having a lot of trouble interpreting our teacher. We asked for clarification and got the following back from him

  1. For execve, send it a environment you se

3条回答
  •  离开以前
    2021-02-05 16:47

    I'm a little late to the party here, but if you want to preserve the old environment variables as well as creating your own, use setenv, and then pass environ to execve().

        setenv("dog", "spike", 1);
        extern char** environ;
        execve(argv[0], argv, environ);
    

    environ is a variable declared in unistd.h, and it keeps track of the environment variables during this running process.

    setenv() and putenv() modify environ, so when you pass it over execve(), the environment variables will be just as you'd expect.

提交回复
热议问题