views:

64

answers:

1

Hi everyone,

I have a program written in Fortran 90/95; after invocation it always reads a certain data file. For the users' convenience I'd like them to just have to place this file in the same directory as the executable itself, without making them set some environment variable/extend $PATH, and without forcing them to use a certain directory for that purpose. The program should 'simply' look for the file in the directory in which it itself is stored, NOT in the directory from which it is run. So far, however, I have failed to find a solution to this problem. I tried using

getarg(0,path)

but that only gave me whatever string I had used to invoke the program, not its absolute path.

If you have any suggestions, also concerning workarounds, please don't hesitate to reply. Thanks a lot in advance!

A: 

Requiring that the binary is in some specific directory strikes me as weird. As long as the binary is found on the PATH, stuff should work. What you can do instead is to try to read the data file from the current working directory, i.e. the directory the user is in when launching the program.

I you don't want to require users to always copy the data file around you could search a few "default" places and then use the first one where the file is found, e.g. the current working directory, then $HOME/.your_program/file.dat, and finally /usr/local/share/your_program_name/file.dat, or something like that.

Edit If you, however, wish to continue down this wrongheaded path, at least on Linux you can use readlink() (you probably need to create a C wrapper for this, see the ISO C BINDING in recent Fortran compilers) to check the /proc/self/exe symlink.

As an aside, GETARG is not part of the Fortran standard, so you're relying on a vendor extension (which admittedly is quite widely supported). As of Fortran 2003, the standard feature for doing this is the GET_COMMAND_ARGUMENT intrinsic.

janneb
Dear janneb, thank you for the edit, it seems to contain exactly the information I was looking for. What I don't understand though is what you mean by 'wrongheaded path': what is wrong with my goal to let the users copy the executable to whatever directory they choose and let them run the program from there? Or is this not the point you are criticising?
canavanin