add, change or delete an environment variable
#include <process.h> int putenv( const char *env_name );
The environment list consists of a number of environment names, each of which has a value associated with it.
When the value of env_name has the format
env_name=value an environment name and its value are added to the environment list.
When the value of env_name has the format
env_name=
the environment name and value are removed from the environment list.
The space into which environment names and their values are placed is limited. Consequently, the putenv() function can fail when there is insufficient space remaining to store an additional value.
putenv( strdup( buffer ) );
To use the export command to assign a string to a variable and place it in the environment list, type something like the following:
export INCLUDE=/usr/include
Type the export command without any arguments to see what variables are in the environment list, and their current assignments. An example of the output is as follows:
SHELL=ksh TERM=qnx LOGNAME=fred PATH=:/bin:/usr/bin HOME=/home/fred INCLUDE=/usr/include LIB=/usr/lib
clearenv(), errno, getenv(), setenv()
The following gets the string currently assigned to INCLUDE and displays it, assigns a new value to it, gets and displays it, and then removes the environment name and value.
#include <stdio.h> #include <stdlib.h> void main() { char *path; path = getenv( "INCLUDE" ); if( path != NULL ) printf( "INCLUDE=%s\n", path ); if( putenv( "INCLUDE=//5/usr/include" ) != 0 ) printf( "putenv failed" ); path = getenv( "INCLUDE" ); if( path != NULL ) printf( "INCLUDE=%s\n", path ); if( putenv( "INCLUDE=" ) != 0 ) printf( "putenv failed" ); }
This program produces the following output:
INCLUDE=/usr/include INCLUDE=//5/usr/include
WATCOM
All (except Netware, DOS/PM)