How to get the executable's path?

How to get the executable's path?

Post by Anthony Roa » Wed, 25 Jul 2001 02:09:54



I need a function that returns the path name (e.g. /usr/local/bin/foo) to
the executable that it is called from. Any suggestions?

--
name: Anthony Roach
homepage: http://www.electriceyeball.com

PGP: http://www.electriceyeball.com/aroach.asc

 
 
 

How to get the executable's path?

Post by Kris Luyte » Wed, 25 Jul 2001 03:28:23



> I need a function that returns the path name (e.g. /usr/local/bin/foo) to
> the executable that it is called from. Any suggestions?

Are you looking for the command "which"?

--
Kris Luyten
kris ad lumumba dot luc dot ac dot be
http://lumumba.luc.ac.be/kris

 
 
 

How to get the executable's path?

Post by Anthony Roa » Sat, 28 Jul 2001 03:26:50


On Mon, 23 Jul 2001 20:28:23 +0200,



>> I need a function that returns the path name (e.g. /usr/local/bin/foo) to
>> the executable that it is called from. Any suggestions?

>Are you looking for the command "which"?

No. I should have been more explicit. I'm looking for a C function that
will return the absolute path of the program from which it was called. A
prototype for such a function would be:

    const char* GetProgramPath(void);

If this function was called from a program named foo in /usr/local/bin,
then it would return "/usr/local/bin/foo".

Is this not the correct newsgroup for such a question?

--
name: Anthony Roach
homepage: http://www.electriceyeball.com

PGP: http://www.electriceyeball.com/aroach.asc

 
 
 

How to get the executable's path?

Post by Andy Jawors » Sat, 28 Jul 2001 06:29:50


It seems to me like there should be such a function.  If there is one, I do
not know about it.

Here is a klugy solution:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {

       char cmd[20];

       strcpy(cmd, "which ");
       strcat(cmd, argv[0]);
       printf("%d\n", system(cmd));
    }

This code will print the path to its actual name.  Unfortunately the system
command only returns 0/1 status value, so one would have to save the path in
some temporary file and read it back in, if the string was actually needed
inside the code.

Cheers,

Andy

-----------------------
Andy Jaworski


> On Mon, 23 Jul 2001 20:28:23 +0200,


> >> I need a function that returns the path name (e.g. /usr/local/bin/foo)
to
> >> the executable that it is called from. Any suggestions?

> >Are you looking for the command "which"?

> No. I should have been more explicit. I'm looking for a C function that
> will return the absolute path of the program from which it was called. A
> prototype for such a function would be:

>     const char* GetProgramPath(void);

> If this function was called from a program named foo in /usr/local/bin,
> then it would return "/usr/local/bin/foo".

> Is this not the correct newsgroup for such a question?

> --
> name: Anthony Roach
> homepage: http://www.electriceyeball.com

> PGP: http://www.electriceyeball.com/aroach.asc

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

How to get the executable's path?

Post by Chris F.A. Johnso » Sat, 28 Jul 2001 07:13:59



> On Mon, 23 Jul 2001 20:28:23 +0200,


> >> I need a function that returns the path name (e.g. /usr/local/bin/foo) to
> >> the executable that it is called from. Any suggestions?

> >Are you looking for the command "which"?

> No. I should have been more explicit. I'm looking for a C function that
> will return the absolute path of the program from which it was called. A
> prototype for such a function would be:

>     const char* GetProgramPath(void);

> If this function was called from a program named foo in /usr/local/bin,
> then it would return "/usr/local/bin/foo".

Why don't you look at the source for the "which" command?
(I just put it up at http://cfaj.freeshell.org/src/which.c )

--

        =================================================================


 
 
 

How to get the executable's path?

Post by Anthony Roa » Sat, 28 Jul 2001 08:08:33


On Thu, 26 Jul 2001 16:29:50 -0500,


>It seems to me like there should be such a function.  If there is one, I do
>not know about it.

>Here is a klugy solution:

>    #include <stdio.h>

>    int main(int argc, char *argv[])
>    {

>       char cmd[20];

>       strcpy(cmd, "which ");
>       strcat(cmd, argv[0]);
>       printf("%d\n", system(cmd));
>    }

>This code will print the path to its actual name.  Unfortunately the system
>command only returns 0/1 status value, so one would have to save the path in
>some temporary file and read it back in, if the string was actually needed
>inside the code.

Unfortunately "which" only works for programs that are in the user's PATH, so
it won't work in general. Thanks for the help though.

--
name: Anthony Roach
homepage: http://www.electriceyeball.com

PGP: http://www.electriceyeball.com/aroach.asc

 
 
 

How to get the executable's path?

Post by Steve Husto » Sat, 28 Jul 2001 12:55:09



> Unfortunately "which" only works for programs that are in the user's PATH,
> so it won't work in general. Thanks for the help though.

whereis, however, has a hard-coded path, which might help.  It by default
looks for the binary, source and manpages, but you could probably hand-hack
that.

--
My email address is munged; there's no dots in my name.

 
 
 

How to get the executable's path?

Post by Tim Orbake » Sat, 28 Jul 2001 14:36:42


Use the following pseudo code

        if argv[0] does not begin with a '/'
                call getcwd()
                store the result from getcwd() in a buffer
                append the contents of argv[0] to the buffer
        otherwise
                copy argv[0] to the buffer
        done
   use strrchr() to locate the last occurrence of '/' in the buffer
        change this character to '0'.

Notes:
-       This has worked on every version of Linux that I have tried it on
-       Your path may not be pretty. If the program is run as:
                ../bin/program with the current dir as /home/user, you
                        will end up with '/home/user/../bin/program' and not
                        '/home/bin/program' in your buffer. All the C system calls
                        seem to handle either properly. If you want to display it
                        for a user to see, I'll leave the pretty printing as an
                        exercise for you.

Tim


> On Mon, 23 Jul 2001 20:28:23 +0200,


> >> I need a function that returns the path name (e.g. /usr/local/bin/foo) to
> >> the executable that it is called from. Any suggestions?

> >Are you looking for the command "which"?

> No. I should have been more explicit. I'm looking for a C function that
> will return the absolute path of the program from which it was called. A
> prototype for such a function would be:

>     const char* GetProgramPath(void);

> If this function was called from a program named foo in /usr/local/bin,
> then it would return "/usr/local/bin/foo".

> Is this not the correct newsgroup for such a question?

> --
> name: Anthony Roach
> homepage: http://www.electriceyeball.com

> PGP: http://www.electriceyeball.com/aroach.asc

 
 
 

How to get the executable's path?

Post by Jeff Bienstad » Sun, 29 Jul 2001 02:09:00



> On Thu, 26 Jul 2001 16:29:50 -0500,

> >It seems to me like there should be such a function.  If there is one, I
do
> >not know about it.

> >Here is a klugy solution:

> >    #include <stdio.h>

> >    int main(int argc, char *argv[])
> >    {

> >       char cmd[20];

> >       strcpy(cmd, "which ");
> >       strcat(cmd, argv[0]);
> >       printf("%d\n", system(cmd));
> >    }

> >This code will print the path to its actual name.  Unfortunately the
system
> >command only returns 0/1 status value, so one would have to save the path
in
> >some temporary file and read it back in, if the string was actually
needed
> >inside the code.

> Unfortunately "which" only works for programs that are in the user's PATH,
so
> it won't work in general. Thanks for the help though.

> --

Try something like this (this is a quick one-off, so don't judge the
style ->)

    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>

    #include <string.h>

    const char *GetProgramPath(void)
    {
        static char pathbuf[1024];
        char        exe[20];

        memset(pathbuf, 0, 1024);

        /* /proc/<pid>/exe is a link to the executable */

        sprintf(exe, "/proc/%d/exe", getpid());
        readlink(exe, pathbuf, 1024);

        return pathbuf;
    }

---jkb

 
 
 

How to get the executable's path?

Post by Bill » Sun, 29 Jul 2001 23:35:41



> Use the following pseudo code

>    if argv[0] does not begin with a '/'
>            call getcwd()
>            store the result from getcwd() in a buffer
>            append the contents of argv[0] to the buffer
>    otherwise
>            copy argv[0] to the buffer
>    done
>    use strrchr() to locate the last occurrence of '/' in the buffer
>    change this character to '0'.

Am I missing something here... If the executable was in the path and
the full path was not explicitly specified on the command line then
this doesn't work at all, right?
> Notes:
> -  This has worked on every version of Linux that I have tried it on
> -  Your path may not be pretty. If the program is run as:
>            ../bin/program with the current dir as /home/user, you
>                    will end up with '/home/user/../bin/program' and not
>                    '/home/bin/program' in your buffer. All the C system calls
>                    seem to handle either properly. If you want to display it
>                    for a user to see, I'll leave the pretty printing as an
>                    exercise for you.

> Tim


> > On Mon, 23 Jul 2001 20:28:23 +0200,


> > >> I need a function that returns the path name (e.g. /usr/local/bin/foo) to
> > >> the executable that it is called from. Any suggestions?