On Linux, an application can easily get its absolute path by querying
/proc/self/exe. On FreeBSD, it's more involved, since you have to build up a sysctl call:
int mib; mib = CTL_KERN; mib = KERN_PROC; mib = KERN_PROC_PATHNAME; mib = -1; char buf; size_t cb = sizeof(buf); sysctl(mib, 4, buf, &cb, NULL, 0);
but it's still completely doable. Yet I cannot find a way to determine this on OS X for a command-line application. If you're running from within an app bundle, you can determine it by running
[[NSBundle mainBundle] bundlePath], but because command-line applications are not in bundles, this doesn't help.
argv is not a reasonable answer, since, if launched from a symlink,
argv will be that symlink--not the ultimate path to the executable called.
argv can also lie if a dumb application uses an
exec() call and forget to initialize argv properly, which I have seen in the wild.)