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
--
name: Anthony Roach
homepage: http://www.electriceyeball.com
PGP: http://www.electriceyeball.com/aroach.asc
--
Kris Luyten
kris ad lumumba dot luc dot ac dot be
http://lumumba.luc.ac.be/kris
>Are you looking for the command "which"?
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
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
> >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
> >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".
--
=================================================================
>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.
--
name: Anthony Roach
homepage: http://www.electriceyeball.com
PGP: http://www.electriceyeball.com/aroach.asc
--
My email address is munged; there's no dots in my name.
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
> >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.
> --
#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
> 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'.
> 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?
Hi all,
I'm looking for a system call that allows me to get the full path of the executable that started the process that's running. I think
on Solaris this is getexecname(). Is there an equivalent under Linux?
Thanks!
2. Any email clients for Windows AND Linux
3. zsh's 'typeset -U path' wipes out path/PATH
4. FTPD
5. Getting the path of an executable
6. kppp
7. how to get process' full path name + executable
9. Getting the full path name in an 'ls'
10. getting 'find' to return absolute paths
11. Getting the executable's name without using argv[0]
12. methods to operate on instances of class ``Path'', was: path utility functions
13. Can't Alter My Path, Can't Find My Path