Say how to let a program A prints its binary location?
Thanks.
Say how to let a program A prints its binary location?
Thanks.
Last edited by meili100; 04-09-2008 at 05:05 PM.
There's no real standard way of doing that, but argv[0] contains the name of the application, and in most shell's the path is included - however, that's no guarantee, as it's the code calling exec() [or one of it's close relatives] that is responsible for filling in argv[0].
The only other option is to search in the path [which of course can also fail if the application was started with a path, but exec called without it, and the path does not contain the executable's directory].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
On windows you can use GetModuleFileName().
That gives you the directory where the application is currently running, but not the location out of which the executable came.
I agree that the question is not entirely clear. If I'm doing:
do you want to know the current directory (/home/me), or the /bin that is the executable /bin/ls's directory [and we're assuming that ls isn't built into the shell - which happens sometimes].Code:/home/me $ ls
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Hmm -- I just realized the question is unclear. The subject line says "working dir" but in the body of your post you ask for the binary's location. Which one of these are you actually interested in?
Does anybody know if there is a dirname() function?
Sorry for the misleading, I mean the binary dir.
In the "/home/me $ ls" case the output shall be /bin/ls
In that case, the solution is messy and somewhat platform dependent. The value of argv[0] is a relative or absolute path to the binary itself. If it is absolute, then you already have your answer. If it is relative, then it is relative to the current directory, so you must get the curr dir and then walk upward until you reach the root of the filesystem to construct the full path.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I found /proc/self/exe is linked to the binary location
for example :
/proc/self/exe -> /bin/ls
But the question how to get the "/bin/ls" string?
A read from /proc/self/exe perhaps?
Of coruse, if you want to do that with /bin/ls, then you need to recompile the source of /bin/ls - if you want to find '/home/me/myproject/bin/myprog', then you just need to open and read /proc/self/exe in your "myprog.c" or whatever your source file is.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thanks, but
is a link.Code:/proc/self/exe -> /home/me/myproject/bin/myprog
How to get the
Code:/home/me/myproject/bin/myprog