Return to the RPG Tips
Use the DSPLY op-code to list each object in a directory in the IFS
Here's an example that just uses the DSPLY op-code to list each object in a directory in the IFS... ** This is a simple test program, to demonstrate reading a directory ** using the IFS API with RPG IV. ** <> D PATHTOLIST C CONST('/QDLS/MLHELP/') D********************************************************************** D* D* Directory Entry Structure (dirent) D* D* struct dirent { D* char d_reserved1[16]; /* Reserved */ D* unsigned int d_reserved2; /* Reserved */ D* ino_t d_fileno; /* The file number of the file */ D* unsigned int d_reclen; /* Length of this directory entry D* * in bytes */ D* int d_reserved3; /* Reserved */ D* char d_reserved4[8]; /* Reserved */ D* qlg_nls_t d_nlsinfo; /* National Language Information D* * about d_name */ D* unsigned int d_namelen; /* Length of the name, in bytes D* * excluding NULL terminator */ D* char d_name[_QP0L_DIR_NAME]; /* Name...null terminated */ D* D* }; D* D p_dirent s * D dirent ds based(p_dirent) D d_reserv1 16A D d_reserv2 10U 0 D d_fileno 10U 0 D d_reclen 10U 0 D d_reserv3 10I 0 D d_reserv4 8A D d_nlsinfo 12A D nls_ccsid 10I 0 OVERLAY(d_nlsinfo:1) D nls_cntry 2A OVERLAY(d_nlsinfo:5) D nls_lang 3A OVERLAY(d_nlsinfo:7) D nls_reserv 3A OVERLAY(d_nlsinfo:10) D d_namelen 10U 0 D d_name 640A D*-------------------------------------------------------------------- D* Open a Directory D* D* DIR *opendir(const char *dirname) D* D* NOTE: We are at V3R2, so we can't use OPTIONS(*STRING) yet :( D*-------------------------------------------------------------------- D opendir PR * EXTPROC('opendir') D dirname * VALUE D*-------------------------------------------------------------------- D* Read Directory Entry D* D* struct dirent *readdir(DIR *dirp) D* D* NOTE: We are at V3R2, so we can't use OPTIONS(*STRING) yet :( D*-------------------------------------------------------------------- D readdir PR * EXTPROC('readdir') D dirp * VALUE D* a few local variables... D dh S * D PathName S 256A D Name S 256A C* Step1: Open up the directory. c eval PathName= PATHTOLIST + x'00' C eval dh = opendir(%addr(PathName)) C if dh = *NULL c eval Msg = 'Cant open directory' c dsply Msg 50 c eval *INLR = *ON c Return c endif C* Step2: Read each entry from the directory (in a loop) c eval p_dirent = readdir(dh) c dow p_dirent <> *NULL C* FIXME: This code can only handle file/dir names under 256 bytes long C* because thats the size of "Name" c if d_namelen < 256 c eval Name = %subst(d_name:1:d_namelen) c movel Name dsply_me 52 c dsply_me dsply c endif c eval p_dirent = readdir(dh) c enddo C* Step3: End Program c dsply Pause 1 c eval *inlr = *On
[report a broken link by clicking here]