dirname Function

public pure function dirname(path) result(dir)

Extract directory component from a file path (“.” if none is present)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: path

Input file path

Return Value character(len=:), allocatable

Extracted directory path


Source Code

pure function dirname(path) result(dir)

   !> Input file path
   character(len=*), intent(in) :: path

   !> Extracted directory path
   character(len=:), allocatable :: dir

   integer :: isep, last

   isep = scan(trim(path), "/\", back=.true.)
   if (isep == 0) then
      dir = "."
      return
   end if

   last = verify(path(:isep), "/\", back=.true.)
   if (last == 0) then
      dir = path(1:1)
   else if (last == 2 .and. path(2:2) == ":") then
      dir = path(1:3)
   else
      dir = path(1:last)
   end if

end function dirname