Get app path+name

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.
Doesn't an environment variable hold the working directory?
Last edited on
argv[0] if argv[0] is an absolute path, otherwise getcwd() + argv[0] should do the trick.
On Windows, it is fairly simple:
1
2
3
4
5
6
7
8
#include <string>
#include <windows.h>

std::string getexepath()
  {
  char result[ MAX_PATH ];
  return std::string( result, GetModuleFileName( NULL, result, MAX_PATH ) );
  }
http://www.google.com/search?btnI=1&q=msdn+GetModuleFileName

On Linux, use the proc filesystem:
1
2
3
4
5
6
7
8
9
10
#include <string>
#include <limits.h>
#include <unistd.h>

std::string getexepath()
  {
  char result[ PATH_MAX ];
  ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
  return std::string( result, (count > 0) ? count : 0 );
  }
http://linux.die.net/man/5/proc

On *nix, it isn't quite as simple.

HP-UX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>

#include <limits.h>
#define _PSTAT64
#include <sys/pstat.h>
#include <sys/types.h>
#include <unistd.h>

std::string getexepath()
  {
  char result[ PATH_MAX ];
  struct pst_status ps;

  if (pstat_getproc( &ps, sizeof( ps ), 0, getpid() ) < 0)
    return std::string();

  if (pstat_getpathname( result, PATH_MAX, &ps.pst_fid_text ) < 0)
    return std::string();

  return std::string( result );
  }
http://www.docs.hp.com/en/B9106-90009/pstat.2.html

As for others, I don't know. Here's a link that might get you started:
http://www.faqs.org/faqs/unix-faq/faq/part3/section-10.html


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...

I have no idea how to do it on a Mac.

Hope this helps.
Last edited on
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)

Thanks everyone. You're a big help as always.
Yes, if your application is in a PATH directory you'll have to search the path.

Here's something I just found:
My current list of the ways it can done on Unix systems is:

Linux
:
/proc/<pid>/exe

Solaris:
/proc/<pid>/object/a.out (filename only)
/proc/<pid>/path/a.out (complete pathname)

*BSD (and maybe Darwing too):
/proc/<pid>/file

They're all symbolic links as randyding pointed out. It's better to use the numeric pid and avoid shortcuts such as "self" that aren't portable.
http://www.linuxquestions.org/questions/programming-9/unix-function-call-to-get-executables-path-470000/


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.
Topic archived. No new replies allowed.