/* Extract the filename; it is always an absolute path */
pPath = strchr (sLine, '/');
/* Get rid of the newline */
tmp = strrchr (pPath, '\n');
if (tmp) *tmp = 0;
/* Get rid of "(deleted)" */
//len = strlen (pPath);
//if (len > 10 && strcmp (pPath + len - 10, " (deleted)")
== 0)
//{
// tmp = pPath + len - 10;
// *tmp = 0;
//}
ret = 0;
strcpy( sFileName, pPath );
}
}
fclose (fp);
}
return ret;
}
测试代码:
int main ( int argc, char** argv ) {
char buffer[256]={0};
getchar();
printf ("ModuleFileName1 is: %s\n", GetModuleFileName1( argv[0],
buffer, 256)==-1?"Not found!":buffer);
printf ("ModuleFileName2 is: %s\n", GetModuleFileName2( argv[0],
buffer, 256)==-1?"Not found!":buffer);
printf ("ModuleFileName3 is: %s\n", GetModuleFileName3( argv[0],
buffer, 256)==-1?"Not found!":buffer);
printf ("ModuleFileName4 is: %s\n", GetModuleFileName4( buffer,
256)==-1?"Not found!":buffer);
return 0;
}
测试结果:
正常输出结果:
ModuleFileName1 is: /home/coldcrane/bin/hello
ModuleFileName2 is: /home/coldcrane/bin/hello
ModuleFileName3 is: /home/coldcrane/bin/hello
ModuleFileName4 is: /home/coldcrane/bin/hello
程序执行时目录被移动
$ mv ~/bin ~/nib
输出结果:
ModuleFileName1 is: Not found!
ModuleFileName2 is: Not found!
ModuleFileName3 is: /home/coldcrane/bin/hello
ModuleFileName4 is: /home/coldcrane/nib/hello
程序执行时文件被改名
$ mv ~/bin/hello ~/bin/hh
输出结果:
ModuleFileName1 is: Not found!
ModuleFileName2 is: Not found!
ModuleFileName3 is: /home/coldcrane/bin/hello
ModuleFileName4 is: /home/coldcrane/bin/hh
程序执行时文件被删除
$ rm ~/bin/hello
输出结果:
ModuleFileName1 is: Not found!
ModuleFileName2 is: Not found!
ModuleFileName3 is: /home/coldcrane/bin/hello
ModuleFileName4 is: /home/coldcrane/bin/hello (deleted)