Advertisement

path to executable

Started by September 16, 2004 04:03 AM
9 comments, last by C-Junkie 20 years, 2 months ago
I currently have the following problem - I have a program that requires an init file, say 'init.scm' to run. This file is in the same dir as the executable (later it might end up in a different dir, but its position relative to the executable will be fixed, say '../lib/init.scm'). Currently I am just opening this file as 'init.scm' and praying that it will work. When the program is run as './program' it is okay, but as soon as it is run from another dir ('programdir/program') it obviously can not find its init file. Is there a way to find the path to the executable in a convenient way on unix? Something that also works on win32 would be great but it appears that on windows it already normal for programs to have to be run with the 'current dir' set to the program directory. Now I could parse argv[0], but this does not always work at all (for example if the program was invoked through a link to it).
yes, you have to read the path from /proc. here's a function which does that:

http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-getexename&forum=cotd&id=-1

Advertisement
Thanks, that seems to do precisely what I need.
Actually, that is Linux-specific, any other solutions that work on all Unixes?
Quote: Original post by marijnh
Actually, that is Linux-specific, any other solutions that work on all Unixes?
No.
Quote: This file is in the same dir as the executable (later it might end up in a different dir, but its position relative to the executable will be fixed, say '../lib/init.scm').
This sort of thing isn't done in the unix world.

Data is installed to specific paths, and programs know these paths at compile time.

if you take a look at automake, you'll see how programs deal with paths.
This might sound too simple but why not just put the absolute path to the init file (i.e. '/usr/lib/init.scm')?
Advertisement
Okay, thanks for the info C-Junkie. I suspected something like that... but then how does this work: Many unix programs that require data files do work both from the source dir right after you compile them and from /usr/local/bin after you install them. The data files are in a different place in these two situations. Does it start by looking in the current dir and look in /usr/local/lib/whatever only after that fails?
yes, maybe. but many (esp. bigger) programs are not started directly. instead, a wrapper skript (usually a shell script) is started which sets up the parameters, either as environment variables or as command line parameters for the program.
Don't you get the command used to invoke the program as the first argument? So if there's a 'get current directory' command, you can use a combination of that and argv[0] to deduce the executable's full path. I'm sure I've done this at some point in the past, anyway...
I mentioned arg[0] in the original post. It is not reliable (sadly). I'll just go with the 'look in current dir first, and then in absolute dir' idea.

This topic is closed to new replies.

Advertisement