By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 1,922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

get full path of application

P: n/a
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

Mar 6 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
rh00667 wrote:
hi all,

i'm confused now. how i can get the full path of an application?
You can't.
Mar 6 '07 #2

P: n/a
On Mar 6, 2:41 pm, Rolf Magnus <ramag...@t-online.dewrote:
You can't.
wow! any hint?

or must i navegate PATH finding the first pathdir/myapp ??????

Mar 6 '07 #3

P: n/a
On Mar 6, 2:41 pm, Rolf Magnus <ramag...@t-online.dewrote:
You can't.
wow! any hint?

or must i navegate PATH finding the first pathdir/myapp ??????

Mar 6 '07 #4

P: n/a
In article <11**********************@n33g2000cwc.googlegroups .com>,
rh*****@gmail.com says...
On Mar 6, 2:41 pm, Rolf Magnus <ramag...@t-online.dewrote:
You can't.

wow! any hint?
Sure: any real answer is platform specific, so you need to ask where
your platform is topical.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 6 '07 #5

P: n/a
rh00667 wrote:
On Mar 6, 2:41 pm, Rolf Magnus <ramag...@t-online.dewrote:
>You can't.

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

Mar 6 '07 #6

P: n/a
On Mar 6, 8:16 am, "rh00667" <rh00...@gmail.comwrote:
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.

Mar 6 '07 #7

P: n/a
On Mar 6, 11:10 am, "AnonMail2...@gmail.com" <AnonMail2...@gmail.com>
wrote:
On Mar 6, 8:16 am, "rh00667" <rh00...@gmail.comwrote:
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.
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

Mar 6 '07 #8

P: n/a
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!

Mar 6 '07 #9

P: n/a
On Mar 6, 2:20 pm, "rh00667" <rh00...@gmail.comwrote:
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

Mar 6 '07 #10

P: n/a
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!

Mar 6 '07 #11

P: n/a
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';
Mar 7 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.