Description
The C library function char *getenv (const char *name) searches for the environment string pointed to by name< /c1> and returns the associated value to the string.
Declaration
Following is the declaration for getenv () function.
Char*getenv(constChar*name)
Parameters
Return Value
This function returns a null-terminated string with the value of the requested environment variable, or null if that envir Onment variable does not exist.
Example
The following example shows the usage of the getenv () function.
#include <stdio.h>#include <stdlib.h>IntMain () { Printf ( "Path:%s\n" , Getenv ( "PATH" Printf ( "HOME:%s\n" , Getenv "HOME" printf ( "ROOT:%s\n" , Getenv ( "ROOT" return (0 /span>
Let us compile and run the above program, that would produce the following result:
PATH:/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/binhome:/root: (NULL)
Std::getenv C + + Utilities Library Program support Utilities
Defined in header <cstdlib> |
|
|
char* getenv ( const char* env_var ) Span class= "Sy4", |
&NBSP; |
&NBSP; |
|
|
|
Searches the environment List provided by the host environment (the OS), for a string that matches T He C string pointed to by , Env_var
and Returns a pointer to the C string which is associated with the Matched Environment list member.
This function was not required to be thre Ad-safe. Another call to getenv, as-well as-a call to the POSIX functions setenv (), unsetenv (), and putenv () ma Y invalidate the pointer returned by a previous call or modify the string obtained from a previous call. |
|
This function was Thread-safe (calling it from multiple threads does not introdu Ce 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. |
|
Modifying the string returned by getenv
invokes undefined behavior.
Parameters
Env_var |
- |
null-terminated character string identifying the name of the environmental variable to |
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 is also accessible through the global variable environ
, declared as extern Char **environ; <unistd.h>
in, and through the optional third argument, envp
, of the main function.
ExampleRun This Code
#include <iostream>#include <cstdlib> int main()if(char= STD:: getenv("PATH")std::cout'\ n ';}
Possible output:
Your PATH is:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Getenv, _wgetenv
Visual StudioOther Versions
Gets a value from the current environment. More secure versions of these functions is available; See getenv_s, _wgetenv_s.
Important |
This API cannot is used in applications, that execute in the Windows Runtime. For more information, see CRT functions not supported WITH/ZW. |
Syntax
Char *getenv ( const char *varname); wchar_t *_wgetenv ( const wchar_t *varname);
Parameters
-
VarName
-
environment variable name.
Return Value
Returns A pointer to the environment table entry containing varname. It is not safe to modify the value of the environment variable using the returned pointer. Use the _putenv function to modify the value of an environment variable. The return value is NULL if varname are not found in the environment table.
Remarks
the getenv function searches the list of environment variables for varname . getenv is not case sensitive in the Windows operating system. getenv and _putenv use The copy of the environment pointed to by the global VARIABLE&NBSP; _environ to access the environment. getenv operates only on the data Structures accessible to the Run-time library and not on the environment ' segment ' created for the process by the Operatin G System. Therefore, programs that use the envp argument To main or wmain may Retrieve Invalid information.
If varname is NULL, this function invokes a invalid parameter handler, as described in Parameter Valida tion. If execution is allowed to continue, the This function sets errno to EINVAL and returns NULL.
_wgetenv is a wide-character version of getenv; The argument and return value of _wgetenv is wide-character strings. The _wenviron global variable is a wide-character version of _environ.
In a MBCS program (for example, in a SBCS ASCII program), _wenviron is initially NULL because the ENVI Ronment is composed of multibyte-character strings. Then, on the first call to _wputenv, or on the first call to _wgetenv if a (MBCS) environment already E Xists, a corresponding Wide-character string environment is created and was then pointed to by _wenviron.
Similarly in a Unicode (_wmain) program, _environ was initially NULL because the environment is Composed of wide-character strings. Then, on the first call to _putenv, or on the first call to getenv if a (Unicode) environment already ex Ists, a corresponding MBCS environment is created and was then pointed to by _environ.
When both copies of the environment (MBCS and Unicode) exist simultaneously in a program, the Run-time system must maintain Both copies, resulting in slower execution time. For example, whenever-call _putenv, A-call- _wputenv is also executed automatically, so the T WO environment strings correspond.
Caution |
In rare instances, when the run-time system is maintaining both a Unicode version and a multibyte version of the Environme NT, these, environment versions may not correspond exactly. This was because, although any unique multibyte-character string maps to a unique Unicode string, the mapping from a unique The Unicode string to a multibyte-character string was not necessarily unique. For more information, see _environ, _wenviron. |
Note |
The _putenv and _getenv families of functions is not thread-safe. _getenv could return a string pointer while _putenv is modifying the string, causing random failures. Make sure, calls to these functions is synchronized. |
Generic-text Routine Mappings
TCHAR. H Routine |
_unicode & _MBCS Not defined |
_mbcs defined |
_unicode defined |
_tgetenv |
Getenv |
Getenv |
_wgetenv |
To check or change the value of the TZ environment variable, use getenv, _putenv and _ Tzset as necessary. For more information about TZ, See_tzset and _daylight, timezone, and _tzname.
Requirements
Routine |
Required Header |
Getenv |
<stdlib.h> |
_wgetenv |
<stdlib.h> or <wchar.h> |
For additional compatibility information, see compatibility.
Example
//crt_getenv.c//compile with:/w3//This program uses getenv to retrieve//the LIB environment variable and then US es//_putenv to change it to a new value. #include <stdlib.h> #include <stdio.h>int main (void) {char *libvar ; Get the value of the LIB environment variable. Libvar = getenv ("LIB"); C4996//note:getenv is deprecated; Consider using getenv_s instead if (libvar! = NULL) printf ("Original LIB variable is:%s\n", Libvar); Attempt to change path. Note that this is only affects the environment//variable of the current process. The command processor ' s//environment is not changed. _putenv ("Lib=c:\\mylib;c:\\yourlib"); C4996//Note: _putenv is deprecated; Consider using putenv_s instead//Get new value. Libvar = getenv ("LIB"); C4996 if (libvar! = NULL) printf ("New LIB variable is:%s\n", Libvar);}
Original lib variable is:c:\progra~1\devstu~1\vc\libnew lib variable is:c:\mylib;c:\yourlib
Getenv, _wgetenv