set one or more environment variables
#include <env.h>
int setenv( const char *name,
const char *newvalue,
int overwrite );
The environment consists of a list of environment variables, each of which has a value associated with it.
The setenv() function searches the environment for an entry of the form name=value. If no such string is present, setenv() adds an entry of the form name=newvalue to the environment. Otherwise, if the overwrite argument is nonzero, setenv() changes the existing value to newvalue.
If the newvalue pointer is NULL, all strings of the form name=value in the environment list will be deleted.
![]() |
The value of the global pointer environ may change across a call to the setenv() function. |
The setenv() function makes copies of the strings associated with name and newvalue before manipulating the environment.
The matching is case-sensitive; lowercase letters are treated as different from uppercase letters.
To assign a string to a variable and place it in the environment list, use the export command, as follows:
export INCLUDE=/usr/include
To see what variables are in the environment list, and their current assignments, type the export command without any arguments. The output will be like the following:
SHELL=ksh
TERM=qnx
LOGNAME=fred
PATH=:/bin:/usr/bin
HOME=/home/fred
INCLUDE=/usr/include
LIB=/usr/lib
The following changes the string assigned to INCLUDE and then displays the new string.
#include <stdio.h>
#include <stdlib.h>
#include <env.h>
int main( void )
{
char *path;
if( setenv( "INCLUDE",
"/usr/include:/home/fred/include",
1 ) == 0 )
if( (path = getenv( "INCLUDE" )) != NULL )
printf( "INCLUDE=%s\n", path );
return( EXIT_SUCCESS );
}
WATCOM
| Safety: | |
|---|---|
| Interrupt handler | No |
| Signal handler | No |
| Thread | No |
clearenv(), errno, exec... functions, getenv(), putenv(), _searchenv(), searchenv(), spawn... functions, system()