Now, Get RPG Codes in all three formats only on this website on every RPGLE related programming article.

Fixed, /Free and Fully Free RPG


Renaming IFS object using Rename() API in RPGLE

Renaming IFS object using Rename() API in RPGLE.
Renaming IFS object using Rename() API in RPGLE, rename() api, ifs object rename in rpgle, working with rpgiv,Qp0lRenameUnlink,Qp0lRenameKeep, ibmi, as400, iseries, systemi, as400andsqltricks, as400 tutorial, ibmi tutorial
Renaming IFS object using Rename() API in RPGLE

We can change the name of an ifs object using rename() api.

What is the Rename() API?

Rename() is allows you to rename an object. This api works with link not with data. Therefore this api has nothing to do with data.

Prototype for the Rename() API in C language

int rename(const char *old, const char *new);
  1. The return value is an int (integer) and values are 0 which indicate success, or -1 if an error occurred.
  2. First parameter is input const char *old where a pointer to the null-terminated path name of the file to be renamed.(old file)
  3. Second parameter is const char *new where pointer to the null-terminated path name of the new name of the file. (new file)

Prototype for the Rename() API in RPG

Please note that, the external procedure name is not "rename" it would be either Qp0lRenameKeep or Qp0lRenameUnlink. Here, you notice two different rename() apis. The difference between them appears when the "new name" (new file) already exists i.e. the name of another object.

If we use Qp0lRenameKeep, and the "new name" (new file) already exists, this api will return the error, and do not delete the existing file.

     D renameK         PR            10I 0 ExtProc('Qp0lRenameKeep')
     D oldname                         *   Value options(*string)   
     D newname                         *   Value options(*string)   

Instead, If we use Qp0lRenameUnlink, and the "new name" (new file) already exists, then this api will first unlink (delete) the existing filename, and then proceed to rename the file name. We must prefer to use Qp0lRenameKeep which ensures that the existing file does not get deleted if the new file name already exists.

     D renameU         PR            10I 0 ExtProc('Qp0lRenameUnlink') 
     D oldname                         *   Value options(*string)      
     D newname                         *   Value options(*string)      

RPGLE program for rename IFS object using RENAME() API

RPG Code in Fixed format for Using rename() api to rename ifs object.
     HDFTACTGRP(*NO)                                                  
     D renameK         PR            10I 0 ExtProc('Qp0lRenameKeep')  
     D oldname                         *   Value options(*string)     
     D newname                         *   Value options(*string)     

     D renameU         PR            10I 0 ExtProc('Qp0lRenameUnlink') 
     D oldname                         *   Value options(*string)      
     D newname                         *   Value options(*string)      
                                                                       
     D errorifs        PR              *   ExtProc('__errno')          
                                                                       
     D strerror        PR              *   ExtProc('strerror')         
     D error_num                     10I 0 value                       

     Difspathold       s            512a                                        
     Difspathnew       s            512a                                        
     Dreturn_renameK   s             10i 0 inz                                  
     Dreturn_renameU   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      ifspathold = '/home/easyclass/helloworld'    
     C                   EVAL      ifspathold = '/home/easyclass/helloworld2'   
     C                   EVAL      return_renameK = renameK(%trim(ifspathold):  
     C                                              %trim(ifspathnew))          
     C                   IF        return_renameK < 0                           
     C                   EVAL      error_ptr = errorIFS()                       
     C                   EVAL      errormsg_ptr = strerror(error_num)           
     C                   ENDIF       
                                                                                   
     C                   EVAL      return_renameU = renameU(%trim(ifspathold):     
     C                                              %trim(ifspathnew))             
     C                   IF        return_renameU < 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 rename() api to rename ifs object.
     HDFTACTGRP(*NO)                                                     
     D renameK         PR            10I 0 ExtProc('Qp0lRenameKeep')     
     D oldname                         *   Value options(*string)        
     D newname                         *   Value options(*string)        
                                                                                                                                   
     D renameU         PR            10I 0 ExtProc('Qp0lRenameUnlink')   
     D oldname                         *   Value options(*string)        
     D newname                         *   Value options(*string)        
                                                                         
     D errorifs        PR              *   ExtProc('__errno')            
                                                                         
     D strerror        PR              *   ExtProc('strerror')           
     D error_num                     10I 0 value                         
                                                                
     Difspathold       s            512a                                 
     Difspathnew       s            512a                                                                                                                                                            Dreturn_renameK   s             10i 0 inz                             
     Dreturn_renameU   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                                                                
       ifspathold = '/home/easyclass/helloworld';                          
       ifspathnew = '/home/easyclass/helloworld2';                         
                                                                           
       return_renameK = renameK(%trim(ifspathold):%trim(ifspathnew));      
       if return_renameK < 0;                                              
         error_ptr = errorIFS();                                           
         errormsg_ptr = strerror(error_num);                               
       endif;              
                                                                      
       return_renameU = renameU(%trim(ifspathold):%trim(ifspathnew)); 
       if return_renameU < 0;                                         
         error_ptr = errorIFS();                                      
         errormsg_ptr = strerror(error_num);                          
       endif;                                                         
                                                                      
       *inlr = *on;                                                   
       return;                                                        
      /end-free                                                                                                                                        
RPG Code in Fully Free format for Using rename() api to rename ifs object.
**FREE                                               
CTL-OPT DFTACTGRP(*NO);                              
DCL-PR renameK int(10) EXTPROC('Qp0lRenameKeep');    
  oldname pointer VALUE options(*string);            
  newname pointer VALUE options(*string);            
END-PR;                                              
                                                     
DCL-PR renameU int(10) EXTPROC('Qp0lRenameUnlink');  
  oldname pointer VALUE options(*string);            
  newname pointer VALUE options(*string);            
END-PR;                                              
                                                     
DCL-PR errorifs pointer EXTPROC('__errno');          
END-PR;                                              
                                                     
DCL-PR strerror pointer EXTPROC('strerror');         
  error_num int(10) VALUE;                           
END-PR;                                              
                                                     
DCL-S ifspathold CHAR(512);                                                 
DCL-S ifspathnew CHAR(512);                                                 
DCL-S return_renameK int(10);                                               
DCL-S return_renameU 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);                              
                                                                            
       ifspathold = '/home/easyclass/helloworld';                           
       ifspathnew = '/home/easyclass/helloworld2';                          
                                                                            
       return_renameK = renameK(%trim(ifspathold):%trim(ifspathnew));       
       if return_renameK < 0;                                               
         error_ptr = errorIFS();                                            
         errormsg_ptr = strerror(error_num);                                
       endif;                                                               
                                                                            
       return_renameU = renameU(%trim(ifspathold):%trim(ifspathnew));       
       if return_renameU < 0;                                               
         error_ptr = errorIFS();             
         errormsg_ptr = strerror(error_num); 
       endif;                                
                                             
       *inlr = *on;                          
       return;                                                                                      

first we call renameK() api i.e. Qp0lRenameKeep api that will rename the file name "helloworld" to new name "helloworld2" in user home directory. In case the new file name already exists then this api will return -1 and do not proceed with rename ifs object.

Secondly, we call renameU() api i.e. Qp0lRenameUnlink api that will rename the file name "helloworld" to new name "helloworld2" in user home directory. In case the new file name already exists then this api will first unlink that file i.e. delete that file and then proceed with rename ifs object to new name from old name.

Please note that when any error returned by any of the rename() api then we call the error handling apis to get error number and corresponding error message.

Post a Comment

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