Check if IFS directory exists through CL program

Check if IFS directory exists through CL program, calling access() api in CL, Unix api's in CLLE program
Check if IFS directory exists through CL program, accss api in CLLE program
Check if IFS directory exists through CL program

In this article, we will learn how to check if IFS directory exists using CL program. We can achieve this easily in RPGLE program by calling C api named "access". Now, here we will call "access" C api in CL program to achieve the desired result. Let's see how we did this.

  • Prototype of access api in C Language
  • Prototype of access api in RPGLE
  • Coding in CL for checking IFS directory existence

                 PGM        PARM(&DIRLOC)                                      
                                                                               
                 DCL        VAR(&DIRLOC) TYPE(*CHAR) LEN(512)                  
                 DCL        VAR(&DIRLOCNULL) TYPE(*CHAR) LEN(513)              
                 DCL        VAR(&MODE) TYPE(*INT) LEN(4)                       
                 DCL        VAR(&RTNVAL) TYPE(*INT)                            
                 DCL        VAR(&NULL) TYPE(*CHAR) LEN(1) VALUE(X'00')         
                 DCL        VAR(&ERRPTR) TYPE(*PTR)                            
                 DCL        VAR(&ERRNUM) TYPE(*INT) STG(*BASED) +              
                              BASPTR(&ERRPTR)                                  
                                                                               
                                                                               
                 CHGVAR     VAR(&DIRLOCNULL) VALUE(%TRIM(&DIRLOC) *CAT +       
                              &NULL)                                           
                 CALLPRC    PRC('access') PARM((&DIRLOCNULL *BYREF *DFT) +     
                              (&MODE *BYVAL *DFT)) RTNVAL(&RTNVAL)             
                                                                               
                 IF         COND(&RTNVAL = -1) THEN(DO)                        
                 CALLPRC    PRC('__errno') RTNVAL(&ERRPTR)                     
                                                                            
                 /* 3025 error number means = no such path or directory */  
                 ENDDO                                                      
                                                                            
                 ENDPGM                                                     

    Explanation of the CL code

               PGM        PARM(&DIRLOC)     

    This CL program takes Directory location as Input parameter using PARM syntax

                 DCL        VAR(&DIRLOC) TYPE(*CHAR) LEN(512)                  
                 DCL        VAR(&DIRLOCNULL) TYPE(*CHAR) LEN(513)              
                 DCL        VAR(&MODE) TYPE(*INT) LEN(4)                       
                 DCL        VAR(&RTNVAL) TYPE(*INT)                            
                 DCL        VAR(&NULL) TYPE(*CHAR) LEN(1) VALUE(X'00')         
                 DCL        VAR(&ERRPTR) TYPE(*PTR)                            
                 DCL        VAR(&ERRNUM) TYPE(*INT) STG(*BASED) +              
                              BASPTR(&ERRPTR)     

    Now, certain variables got declared using DCL command including the Input parameter to this program.

  • &DIRLOCNULL will hold the NULL terminated string which almost all C api's accepts.
  • &NULL will hold the null value constant in hex format i.e. X'00'.
  • &ERRPTR is a pointer variable and &ERRNUM is declared as Integer variable based upo the pointer variable and other variables are required to call the access C api.
  •  CHGVAR     VAR(&DIRLOCNULL) VALUE(%TRIM(&DIRLOC) *CAT +       
                              &NULL) 

    Evaluate &DIRLOCNULL by concatenating the input paramater &DIRLOC to this CL program and the NULL value to make the directory location as null terminated string so that then we can pass it to the C access() api.

                 CALLPRC    PRC('access') PARM((&DIRLOCNULL *BYREF *DFT) +     
                              (&MODE *BYVAL *DFT)) RTNVAL(&RTNVAL)   

    Now, we call the C ifs api named 'access' to check the existence of a file in the IFS. So, this access() api can check file existence in IFS along with its accessibility for reading, writing. This access() api accepts 2 parameters namely ifs path and mode means access to the file that we want to check.For checking file existence this mode parameter value would be 0. For more details about access() api parameter refer here. The access api rturns an integer which is either 0 if file exist or accessible or -1 if its not exist or accessible.

                 IF         COND(&RTNVAL = -1) THEN(DO)                        
                 CALLPRC    PRC('__errno') RTNVAL(&ERRPTR)                     
                                                                            
                 /* 3025 error number means = no such path or directory */  
                 ENDDO      

    Here, we check if return value from access() api is -1 then its error and means file does not exists or not accessible for some reason. Therefore, to know that exact reason we now going to call '__errno' api to get the error pointer and we can check the error number in ERRNUM variable as its based on the ERRPTR variable.

    Post a Comment

    © AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain