On 01/19/2010 06:09 PM, Craig Dedo wrote:
>
> I hate to say it, but I don’t think highly of your idea. I really
> sympathize with your situation because you are really in a tough spot
> if you have to work with any of the Unixes.
>
The following works with Linux, but presumably not with other Unix
systems as it assumes the presence of /proc/<pid>/exe. Searching around,
it seems to be much more difficult with other Unix systems, where one
seem to have to use things like lsof. As "$0" in the shell includes the
directory, it might make more sense to use a wrapper script; but be
aware of symbolic links (e.g. using "readlink" [possibly with option -f,
-e or -m; is part of GNU Coreutils and thus probably also not portable.])
The Linux program is:
use iso_c_binding
implicit none
character(len=200) :: str, str2
integer :: pid
integer(c_size_t) :: length
interface
! pid_t, could also be, e.g., c_long
integer(c_int) function getpid() bind(C)
import
end function
function readlink(path,buf,bufsize) bind(C)
import
character(kind=c_char) :: path(*), buf(*)
integer(C_SIZE_T) :: bufsize, readlink
end function
end interface
pid = getpid()
write(str,'(a,i0,a)') "/proc/", pid, "/exe"
length = readlink(trim(str)//C_NULL_CHAR,str2,len(str2,kind=C_SIZE_T))
write(*,*) "The pathname of the executable is: ", str2(1:length)
end
Tobias
|