Looking for a way to get the executable's full path and filename. *nix solutions in particular, but Windows solutions, if you have them handy, would also be nice (though I have no immediate use for them). Using the commandline (argv) is okay, but alternatives welcome/preferred.
So far the only way I can think of to get the app name is via argv[0]... and as for the path... the only way I've come up with is to take argv[0] if it has a full path -- and if it doesn't, start searching each and every dir from getenv("PATH") until I find the first matching filename. Not exactly preferable, but I suppose it'd work.
If anyone has a simpler way I'd love to hear it! Thanks in advance.
If all else fails, you might just take note of argv[0] when your program starts and the current working directory (use getcwd()). This is much less reliable, but does generally work anyway...
Excellent! Thanks very much Duoas. The documentation linked to raises a few questions, but I can answer those myself by testing (that HP page is a nightmare!). Much appreciated!
@everyone, re: getcwd(). I thought about this too but I don't think it works if the app is in a PATH directory (ie /usr/share/) and is run via commandline without any path, since the cwd in that case would be whatever directory you were cd'd to at the time (and not necessarily the exe's directory)
One final note: argv[0] is not required to contain any useful information -- it is technically legal (and unfortunately, not all that uncommon) to put something other than the executable name in it.