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 look at the
/proc/self/exe
softlink, but that doesn’t work on other *nix, specifically OS X. Is there a more portable way to determine the dir in which the app lives?
I ended up mimicking the ‘which’ program and looking at each dir in $PATH to see if $dir/app is executable: