#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int GetCurrentExcutableFilePathName( char* processdir,char* processname, size_t len)
{
char* path_end;
int size;
size = readlink("/proc/self/exe", processdir,len);
if((size <= 0) || (size > len)) {
printf("read:%d len:%ld\n", size, len);
return -1;
}
path_end = strrchr(processdir, '/');
if(path_end == NULL)
return -1;
++path_end;
strcpy(processname, path_end);
*path_end = '\0';
return (int)(path_end - processdir);
}
#define DIR_SIZE 1024
int main(void) {
char pro[DIR_SIZE]={0};
char name[DIR_SIZE]={0};
int ret;
ret = GetCurrentExcutableFilePathName(pro, name , sizeof(pro));
if (ret > 0) {
printf("dir len:%d dir:%s\nname:%s\n", ret, pro, name);
} else {
printf("ret:%d\n", ret);
}
return 0;
}