LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-27-2004, 02:52 PM   #1
nysb
LQ Newbie
 
Registered: May 2004
Posts: 7

Rep: Reputation: 0
Check programmatically where an executable is located?


Hi All,

In linux, when you put an executable in the user's search path, and then when the user types the executable's name at the console, somehow the system finds out where the executable is located and executes it. My question is: is there anyway programmatically inside the executable file itself to know where it is located? To make it clear, say for example, you have an executable called "test", and you put this file in /usr/bin, then what I want is that during run time, test knows itself resides in /usr/bin.

The reason I ask this question is that I want to put some other accessory files for the executable together in the same directory, but the executable needs to know where itself is located. Somewhat irony! Is there any other way to deal with this issue?

Thanks a lot for your help.
 
Old 05-27-2004, 03:06 PM   #2
Zoombie
Member
 
Registered: Jul 2003
Distribution: Debian Testing
Posts: 180

Rep: Reputation: 30
Most programs know where they're installed because of configure, but that path has no meaning to the program in most situations under Linux. Data files and the like are installed to different directories. If you absolutely need that script to run in that directory, wrap it with a shell script.
 
Old 05-27-2004, 03:21 PM   #3
nysb
LQ Newbie
 
Registered: May 2004
Posts: 7

Original Poster
Rep: Reputation: 0
One file I was thinking that needed to be put in the same directory as the executable is a logo file, basically a .xpm file. The executable has to know where the logo file is in order to load the logo automatically.

With configure, how do the programs know where they are installed since those information has to be stored in a file most likely, but the program needs to know where to locate that file? Sorry for my ignorance.
 
Old 05-27-2004, 03:40 PM   #4
nysb
LQ Newbie
 
Registered: May 2004
Posts: 7

Original Poster
Rep: Reputation: 0
I guess one option I have is to ask the user to create an environment variable pointing to that directory.

Any other suggestions?
 
Old 05-27-2004, 04:28 PM   #5
Aeiri
Member
 
Registered: Feb 2004
Posts: 307

Rep: Reputation: 30
I don't think Zombie understood you completely...

"which <progname>"

Gives you where an application is if it's in PATH.
 
Old 05-28-2004, 09:39 AM   #6
nysb
LQ Newbie
 
Registered: May 2004
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks, Aeiri,

using your way would give the user the location of the executable, but I need to have the executable itself know programattically where it is located.



Quote:
Originally posted by Aeiri
I don't think Zombie understood you completely...

"which <progname>"

Gives you where an application is if it's in PATH.
 
Old 05-28-2004, 10:07 AM   #7
PaulFrields
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core
Posts: 10

Rep: Reputation: 1
A program can do getpid() to get its own process ID, and if you wanted to, you could then have it use a call to the procfs to get its "exe" name (full pathname). Pick a process on your machine and do ls -l /proc/<pid>/exe to see what I mean.
 
Old 05-28-2004, 10:14 AM   #8
nysb
LQ Newbie
 
Registered: May 2004
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks, PaulFrields.

Your method worked great, by the way, how portable is this method among different linux and unix?



Quote:
Originally posted by PaulFrields
A program can do getpid() to get its own process ID, and if you wanted to, you could then have it use a call to the procfs to get its "exe" name (full pathname). Pick a process on your machine and do ls -l /proc/<pid>/exe to see what I mean.
 
Old 05-29-2004, 03:51 PM   #9
PaulFrields
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core
Posts: 10

Rep: Reputation: 1
Not sure about other UNIX... I would suspect getpid() is fairly portable, since it conforms to POSIX, BSD 4.3, etc.... see man page for more info. Procfs is a Linux animal though. Seems to me you should be able to Google for more info.
 
  


Reply



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
programmatically wake up monitor dovkruger Programming 1 11-03-2005 04:27 PM
How do I get my machine's up time programmatically? MClayton Linux - General 2 03-11-2005 12:39 PM
programmatically updating /etc/passwd ananthbv Programming 7 01-12-2005 12:11 AM
Set $PATH programmatically c_coder Programming 6 02-26-2004 07:40 AM
Boot disk; check. CD in drive; check. Doesn't work; check. Hal DamnSmallLinux 7 02-04-2004 02:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:31 PM.

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
Open Source Consulting | Domain Registration