LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 08-02-2006, 03:56 AM   #1
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Unix Function call to get executable's path


NOT The current working directory

This is probably a very silly question but I seem to be stuck where I cannot find the solution. What is the Unix/Linux system call that gets the running executable's path?

The reason is that I want to use a particular file located in the exe's home dir, but unless I run the app from the command line from the same directory, it seems unable to locate the file.

Last edited by vharishankar; 08-02-2006 at 04:03 AM.
 
Old 08-02-2006, 04:33 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
http://www.linuxquestions.org/questi...xecutable+path

Sorry Harishankar it seems I keep urinating on your bonfire

Last edited by dmail; 08-02-2006 at 04:37 AM.
 
Old 08-02-2006, 04:50 AM   #3
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178

Original Poster
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Wholly unsatisfactory explanation for why such a function doesn't exist. There are plenty of legitimate reasons why I would want the running executable's path. One reason is loading data files located in the same path as the executable. I know that it's not good "form" (hahaha) to store data files in the exe's path, but a program in the development stage cannot expect to have its own directory structure all laid out in advance same as every other program.

Also when people run an executable from a GUI file browser or don't execute the file from its own location, the so-called "current working directory" is not the same as the executable's location and so screws up the running of the program.

If only there were a simple way to do this. But of course UNIX gurus would say - "your program design is flawed". Bullshit excuse for not incorporating something as simple as this in a platform independent manner (i.e. works on all Unices).

Last edited by vharishankar; 08-02-2006 at 04:55 AM.
 
Old 08-02-2006, 05:11 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
...but a program in the development stage cannot expect to have its own directory structure all laid out in advance same as every other program.
Really, is this how you do things?
I always have a directory structure, which may change depending on the application but which is normally the same.
 
Old 08-02-2006, 05:11 AM   #5
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Your program design is flawed.
 
Old 08-02-2006, 05:18 AM   #6
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178

Original Poster
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Pray how can you tell if it's flawed without seeing the code?

Surely when I write a program and want to load a data file how would I know where the final location of the data file on installation would be? And how on earth do I plan the install routine without even having a basic program in place?

Do I simply hardcode everything in and leave everything to sort itself out later?

Teach me.. since you're so full of Unix wisdom, introuble.

dmail, I have a dir structure, but this is the "project" dir structure and not the final application structure. Since the project dir would vary from the final app dir location how on earth do you guys plan on locating data files used by your programs? Currently for development I just keep the data in a subdirectory of the project dir. That's why I said I needed the path.

Surely there must be one programmer who understands what I'm getting at Do you guys never use data files in your programs?

I'm genuinely seeking help here from knowledgeable Unix programmers and how they handle this kind of a situation.

I'm only a hobbyist... not a professional for god's sake.

Last edited by vharishankar; 08-02-2006 at 05:25 AM.
 
Old 08-02-2006, 12:12 PM   #7
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hi,

Quote:
Surely there must be one programmer who understands what I'm getting at Do you guys never use data files in your programs?
Of course we do.

For all of the applications I have ever created I predefine where I want the data files to be located when I perform the initial design, hard code that information into the application, and make that location a requirement for running the application.

I understand what you are trying to do, I have run into that same sort of requirement when programming a few applications for the Windows world and got around that problem by having the installer set the application's working directory into the registry. However there is no such thing like that under Linux/UNIX. I guess you could come up with something similiar in that you could have your installer script create a data file that contains the application's working directory. That data file can then be written out to a predefined location (such as the /tmp directory). Then when your application is run it can read in the data file in order to find out what directory it should use at run time.

Just a suggestion, hope this helps!
 
Old 08-02-2006, 08:11 PM   #8
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
1) you could set an environment variable and read in in your code
2) many languages pass the executable name as the fist argument of the code
3) maybe the 'which' function would do what you want
 
Old 08-02-2006, 10:05 PM   #9
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178

Original Poster
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Thanks a lot rstewart and sirclif. I am using C++. As you say, I will probably have to hardcode the path for now and later (if I am going to make a distributable package) use one of the methods you suggest.
 
Old 08-03-2006, 12:29 AM   #10
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
/proc/self/exe
is always a symbolic link that points to your running process.
Code:
#include <stdio.h>
#include <unistd.h>

int main() {
    char buf[256];
    readlink("/proc/self/exe", buf, sizeof(buf));
    printf("%s\n",buf);
    return 0;
}
 
Old 08-03-2006, 01:24 AM   #11
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
BTW,
(1) `which` is a simple shell script. So unless you want to debate whats a "Unix function call" and whats not, this is OK.
(2) On most unixes argv[0] returns only the exe minus the path. (Unlike windoze).
(3) Hari, from a purely technical view, please don't go further into "hard coding". It is better to immediately review all the code you have produced so far and make corrections ASAP. The longer you delay, the more you are committing to"hard code" type thinking and possibly one more hard coded statement. Bite the bullet. Factor in the extra time and effort. "Later" never happens.

All the best.

end
 
Old 08-03-2006, 05:55 AM   #12
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
My current list of the ways it can done on Unix systems is:

Linux:
/proc/<pid>/exe

Solaris:
/proc/<pid>/object/a.out (filename only)
/proc/<pid>/path/a.out (complete pathname)

*BSD (and maybe Darwing too):
/proc/<pid>/file

They're all symbolic links as randyding pointed out. It's better to use the numeric pid and avoid shortcuts such as "self" that aren't portable.
 
Old 08-03-2006, 12:18 PM   #13
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by introuble
Your program design is flawed.
As moderator I'm asking you to please contribute to maintaining a friendly and welcoming atmosphere at LQ for everyone by making constructive and helpful posts. If you can't manage anything else but drive-by hit-n-run oneliners then please then do not reply at all.
Thanks in advance.

//If you feel the need to discuss this you are invited to take it up with me by email.
 
Old 04-20-2010, 12:53 PM   #14
yech
LQ Newbie
 
Registered: Dec 2007
Posts: 22

Rep: Reputation: 0
With shell script I'm doing:

cd `dirname $0`
SCRIPT_DIR=`pwd`
cd -

Could be same way in application?
 
  


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
script to change unix path to windows path in all files csross Programming 8 04-29-2006 01:05 PM
How to call function in kernel vishalbutte Programming 10 02-07-2006 02:50 AM
How to call another function from a function? geminigal Programming 4 04-21-2005 10:41 PM
how to call function?? harpal Programming 3 04-29-2003 05:59 AM
How to get executable's full path using C/C++? oldbee Programming 6 02-19-2002 09:21 AM

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

All times are GMT -5. The time now is 06:20 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
Open Source Consulting | Domain Registration