get full path of application

R

rh00667

hi all,

i'm confused now. how i can get the full path of an application?

if myapp is in a directory which belongs to PATH, argv[0] gives me the
first token of cmd line, and not the real path of the executed
program.

so if i write anywhere:

% myapp

it runs ok from the file (for example) /usr/local/bin/myapp

but argv[0] returns me

../myapp

but i need

/usr/local/bin/myapp

thanks in advance
 
K

Kai-Uwe Bux

rh00667 said:
wow! any hint?

It's compiler/OS specific. Standard C++ has no means of getting the path of
the executable. You will need to use some platform specific code. What that
code looks like, is a question you will have to ask in a forum for your
particular compiler/OS.


Best

Kai-Uwe Bux
 
A

AnonMail2005

hi all,

i'm confused now. how i can get the full path of an application?

if myapp is in a directory which belongs to PATH, argv[0] gives me the
first token of cmd line, and not the real path of the executed
program.

so if i write anywhere:

% myapp

it runs ok from the file (for example) /usr/local/bin/myapp

but argv[0] returns me

./myapp

but i need

/usr/local/bin/myapp

thanks in advance
Of course this is platform specific but the issue is common across
platforms and any solutions would be useful across platforms. Here's
what we do (which has nothing to do with C++ even though the apps
are written in C++). Note that most of this stuff can be done in C++
but it just seems easier to use a script since one is needed for
launching the apps for other reasons.

1. Start your applications via a script and have it know specifically
where your application lives relative to where the script lives.

2. Detect the full pathname of where your script lives. Here's a
simple bash shell function which sets a variable INSDIR to the
full pathname of where the script lives. The function allows
the script to be started from any directory.

install_directory ()
{
if [ `printf '%c' $0` == "/" ]; then
INSDIR=`dirname $0`;
else
INSDIR=`pwd`"/"`dirname $0`;
fi
}

3. Launch your application with a full pathname by manipulating the
full path name of your script and the relative path of your
executable
to get the full patha name of your excutable.

Hope that helps.
 
A

adrian.hawryluk

i'm confused now. how i can get the full path of an application?
if myapp is in a directory which belongs to PATH, argv[0] gives me the
first token of cmd line, and not the real path of the executed
program.
so if i write anywhere:
it runs ok from the file (for example) /usr/local/bin/myapp
but argv[0] returns me

but i need
/usr/local/bin/myapp

thanks in advance

Of course this is platform specific but the issue is common across
platforms and any solutions would be useful across platforms. Here's
what we do (which has nothing to do with C++ even though the apps
are written in C++). Note that most of this stuff can be done in C++
but it just seems easier to use a script since one is needed for
launching the apps for other reasons.

1. Start your applications via a script and have it know specifically
where your application lives relative to where the script lives.

2. Detect the full pathname of where your script lives. Here's a
simple bash shell function which sets a variable INSDIR to the
full pathname of where the script lives. The function allows
the script to be started from any directory.

install_directory ()
{
if [ `printf '%c' $0` == "/" ]; then
INSDIR=`dirname $0`;
else
INSDIR=`pwd`"/"`dirname $0`;
fi

}

3. Launch your application with a full pathname by manipulating the
full path name of your script and the relative path of your
executable
to get the full patha name of your excutable.

Hope that helps.

You could also traverse the PATH environment variable, but you will
have to know something about the OS you are using.

If you are using UNIX, or Windoze with MiNGW, or Cygwin, you could
call the command which the argv[0] as an argument. But then you would
have to fork with a pipe to set its stdout to which can be a pain, or
you can use system() and redirect it to a named pipe which you can
read from more easliy. i.e. system("which prog > namedPipe").


Adrian
 
R

rh00667

thank to all for comments and ideas!

i tested this for redhat4:

pid = getpid();
sprintf(proc_exe, "/proc/%d/exe", pid);
nr = readlink(proc_exe, buff, BUFFSIZE);
buff[nr]=0;
return buff;

(defs, checks, cosmetics are not shown)

tks!
 
A

adrian.hawryluk

thank to all for comments and ideas!

i tested this for redhat4:

pid = getpid();
sprintf(proc_exe, "/proc/%d/exe", pid);
nr = readlink(proc_exe, buff, BUFFSIZE);
buff[nr]=0;
return buff;

(defs, checks, cosmetics are not shown)

tks!

Very proprietary, will work on only LINUX and I don't even know if it
will work on all of them. :)


Adrian
 
R

rh00667

Very proprietary, will work on only LINUX and I don't even know if it
will work on all of them. :)

yes, of course, i'm agree with you. as you saix, it is not standard
form. i decided to test in my enviroment, and in future make the
"select case" for each os included ....

tks!
 
R

rh00667

yes, of course, i'm agree with you. as you saiD, it is not standard
form. i decided to test in my enviroment, and in future make the
"select case" for each os included ....

a bit simpler, sent by a friend. he tested in CentOs, Debian, RHEL,
Knoppix, Ubuntu, ...

length = readlink("/proc/self/exe", _pwd, _MAXPATH);
_pwd[length] = '\0';
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top