7.8. Example of reading a directory

In this simple example, we will open up our /ifstest directory that we've been using for all of our sample code, and we'll read the contents of it. For each entry in that directory, we'll use the DSPLY op-code to display the first 52 bytes of the file name.

      * CH7READDIR: Example of reading a directory in the IFS
      *  (From Chap 7)
      *
      * To compile:
      *   CRTBNDRPG CH7READDIR SRCFILE(xxx/QRPGLESRC) DBGVIEW(*LIST)
      *
     H DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE')

     D/copy IFSEBOOK/QRPGLESRC,IFSIO_H
     D/copy IFSEBOOK/QRPGLESRC,ERRNO_H

     D dir             s               *
     D Msg             S             52A

     c                   eval      dir = opendir('/ifstest')
     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

      /DEFINE ERRNO_LOAD_PROCEDURE
      /COPY IFSEBOOK/QRPGLESRC,ERRNO_H

If you want to experiment with it, you can change the directory name to other directories, and run it again. Or, maybe have it always list your current directory, and use chdir() to change which directory it lists.