ASM allowed in Fortran? and OpenDir?

Walter Spector w6ws@earthlink.net
Wed Aug 8 06:29:00 GMT 2007


Tobias Burnus wrote:
> For opendir/readdir one should probably write a C wrapper.

Yes.  And while at it, why not make it POSIX compliant?

Here is a sample program to list all the files in the current directory,
using the PXF calls:

  program dir
    implicit none

    character(*), parameter :: mydir = '.'

    character(1023) :: name
    integer :: name_l

    integer :: dirid
    integer :: jdirent
    integer :: pxferr

    call pxfopendir (mydir, len (mydir), dirid, pxferr)
    call assert_zr (pxferr, mydir // ': not openable')

    call pxfstructcreate ('dirent', jdirent, pxferr)

    do
      call pxfreaddir (dirid, jdirent, pxferr)
      if (pxferr /= 0) exit

      call pxfstrget (jdirent, 'd_name', name, name_l, pxferr)
      print *, name(:name_l)
    end do

  contains

    subroutine assert_zr (i, str)
      integer, intent(in) :: i
      character(*), intent(in) :: str

      if (i == 0) return
      write (0,*) str
      stop 1

    end subroutine

  end program

As noted, attempting to use F2003 C Interop to call readdir will not
lead to portable code, since readdir needs a 'dirent' struct.
The definition of a dirent can be quite different from system to system,
and there is no way to tell C Interop to look into <dirent.h> to get the
appropriate definition for the system in use.

W.



More information about the Fortran mailing list