Tip

Simplifying environment variables

Environment variable API's can be encapsulated and simplified for use.
 
/TITLE Utility, Environment Variables module
(SrvPgm)   
 *PARMS                                                  
 *                                                       
H NOMAIN DATFMT(*USA)                                    
H OPTION(*NOXREF)                                        
H CVTOPT(*DATETIME) ALWNULL(*USRCTL)                     
H ENBPFRCOL(*FULL) BNDDIR('QC2LE')                       
 *                                                       
 *-- begin copy book                                     
 * * * * * * * * * * * * * * * * * * * * * * * * * * *   
 *  Standard type definitions.                           
 * * * * * * * * * * * * * * * * * * * * * * * * * * *   
D StdNulPtr       S               *                      
D StdInd          S              1n   Based( StdNulPtr ) 
D StdInt          S             10i 0 Based( StdNulPtr ) 
D StdPtr          S               *   Based( StdNulPtr ) 
D StdStr          S            256    Based( StdNulPtr ) 
 *                                                                
 *-- begin copy book                                              
 * * * * * * * * * * * * * * * * * * * * * * * * * * *            
 *  Environment prototypes.                                       
 * * * * * * * * * * * * * * * * * * * * * * * * * * *            
 * ---------------------------------------------------------      
 *                                                                
 *  Put environment variable.                                     
 *                                                                
D putenv          PR                  extproc('Qp0zPutEnvNoCCSID')
D                                     like(StdInt)                
D  i_envVar                           like(StdPtr) value          
D                                     options(*string)            
 * ---------------------------------------------------------      
 *                                                                
 *  Get environment variable.                                     
 *                                                                
D getenv          PR                  extproc('Qp0zGetEnvNoCCSID')
D                                     like(StdPtr)                
D  i_envVar                           like(StdPtr) value          
D                                     options(*string)      
 * ---------------------------------------------------------
 *                                                          
 *  Get environment variable.                               
 *                                                          
D GetEnvVar       PR         32767    varying               
D  EnvVarNam                    64    varying const         
D                                     options(*varsize)     
 * ---------------------------------------------------------
 *                                                          
 *  Get environment variable as indicator.                  
 *                                                          
D GetEnvVarInd    PR                  like(StdInd)          
D  EnvVarNam                    64    varying const         
D                                     options(*varsize)     
 * ---------------------------------------------------------
 *                                                          
 *  Delete environment variable.                            
 *                                                          
D dltenv          PR                  extproc('Qp0zDltEnv') 
D                                     like(StdPtr)          
D  i_envVar                           like(StdPtr) value             
D                                     options(*string)               
 * ---------------------------------------------------------         
 *                                                                   
 *  Environment variable workfields.                                 
 *                                                                   
D EnvVarNam       s             64                   inz             
D pEnvVar         s                   like(StdPtr)   inz             
D EnvVar          s          32767    based(pEnvVar)                 
 *                                                                   
 *-- end copy book                                                   
 * **********************************************************        
 * Get environment variable.                                         
 * **********************************************************        
P GetEnvVar       B                   Export                         
 *                                                                   
D GetEnvVar       PI         32767    varying                        
D  EnvVarNam                    64    options(*varsize) varying const
 *                                                                   
D  RtnVar         s          32767    varying inz                    
 * ---------------------------------------------------------         
 *                                                              
 *    Test for no input.                                        
 *                                                              
C                   if        EnvVarNam = *blanks               
C                   return    *blanks                           
C                   endif                                       
 * ---------------------------------------------------------    
 *                                                              
 *    Get environment variable.                                 
 *                                                              
C                   eval      pEnvVar = getenv(%trim(EnvVarNam))
 *                                                              
C                   if        pEnvVar <> *null                  
C                   eval      RtnVar   = %str(pEnvVar)          
C                   else                                        
C                   eval      RtnVar   = *blanks                
C                   endif                                       
 *                                                              
C                   return    RtnVar                            
 *                                                              
P GetEnvVar       E                            *
**********************************************************        
 * Get environment variable as indcator.                             
 * **********************************************************        
P GetEnvVarInd    B                   Export                         
 *                                                                   
D GetEnvVarInd    PI                  like(StdInd)                   
D  EnvVarNam                    64    options(*varsize) varying const
 *                                                                   
D  RtnInd         s                   like(StdInd) inz               
 * ---------------------------------------------------------         
 *                                                                   
 *    Test for no input.                                             
 *                                                                   
C                   if        EnvVarNam = *blanks                    
C                   return    *off                                   
C                   endif                                            
 * ---------------------------------------------------------         
 *                                                                   
 *    Get environment variable.                                      
 *                                                                   
C                   eval      pEnvVar = getenv(%trim(EnvVarNam))     
                  
 *                                                    
C                   if        pEnvVar <> *null        
C                   eval      RtnInd   = %str(pEnvVar)
C                   else                              
C                   eval      RtnInd   = *off         
C                   endif                             
 *                                                    
C                   return    RtnInd                  
 *                                                    
P GetEnvVarInd    E                                   


  
  


This was first published in May 2006

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.