Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::getenv(3)		      C++ Standard Libary		std::getenv(3)

NAME
       std::getenv - std::getenv

Synopsis
	  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  mem-
       ber.

	  This function	is not required	to be thread-safe. Another call	to
	  getenv,  as  well  as	 a  call  to the POSIX functions setenv(), un-
       setenv(), (until	C++11)
	  and putenv() may invalidate the pointer returned by a	previous  call
       or
	  modify the string obtained from a previous call.
	  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(),
       (since C++11)
	  unsetenv(), and putenv() would introduce a data race if called with-
       out
	  synchronization.

	  Modifying the	string returned	by getenv invokes undefined behavior.

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

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

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.

Example
       // Run this code

	#include <iostream>
	#include <cstdlib>

	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/lo-
       cal/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

See also
http://cppreference.com		  2022.07.31			std::getenv(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::getenv&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help