Check if an IFS file exists and permissions to file in RPGLE

Check if an IFS file exists and permissions to file using access() C api in RPGLE.
Check if an IFS file exists and permissions to file using C access API in RPGLE, access () api, , write(), read(),  as400, ibmi, iseries, systemi, as400andsqltricks, as400 tutorials, ibmi tutorials
Check if an IFS file exists and permissions to file using C access API in RPGLE

What is Access() API?

Access() API can be used to check whether the file exists and it's accessible for writing and reading and execution data to and from the ifs stream file in RPGLE.

Prototype for the Access() API in C language

int access(const char *path, int amode);
  1. The int API returns an integer which can either be 0 if the file is accessible otherwise -1.
  2. The access is the name of the C api.
  3. const char *path is the name of the file whose accessibility you want to determine.
  4. int amode (Input) A bitwise representation of the access permissions to be checked. It uses the rightmost 3 bits of the parameter to find out which access we want to check. If the rightmost bit is ON, C api access() checks for execute authority, The 2nd bit from the right checks for write access and the 3rd bit from the right checks for read access. If no bits sets to ON then the C api access() will only check if object exists at the ifs path. Let's see how we define the named constant flags for the bits to be set ON.
  5.  * <-----oflag---->  
      *Access mode flags for access()      
     D A_readaccess    C                   4                                                           
     D A_writeaccess   C                   2                                                           
     D A_executeaccess...                                                                              
     D                 C                   1                                                           
     D A_fileexists    C                   0      

    Here, we are passing decimal values instead of binary. These decimal values actually sets some bits ON from the right most.
    In case of A_fileexists decimal value is zero whose binary is zero means we are not setting any bits and access() api will only check the object existence.
    For A_executeaccess flag decimal value is 1 and corresponding binary value is 0000000000000001 i.e. setting the rightmost bit to ON to check whether the object exists and having execute authority.
    For A_writeaccess flag decimal value is 2 and corresponding binary value is 0000000000000010 i.e. setting the 2nd bit from the right to ON to check whether the object exists and having write authority.
    For A_readaccess flag decimal value is 4 and corresponding binary value is 0000000000000100 i.e. setting the 3r bit from the right to ON to check whether the object exists and having read authority.

Prototype for the Access() API in RPG

 D access          PR            10I 0 ExtProc('access')                                           
 D ifspath                         *   Value Options(*string)                                      
 D accessmode                    10I 0 Value              

RPGLE program for check if an IFS file exists and permissions to file using C access API in RPGLE

RPG Code in Fixed format for Using access() api in RPGLE to check ifs file existence and permissions to it.
     HDFTACTGRP(*NO)                                              
                                                                  
     D access          PR            10I 0 ExtProc('access')      
     D ifspath                         *   Value Options(*string) 
     D accessmode                    10I 0 Value                  
                                                                  
     D errorifs        PR              *   ExtProc('__errno')     
                                                                  
     D strerror        PR              *   ExtProc('strerror')    
     D error_num                     10I 0 value                  
                                                                  
                                                                  
      * <-----oflag----7gt;                                        
     D A_readaccess    C                   4                      
     D A_writeaccess   C                   2                      
     D A_executeaccess...                                         
     D                 C                   1                      
     D A_fileexists    C                   0                      
      *                                                           
     Difspath          s            512a                                   
     Dreturn_access    s             10i 0 inz                             
     Derror_ptr        S               *                                   
     Derror_num        S             10I 0 based(error_ptr)                
                                                                           
     Derrormsg_ptr     S               *                                   
     Derror_msg        S            500a   based(errormsg_ptr)             
                                                                           
     C                   EVAL      ifspath = '/home/easyclass/helloworld'  
     C                   EVAL      return_access = access(%trim(ifspath):  
     C                             A_fileexists)                           
     C                   IF        return_access < 0                       
     C                   EVAL      error_ptr = errorIFS()                  
     C                   EVAL      errormsg_ptr = strerror(error_num)      
     C                   RETURN                                            
     C                   ENDIF                                             
                                                                           
     C                   EVAL      return_access = access(%trim(ifspath):  
     C                             A_executeaccess)                        
     C                   IF        return_access < 0                       
     C                   EVAL      error_ptr = errorIFS()                     
     C                   EVAL      errormsg_ptr = strerror(error_num)         
     C                   RETURN                                               
     C                   ENDIF                                                
                                                                              
                                                                              
     C                   EVAL      return_access = access(%trim(ifspath):     
     C                             A_writeaccess)                             
     C                   IF        return_access < 0                          
     C                   EVAL      error_ptr = errorIFS()                     
     C                   EVAL      errormsg_ptr = strerror(error_num)         
     C                   RETURN                                               
     C                   ENDIF                                                
                                                                              
     C                   EVAL      return_access = access(%trim(ifspath):     
     C                             A_readaccess)                              
     C                   IF        return_access < 0                          
     C                   EVAL      error_ptr = errorIFS()                     
     C                   EVAL      errormsg_ptr = strerror(error_num)         
     C                   RETURN                                               

     C                   ENDIF                                               
                                                                             
     C                   EVAL      return_access = access(%trim(ifspath):    
     C                             A_readaccess + A_writeaccess +            
     C                             A_executeaccess)                          
     C                   IF        return_access < 0                         
     C                   EVAL      error_ptr = errorIFS()                    
     C                   EVAL      errormsg_ptr = strerror(error_num)                                                 
     C                   ENDIF                                               
                                                                             
     C                   EVAL      *INLR = *ON                               
     C                   RETURN                                                   
RPG Code in /Free and /End-Free format for Using access() api in RPGLE to check ifs file existence and permissions to it.
     HDFTACTGRP(*NO)                                               
                                                                   
     D access          PR            10I 0 ExtProc('access')       
     D ifspath                         *   Value Options(*string)  
     D accessmode                    10I 0 Value                   
                                                                   
     D errorifs        PR              *   ExtProc('__errno')      
                                                                   
     D strerror        PR              *   ExtProc('strerror')     
     D error_num                     10I 0 value                   
                                                                   
                                                                   
      * <-----oflag---->                                          
     D A_readaccess    C                   4                       
     D A_writeaccess   C                   2                       
     D A_executeaccess...                                          
     D                 C                   1                       
     D A_fileexists    C                   0                       
      *                                                            
     Difspath          s            512a                           
     Dreturn_access    s             10i 0 inz                     
     Derror_ptr        S               *                           
     Derror_num        S             10I 0 based(error_ptr)        
                                                                   
     Derrormsg_ptr     S               *                           
     Derror_msg        S            500a   based(errormsg_ptr)     
                                                                   
      /free                                                        
       ifspath = '/home/easyclass/helloworld';                     
                                                                   
       return_access = access(%trim(ifspath):A_fileexists);        
       if return_access < 0;                                       
         error_ptr = errorIFS();                                   
         errormsg_ptr = strerror(error_num);                       
       endif;                                                      
                                                                   
       return_access = access(%trim(ifspath):A_executeaccess);     
       if return_access < 0;                                       
         error_ptr = errorIFS();                                   
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       return_access = access(%trim(ifspath):A_writeaccess);              
       if return_access < 0;                                              
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       return_access = access(%trim(ifspath):A_readaccess);               
       if return_access < 0;                                              
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
                                                                          
       return_access = access(%trim(ifspath):A_readaccess+A_writeaccess+  
                                             A_executeaccess);            
       if return_access < 0;                                              
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);     
       endif;                                    
                                                 
       *inlr = *on;                              
       return;                                   
      /end-free                                                                                      
RPG Code in Fully Free format for Using access() api in RPGLE to check ifs file existence and permissions to it.
**FREE                                            
CTL-OPT DFTACTGRP(*NO);                           
DCL-PR access int(10) EXTPROC('access');          
  ifspath pointer VALUE options(*string);         
  accessmode  int(10) VALUE;                      
END-PR;                                           
                                                  
DCL-PR errorifs pointer EXTPROC('__errno');       
END-PR;                                           
                                                  
DCL-PR strerror pointer EXTPROC('strerror');      
  error_num int(10) VALUE;                        
END-PR;                                           
                                                  
// * <-----oflag---->                             
DCL-C A_readaccess 4;                             
DCL-C A_writeaccess 2;                            
DCL-C A_executeaccess 1;                          
DCL-C A_fileexists 0;                             
                                                                     
DCL-S ifspath CHAR(512);                                             
DCL-S return_access int(10);                                         
DCL-S error_ptr pointer;                                             
DCL-S error_num int(10) based(error_ptr);                            
DCL-S errormsg_ptr pointer;                                          
DCL-S error_msg char(500) based(errormsg_ptr);                       
                                                                     
       ifspath = '/home/easyclass/helloworld';                       
                                                                     
       return_access = access(%trim(ifspath):A_fileexists);          
       if return_access < 0;                                         
         error_ptr = errorIFS();                                     
         errormsg_ptr = strerror(error_num);                         
       endif;                                                        
                                                                     
       return_access = access(%trim(ifspath):A_executeaccess);       
       if return_access < 0;                                         
         error_ptr = errorIFS();                                     
         errormsg_ptr = strerror(error_num);                         
       endif;                                                             
                                                                          
       return_access = access(%trim(ifspath):A_writeaccess);              
       if return_access < 0;                                              
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       return_access = access(%trim(ifspath):A_readaccess);               
       if return_access < 0;                                              
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
                                                                          
       return_access = access(%trim(ifspath):A_readaccess+A_writeaccess+  
                                             A_executeaccess);            
       if return_access < 0;                                              
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;           
                        
       *inlr = *on;     
       return;                                  

Here, In the above code we first check using access() api whether the file named helloworld exists at the user home directory if not present called the respective error apis to get error number and error message. Again, checking whether file has execute, write, read or all(reas+write+execute) authority respectively using access() api.

Create the program using option 14 on source member or using CRTSQLRPGI command since we always create source member of type SQLRPGLE so that we can use embedded SQL whenever required.

Post a Comment

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