ILE C/C++ Run-Time Library Functions


putenv() -- Change/Add Environment Variables

Format

#include <stdlib.h>
int *putenv(const char *varname);

Language Level: XPG4

Threadsafe: Yes,

Description

The putenv() function sets the value of an environment variables 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.

Note:
This function accepts only EBCDIC character strings. When LOCALETYPE(*LOCALEUTF) is specified on the compilation command, the user may need to convert any non-EBCDIC character strings to EBCDIC before calling this function.

Return Value

The putenv() function returns 0 is successful. If putenv() fails then -1 is returned and errno is set to indicate the error.

Example that uses putenv()


#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

Related Information


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]