Re: finding the full path of the program which ran.



On Thu, 21 Dec 2006 22:24:55 -0000 Grant Edwards <grante@xxxxxxxx> wrote:

| Well, he did say "is linked" rather than "was linked", so I
| assumed he wanted the current path of the current executable in
| who's context the library is executing.
|
| There isn't really a good, general way to find out. Most
| people who want to know are asking for the wrong reasons
| anyway. Usually when people ask "how do I find the location of
| the program's exectuable" it's becuase they're Windows
| programmers who think configuration files are stored in the
| same directory as executables.
|
| Under Unix that's most definitely _not_ where configuration
| files go, so there shouldn't be any real need to know.

I see more users doing this to find resource files, such as the
collection of dynamically loadable modules that come with the
application. But even this should be elsewhere in Unix, such
as /usr/share (but this varies by distribution).

About the only thing I think the application will need to know is
the base name it was run from. Then it can look for:
${HOME}/.${basename}.conf
${HOME}/.${basename}.d/default.conf
${HOME}/.etc/${basename}.conf
${HOME}/.etc/${basename}.d/default.conf
/etc/${basename}.conf
/etc/${basename}.d/default.conf
or some such thing, and from there obtain any overrides to default
behaviours looking for resources files. A reasonable application
could have a list of paths to look for it's resources based on what
is common for many major distributions of Linux and other Unix class
systems, with tweaks and overrides to that coming from command line
options, environment variables, and config files in the user home
directory or /etc for system wide settings.

If link is made so the application appears to have a different name
than it's original file basename, then the alternate basename would
be used in the above search. If all searches fail to find anything,
then it could rerun the search with it's original package name as
a fallback. It might also be prudent to do this search based on
version variations of the application name (in shell style):
for basename in \
${0} \
${appname}-${majorversion}.${minorversion}.${tweakversion} \
${appname}-${majorversion}.${minorversion} \
${appname}-${majorversion} \
${appname} ; do
search for config file for ${basename}
done

I probably should write a C function to do all this searching in a
nicely callable form for C/C++ programmers.
.



Relevant Pages

  • Re: finding the full path of the program which ran.
    ... The presence of hard links and symbolic links allows for many different ... Never mind that the full path at link time isn't likely to be the same ... who's context the library is executing. ... programmers who think configuration files are stored in the ...
    (comp.os.linux.development.system)
  • Re: Re: Login problem
    ... My system is opening without executing those files and I am looking for ... It is like that there is a nologin specification somewhere in ... configuration files. ... I created another linux user and tried with it but nothing is changed. ...
    (Debian-User)