Skip navigation.
 
mlRe: Finding out executable location from a c program
FROM : Douglas Davidson
DATE : Mon Nov 26 19:44:31 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

Related mailsAuthorDate
mlFinding out executable location from a c program Antti Karanta Nov 19, 17:06
mlRe: Finding out executable location from a c program I. Savant Nov 19, 17:12
mlRe: Finding out executable location from a c program Antti Karanta Nov 19, 17:24
mlRe: Finding out executable location from a c program Paul Sargent Nov 19, 17:37
mlRe: Finding out executable location from a c program I. Savant Nov 19, 17:44
mlRe: Finding out executable location from a c program Christiaan Hofman Nov 19, 17:51
mlRe: Finding out executable location from a c program Roy Lovejoy Nov 19, 23:53
mlRe: Finding out executable location from a c program Scott Stevenson Nov 19, 23:55
mlRe: Finding out executable location from a c program Clark Cox Nov 20, 03:09
mlRe: Finding out executable location from a c program Scott Stevenson Nov 20, 06:31
mlRe: Finding out executable location from a c program Clark S. Cox III Nov 20, 06:37
mlRe: Finding out executable location from a c program Alastair Houghton Nov 20, 13:36
mlRe: Finding out executable location from a c program Antti Karanta Nov 23, 21:22
mlRe: Finding out executable location from a c program Antti Karanta Nov 23, 21:52
mlRe: Finding out executable location from a c program Douglas Davidson Nov 26, 19:44
mlRe: Finding out executable location from a c program Alastair Houghton Nov 27, 13:11