LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-20-2007, 08:49 PM   #1
the_darkside_986
Member
 
Registered: Feb 2006
Distribution: Ubuntu Feisty (7.04)
Posts: 106

Rep: Reputation: 15
Executable paths and environment variables - troubles


Is there a way for a program to know what directory its file is in when it is run? I want a function that will return the string, for example, "/usr/local/bin" if my program is "/usr/local/bin/myprog" if I am running it.

Google didn't help me with this.

Last edited by the_darkside_986; 02-21-2007 at 07:41 PM.
 
Old 02-21-2007, 08:18 PM   #2
the_darkside_986
Member
 
Registered: Feb 2006
Distribution: Ubuntu Feisty (7.04)
Posts: 106

Original Poster
Rep: Reputation: 15
Actually, what I am trying to do is have program data in /usr/local/share/myprog/ or /opt/blah/blah/myprog depending on where it is installed without referring to absolute paths. I know that other *nix apps do this and I want to know how they do that. I can't think of any small open source projects to look at in order to know it.
 
Old 02-21-2007, 08:54 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Here's a project: which

I haven't looked at the source to see how complicated it is, but it's a place to start. Using which will tell you the path to an executable found in your PATH.

If the program was executed with a path at the command line (for example: "/home/progs/cool1/start" or "../../progs/cool1/start") then argv[0] ought to contain the path provided as well as the executable name.

I have some code that turns a relative path (e.g. "../../progs/cool1/start") into an absolute path by combining the current working directory with the relative path and then applying effects of the "up-dir" references.
 
Old 02-21-2007, 09:03 PM   #4
the_darkside_986
Member
 
Registered: Feb 2006
Distribution: Ubuntu Feisty (7.04)
Posts: 106

Original Poster
Rep: Reputation: 15
the "which" seems to work but using getenv to extract it causes the program to crash... I don't understand functions in stdlib.h too well I guess.

Thanks for the responses.

Last edited by the_darkside_986; 02-21-2007 at 09:16 PM.
 
Old 02-21-2007, 09:10 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by the_darkside_986
When I install it I can run it at the command line without typing anything but the name of the program, but argv[0] only contains what the user types in.
I know; that's what I was getting at (unless I misunderstand your question). Your program should check argv[0] to see if it contains a path (argv[0] would contain a '/' if so). If it does not contain a path, then the executable was started by searching through PATH. In that case, look at the source code for the which project, and see if you can pull in some/all of their code.

Last edited by Dark_Helmet; 02-21-2007 at 09:11 PM.
 
Old 02-21-2007, 09:14 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by the_darkside_986
the "which" seems to work but using getenv to extract it causes the program to crash...
Post some code to see. I've used getenv() before. It's 90% of the time a string-handling issue.
 
Old 02-21-2007, 09:19 PM   #7
the_darkside_986
Member
 
Registered: Feb 2006
Distribution: Ubuntu Feisty (7.04)
Posts: 106

Original Poster
Rep: Reputation: 15
cout << getenv("`which lexmk`") << endl;

This successfully prints the executable path (with or without putting ` ` around the shell command) but the program just simply quits now. It doesn't say segfault unless I try to put the corrupted value inside of an STL c++ string.

EDIT: I found out that the getenv is actually returning NULL according to an if statement that I made... very strange that it would still manage to print something.

Oh well, is there some other way to extract command line output results into a C/C++ data structure?

Last edited by the_darkside_986; 02-21-2007 at 09:40 PM.
 
Old 02-21-2007, 09:45 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I'll be honest, I'm not sure I understand what's going on with that code.

getenv() returns a char * to the "value" portion of a "name = value" pair (which you can see by executing env at a prompt). There should be no "which lexmk" environment setting for getenv() to find.

So I'm honestly stumped as to what that code is doing and exactly how it's working. I would have assumed that if you were going to use the which executable itself (rather than importing their code) that you would have used popen() or something similar.

EDIT: Didn't see your previous edit. So I'm not a nut after all. As for the popen() command, I realize it returns a FILE *, but I imagine there is some C++ way to convert that to an fstream. I can't say because I don't program in C++ very often (translation: I'm more than rusty)

Last edited by Dark_Helmet; 02-21-2007 at 09:49 PM.
 
Old 02-21-2007, 10:07 PM   #9
the_darkside_986
Member
 
Registered: Feb 2006
Distribution: Ubuntu Feisty (7.04)
Posts: 106

Original Poster
Rep: Reputation: 15
Thanks for the help. I figured out how to use popen to do this but it is really messy so I am going to make a class whose sole purpose is to extract output from command line and return it as a C++ string.

EDIT: Well this works with normal compile-time arrays but not with dynamically allocated arrays (char *).
EDIT2: Just typical programming screw ups. I'm getting an incorrect filesize of -1 when using popen. I gotta go to bed but I'm sure I'll figure it out.
EDIT3: I got it working now. One cannot get the filesize of a fake TTY of popen so one has to read each char individually until reaching EOF. I got to wake up at 6 AM and it's 11:37 PM here. I was born to program lol

Last edited by the_darkside_986; 02-21-2007 at 10:37 PM.
 
  


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
How to use multiple paths in configure env variables radam Linux - Software 4 07-17-2009 02:18 PM
Getting all environment variables Harlin Linux - General 2 10-06-2006 12:30 PM
environment variables naren Linux - Software 1 12-15-2004 06:44 AM
environment variables moeminhtun Linux - General 4 01-15-2003 09:50 AM
Environment variables kdnt Linux - Newbie 11 11-01-2002 08:19 AM


All times are GMT -5. The time now is 09:38 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration