Removing directories on IFS in RPGLE

Removing directories on IFS in RPGLE.
Removing directories in IFS using RPGLE, removing directory, RMDIR() API, open(), write(), close(), SQLRPGLE , ifs , RPGLE, write data into the IFS file, Closes the file, open(), write(),  close(),  c apis, as400, ibmi, as400 and sql tricks, as400 tutorial, ibmi tutorial, working with ifs, integrated file system,UNIX-type APIs,C language prototype of read() ,extproc,Working with the IFS in RPG IV, prototyping of read() api,The path parameter,The oflag parameter,The mode parameter, The codepage parameter,Introduction to the IFS,
Removing directories in IFS using RPGLE

What is RMDIR() API/ Removing directories?

RMDIR is a command which will remove an empty directory on various operating systems. RMDIR() API is used to delete or remove any directory. On success zero is returned. On error, -1 is returned and errno is set to indicate the error. We cannot delete the directory having that has files within it.

Prototype of RMDIR() api in C Language

int rmdir(const char *path)
  • int: rmdir() api return integer value i.e. 0 for success and -1 for error.
  • rmdir: Remove directory C function to remove ifs directory.
  • const char *path: constant directory path can be passed as string or as an pointer address to the api.
  • Prototype of RMDIR() api in RPGLE

         D rmdir           PR            10I 0 ExtProc('rmdir')         
         D  dirpath                        *   Value options(*string)   

    RPGLE Program for Removing directories.

    RPG Code in Fixed format for Removing directories on IFS in RPGLE.
         HDFTACTGRP(*NO)                                                
         D rmdir           PR            10I 0 ExtProc('rmdir')         
         D  dirpath                        *   Value options(*string)   
          *                                                             
         D errorifs        PR              *   ExtProc('__errno')       
          *                                                             
         D strerror        PR              *   ExtProc('strerror')      
         D error_num                     10I 0 value                    
          *                                                             
         Dreturn_rmdir     s             10i 0 inz                      
         D dirpath         s            512a   inz                      
                                                                        
         Derror_ptr        S               *                            
         Derror_num        S             10I 0 based(error_ptr)         
                                                                        
         Derrormsg_ptr     S               *                            
         Derror_msg        S             50a   based(errormsg_ptr)      
                                                                        
         C                   EVAL      dirpath = '/home/easyclass/dir1' 
         C                   EVAL      return_rmdir = rmdir(%trim(dirpath))   
         C                   IF        return_rmdir < 0                      
         C                   EVAL      error_ptr = errorIFS()                 
         C                   EVAL      errormsg_ptr = strerror(error_num)     
         C     error_msg     DSPLY                                            
         C                   RETURN                                           
         C                   ELSE                                             
         C     'DIR1 REMOVED'DSPLY                                            
         C                   RETURN                                           
         C                   ENDIF                                                                                
    
    RPG Code in /Free and /End-Free format for Removing directories on IFS in RPGLE.
         HDFTACTGRP(*NO)                                                   
         D rmdir           PR            10I 0 ExtProc('rmdir')            
         D  dirpath                        *   Value options(*string)      
          *                                                                
         D errorifs        PR              *   ExtProc('__errno')          
          *                                                                
         D strerror        PR              *   ExtProc('strerror')         
         D error_num                     10I 0 value                       
          *                                                                
         Dreturn_rmdir     s             10i 0 inz                         
         D dirpath         s            512a   inz                         
                                                                           
         Derror_ptr        S               *                               
         Derror_num        S             10I 0 based(error_ptr)            
                                                                           
         Derrormsg_ptr     S               *                               
         Derror_msg        S             50a   based(errormsg_ptr)         
         /Free                                    
          dirpath = '/home/easyclass/dir1';       
                                                  
          return_rmdir = rmdir(%trim(dirpath));   
          if return_rmdir < 0;                   
            error_ptr = errorIFS();               
            errormsg_ptr = strerror(error_num);   
            DSPLY error_msg;                      
            return;                               
          else;                                   
            DSPLY 'DIR1 REMOVED';                 
          endif;                                  
         /End-Free                                                                                                 
    
    RPG Code in Fully Free format Removing directories on IFS in RPGLE.
    **FREE                                         
    CTL-OPT DFTACTGRP(*NO);                        
                                                   
    DCL-PR errorifs pointer EXTPROC('__errno');    
    END-PR;                                        
                                                   
    DCL-PR strerror pointer EXTPROC('strerror');   
      error_num int(10) VALUE;                     
    END-PR;                                        
                                                   
    DCL-PR rmdir int(10) EXTPROC('rmdir');         
      dirpath pointer VALUE options(*string);      
    END-PR;                                        
                                                   
    DCL-S return_rmdir int(10) inz;                
    DCL-S dirpath CHAR(512);                       
                                                   
    DCL-S error_ptr pointer;                       
    DCL-S error_num int(10) based(error_ptr);      
    DCL-S errormsg_ptr pointer;                       
    DCL-S error_msg char(50) based(errormsg_ptr);     
                                                      
           dirpath = '/home/easyclass/dir1';          
                                                      
           return_rmdir = rmdir(%trim(dirpath));      
           if return_rmdir < 0;                      
             error_ptr = errorIFS();                  
             errormsg_ptr = strerror(error_num);      
             DSPLY error_msg;                         
             return;                                  
           else;                                      
             DSPLY 'DIR1 REMOVED';                    
           endif;                                                      
    

    The above code removes the sub-directory in the user home directory by passing the directory path to the rmdir() api. In case remove directory failed we do called the error handling api's to get the error number and the corresponding error message otherwise the directory gets removed and we display the message "DIR1 REMOVED".

    Post a Comment

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