putenv

Process Control Functions

Inserts a new definition into an environment table.

int putenv( const char *envstring); 

envstring A string containing the name of the variable, an equal sign (=) and the value for the environment variable:
varname=value

Return Values

0 if successful and -1 in case of error.

For putenv details, refer to your C language documentation.

Example

The following example uses putenv to create a new environment variable LOGFILE_NAME. It is retrieved later on with the getenv function and used to write to the log file in the system's temporary directory.

    #include <stdio.h>
    char * tmp, logfile[512], dir_separator;
    FILE* file;
    // Create an environment variable 
    putenv("LOGFILE_NAME=lr_test.txt");
    // Retrieve the temporary directory environment directory
    if(tmp = (char *)getenv("TEMP"))
        lr_output_message ("Temp Dir = %s", tmp);
    else {
        lr_output_message ("TEMP environment variable undefined");
        return -1;
    }
    sprintf(logfile, "%s\\%s", tmp, (char *)getenv("LOGFILE_NAME"));
    // Open and write to the log file
    if ((file = fopen(logfile, "w")) == NULL ) {
        lr_error_message ("Cannot open %s", logfile);
        return -1;
    }
    else
        lr_output_message ("Opened %s", logfile);
    fprintf(file, "this is a log file\n");
    fclose(file);
Example: Output:
Action.c(11): Temp Dir = D:\TEMP
Action.c(24): Opened D:\TEMP\lr_test.txt