Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » GetExeFilePath() in Linux
Re: GetExeFilePath() in Linux [message #6415 is a reply to message #6411] Sat, 11 November 2006 13:00 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
First impression:
On FreeBSD you can use /proc/$pid/file, not /proc/$pid/exe (as on Linux).
Re: GetExeFilePath() in Linux [message #6416 is a reply to message #6415] Sat, 11 November 2006 13:37 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
OK.

Mirek
Re: GetExeFilePath() in Linux [message #6431 is a reply to message #6411] Sun, 12 November 2006 18:11 Go to previous messageGo to next message
masu is currently offline  masu
Messages: 378
Registered: February 2006
Senior Member
I tried the latest version and got the right path with mounted proc and also without.

I had to make a tiny change (marked red, therefore the ugly formatting Sad).

luzr wrote on Sat, 11 November 2006 12:48

OK, this is my final version:

const char *procexepath_() {
static char h[_MAX_PATH + 1];
ONCELOCK {
char link[100];
sprintf(link, "/proc/%d/exe", getpid());
int ret = readlink(link, h, _MAX_PATH);
if(ret > 0 && ret < _MAX_PATH)
h[ret] = '\0';
else
*h = '\0';
}
return h;
}

String GetExeFilePath()
{
static String exepath;
ONCELOCK {
const char *exe = procexepath_();
if(*exe)
exepath = exe;
else {
String x = Argv0__;
if(IsFullPath(x) && FileExists(x))
exepath = x;
else {
exepath = GetHomeDirFile("upp");
Vector<String> p = Split(FromSystemCharset(Environment().Get("PATH")), ':');
if(x.Find('/') >= 0)
p.Add(GetCurrentDirectory());
for(int i = 0; i < p.GetCount(); i++) {
String ep = NormalizePath(AppendFileName(p[i], x));
if(FileExists(ep))
exepath = ep;
}
}
}
}
return exepath;
}

Please check.

Mirek

P.S.: I need the name of executable in log files, without using the heap and before entering the main - that is why I have separated "procexepath_"...


Also, I think we should delete the green line since the result defaults to $HOME/app_name even if it does not exist. The application has to be in the PATH or in the current dir if it was not specified with the complete path on the command line. So this assignment is useless in my opinion since we check all possibilities.

Function SetExeTitle does not exist anymore, commented out in idewin.cpp:
	#ifdef _DEBUG
		SetExeTitle("debugide");
	#else
		SetExeTitle("theide");
	#endif


Matthias


931b81e7ea53320dccc37375b34b38c3

[Updated on: Sun, 12 November 2006 18:27]

Report message to a moderator

Re: GetExeFilePath() in Linux [message #6597 is a reply to message #6411] Mon, 20 November 2006 07:55 Go to previous messageGo to next message
guido is currently offline  guido
Messages: 169
Registered: April 2006
Experienced Member
Hi,

I think there is a simpler implementation for POSIX.
Acrobat Reader, OpenOffice, Firefox, ROX-Filer.. achieve relocatability by means of a simple shell launcher script:

#!/bin/sh

PROG=SampleApp

APP_DIR=`dirname "$0"`
APP_DIR=`cd "$APP_DIR";pwd`; export APP_DIR

exec "$APP_DIR/SampleApp" "$@"


Then, from inside the app simply
app_dir = strdup(getenv(APP_DIR));


Now I thought, why not port this shell script to C++?:

String GetExeFilePath()
{
	static String exepath;
	ONCELOCK {
		String x = Argv0__;
		if(IsFullPath(x) && FileExists(x))
			exepath = x;
		else {
			String cwd = GetCurrentDirectory();
			String filename = GetFileName(x);
			x = x.Left(x.ReverseFind('/'));
			chdir(ToSystemCharset(x));
			exepath = GetCurrentDirectory() + "/" + filename;
			chdir(ToSystemCharset(cwd));
		}
	}
	return exepath;
}


I don't know if there are tricky circumstances of the environment that would break this, but so far it seems to be working.

Guido
Re: GetExeFilePath() in Linux [message #6681 is a reply to message #6597] Wed, 22 November 2006 16:30 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Well, I was thinking about this last 4 days (of course, not all the time) and I must admit I am completely clueless how is this supposed to work...
Re: GetExeFilePath() in Linux [message #6683 is a reply to message #6681] Wed, 22 November 2006 19:04 Go to previous messageGo to next message
guido is currently offline  guido
Messages: 169
Registered: April 2006
Experienced Member
luzr wrote on Wed, 22 November 2006 16:30

Well, I was thinking about this last 4 days (of course, not all the time) and I must admit I am completely clueless how is this supposed to work...


Well, as a shell script this method works. You can download e.g. OpenOffice or Firefox from their original site, extract into your home dir and run from there. And that's how they do it. Just open the launch wrapper and see.
If, however, this works without going through a shell in all cases, I'm not sure.
I created a sample console app, which prints exepath to stdout, and a gui app, which pops up a dialog with the exepath, and that worked fine, launching from both a terminal or the file-manager.

Guido
Re: GetExeFilePath() in Linux [message #6684 is a reply to message #6683] Wed, 22 November 2006 19:10 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
OK, what seem wrong to me:

You have your "foo" application somewhere on the PATH. You start it by typing "foo" -> no '\' in the name -> x = x.Left(x.ReverseFind('/')) crashes.

Mirek
Re: GetExeFilePath() in Linux [message #6687 is a reply to message #6684] Wed, 22 November 2006 22:53 Go to previous messageGo to next message
guido is currently offline  guido
Messages: 169
Registered: April 2006
Experienced Member
luzr wrote on Wed, 22 November 2006 19:10

OK, what seem wrong to me:

You have your "foo" application somewhere on the PATH. You start it by typing "foo" -> no '\' in the name -> x = x.Left(x.ReverseFind('/')) crashes.

Mirek


Oops, sorry Embarassed

Well, then, when x.ReverseFind('/') fails, a path search, like your current implementation, must be done.

Or this is how OO.org does it:
guido@Sid:~$ cat /usr/bin/openoffice.org-2.0
#!/bin/sh
exec /etc/openoffice.org-2.0/program/soffice "$@"


I only suggest it, because I'm familiar with this method, and because it's portable across POSIX systems. I know, it is how ISPs deal with it, to make their software relocatable and distribution independent.

Guido
Re: GetExeFilePath() in Linux [message #6688 is a reply to message #6687] Wed, 22 November 2006 23:07 Go to previous message
guido is currently offline  guido
Messages: 169
Registered: April 2006
Experienced Member
...ISVs, not ISPs, of course Rolling Eyes

Guido
Previous Topic: Double formatting bug?
Next Topic: How to catch keyboard input?
Goto Forum:
  


Current Time: Fri Mar 29 06:10:57 CET 2024

Total time taken to generate the page: 0.02350 seconds