|
|
Home » U++ Library support » U++ Core » GetExeFilePath() in Linux
GetExeFilePath() in Linux [message #6334] |
Tue, 07 November 2006 00:47 |
zsolt
Messages: 698 Registered: December 2005 Location: Budapest, Hungary
|
Contributor |
|
|
I have implemented GetExeFilePath(). It is a little bit tricky, I dont know if it is able to work in BSD systems, but OK for Linux. It is based on the proc filesystem, as there is no standard posix function to this problem (AFAIK).
String GetExeFilePath()
{
char h[_MAX_PATH + 1];
String link;
link << "/proc/" << getpid() << "/exe";
int ret = readlink(link, h, _MAX_PATH);
if(ret == -1 || ret > _MAX_PATH)
return "";
h[ret] = 0;
return FromSystemCharset(h);
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: GetExeFilePath() in Linux [message #6411 is a reply to message #6401] |
Sat, 11 November 2006 12:48 |
|
mirek
Messages: 13975 Registered: November 2005
|
Ultimate Member |
|
|
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_"...
[Updated on: Sat, 11 November 2006 12:49] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Sat May 04 10:55:17 CEST 2024
Total time taken to generate the page: 0.03377 seconds
|
|
|