473,461 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

get full path of application

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
11 8237
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: (Pete Cresswell) | last post by:
I've got a .BAT file that I use for executing various MS Access apps that I wrote way back in the days of 2.0. It's evolved over time, but it still contains a number of possible paths to...
2
by: R A | last post by:
Hi, I have a Windows Service C# application. How can I get the full path of the application exe? Thanks, Ronen
2
by: Sridhar | last post by:
Hi, I have a web form where it has a <input type=file id=file1> control. I have an Upload button to upload the file. WHen I click on browse and select one file, it is showing the full file path...
4
by: Dots | last post by:
I have a class library with a method called getpath(). I want to be able to get the full path of a folder and write some files to the (my_files_dir) folder. A console application will use this...
4
by: tommaso.gastaldi | last post by:
Hi friends I was in the need to find a sort of "definitive" :) solution to transform a virtual path such as "~/MyDir/MyFile into a full web address. In particular I needed it * within web...
5
by: =?Utf-8?B?TWFydHluIEZld3RyZWxs?= | last post by:
Hi there. I posted an earlier issue under the name "That assembly does not allow partially trusted callers" but have now identified what the issue is. As explained before I am working in...
3
by: Ken | last post by:
I have application files under "C:\$\TestApp". there is a test.xml file under "C:\$\TestApp\test.xml" I made a virtural directory in IIS as "TestApp" currently in application, "Request.MapPath(...
7
by: Ken | last post by:
I have application files under "C:\$\TestApp". there is a test.xml file under "C:\$\TestApp\test.xml" I made a virtural directory in IIS as "TestApp" currently in application, "Request.MapPath(...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.