Operating System - HP-UX
1828359 Members
3108 Online
109976 Solutions
New Discussion

C++ App determining full path of executable

 
Allan Stirrett
Occasional Advisor

C++ App determining full path of executable

Hi,

I've got a set of apps which I've ported to Linux & Solaris, and I've almost got them completely ported to HP-UX 11i using aCC. There's one last roadblock I've hit.

My apps need to be able to figure out where the executable being run is actually located (since the executable could be started by name via a PATH search, or "./appname", or...) I need to find the COMPLETE path to the running executable from WITHIN the executable.

In Linux I can read the link at /proc/PID/exe, while in Solaris I read the info from /proc/PID/psinfo (where PID is the current process ID from getpid()). Any ideas how I can do the same thing on HP-UX with an aCC app? Thanks in advance.

Allan Stirrett.
6 REPLIES 6
Paula J Frazer-Campbell
Honored Contributor

Re: C++ App determining full path of executable

Hi

Try whereis.

whereis grep | awk '{print $2}'


HTH

Paula
If you can spell SysAdmin then you is one - anon
Paula J Frazer-Campbell
Honored Contributor

Re: C++ App determining full path of executable

Hi

Another option is to sh out to / and do a search for it, holding the result in a string.

or

During the install/porting track where the executable was placed and then transfer that to your programme.

HTH

Paula
If you can spell SysAdmin then you is one - anon
A. Clay Stephenson
Acclaimed Contributor

Re: C++ App determining full path of executable

Hi:

When I've needed to do this in a completely portable way and not rely upon the vagaries of a particular OS, I've found that the most reliable and portable way is to do it myself.

1) Check argv[0] - does it begin with a '/'? If so, you have your answer otherwise,
2) Call getenv() to get PATH and then parse PATH for each component. Test each component directoiry in turn for the existence of the concated pathname string "path_component/argv[0]". The first match is your answer.

This method is a little tedious but it works anywhere.

Regards, Clay
If it ain't broke, I can fix that.
Allan Stirrett
Occasional Advisor

Re: C++ App determining full path of executable

Clay,

That's sort of what I'm thinking might be my only alternative if I can't find a more "neato" way of doing this on HP.

One addition to your suggestion (since I've already implemented this as a temporary solution): If argv[0] does NOT start with a "/" but DOES start with ".", paste a getcwd() onto the front of argv[0] and filter out the "../"'s (ie. /usr/local/myapp/etc (getcwd) + ../../bin/myexe (argv[0]) becomes /usr/local/bin/myexe). Failing that, run through PATH and see where it's located as you mentioned.

The reason I'm still looking for something better is:
1) where I do the executable path lookup is actually deep inside one of my own shard libraries, where I do not have access to argv[0]. Thus, I must rely on any executables using that library to set some GLOBAL argv/argc's for my library to use (ewww!)
2) I have a bad feeling there could be a way that an executable could be launched and NOT be found by the three steps you & I worked out (ie. not /path..., not ../../....., not in PATH)--I hope I'm wrong. The Linux & Solaris methods are a bit more foolproof that way (ie. the info they use is ALWAYS guaranteed to be there).

Thank you very much for the suggestion.

Allan.
Paula J Frazer-Campbell
Honored Contributor

Re: C++ App determining full path of executable

Hi
Futher to Clay's suggestion - once you have found the path to the execuatable, store it so you do not have the search to do again.

HTH

Paula
If you can spell SysAdmin then you is one - anon
Allan Stirrett
Occasional Advisor

Re: C++ App determining full path of executable

Paula,

Yes, DEFINITELY! I do the executable location lookup quite often in the program. Especially if I'm going to do the manual PATH parsing, I don't want to have to do it more than once, so I keep it in a global buffer along with a global bool which keeps track of whether I've done the lookup already. Thanks for the tip!

Allan.