Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/program/getenv"

From cppreference.com
< cpp‎ | utility‎ | program
m (Updated langlinks)
m (fmt)
 
Line 1: Line 1:
{{cpp/title| getenv}}
+
{{cpp/title|getenv}}
 
{{cpp/utility/program/navbar}}
 
{{cpp/utility/program/navbar}}
{{ddcl | header=cstdlib |
+
{{ddcl|header=cstdlib|
 
char* getenv( const char* env_var );
 
char* getenv( const char* env_var );
 
}}
 
}}
Line 9: Line 9:
 
{{rev begin}}
 
{{rev begin}}
 
{{rev|until=c++11|
 
{{rev|until=c++11|
This function is not required to be thread-safe. Another call to getenv, as well as a call to the POSIX functions [http://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html setenv()], [http://pubs.opengroup.org/onlinepubs/9699919799/functions/unsetenv.html unsetenv()], and [http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html putenv()] may invalidate the pointer returned by a previous call or modify the string obtained from a previous call.
+
This function is not required to be thread-safe. Another call to {{tt|getenv}}, as well as a call to the POSIX functions [https://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html {{tt|setenv()}}], [https://pubs.opengroup.org/onlinepubs/9699919799/functions/unsetenv.html {{tt|unsetenv()}}], and [https://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html {{tt|putenv()}}] may invalidate the pointer returned by a previous call or modify the string obtained from a previous call.
 
}}
 
}}
 
{{rev|since=c++11|
 
{{rev|since=c++11|
This function is thread-safe (calling it from multiple threads does not introduce a data race) as long as no other function modifies the host environment. In particular, the POSIX functions [http://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html setenv()], [http://pubs.opengroup.org/onlinepubs/9699919799/functions/unsetenv.html unsetenv()], and [http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html putenv()] would introduce a data race if called without synchronization.
+
This function is thread-safe (calling it from multiple threads does not introduce a data race) as long as no other function modifies the host environment. In particular, the POSIX functions [https://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html {{tt|setenv()}}], [https://pubs.opengroup.org/onlinepubs/9699919799/functions/unsetenv.html {{tt|unsetenv()}}], and [https://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html {{tt|putenv()}}] would introduce a data race if called without synchronization.
 
}}
 
}}
 
{{rev end}}
 
{{rev end}}
Line 20: Line 20:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | env_var | null-terminated character string identifying the name of the environmental variable to look for }}
+
{{par|env_var|null-terminated character string identifying the name of the environmental variable to look for}}
 
{{par end}}
 
{{par end}}
  
Line 27: Line 27:
  
 
===Notes===
 
===Notes===
On POSIX systems, the [http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08 environment variables] are also accessible through the global variable {{tt|environ}}, declared as {{c|extern char **environ;}} in {{tt|<unistd.h>}}, and through the optional third argument, {{tt|envp}}, of [[cpp/language/main function|the main function]].
+
On POSIX systems, the [https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08 environment variables] are also accessible through the global variable {{tt|environ}}, declared as {{c|extern char** environ;}} in {{header|unistd.h|<!--to skip red-link generation-->lang=c}}, and through the optional third argument, {{tt|envp}}, of [[cpp/language/main function|the main function]].
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| p=true | code=
+
#include <iostream>
+
 
#include <cstdlib>
 
#include <cstdlib>
 +
#include <iostream>
  
 
int main()
 
int main()
 
{
 
{
     if(const char* env_p = std::getenv("PATH"))
+
     if (const char* env_p = std::getenv("PATH"))
 
         std::cout << "Your PATH is: " << env_p << '\n';
 
         std::cout << "Your PATH is: " << env_p << '\n';
 
}
 
}
| output=
+
|p=true
 +
|output=
 
Your PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
 
Your PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
 
}}
 
}}
Line 47: Line 47:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc see c | c/program/getenv}}
+
{{dsc see c|c/program/getenv}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 12:34, 13 October 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <cstdlib>
char* getenv( const char* env_var );

Searches the environment list provided by the host environment (the OS), for a string that matches the C string pointed to by env_var and returns a pointer to the C string that is associated with the matched environment list member.

This function is not required to be thread-safe. Another call to getenv, as well as a call to the POSIX functions setenv(), unsetenv(), and putenv() may invalidate the pointer returned by a previous call or modify the string obtained from a previous call.

(until C++11)

This function is thread-safe (calling it from multiple threads does not introduce a data race) as long as no other function modifies the host environment. In particular, the POSIX functions setenv(), unsetenv(), and putenv() would introduce a data race if called without synchronization.

(since C++11)

Modifying the string returned by getenv invokes undefined behavior.

Contents

[edit] Parameters

env_var - null-terminated character string identifying the name of the environmental variable to look for

[edit] Return value

Character string identifying the value of the environmental variable or null pointer if such variable is not found.

[edit] Notes

On POSIX systems, the environment variables are also accessible through the global variable environ, declared as extern char** environ; in <unistd.h>, and through the optional third argument, envp, of the main function.

[edit] Example

#include <cstdlib>
#include <iostream>
 
int main()
{
    if (const char* env_p = std::getenv("PATH"))
        std::cout << "Your PATH is: " << env_p << '\n';
}

Possible output:

Your PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

[edit] See also

C documentation for getenv