Gets a value from the current environment.
#include <stdlib.h> | Required only for function declarations |
char *getenv( const char *varname );
varname | Name of environment variable |
The getenv function searches the list of environment variables for an entry corresponding to varname. Environment variables define the environment in which a process executes. (For example, the LIB environment variable defines the default search path for libraries to be linked with a program.) Because the getenv function is case sensitive, the varname variable should match the case of the environment variable.
The getenv function returns a pointer to an entry in the environment table. It is, however, only safe to retrieve the value of the environment variable using the returned pointer. To modify the value of an environmental variable, use the _putenv function.
The getenv and _putenv functions use the copy of the environment contained in the global variable environ to access the environment. Programs that use the envp argument to main and the _putenv function may retrieve invalid information. The safest programming practice is to use getenv and _putenv.
The getenv function operates only on the data structures accessible to the run-time library and not on the environment “segment” created for the process by the operating system.
The getenv function returns a pointer to the environment table entry containing the current string value of varname. The return value is NULL if the given variable is not currently defined.
Standards:ANSI, UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
/* GETENV.C: This program uses getenv to retrieve the LIB environment
* variable and then uses _putenv to change it to a new value.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *libvar;
/* Get the value of the LIB environment variable. */
libvar = getenv( "LIB" );
if( libvar != NULL )
printf( "Original LIB variable is: %s\n", libvar );
/* Attempt to change path. Note that this only affects the environment
* variable of the current process. The command processor's environment
* is not changed.
*/
_putenv( "LIB=c:\\mylib;c:\\yourlib" );
/* Get new value. */
libvar = getenv( "LIB" );
if( libvar != NULL )
printf( "New LIB variable is: %s\n", libvar );
}
Original LIB variable is: C:\LIB
New LIB variable is: c:\mylib;c:\yourlib