Getting the path of an executable

Getting the path of an executable

Post by Are Magnus Bruase » Tue, 09 Feb 1999 04:00:00



Hi,

I wonder if someone knows the solution to this:

In some software we are currently developing, we are facing the problem
of figuring out the complete path for the executable from within the
running process itself. That is, if someone says

Unix> app myfile.i

where app is the name of an executable, e.g. located in /usr/local/bin ,
the running process should be able by itself to figure out that  it is
an instance of the executable /ust/local/bin/app. I have browsed all
kinds of man pages but have not seen any such utility. Moreover, we need
this mechanism to be portable across major Unix versions. Ironically,
Windows offer exactly this functionality as one system call, _pgmptr.
Hopefully, there is a similar Unix solution also. Otherwise, I guess the
alternative is to walk along the contents of the PATH variable and check
whether there is an executable matching the name of the currently
running program (as grabbed from the command line).

Any input is most welcome. Since I'm not a regular reader of this
newsgroup, I would appreciate that any answers to be copied to

Regards

Are Magnus Bruaset

--
----------------------------------------------------------------------

   Technical Director           Phone : +47 22 06 76 10
   Numerical Objects AS         Mobile: +47 92 61 00 63
   P.O. Box 124 Blindern        Fax   : +47 22 06 73 50
   N-0314 Oslo, Norway          WWW   : http://www.nobjects.com

 
 
 

Getting the path of an executable

Post by Ian Collin » Tue, 09 Feb 1999 04:00:00



> Hi,

> I wonder if someone knows the solution to this:

> In some software we are currently developing, we are facing the problem
> of figuring out the complete path for the executable from within the
> running process itself. That is, if someone says

> Unix> app myfile.i

> where app is the name of an executable, e.g. located in /usr/local/bin ,
> the running process should be able by itself to figure out that  it is
> an instance of the executable /ust/local/bin/app. I have browsed all
> kinds of man pages but have not seen any such utility. Moreover, we need
> this mechanism to be portable across major Unix versions. Ironically,
> Windows offer exactly this functionality as one system call, _pgmptr.
> Hopefully, there is a similar Unix solution also. Otherwise, I guess the
> alternative is to walk along the contents of the PATH variable and check
> whether there is an executable matching the name of the currently
> running program (as grabbed from the command line).

argv[0] ?

        Ian Collins.

 
 
 

Getting the path of an executable

Post by Andrew Giert » Tue, 09 Feb 1999 04:00:00


 [finding location of executable]
 Are> I have browsed all kinds of man pages but have not seen any such
 Are> utility.

That's because there isn't one.

 Are> Hopefully, there is a similar Unix solution also. Otherwise, I
 Are> guess the alternative is to walk along the contents of the PATH
 Are> variable and check whether there is an executable matching the
 Are> name of the currently running program (as grabbed from the
 Are> command line).

Which doesn't necessarily work either.

The question is: do you want a mechanism that works some or most of
the time, or do you need it to work *all* the time? If the latter,
then give up now. (Most significant is the question of whether you're
bothered about the user deliberately deceiving you.)

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

Getting the path of an executable

Post by Randy Kaelb » Tue, 09 Feb 1999 04:00:00



: >
: > where app is the name of an executable, e.g. located in /usr/local/bin ,
: > the running process should be able by itself to figure out that  it is
: > an instance of the executable /ust/local/bin/app. I have browsed all
: > kinds of man pages but have not seen any such utility. Moreover, we need
: > this mechanism to be portable across major Unix versions. Ironically,
: > Windows offer exactly this functionality as one system call, _pgmptr.
: > Hopefully, there is a similar Unix solution also. Otherwise, I guess the
: > alternative is to walk along the contents of the PATH variable and check
: > whether there is an executable matching the name of the currently
: > running program (as grabbed from the command line).
: >

You should check the FAQ for the answer. The FAQ rightly asks "Why do you
want to do this in the first place?". Most of the answers to this question
usually point to a better solution.

Maybe that's a good thing to do with Windows, but in Unix, the need to
know just where your executable is can be a liability.

Randy

 
 
 

Getting the path of an executable

Post by Nate Eldredg » Tue, 09 Feb 1999 04:00:00



> Hi,

> I wonder if someone knows the solution to this:

> In some software we are currently developing, we are facing the problem
> of figuring out the complete path for the executable from within the
> running process itself. That is, if someone says

> Unix> app myfile.i

> where app is the name of an executable, e.g. located in /usr/local/bin ,
> the running process should be able by itself to figure out that  it is
> an instance of the executable /ust/local/bin/app. I have browsed all
> kinds of man pages but have not seen any such utility. Moreover, we need
> this mechanism to be portable across major Unix versions. Ironically,
> Windows offer exactly this functionality as one system call, _pgmptr.
> Hopefully, there is a similar Unix solution also. Otherwise, I guess the
> alternative is to walk along the contents of the PATH variable and check
> whether there is an executable matching the name of the currently
> running program (as grabbed from the command line).

See the FAQ for this newsgroup, section 1.14.  The answer, basically, is
"Don't".
--

Nate Eldredge