Friday, June 1, 2012

Finding current executable"s path without /proc/self/exe


It seems to me that Linux has it easy with /proc/self/exe. But I'd like to know if there is a convenient way to find the current application's directory in C/C++ with cross-platform interfaces. I've seen some projects mucking around with argv[0], but it doesn't seem entirely reliable.



If you ever had to support, say, Mac OS X, which doesn't have /proc/, what would you have done? Use #ifdefs to isolate the platform-specific code (NSBundle, for example)? Or try to deduce the executable's path from argv[0], $PATH and whatnot, risking finding bugs in edge cases?


Source: Tips4all

7 comments:

  1. Some OS-specific interfaces:


    Mac OS X: _NSGetExecutablePath() (man 3 dyld)
    Linux: readlink /proc/self/exe
    Solaris: getexecname()
    FreeBSD: sysctl CTL_KERN KERN_PROC KERN_PROC_PATHNAME -1
    BSD with procfs: readlink /proc/curproc/file
    Windows: GetModuleFileName() with hModule = NULL


    The portable (but less reliable) method is to use argv[0]. Although it could be set to anything by the calling program, by convention it is set to either a path name of the executable or a name that was found using $PATH.

    Some shells, including bash and ksh, set the environment variable "_" to the full path of the executable before it is executed. In that case you can use getenv("_") to get it. However this is unreliable because not all shells do this, and it could be set to anything or be left over from a parent process which did not change it before executing your program.

    ReplyDelete
  2. You can use argv[0] and analyze the PATH environment variable.
    Look at : A sample of a program that can find itself

    ReplyDelete
  3. If you ever had to support, say, Mac
    OS X, which doesn't have /proc/, what
    would you have done? Use #ifdefs to
    isolate the platform-specific code
    (NSBundle, for example)?


    Yes, writing platform specific code, and then isolating it with #ifdefs is the way to do.
    For example, check out how Poco C++ library does something similar for their Environment class.

    ReplyDelete
  4. AFAIK, no such way. And there is also an ambuiguity: what would you like to get as the answer if the same executable has multiple hard-links "pointing" to it? (Hard-links don't actually "point", they are the same file, just at another place in the FS hierarchy.) Once execve() successfully executes a new binary, all information about its arguments is lost.

    ReplyDelete
  5. Apparently very close from "Programatically retrieving the absolute path of an OS X command-line app".

    ReplyDelete
  6. For Linux/Unix try

    sprintf(cmd, "which %s", argv[0]);
    file = popen(cmd, "r");
    fgets(path, sizeof(path), file);

    ReplyDelete
  7. The absolute value path of a program is in the PWD of the envp of your main function, also there's a function in C called getenv, so there's that.

    ReplyDelete