Name

getenv, putenv, setenv, unsetenv
- environment variable functions

Library

libc.lib

Synopsis

  #include <stdlib.h>
  char * getenv (const char *name);
  int setenv (const char *name, const char *value, int overwrite);
  int putenv (const char *string);
  void unsetenv (const char *name);

Return values

The getenv function returns the value of the environment variable as a NUL -terminated string. If the variable name is not in the current environment, NULL is returned.

The setenv and putenv functions return the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

The unsetenv function never returns.


Detailed description

These functions set, unset and fetch environment variables from the host environment list. For compatibility with differing environment conventions, the given arguments name and value may be appended and prepended, respectively, with an equal sign "=."

The getenv function obtains the current value of the environment variable, name.

The setenv function inserts or resets the environment variable name in the current environment list. If the variable name does not exist in the list, it is inserted with the given value. If the variable does exist, the argument overwrite is tested; if overwrite is zero, the variable is not reset, otherwise it is reset to the given value.

The putenv function takes an argument of the form ‘‘name=value’’ and is equivalent to:

setenv(name, value, 1);


The unsetenv function deletes all instances of the variable name pointed to by name from the list.


Examples

#include <stdlib.h> //getenv
#include <stdio.h> //printf
 
int main( void )
{
  int iret = putenv("PATH=c:\sys\bin;");
 
  if( iret == -1)
     printf( "putenv() failed!!\n" );
 
   /* fetch new value. */
  char *psc = getenv("PATH");
 
  if( psc != NULL )
      printf( "new PATH variable is: %s\n", psc );
 
  return 0;
}


Output

new PATH variable is: c:\sys\bin;


#include <stdlib.h> //setenv, getenv
#include <stdio.h> //printf
 
int main( void )
{
   char *var;
 
   /* Unset the value of the HOME environment variable
    * to make sure that it is non-existing.
    */
   unsetenv( "HOME" );
 
   /* Set the value of HOME environment with no overwrite option */
   int iret = setenv("HOME", "/home", 0);
    
   if(iret == -1)
      printf( "Setenv failed!!" );
    
   /* Get new value. */
   var = getenv( "HOME" );
 
   if( var != NULL )
      printf( "new HOME variable is: %s\n", var );
    
   /* Set the value of HOME environment with the overwrite option */
   iret = setenv("HOME", "/check", 1);
    
   /* Get new value. */
   var = getenv( "HOME" );
 
   if( var != NULL )
      printf( "new HOME variable is: %s\n", var );
     
   return 0;
}


Output

new HOME variable is: \home
new HOME variable is: \check


Errors

[ENOMEM]
  The function setenv or putenv failed because they were unable to allocate memory for the environment.

Feedback

For additional information or queries on this page send feedback

© 2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user.

Top