putenv() — Change/Add Environment Variables
Format
#include <stdlib.h>
int putenv(const char *varname);
Language Level
XPG4
Threadsafe
Yes
Job CCSID Interface
All character data sent to this function is expected to be in the CCSID of the job. All character data returned by this function is in the CCSID of the job. See Understanding CCSIDs and Locales for more information.
Description
The putenv()
function
sets the value of an environment variable by altering an existing
variable or creating a new one. The varname parameter
points to a string of the form var=x, where x is
the new value for the environment variable var
.
The name cannot contain a blank or an equal (
= ) symbol. For example,
PATH NAME=/my_lib/joe_user
is
not valid because of the blank between PATH and NAME.
Similarly, PATH=NAME=/my_lib/joe_user
is
not valid because of the equal symbol between PATH and NAME.
The system interprets all characters following the first equal symbol
as being the value of the environment variable.Return Value
The putenv()
function
returns 0 if successful. If putenv()
fails
then -1 is returned and errno is set to indicate the error.
Example
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *pathvar;
if (-1 == putenv("PATH=/:/home/userid")) {
printf("putenv failed \n");
return EXIT_FAILURE;
}
/* getting and printing the current environment path */
pathvar = getenv("PATH");
printf("The current path is: %s\n", pathvar);
return 0;
}
/**********************************************************
The output should be:
The current path is: /:/home/userid