LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2011, 03:55 AM   #1
tank junior
Member
 
Registered: Apr 2010
Posts: 42

Rep: Reputation: 0
get executable(self) full path


Hi,

I am new to Linux as well as my c/c++ skills are limited.
I have created a simple executable which is working fine when I am executing it either by mouse click or by starting it from terminal.(provided I am in same directory where executable resides)
When I am starting the executable from terminal by giving it's full path(I am in another directory), the hard coded paths, for example
Code:
"lib/data.zip"
are not working.
After searching on this topic I have came out with a solution.
I am not sure if it's the correct way of solving the problem.

1.
When I start the executable from terminal and I am in the same directory, these are the values of argv[0] and readlink
Code:
readlink = /home/<user>/installed/distro-test/library/dist-linux/walker
argv[0] = ./walker
2.
When I start the executable from terminal by full path and I am in *NOT* the same directory, these are the values of argv[0] and readlink
Code:
readlink = /home/<user>/installed/distro-test/library/dist-linux/walker
argv[0] = /home/<user>/installed/distro-test/library/dist-linux/./walker
Based on these two situations I can use a condition to check whether I have to use relative path or absolute path inside my application.

Condition:
If argv[0] starts with "./" character then app started from same directory and I can use relative path.
If argv[0] starts with "/" character then I have to use full path inside my application.

Do you think this will work?

For readlink I am using:
Code:
len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
Is this is portable on modern Linux distros like ubuntu, open-suse or fedora?

How do I retrieve directory name from readlink result?
The result include executable name (walker) also and I need path upto dist-linux

Thanks

Prashant
 
Old 02-24-2011, 03:50 AM   #2
tommyttt
Member
 
Registered: Oct 2009
Location: Federal Way, WA
Distribution: openSUSE 11.4 x86_64, openSuSE 12.1, Fedora 15
Posts: 207

Rep: Reputation: 34
Quote:
Originally Posted by tank junior View Post
Hi,

I am new to Linux as well as my c/c++ skills are limited.
I have created a simple executable which is working fine when I am executing it either by mouse click or by starting it from terminal.(provided I am in same directory where executable resides)
When I am starting the executable from terminal by giving it's full path(I am in another directory), the hard coded paths, for example
Code:
"lib/data.zip"
are not working.
After searching on this topic I have came out with a solution.
I am not sure if it's the correct way of solving the problem.

1.
When I start the executable from terminal and I am in the same directory, these are the values of argv[0] and readlink
Code:
readlink = /home/<user>/installed/distro-test/library/dist-linux/walker
argv[0] = ./walker
2.
When I start the executable from terminal by full path and I am in *NOT* the same directory, these are the values of argv[0] and readlink
Code:
readlink = /home/<user>/installed/distro-test/library/dist-linux/walker
argv[0] = /home/<user>/installed/distro-test/library/dist-linux/./walker
Based on these two situations I can use a condition to check whether I have to use relative path or absolute path inside my application.

Condition:
If argv[0] starts with "./" character then app started from same directory and I can use relative path.
If argv[0] starts with "/" character then I have to use full path inside my application.

Do you think this will work?

For readlink I am using:
Code:
len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
Is this is portable on modern Linux distros like ubuntu, open-suse or fedora?

How do I retrieve directory name from readlink result?
The result include executable name (walker) also and I need path upto dist-linux

Thanks

Prashant
Hi Phrashant;

The most likely cause for your problem is that you don't have rights in the directory where the file is located. For instance, /lib is has "root user" as owner and group so only "root user" has access to that directory. You should do some reading about privledges in linux (Google for it).

As a "non-root" user, you don't have access rights to any directory belonging to "root". Since you don't say which distro you are using, I can only give general information. One possibility is to use the sudo command before your program command (see man sudo). Another is to put the program into your /home/user/bin directory since that is probably in your path.

Or you could log in as "root" and run the program that way BUT BE WARNED, that is dangerous way of doing it. Any open session by "root" leaves your system vulnerable to outside attack.

Let us know if you need more help.

Tom
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php executable path linuxown Linux - Software 7 06-01-2009 01:46 AM
getting the path of the running executable lorebett Programming 17 04-15-2009 11:48 AM
Shell: tr can only be used when given full path (despite being in $PATH) chochem Programming 2 06-27-2008 09:37 AM
Path of the executable brainstormingin Programming 4 03-25-2004 10:24 PM
Executable, but not in path. Sledge Red Hat 0 12-03-2003 09:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration