How to get the full path of an executable program from within that program itself

How to get the full path of an executable program from within that program itself

Post by Vish Viswanatha » Fri, 16 Oct 1998 04:00:00



Hi,

I need to know a way of finding out the directory (full path) from where a
executable gets loaded programmatically. I think I can get some of this info
from argv[0], though it gives only the relative path. I was told that
'which' command can print out the full path of the executable. However, I
want to do this from 'C' program. Any help in this area would be
appreciated.

Thanks

Vish


 
 
 

How to get the full path of an executable program from within that program itself

Post by Lee Alle » Fri, 16 Oct 1998 04:00:00



>I need to know a way of finding out the directory (full path) from where a
>executable gets loaded programmatically. I think I can get some of this
info
>from argv[0], though it gives only the relative path. I was told that
>'which' command can print out the full path of the executable. However, I
>want to do this from 'C' program. Any help in this area would be
>appreciated.

If you don't get a satisfactory answer, post in comp.unix.programmer.
Those guys will know how to do this, and that's the right forum.

-Lee Allen

 
 
 

How to get the full path of an executable program from within that program itself

Post by Joe Ze » Fri, 16 Oct 1998 04:00:00



> I was told that
>'which' command can print out the full path of the executable. However, I
>want to do this from 'C' program. Any help in this area would be
>appreciated.

If nothing else, you can have your program execute which, with output
redirected to a file.

---
Joe Zeff
     The Guy With the Sideburns

If you can't play with words, what good are they?
http://www.lasfs.org

 
 
 

How to get the full path of an executable program from within that program itself

Post by Mike McDona » Sat, 17 Oct 1998 04:00:00




Quote:> Hi,

> I need to know a way of finding out the directory (full path) from where a
> executable gets loaded programmatically. I think I can get some of this info
> from argv[0], though it gives only the relative path. I was told that
> 'which' command can print out the full path of the executable. However, I
> want to do this from 'C' program. Any help in this area would be
> appreciated.

  which won't always give you the correct results. You need to first look at
argv[0]. If it has any / cjaracters in it, then that's the path to the
program. If it doesn't, then you have to search thru the PATH environment list
and look for an executable by the name of argv[0]. (This is latter search is
what which does.)

  Mike McDonald

 
 
 

How to get the full path of an executable program from within that program itself

Post by Gérard Milmeist » Sat, 17 Oct 1998 04:00:00




>>I need to know a way of finding out the directory (full path) from where a
>>executable gets loaded programmatically. I think I can get some of this
>info
>>from argv[0], though it gives only the relative path. I was told that
>>'which' command can print out the full path of the executable. However, I
>>want to do this from 'C' program. Any help in this area would be
>>appreciated.

If you want to find out the location of libraries and support files
needed by your executable, hard code it into your source file, but let
the user decide at compile and installation time where the files
should be located. It's best to use autoconf for this type of
installation.

--

Tannenrauchstrasse 35
8038 Zrich
Switzerland
+41 1 481 52 48

 
 
 

How to get the full path of an executable program from within that program itself

Post by Boris Tobotra » Sat, 17 Oct 1998 04:00:00


Quote:>>>>> "Vish" == Vish Viswanathan writes:

Vish> Hi, I need to know a way of finding out the directory (full path)
Vish> from where a executable gets loaded programmatically.

        No way.
--
        Best regards, -- Boris.

 
 
 

How to get the full path of an executable program from within that program itself

Post by Han-Wen Nienhu » Sat, 17 Oct 1998 04:00:00




Quote:>>>>>> "Vish" == Vish Viswanathan writes:

>Vish> Hi, I need to know a way of finding out the directory (full path)
>Vish> from where a executable gets loaded programmatically.

>    No way.

If you don't care about portability, you can look at where
/proc/self/exe is pointing to

--


      http://www.cs.uu.nl/people/hanwen/lilypond/index.html

 
 
 

How to get the full path of an executable program from within that program itself

Post by Joerg Beye » Sat, 17 Oct 1998 04:00:00



> Hi,

> I need to know a way of finding out the directory (full path) from where a
> executable gets loaded programmatically. I think I can get some of this info
> from argv[0], though it gives only the relative path. I was told that
> 'which' command can print out the full path of the executable. However, I
> want to do this from 'C' program. Any help in this area would be
> appreciated.

No chance to do it portable. Really. Maybe you post _why_ you want
to find out where the executable lives and we can work something out.

        Joerg
--
There are two ways of constructing a software design. One way is
to make it so simple that there are obviously no deficiencies
and the other is to make it so complicated that there are no
obvious deficiencies.                            --  C A R Hoare

 
 
 

How to get the full path of an executable program from within that program itself

Post by Steffen Klu » Sat, 17 Oct 1998 04:00:00




>Hi,

>I need to know a way of finding out the directory (full path) from where a
>executable gets loaded programmatically. I think I can get some of this info
>from argv[0], though it gives only the relative path. I was told that
>'which' command can print out the full path of the executable. However, I
>want to do this from 'C' program. Any help in this area would be
>appreciated.

I quickly checked in perl, it seems to prefer a relative path if
it doesn't have to put ../ in front, and an absolute path
otherwise.  Anyway, you can always check whether you got back an
absolute pathname or not, and prepend it with the cwd if
required.

Hope this helps
Steffen.

--

Fujitsu Australia Ltd
Keywords: photography, Mozart, UNIX, Islay Malt, dark skies
--

 
 
 

How to get the full path of an executable program from within that program itself

Post by Joerg Beye » Sat, 17 Oct 1998 04:00:00





> >Hi,

> >I need to know a way of finding out the directory (full path) from where a
> >executable gets loaded programmatically. I think I can get some of this info
> >from argv[0], though it gives only the relative path. I was told that
> >'which' command can print out the full path of the executable. However, I
> >want to do this from 'C' program. Any help in this area would be
> >appreciated.

> I quickly checked in perl, it seems to prefer a relative path if
> it doesn't have to put ../ in front, and an absolute path
> otherwise.  Anyway, you can always check whether you got back an
> absolute pathname or not, and prepend it with the cwd if
> required.

I think this does not work if the executable lives somewhere in
the path. E.g. my programm is called "prg", libes in /usr/local/bin
an I am in my ~ directory (not in /usr/local/bin). argv[0] == "prg"
cwd == "~jbeyer". (Yes, /usr/local/bin is in my PATH). Agreed?

        Joerg
--
There are two ways of constructing a software design. One way is
to make it so simple that there are obviously no deficiencies
and the other is to make it so complicated that there are no
obvious deficiencies.                            --  C A R Hoare

 
 
 

How to get the full path of an executable program from within that program itself

Post by Steffen Klu » Sat, 17 Oct 1998 04:00:00




Quote:>I think this does not work if the executable lives somewhere in
>the path. E.g. my programm is called "prg", libes in /usr/local/bin
>an I am in my ~ directory (not in /usr/local/bin). argv[0] == "prg"
>cwd == "~jbeyer". (Yes, /usr/local/bin is in my PATH). Agreed?

No, as I said, I always get some path in argv[0] ($0 in perl).
It is a relative path if the full path contains cwd, or an
absolute path otherwise. I'm not sure now whether perl plays
any tricks with $0, but after all this is a shell feature
(it's the shell that assembles the arg list for execve).
I was using ksh, btw.

I'll try it in C and let you know...

Cheers
Steffen.

--

Fujitsu Australia Ltd
Keywords: photography, Mozart, UNIX, Islay Malt, dark skies
--

 
 
 

How to get the full path of an executable program from within that program itself

Post by Jeremy Mathe » Sat, 17 Oct 1998 04:00:00






>>>>>>> "Vish" == Vish Viswanathan writes:

>>Vish> Hi, I need to know a way of finding out the directory (full path)
>>Vish> from where a executable gets loaded programmatically.

>>        No way.

>If you don't care about portability, you can look at where
>/proc/self/exe is pointing to

This is good (This is, after all, a Linux newsgroup), but what that
gives you is an inode number.  This seems to be correct in my testing.

However, I see no "direct" way to convert the inode number back to a
filename.  I think the "standard" way is something like: find / -inum X
But that is obviously a "haystack" method.

 
 
 

How to get the full path of an executable program from within that program itself

Post by Robert Nicho » Sat, 17 Oct 1998 04:00:00



:I need to know a way of finding out the directory (full path) from where a
:executable gets loaded programmatically. I think I can get some of this info
:from argv[0], though it gives only the relative path. I was told that
:'which' command can print out the full path of the executable. However, I
:want to do this from 'C' program. Any help in this area would be
:appreciated.

It can't be done, at least not 100% reliably.  If argv[0] is a simple
name you can try to emulate the PATH search performed by pexec(), but
you have no assurance that your program was invoked that way or that
your program has the necessary rights to traverse those paths.  Some
shells will place only the final component of the path in argv[0] even
when the user types a full absolute path.  Also, if the final component
of the path is a hard or symbolic link to your executable you won't
locate the directory where it is really installed.  With a some extra
work you could handle the symbolic link case, but not the hard link.
And finally, a path that was vaild when your program was invoked might
no longer be valid by the time you get around to trying to reconstruct
it.

About the best you can do is to try to emulate the PATH search.  That
will work most of the time, but you'll want to have an environment
variable that can be used to give you the needed information in cases
where the PATH search fails.

--


PGP public key 1024/9A9C7955
Key fingerprint = 2F E5 82 F8 5D 06 A2 59  20 65 44 68 87 EC A7 D7

 
 
 

How to get the full path of an executable program from within that program itself

Post by Ernesto Hernandez-Novi » Sat, 17 Oct 1998 04:00:00


: Hi,

: I need to know a way of finding out the directory (full path) from where a
: executable gets loaded programmatically. I think I can get some of this info
: from argv[0], though it gives only the relative path. I was told that
: 'which' command can print out the full path of the executable. However, I
: want to do this from 'C' program. Any help in this area would be
: appreciated.

If you want to do it in C, you'll have to do what "which" does. Searches
each directory listed in the environment variable PATH, tacks the name of
the executable to the end of it, and checks to see if that full name exists
as an executable. Very easy

#!/bin/bash
# Poor man's which

IFS=:
for dir in $PATH
do
  if [ -x $dir/$1 ]
  then
    echo $dir/$1
    exit 0
  fi
done
echo $i is not in your PATH
exit 1

--

Just another Unix/Perl/Java hacker running Linux 2.0.35
One thing is to be the most popular, and another is to be the best.
Unix: Live free or die!

 
 
 

How to get the full path of an executable program from within that program itself

Post by David Z. Maz » Sun, 18 Oct 1998 04:00:00






  BT> "Vish" == Vish Viswanathan writes:
  BT>
   Vish> Hi, I need to know a way of finding out the directory (full
   Vish> path) from where a executable gets loaded programmatically.
  BT>
  BT> No way.
 HWN>
 HWN> If you don't care about portability, you can look at where
 HWN> /proc/self/exe is pointing to
JM>
JM> This is good (This is, after all, a Linux newsgroup), but what
JM> that gives you is an inode number.  This seems to be correct in my
JM> testing.

Ah, and on my system it gives you a filename.  The basic answer here
is that what /proc/self/exe links to differs between the 2.0.x and
2.1.x kernels, so using it isn't even portable between different
versions of Linux.  And different Unices have a different interface
under /proc, or possibly no /proc at all.  Depending on this is a
*total* portability lose.

JM> However, I see no "direct" way to convert the inode number back to
JM> a filename.  I think the "standard" way is something like: find /
JM> -inum X But that is obviously a "haystack" method.

Given a particular inode number, an arbitrary number of directory
entries on that filesystem can point to that inode.  AFAIK there isn't
a good way to get from an inode number to the directory entries that
point to it, though.

--
 _____________________________
/                             \       "Dad was reading a book called
|          David Maze         |     _Schroedinger's Kittens_.  A*

| http://www.veryComputer.com/|               -- Abra Mitchell
\_____________________________/

 
 
 

1. How to get the full path of an executable from within it

Hi,

I need to know a way of finding out the directory (full path) from where a
executable gets loaded programmatically. I think I can get some of this info
from argv[0], though it gives only the relative path. I was told that
'which' command can print out the full path of the executable. However, I
want to do this from 'C' program. Any help in this area would be
appreciated.

Thanks

Vish


2. How to tell "imake" that the system has gcc ?

3. How do you find pid of a program (in the program itself)

4. Help! I have an old hitachi cdrom interface

5. put a running program in background in the program itself

6. via82cxxx recording bug

7. Finding a program's path from within

8. Redhat no binaries for 2.1.1?

9. How to run other programs within a C/C++ program?

10. REALLY SIMPLE: how to get PID of C program within the program

11. pascal, tput, calling programs from within a program.

12. How can I run a program without writing full path?

13. How do I find full path to program in 'c'