|
Hi Bob, On Sat, 27 May 2000, Marion, Bob wrote: > I'm trying to programmatically generate a list of objects in an IFS > directory. I'm trying to use opendir() and readdir(), but since I am not a > C programmer I was not able to understand how to define these API's > parameters in RPG. Could anyone give me sample PR definitions for these > procedures? A little guidance on how to use them will be greatly > appreciated also. > > TIA > > Bob Marion > Sure, here's an example that just uses the DSPLY op-code to list each object in a directory in the IFS... Have Fun! ------------------------------cut here----------------------------------- ** This is a simple test program, to demonstrate reading a directory ** using the IFS API with RPG IV. ** <<CHANGE THIS!!>> 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 ------------------------------cut here----------------------------------- +--- | This is the RPG/400 Mailing List! | To submit a new message, send your mail to RPG400-L@midrange.com. | To subscribe to this list send email to RPG400-L-SUB@midrange.com. | To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com. | Questions should be directed to the list owner/operator: david@midrange.com +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.