How can a running application in Linux/*nix determine its own absolute path?
- by Dave Wade-Stein
Suppose you run the application 'app' by typing 'app', rather than its absolute path. Due to your $PATH variable, what actually runs is /foo/bar/app. From inside app I'd like to determine /foo/bar/app. argv[0] is just 'app', so that doesn't help.
I know in Linux I can get do
pid = getpid();
and then look at the /proc/pid/exe softlink, but that doesn't work on other *nix. Is there a more portable way to determine the dir in which the app lives?