How to change permissions on an existing IFS Object using RPGLE

How to change permissions on an existing IFS Object using RPGLE.
How to change permissions on an existing IFS Object using RPGLE, chmod(), change mode, Prototype for the chmod() API, ifspath, write(), read(),  as400, ibmi, iseries, systemi, as400andsqltricks, as400 tutorials, ibmi tutorials
How to change permissions on an existing IFS Object using RPGLE

What is chmod() API?

chmod() API known as Change Mode API that allows us to change the permissions/authorities of an existing IFS object.

Prototype for the chmod() API in C Language

int chmod(const char *path, mode_t mode)
  • The int API returns an integer which can either be 0 if the object permissions changed successfully otherwise -1.
  • The chmod is the name of the C api.
  • const char *path is the path/name of the object whose permisions we want to change.
  • mode_t mode (Input) This mode parameter works same as the mode parameter in open() C api. We will use the same bits flag to assign the permissions to the owner, group and others as we used in the open() api mode parameter.
  • Prototype for the chmod() API in RPG

    D chmod           PR            10I 0 ExtProc('chmod')                                             
     D ifspath                         *   Value options(*string)                                       
     D changemode                    10U 0 Value        

    The chmod() function shall change the file permission bits of the file named by the pathname pointed to by the path to the corresponding bits in the mode. The mode parameter works exactly the same as the mode parameter on the open() api.

    RPGLE program for change permissions on an existing IFS Object

    RPG Code in Fixed format for Using chmod() api in RPGLE to change ifs object permissions.
         HDFTACTGRP(*NO)                                                  
         D chmod           PR            10I 0 ExtProc('chmod')           
         D ifspath                         *   Value options(*string)     
         D changemode                    10U 0 Value                      
                                                                          
                                                                          
         D errorifs        PR              *   ExtProc('__errno')         
                                                                          
         D strerror        PR              *   ExtProc('strerror')        
         D error_num                     10I 0 value                      
                                                                          
          * owner,group,other (RWX)                                       
          *                                         owner authority       
         D M_readowner     C                   256                        
         D M_writeowner    C                   128                        
         D M_executeowner  C                   64                         
          *                                         group authority       
         D M_readgroup     C                   32                         
         D M_writegroup    C                   16                         
         D M_executegroup  C                   8                               
          *                                         other people               
         D M_readother     C                   4                               
         D M_writeother    C                   2                               
         D M_executeother  C                   1                               
          *                                                                    
                                                                               
          *                                                                    
         Difspath          s            512a                                   
         Dreturn_chmod     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_chmod = chmod(%trim(ifspath):    
         C                                            M_readowner  +           
         C                                            M_writeowner  +          
         C                                            M_executeowner )       
         C                   IF        return_chmod < 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 chmod() api in RPGLE to change ifs object permissions.
         HDFTACTGRP(*NO)                                              
         D chmod           PR            10I 0 ExtProc('chmod')       
         D ifspath                         *   Value options(*string) 
         D changemode                    10U 0 Value                  
                                                                      
                                                                      
         D errorifs        PR              *   ExtProc('__errno')     
                                                                      
         D strerror        PR              *   ExtProc('strerror')    
         D error_num                     10I 0 value                  
                                                                      
          * owner,group,other (RWX)                                   
          *                                         owner authority   
         D M_readowner     C                   256                    
         D M_writeowner    C                   128                    
         D M_executeowner  C                   64                     
          *                                         group authority   
         D M_readgroup     C                   32                     
         D M_writegroup    C                   16                     
         D M_executegroup  C                   8                        
          *                                         other people        
         D M_readother     C                   4                        
         D M_writeother    C                   2                        
         D M_executeother  C                   1                        
          *                                                             
                                                                        
          *                                                             
         Difspath          s            512a                            
         Dreturn_chmod     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_chmod = chmod(%trim(ifspath):                         
                                  M_readowner  +       
                                  M_writeowner  +      
                                  M_executeowner );    
           if return_chmod < 0;                        
             error_ptr = errorIFS();                   
             errormsg_ptr = strerror(error_num);       
           endif;                                      
                                                       
           *inlr = *on;                                
           return;                                     
          /end-free                                                                                                      
    
    RPG Code in Fully Free format for Using chmod() api in RPGLE to change ifs object permissions.
    **FREE                                                                
    CTL-OPT DFTACTGRP(*NO);                                               
    DCL-PR chmod int(10) EXTPROC('chmod');                                
      ifspath pointer VALUE options(*string);                             
      changemode 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;                                                               
                                                                          
    //    * <-----mode---->                                              
    //    * owner,group,other (RWX)                                       
    //    *                                         owner authority       
    DCL-C M_readowner 256;                                                
    DCL-C M_writeowner 128;                                               
    DCL-C M_executeowner 64;                                       
    //    *                                         group authority
    DCL-C M_readgroup 32;                                          
    DCL-C M_writegroup 16;                                         
    DCL-C M_executegroup 8;                                        
    //    *                                         other people   
    DCL-C M_readother 4;                                           
    DCL-C M_writeother 2;                                          
    DCL-C M_executeother 1;                                        
    DCL-S ifspath CHAR(512);                                       
    DCL-S return_chmod 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_chmod = chmod(%trim(ifspath):                    
                                  M_readowner  +                   
                                  M_writeowner  +     
                                  M_executeowner );   
           if return_chmod < 0;                       
             error_ptr = errorIFS();                  
             errormsg_ptr = strerror(error_num);      
           endif;                                     
                                                      
           *inlr = *on;                               
           return;                                                                
    

    In the above source code, we tried to change the permissions(Read, write and execute authority) of the file named helloworld on the user home directory. If the chmod() api returns 0 that means permissions have been changed and if it returns -1 then that means failed to change file permissions and in case of error we do call error handling apis to return error number and corresponding error message.

    Post a Comment

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