Complete Path of Executable

Complete Path of Executable

Post by Surya Kir » Tue, 30 Apr 2002 21:29:46



Hi,
I'd like to know how to get the complete path of an executable file
using a C program. Ideally i need the following.
I've an executable file say "executable" in /usr/local/bin (or some
other directory). I'd like to get the complete path
(/usr/local/bin/executable) using a c-program.
how can i get that.
thanks in advance
surya
 
 
 

Complete Path of Executable

Post by John Gordon,217-352-6511x7418,CEERD-CF- » Tue, 30 Apr 2002 22:24:38



> Hi,
> I'd like to know how to get the complete path of an executable file
> using a C program. Ideally i need the following.
> I've an executable file say "executable" in /usr/local/bin (or some
> other directory). I'd like to get the complete path
> (/usr/local/bin/executable) using a c-program.
> how can i get that.

generally, you can do this by looking at argv[0] and $PATH.  but there is
no guaranteed reliable method.

---
John Gordon                  "No Silicon Heaven?  Preposterous!  Where would


 
 
 

Complete Path of Executable

Post by Chuck Dillo » Tue, 30 Apr 2002 23:12:56



> Hi,
> I'd like to know how to get the complete path of an executable file
> using a C program. Ideally i need the following.
> I've an executable file say "executable" in /usr/local/bin (or some
> other directory). I'd like to get the complete path
> (/usr/local/bin/executable) using a c-program.
> how can i get that.
> thanks in advance
> surya

See http://www.faqs.org/faqs/unix-faq/programmer/faq/ item "1.14 How can I find
a process' executable file?"

-- ced

--
Chuck Dillon
Senior Software Engineer
Accelrys Inc., a subsidiary of Pharmacopeia, Inc.

 
 
 

Complete Path of Executable

Post by RSmith65 » Wed, 01 May 2002 08:15:33


I'm no whiz, but:

if( argv[0][0]  == '/' )
        fullpath = argv[0];
else
        fullpath = $PWD + argv[0];

PWD should be in the environment.

Roger

 
 
 

Complete Path of Executable

Post by Malte Starosti » Wed, 01 May 2002 08:19:49



Quote:> I'm no whiz, but:

> if( argv[0][0]  == '/' )
> fullpath = argv[0];
> else

else if ( strchr( argv[0], '/' ) )
Quote:> fullpath = $PWD + argv[0];

else fullpath = search_in_$PATH( argv[0] );

Quote:> PWD should be in the environment.

Better use getcwd() from a program...

-Malte

 
 
 

Complete Path of Executable

Post by David Schwart » Wed, 01 May 2002 08:35:31




> > Hi,
> > I'd like to know how to get the complete path of an executable file
> > using a C program. Ideally i need the following.
> > I've an executable file say "executable" in /usr/local/bin (or some
> > other directory). I'd like to get the complete path
> > (/usr/local/bin/executable) using a c-program.
> > how can i get that.

> generally, you can do this by looking at argv[0] and $PATH.  but there is
> no guaranteed reliable method.

        1) There may not be a path -- the executable could have been deleted.

        2) The program could have been renamed and a new executable taken its
place that's not the code you're actually running.

        3) The program could have more than one name in the filesystem.

        Since the information you are looking for doesn't make sense, you
probably should explain your actual problem or requirement. Odds are
there's a way to meet that has nothing to do with finding the path of an
executable.

        DS

 
 
 

Complete Path of Executable

Post by GoogleF » Wed, 01 May 2002 17:44:25



> I'm no whiz, but:

> if( argv[0][0]  == '/' )
>    fullpath = argv[0];
> else
>    fullpath = $PWD + argv[0];

> PWD should be in the environment.

> Roger

NEVER EVER rely on PWD. I used to do that and 3rd party apps (I think
clearcase might have been the culprit) can do a chdir() internally
without updating PWD so by the time you get invoked PWD and getcwd()
dont agree.

PWD was a timebomb in Ksh waiting to happen and it should be removed -
forcing reliant apps to break and be fixed :-) (Its potentially a
security whole if any app which is setuid relies on it).

 
 
 

Complete Path of Executable

Post by Mohun Biswa » Wed, 01 May 2002 21:13:59



> NEVER EVER rely on PWD. I used to do that and 3rd party apps (I think
> clearcase might have been the culprit) can do a chdir() internally
> without updating PWD so by the time you get invoked PWD and getcwd()
> dont agree.

Forget third-party apps. What do you think happens if you run a csh from
within a ksh and then cd within the csh?

Quote:> PWD was a timebomb in Ksh waiting to happen and it should be removed

That's too strong. It's a useful feature which is subject to misuse by
people who don't understand it. The same could be said for Unix as a
whole.

Mohun

 
 
 

Complete Path of Executable

Post by Dan Merc » Thu, 02 May 2002 00:44:54






>> > Hi,
>> > I'd like to know how to get the complete path of an executable file
>> > using a C program. Ideally i need the following.
>> > I've an executable file say "executable" in /usr/local/bin (or some
>> > other directory). I'd like to get the complete path
>> > (/usr/local/bin/executable) using a c-program.
>> > how can i get that.

>> generally, you can do this by looking at argv[0] and $PATH.  but there is
>> no guaranteed reliable method.

>    1) There may not be a path -- the executable could have been deleted.

Most unixes don't allow you to delete a text file while in use -
you have to rename it to replace it.

Quote:

>    2) The program could have been renamed and a new executable taken its
> place that's not the code you're actually running.

>    3) The program could have more than one name in the filesystem.

argv[0] doesn't have to even point to the name - we have a program
that runs as a daemon that calls other programs - their actual names
are different than the argv[0] we pass - their virtual names are
used to access X Resources.  Why this was done originally is a decision
lost in antiquity.

That all having been said,  it is still a reasonable question.
Applixware, for instance,  uses the path to the applix executable to
determine the base path of its software.  On HP-UX,  you would combine
findpath and realpath:

      $ cat src/qe.c
      #include <limits.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <libgen.h>

      main (int argc, char **argv)
      {
      char *path;
      char pb[PATH_MAX+1];

      if (NULL == (path = realpath(argv[0],pb)))
         {
         if (NULL == (path = pathfind (getenv("PATH"), argv[0], "x")))
            printf("Can't trace real path to %s\n", argv[0]);
         else
            printf("real path of %s is %s\n",argv[0],realpath(path,pb));
         }
      else
         {
         printf("real path of %s is %s\n", argv[0], path);
         }
      }
      $ cc -o src/qe src/qe.c -lgen
      $ qe
      ksh: qe:  not found
      $ src/qe
      real path of src/qe is /home/dam/src/qe
      $ bin/../bin2/../src/qe
      real path of bin/../bin2/../src/qe is /home/dam/src/qe
      $ ln -s ~/src/qe .
      $ ./qe
      real path of ./qe is /home/dam/src/qe
      $ export PATH=$PATH:~/src
      $ rm -f qe
      $ qe
      real path of qe is /home/dam/src/qe

My Linux has a realpath,  but no pathfind.  But a Google search
indicates it is available on Linux and other OS's.

--
Dan Mercer

Quote:

>    Since the information you are looking for doesn't make sense, you
> probably should explain your actual problem or requirement. Odds are
> there's a way to meet that has nothing to do with finding the path of an
> executable.

>    DS

Opinions expressed herein are my own and may not represent those of my employer.
 
 
 

Complete Path of Executable

Post by Mohun Biswa » Thu, 02 May 2002 01:12:45



> My Linux has a realpath,  but no pathfind.  But a Google search
> indicates it is available on Linux and other OS's.

Solaris has getexecname() and Linux, at least 2.4.x versions, has
/proc/self/exe. Both of these have the caveats mentioned previously but
they're "as good as it gets" for their platforms.

Mohun