#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); |
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.
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.
#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: ome new HOME variable is: \check
[ENOMEM] | |
The function setenv or putenv failed because they were unable to allocate memory for the environment. | |
© 2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user. |