Oliver Charlesworth Oliver Charlesworth - 1 month ago 6
C Question

Can an executable discover its own path? (Linux)


Possible Duplicate:

how to find the location of the executable in C




I would like an executable to be able to discover its own path; I have a feeling that the answer is "you can't do this", but I would like this to be confirmed!

I don't think I can use
getcwd()
, because I might not be executing it from the same directory. I don't think I can use
argv[0]
, because that is based on the string that's used to execute it. Are there any other options?

Rationale

The real problem is that I'd like to place an executable somewhere on a filesystem, and place a default config file alongside it. I want the executable to be able to read its config file at runtime, but I don't want to hardcode this location into the executable, nor do I want the user to have to set environment variables. If there's a better solution to this situation, I'm all ears...

Answer

The file /proc/self/exe is a simlink to the currently running executable.