Blog Archive

IFS Directory and File Listing Sample Program

iSeries IFS Directory Listing using Qshell Command and C API's opendir(), readdir() and closedir()


Using the Qshell command ls


Start the Qshell
  • Type QSH press ENTER on command line
Change current directory
  • cd /dir
List directory contents
  • ls
To get usage for any command
  • Type command -h
More ls Options
  • ls –ltra
in this case, the command modifiers
  • 'a' means 'all', do not hide the directory entries starting with '.'
  • 'l' means 'long', show all the details about each entry
  • 't' means sort by modification time
  • 'r' means reverse order for the sort. Effectively, the latest entries will be the last ones in the listing, which is practical for reading.

CL Program using Shell command ls to dump directory listing to a file

PGM                                                         
                                                            
CD         DIR('/myxml/data')                               
CRTPF      FILE($library/ifsdir) RCDLEN(128)                     
STRQSH     CMD('ls -lt *.* > +                              
             /qsys.lib/$library.lib/ifsdir.file/ifsdir.mbr')     
                                                            
ENDPGM 

Sample output the above program
000001 ----rwx---  1 VAI  0                  1216 May 26 19:24 Order.xml 
000002 -rwx------  1 VAI  0                   231 May  7 16:14 odetl.csv 
000003 -rwx------  1 VAI  0                   106 May  7 16:14 ohead.csv  

Sample RPGLE program using C APIs


The opendir() API is similar in some ways to the open() API. It accepts a parameter that tells the name of a directory to open, and it returns a handle that can be used to read through that directory. Here is the C-language prototype for the opendir() API:

DIR *opendir(const char *dirname)

Once the directory has been opened, we will want to read it's contents. This is accomplished using the readdir() "Read Directory" API. Here is the C language prototype for the readdir() API:

struct dirent *readdir(DIR *dirp) 

When we're done reading the directory, we need to close it. To do that, we will call the Close Directory API, closedir(). Here's the C prototypes for closedir():

int closedir(DIR *dirp)
H DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE')                               
                                                                            
D/copy *libl/QRPGLESRC,IFSIO_H                                              
D/copy *libl/QRPGLESRC,ERRNO_H                                                    
                                                                            
D dir             s               *                                         
D Msg             S             52A                                         
                                                                            
D die             PR             1N                                         
D    msg                       256A   const                                 
                                                                            
D EscErrno        PR             1N                                         
D   errnum                      10i 0 value                                 
                                                                            
c                   eval      dir = opendir('/Vai_Java')                    
c                   if        dir = *NULL                                   
c                   callp     die('opendir(): '+%str(strerror(errno)))      
c                   endif                                                    
                                                                             
c                   eval      p_dirent = readdir(dir)                        
c                   dow       p_dirent <> *NULL                              
c                   eval      Msg = %subst(d_name:1:d_namelen)               
c     msg           dsply                                                    
c                   eval      p_dirent = readdir(dir)                        
c                   enddo                                                    
                                                                             
c                   callp     closedir(dir)                                  
                                                                             
c                   eval      Msg = 'Press ENTER to end'                     
c                   dsply                   Msg                              
                                                                             
c                   eval      *inlr = *on                                    
P die             B                                                          
D die             PI             1N                                          
D    msg                       256A   const                                  
                                                                             
D QMHSNDPM        PR                  ExtPgm('QMHSNDPM')                     
D   MessageID                    7A   Const                                  
D   QualMsgF                    20A   Const                                  
D   MsgData                    256A   Const                                  
D   MsgDtaLen                   10I 0 Const                          
D   MsgType                     10A   Const                          
D   CallStkEnt                  10A   Const                          
D   CallStkCnt                  10I 0 Const                          
D   MessageKey                   4A                                  
D   ErrorCode                  256A                                  
                                                                     
D dsEC            DS                                                 
D  dsECBytesP             1      4I 0 inz(%size(dsEC))               
D  dsECBytesA             5      8I 0 inz(0)                         
D  dsECMsgID              9     15                                   
D  dsECReserv            16     16                                   
D  dsECMsgDta            17    256                                   
                                                                     
D MsgLen          S             10I 0                                
D TheKey          S              4A                                  
                                                                     
c     ' '           checkr    msg           MsgLen                   
c                   if        MsgLen<1                               
c                   return    *off                                   
c                   endif                                            
                                                                     
c                   callp     QMHSNDPM('CPF9897': 'QCPFMSG   *LIBL': 
c                               Msg: MsgLen: '*ESCAPE':     
c                               '*': 3: TheKey: dsEC)       
                                                            
c                   return    *off                          
P                 E                                         
                                                            
P EscErrno        B                                         
D EscErrno        PI             1N                         
D   errnum                      10i 0 value                 
                                                            
D QMHSNDPM        PR                  ExtPgm('QMHSNDPM')    
D   MessageID                    7A   Const                 
D   QualMsgF                    20A   Const                 
D   MsgData                      1A   Const                 
D   MsgDtaLen                   10I 0 Const                 
D   MsgType                     10A   Const                 
D   CallStkEnt                  10A   Const                 
D   CallStkCnt                  10I 0 Const                 
D   MessageKey                   4A                         
D   ErrorCode                  256A                         
                                                            
D dsEC            DS                                        
D  dsECBytesP             1      4I 0 inz(%size(dsEC))       
D  dsECBytesA             5      8I 0 inz(0)                        
D  dsECMsgID              9     15                                  
D  dsECReserv            16     16                                  
D  dsECMsgDta            17    256                                  
                                                                    
D TheKey          S              4A                                 
D MsgID           S              7A                                 
                                                                    
c                   move      errnum        MsgID                   
c                   movel     'CPE'         MsgID                   
                                                                    
c                   callp     QMHSNDPM(MsgID: 'QCPFMSG   *LIBL':    
c                               ' ': 0: '*ESCAPE':                  
c                               '*': 3: TheKey: dsEC)               
                                                                    
c                   return    *off                                  
P                 E                                                 
C/define ERRNO_LOAD_PROCEDURE                                       
C/copy *libl/QRPGLESRC,ERRNO_H   

Source for ERRNO_H
Source for IFSIO_H

Reference: http://www.scottklement.com