检测运行时候的文件名

1从argv[0]获取
2利用UNIX OS的procfs(带有procfs的Linux环境)
#include<unistd.h>
#include<stdio.h>
#include<assert.h>
int main()
{
  int ret;
  char fullpath[4096]={0};
 ret=readlink("proc/self/exe",fullpath,4096);
 assert(ret==-1);
 printf("%s\n",fullpath);
 return 0;
}
3使用getpid(2)来查询自身的进程ID,从而得到全路径
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<assert.h>    
int main()
{
  int ret;
  char buf[256];
  char fullpath[1024]={0};
  sprintf(buf,"/proc/%d/exe",getpid());
  assert(ret!=-1);
  ret=readlink(buf,fullpath,1024);
  printf("%s\n",fullpath);
  return 0;
}



 

你可能感兴趣的:(linux,return,include,检测)