flat assembler
Message board for the users of flat assembler.

flat assembler > Linux > Determining executable's directory

Author
Thread Post new topic Reply to topic
ronware



Joined: 08 Jan 2004
Posts: 179
Location: Israel
It's possible to find the executable file's location by looking at "argv[0]" which is just the second item on the stack when the ELF exe starts up under Linux.

However, my experimentation indicates that the value given only contains the path to the file if it was explicitly given, but if the exe was found on $PATH somewhere, it will not be given.

So for example, if you have the exe 'myexe' then the 'argv[0]' will be:

if myexe is in the $PATH and you just type 'myexe', you will get 'myexe'
if you type the path to it, e.g. '/bin/myexe' you will get '/bin/myexe'.

Does anyone know how one can get the actual directory (fully qualified) where the exe being executed resides?
Post 19 May 2005, 17:14
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
Hmm, I saw a post about this on a game developing oriented site a while ago (probably flipcode?), and it seemed that it was all but a trivial task. There was even a developer from loki soft (I think) who griped about it...
Post 19 May 2005, 19:34
View user's profile Send private message Visit poster's website Reply with quote
ronware



Joined: 08 Jan 2004
Posts: 179
Location: Israel
Yeah, I was thinking I could probably get it out of /proc ... I really don't want to do that if I don't have to.
Post 19 May 2005, 19:46
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger Reply with quote
ronware



Joined: 08 Jan 2004
Posts: 179
Location: Israel
In fact, /proc/<yourpid>/exe is a symlink to the executable. Now does anyone know just how to resolve a symlink to the file it points to? I know it's not hard, I just don't know how to do it Smile
Post 19 May 2005, 20:12
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger Reply with quote
ronware



Joined: 08 Jan 2004
Posts: 179
Location: Israel
The 'net is great! "readlink" is the function.

First 'getpid' to get your process id, then readlink on '/proc/yourpid/exe' and you're done. 'readlink' returns the length of the string return, or -1 on failure.
Post 19 May 2005, 20:15
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.