LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2004, 10:38 AM   #1
brainstormingin
LQ Newbie
 
Registered: Mar 2004
Location: india
Distribution: red hat 7.3
Posts: 6

Rep: Reputation: 0
Question Path of the executable


How to get the path of the binary that is executing from c ; i dont need the current path.
 
Old 03-25-2004, 02:12 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
There may not be a great way to do this since the executable could be accesible by several paths (hard links) or no paths (It was deleted after starting to execute). As a best guess, you could check argv[0]. If it contains a slash, you could monkey with getcwd() and realpath() to get its canonical path. If it doesn't, you could run through PATH until you find a file with the same name as argv[0].

Of course if this program runs with privileges you need to be aware that the invoker of the program can lie to it about argv[0].
 
Old 03-25-2004, 09:02 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
There may be a better way. See this thread:

http://www.linuxquestions.org/questi...018#post478018

Last edited by Hko; 03-25-2004 at 09:08 PM.
 
Old 03-25-2004, 09:07 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
Ah, excellent.

To answer the comment about readlink(), readlink() does not NUL terminate the string it writes. In your case you must have had an '@', possibly some space or non-printing stuff, and then a 0 in memory after the string. Why they wrote the function this way is beyond me.
 
Old 03-25-2004, 10:24 PM   #5
brainstormingin
LQ Newbie
 
Registered: Mar 2004
Location: india
Distribution: red hat 7.3
Posts: 6

Original Poster
Rep: Reputation: 0
Thank you guys for all your help.
 
  


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
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 08:45 AM
Running a Java executable class from another executable class LUB997 Programming 22 07-24-2005 05:57 AM
wo to set up executable so i can run it without typing whole path? Lechium Linux - Newbie 2 07-03-2004 05:40 PM
How to Chnage Python's module search path (sys.path)? lramos85 Linux - Software 1 05-02-2004 07:10 PM
Executable, but not in path. Sledge Red Hat 0 12-03-2003 09:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:30 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