Functions and Directives

Function Getenv() Foundation

Retrieves the contents of an operating system environment variable.

Syntax
Getenv( <cSetVar> ) --> cString
Parameters
<cSetVar>
<cSetVar> is the name of an OS environment variable. It can be specified in upper or lower case letters since Getenv() does not differentiate between upper and lower case letters.
Return

Getenv() returns the contents of the specified OS environment variable as a character string. If the variable is not set, a null string ("") is returned.

Description

The environment function Getenv() provides information about the operating system environment from within a program. Generally this information concerns the configuration. Getenv() is most frequently used for retrieving the path name which designates the path where the files accessed by an application program are found. This is especially advantageous in a network environment.

Examples
Retrieve environment variables

// The example uses the environment variable DBF_PATH as a 
// path for DBF files. Before the start of a program this 
// variable must be defined at the operating system level. 
// For example:   C:>SET DBF_PATH=c:\xpp\samples\data\misc 

PROCEDURE SetDataPath() 
   LOCAL cPath := Getenv("DBF_PATH") 

   IF ! Empty(cPath) 
      SET PATH TO (cPath) 
   ELSE 
      ? "Environment variable DBF_PATH is not defined" 
   ENDIF 
RETURN 

Login routine using Getenv()

// In this example an environment variable is used to 
// establish whether a LOGIN is permitted in a network 
// environment. Access is denied when a file save is 
// currently taking place. 

PROCEDURE Login() 
   LOCAL cPath:=Getenv("DBF_PATH") 
   CLS 
   IF File(cPath+"\BACKUP.LOG") 
      ? "The files are in the process of being saved." 
      ? "Access is not possible at this time!" 
      QUIT 
   ENDIF 
RETURN 

PROCEDURE Backup() 
   LOCAL cPath:=Getenv("DBF_PATH"), nHandle 
   CLS 
   IF ( nHandle := FCreate(cPath+"BACKUP.LOG") ) > 0 
      FClose( nHandle ) 
      ? "Files are saved with a backup routine" 
      IF FErase( cPath+"BACKUP.LOG" ) == 0 
         ? "Files are saved" 
      ENDIF 
   ELSE 
      ? "File error:", FError() 
   ENDIF 
RETURN 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.