interface
function c_getcwd (buf, size) bind(C,name="getcwd") result(r)
import
type(c_ptr) :: r
character(kind=c_char), dimension(*), intent(out) :: buf
integer(kind=c_size_t), value :: size
end function c_getcwd
end interface
if (c_associated(c_getcwd (buf, size(buf,kind=c_size_t)))) then
n = findloc(buf,achar(0),1)
allocate (character(len=n-1) :: dir)
dir(1:n-1) = transfer(buf(1:n-1),dir(1:n-1))
end if