Share this page

# getenv

Process Control Functions

Gets the definition of an environment variable.

    char *getenv( const char *varname );

varname | The name of the environment variable whose definition is returned.

## Return Values

Gets the value of the specified environment variable or NULL if that environment variable does not exist.

For getenv details, refer to your C language documentation.

## Example

The following example uses getenv to retrieve the operating system's TEMP environment variable. It then writes a log file to that directory.

Copy code

        #include <stdio.h>
        #include <stdlib.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

Tips to improve your search results... VuGen Function Reference Version 26.1-26.3 Function Reference 26.1-26.3 All View Local Help Center View Online Help Center Version: