Finding out executable location from a c program
Douglas Davidson
ddavidso at apple.com
Mon Nov 26 10:44:31 PST 2007
On Nov 20, 2007, at 4:36 AM, Alastair Houghton wrote:
> It's perhaps worth saying though that using the path of your tool is
> generally an evil thing to do. It's normally best, for command line
> tools, to hard-code paths to any resources they need in the normal
> UNIX places, or to stick them inside an associated application and
> use Launch Services to locate that (you probably want
> LSFindApplicationForInfo()), then use CF or NSBundle to grab
> references to the relevant files from there.
I might also mention that the path to the executable is not
necessarily well-defined in general. For example, by the time your
executable is launched and running, the file system might have changed
so that the file that was there no longer exists; something else
entirely might be in its place. There are a number of cases in which
the executable content is actually defined by something other than the
currently running Mach-o binary--for example, shell scripts, CFM
executables, Java, and so forth. If consistency with other behavior
on the platform is an issue, I would recommend using CFBundle/NSBundle
to obtain the executable path; it does its best to give a reasonable
result, and it is the mechanism most generally used on the system.
However, keep in mind that it may fail, and that no mechanism of this
sort can succeed in all cases. In particular, don't make security
mechanisms dependent on being able to locate your executable. If you
need to examine the binary contents of your executable, you can look
at the loaded version in your address space.
Douglas Davidson
More information about the MacOSX-dev
mailing list