What is the use of the third argument of the environment variable for C ++ main ()?

I realized that char **envp is the third argument to main , and with the code below, I was able to see what it actually contains.

 int main(int argc, char *argv[], char *env[]) { int i; for (i=0 ; env[i] ; i++) std::cout << env[i] << std::endl; std::cout << std::endl; } 

My question is: why (in what situations) should programmers use this? I found many explanations of what this argument does, but nothing that could tell me where it is commonly used. An attempt to understand in what real situations this can be used.

+5
source share
4 answers

This is an array containing all environment variables. This can be used, for example, to obtain the username or home directory of the currently registered user. For example, if I want to save the configuration file in the user's home directory and I need to get PATH;

 int main(int argc, char* argv[], char* env[]){ std::cout << env[11] << '\n'; //this prints home directory of current user(11th for me was the home directory) return 0; } 

The equivalent of env is the char * getenv (const char * name) function , which is easier to use, for example:

  std::cout << getenv("USER"); 

prints the username of the current user.

+8
source

getenv() function allows you to find the value of a specific environment variable, but does not provide a mechanism for scanning the entire list of environment variables. The envp argument allows you to envp over the entire list of environment variables, as your demo code shows, which is simply not possible using the getenv() interface.

On POSIX systems, there is a global variable extern char **environ; , which also indicates the environment. The putenv() functions (the old one, not preferable, because it presents memory management problems), setenv() and unsetenv() can also manage the list of environment variables (as defined by environ ). A program can directly modify environ or the values ​​it points to, but this is not recommended.

If you use fork() and the exec*() family of functions, if you do not use execve() and specify the environment explicitly, the child process will receive the environment defined by environ .

The header does not declare environ - AFAIK, it is the only variable defined by POSIX without a header to declare it. The C standard recognizes the signature int main(int argc, char **argv, char **envp) for main() as the general extension of the standard documented in Appendix J.

+3
source

This is usually used to set configuration parameters or other information for the entire group of programs. Another use is to specify environment settings for a specific machine or user preference.
Well-known examples are the PATH variable, which contains search patterns for executable files, or the LD_LIBRARY_PATH variable, which contains templates where to look for shared libraries.

+2
source

env allows you to access environment variables. It contains an array of strings. Examples are the users home directory, the configured language scheme, the PATH variable (where to look for directly executable programs?), ...

You can also set individual environment variables. For example, if you have testing (training), as well as the production system to which you deploy your application. On one system you can set the variable MY_APP_MODE = TEST, and on the other system you can specify "MY_APP_MODE = PROD". Therefore, you do not need to deploy various applications in test and production systems. Your application can determine itself in what environment it starts.

+1
source

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


All Articles