我不能谈论窗户。 然而,在UNIX,可以达到一定比例的时间,但并没有保证。 造成这种情况的原因包括要求从诸如炮弹等其他方案中解脱。 在某些情况下,这种方案可能会使可迫害的生活,例如,一种相对的道路,如“././.mybin/exe/myprogram”。 《太平洋行动计划》的变量也使跟踪无法执行。
Let me ask: what are you trying to do, or, more correctly, why do you need to know?
IMO you don t need to know. you can check getcwd() and if you are not running in a directory that works, exit. It should not matter where your executable image lives.
这里是一部法典的空白,大部分时间都奏效,并不总是!
要求主人发出呼吁,使用“纳尔吉斯”(0)号,我使用开放式电话,使该守则适合少数人群。
char *
mypath(const char *src)
{
FILE *cmd=NULL;
static char path_2_me[PATH_MAX]={0x0};
char tmp[PATH_MAX]={0x0};
const char *basename=strrchr(src, / );
if(basename==NULL)
basename=src;
else
basename++;
if(memcmp(src, "./", 2)==0)
sprintf(path_2_me,"%s/%s", getcwd(tmp, PATH_MAX), basename);
else
{
sprintf(tmp, "/usr/bin/which %s", basename);
cmd=popen(tmp, "r");
fgets(path_2_me, sizeof(path_2_me), cmd); /* one read only */
pclose(cmd);
/* check for what your version of which says on failure */
if(memcmp(path_2_me, "no ", 3)==0)
*path_2_me=0x0;
}
return (*path_2_me) ?path_2_me: NULL;
}