Updated: April 19, 2023 |
Create or change an environment variable
#include <stdlib.h> int setenv( const char* name, const char* value, int overwrite );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The setenv() function sets the environment variable name to value. If name doesn't exist in the environment, it's created; if name exists and overwrite is nonzero, the variable's old value is overwritten with value; otherwise, it isn't changed.
Copies of the specified name and value are placed in the environment.
If value is NULL, the environment variable specified by name is removed from the environment.
Environment variable names are case-sensitive.
Change the string assigned to INCLUDE and then display the new string:
#include <stdio.h> #include <stdlib.h> int main( void ) { char* path; if( setenv( "INCLUDE", "/usr/nto/include:/home/fred/include", 1 ) == 0 ) { if( (path = getenv( "INCLUDE" )) != NULL ) { printf( "INCLUDE=%s\n", path ); } } return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
The setenv() function manipulates the environment pointed to by the global environ variable.