#include <stdio.h>
#include <unistd.h>
char * get_exe_path( char * buf, int count)
{
int i;
int rslt = readlink("/proc/self/exe", buf, count - 1); //将seld改为进程pid就是获取任意进程路径
if (rslt < 0 || (rslt >= count - 1))
{
return NULL;
}
buf[rslt] = '\0';
for (i = rslt; i >= 0; i--)
{
if (buf[i] == '/')
{
buf[i + 1] = '\0';
break;
}
}
return buf;
}
int main(int argc, char ** argv)
{
char path[1024];
printf("%s\n", get_exe_path(path, 1024));
return 0;
}