How to get name of exe file

How to get name of exe file

Post by Frank Huben » Tue, 03 Jun 1997 04:00:00




> I want to get name of the executable from a common function. that is to
> say, I dont have access to argv[0]. Is there any way?

   The following program displays its name using the environment
variable "_":

int main()
{
   printf("The name of the executable is %s\n", getenv("_"));
   return 0;

Quote:}  

--
Frank Hubeny

Wheaton, IL 60187
 
 
 

How to get name of exe file

Post by Nitin Data » Tue, 03 Jun 1997 04:00:00


I want to get name of the executable from a common function. that is to
say, I dont have access to argv[0]. Is there any way?

Thanks,

 -- nitin datar

 
 
 

How to get name of exe file

Post by Dave Plon » Tue, 03 Jun 1997 04:00:00



> I want to get name of the executable from a common function. that is to
> say, I dont have access to argv[0]. Is there any way?

There is no portable way to find the executable.  (Most Unix system
allow the executable to be renamed while its' running and some even
allow you to remove it.)

An alternative might be to interrogate what ps(1) would display for
the process in question...  This isn't *necessarily* the same as
argv[0] or the executable name, but often is.  Still, don't depend
on it, but it may be useful for informational purposes.

Example code to read the process table is in Andrew Gierth's
comp.unix.programmer FAQ:

                             <URL: http://www.erlenstar.demon.co.uk/unix/>
                             <URL: http://www.whitefang.com/unix/>

Dave

--

 
 
 

How to get name of exe file

Post by Andy Knig » Wed, 04 Jun 1997 04:00:00



(Boyd Roberts), the following was written:

Quote:> If and only if the program was invoked by that * known as the
> Korn shell.> .

I assume you don't like the Korn Shell then!

--

 
 
 

How to get name of exe file

Post by Andy Knig » Wed, 04 Jun 1997 04:00:00





>> I want to get name of the executable from a common function. that is
>> to say, I dont have access to argv[0]. Is there any way?

> The following program displays its name using the environment
> variable "_":

> int main()
> {
> printf("The name of the executable is %s\n", getenv("_"));
> return 0;
> }

I believe this is a Korn Shell thing and NOT a general UNIX rule.

--

 
 
 

How to get name of exe file

Post by Boyd Rober » Wed, 04 Jun 1997 04:00:00



>   The following program displays its name using the environment variable "_":

>int main()
>{
>   printf("The name of the executable is %s\n", getenv("_"));
>   return 0;
>}  

If and only if the program was invoked by that * known as the Korn shell.

--

``Not only is UNIX dead, it's starting to smell really bad.'' -- rob

 
 
 

How to get name of exe file

Post by R!ch » Wed, 04 Jun 1997 04:00:00



> I want to get name of the executable from a common function. that is to
> say, I dont have access to argv[0]. Is there any way?

A quick hacky way (assuming you've got the source to main()!):

Use a global variable, and set it to argv [0] in main().
horrible, I admit, but...

--
R!ch

If it ain't analogue, it ain't music.
#include <disclaimer.h>                          \\|// - ?
                                                 (o o)
          /==================================oOOo=(_)=oOOo========\

          |  Sun Service Contractor                               |
          |                            Voice: +44 (0)1276 691974  |
          |                                 .oooO                 |
          |                                  (  )  Oooo.          |
          \===================================\ (==(   )==========/
                                               \_)  ) /
                                                   (_/

 
 
 

How to get name of exe file

Post by Dr. Bor » Wed, 04 Jun 1997 04:00:00


.........

Quote:> >int main()
> >{
> >   printf("The name of the executable is %s\n", getenv("_"));
> >   return 0;
> >}

> If and only if the program was invoked by that * known as
> the Korn shell.

----------------------Xterm capture---------------------------

 /home/vlad/C/unix>$ echo $SHELL
/bin/bash

 /home/vlad/C/unix>$ echo $BASH_VERSION
1.14.7(1)

 /home/vlad/C/unix>$ cat getenv2.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
   printf("The name of the executable is %s\n", getenv("_"));
   return 0;
Quote:}


 /home/vlad/C/unix>$ gcc -Wall -O2 getenv2.c -o getenv2

 /home/vlad/C/unix>$ ./getenv2
The name of the executable is ./getenv2
--------------end Xterm capture-------------------------------

Worked for me.

Notice for potential followupers: I am not trying to say anything
about the quality of the program as it can be easily cheated. I
just wanted to show that it can work with other shells too.

--

#include <disclaimer.h>  |     *Good pings come in small packets*
       Vancouver         |     Windows: for IQs smaller than 95
   British Columbia      | SIGSIG -- signature too long (core dumped)

 
 
 

How to get name of exe file

Post by Andreas Kessemeie » Thu, 05 Jun 1997 04:00:00






> >> I want to get name of the executable from a common function. that is
> >> to say, I dont have access to argv[0]. Is there any way?

> > The following program displays its name using the environment
> > variable "_":

> > int main()
> > {
> > printf("The name of the executable is %s\n", getenv("_"));
> > return 0;
> > }

> I believe this is a Korn Shell thing and NOT a general UNIX rule.

GNU's bash can do the same, but other shells can't.
A more portable way is to retrieve the name of the executable from
argv[0]. Under any flavour of *IX i know of, the whole path and filename
of the executable is stored here.

Best regards,
        A. Kessemeier

 
 
 

How to get name of exe file

Post by Boyd Rober » Thu, 05 Jun 1997 04:00:00



>Worked for me.

>Notice for potential followupers: I am not trying to say anything
>about the quality of the program as it can be easily cheated. I
>just wanted to show that it can work with other shells too.

Well, it probably went without saying that bash had that brain-damage in it too.

The point is is that $_ is not a general solution to this problem.

I could right my own shell (lets call it 'rash') which had similar
brain-damage built in, but that would not make this solution _general_.

And normally, general == useful, possibly even _correct_.

--

``Not only is UNIX dead, it's starting to smell really bad.'' -- rob

 
 
 

How to get name of exe file

Post by Andrew Giert » Thu, 05 Jun 1997 04:00:00


 Andreas> A more portable way is to retrieve the name of the
 Andreas> executable from argv[0]. Under any flavour of *IX i know of,
 Andreas> the whole path and filename of the executable is stored
 Andreas> here.

Under *no* flavour of Unix is the whole path and filename of the
executable stored in argv[0].

The value of argv[0] is *ENTIRELY* at the discretion of the program
that performs the exec() call. Shells (by convention) use the name
under which the program was invoked, as the user typed it (i.e. if
the user types 'ls' then argv[0]=="ls", if the user types '/bin/ls'
then argv[0]=="/bin/ls" etc.).

However, consider (assumes perl5):

$ perl -e '$prog="/bin/sleep"; exec $prog "FOOLED-YOU",600;'

Look at the result using ps.

--
Andrew.

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

 
 
 

How to get name of exe file

Post by Andy Knig » Fri, 06 Jun 1997 04:00:00




Quote:> A more portable way is to retrieve the name of the executable from
> argv[0]. Under any flavour of *IX i know of, the whole path and
> filename of the executable is stored here.

Close but not quite right. The name of the executable can be obtained by
determining the basename (final pathname component) of the string
pointed to by argv[0]. The absolute pathname of the executable is NOT
directly available.

--

 
 
 

How to get name of exe file

Post by Andrew Giert » Fri, 06 Jun 1997 04:00:00


 Andy> Close but not quite right. The name of the executable can be
 Andy> obtained by determining the basename (final pathname component)
 Andy> of the string pointed to by argv[0]. The absolute pathname of
 Andy> the executable is NOT directly available.

How many times do I have to say this? argv[0] is an *ARBITRARY STRING*,
that shells usually set to the name of the program as a convention, but
which can be set to whatever the hell you like.

Take a look at execve(). You'll notice that the executable to invoke
and the value of argv[] (including argv[0]) are completely separate
parameters.

--
Andrew.

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

 
 
 

How to get name of exe file

Post by Boyd Rober » Fri, 06 Jun 1997 04:00:00



>Close but not quite right. The name of the executable can be obtained by
>determining the basename (final pathname component) of the string
>pointed to by argv[0].

Of course,

        execl("/bin/broken", (char *)0);

--

``Not only is UNIX dead, it's starting to smell really bad.'' -- rob

 
 
 

How to get name of exe file

Post by Andy Knig » Sat, 07 Jun 1997 04:00:00



(Boyd Roberts), the following was written:

Quote:>> Close but not quite right. The name of the executable can be obtained

  >> by determining the basename (final pathname component) of the
string >> pointed to by argv[0].

Quote:

> Of course,

> execl("/bin/broken", (char *)0);

Indeed, that is correct. I guess I should have qualified my statement by
saying "Assuming argv[0] is not NULL".

I wonder if any unhelpful souls have ever *NOT* set up the first
argument vector!?!!?

--